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 | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 752 | /* Returns a 302 for a redirectable request. This may only be called just after |
| 753 | * the stream interface has moved to SI_ST_ASS. Unprocessable requests are |
| 754 | * left unchanged and will follow normal proxy processing. |
Willy Tarreau | cde18fc | 2012-05-30 07:59:54 +0200 | [diff] [blame] | 755 | * NOTE: this function is designed to support being called once data are scheduled |
| 756 | * for forwarding. |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 757 | */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 758 | void perform_http_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 | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 820 | if (srv) |
| 821 | srv_inc_sess_ctr(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 822 | } |
| 823 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 824 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 825 | * that the server side is closed. Note that err_type is actually a |
| 826 | * bitmask, where almost only aborts may be cumulated with other |
| 827 | * values. We consider that aborted operations are more important |
| 828 | * than timeouts or errors due to the fact that nobody else in the |
| 829 | * logs might explain incomplete retries. All others should avoid |
| 830 | * being cumulated. It should normally not be possible to have multiple |
| 831 | * aborts at once, but just in case, the first one in sequence is reported. |
| 832 | */ |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 833 | void http_return_srv_error(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 834 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 835 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 836 | |
| 837 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 838 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 839 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 840 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 841 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 842 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 843 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 844 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 845 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 846 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 847 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 848 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 849 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 850 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 851 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 852 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 853 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 854 | 503, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 855 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 856 | http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C, |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 857 | 500, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 858 | } |
| 859 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 860 | extern const char sess_term_cond[8]; |
| 861 | extern const char sess_fin_state[8]; |
| 862 | extern const char *monthname[12]; |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 863 | struct pool_head *pool2_requri; |
Willy Tarreau | 193b8c6 | 2012-11-22 00:17:38 +0100 | [diff] [blame] | 864 | struct pool_head *pool2_capture = NULL; |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 865 | struct pool_head *pool2_uniqueid; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 866 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 867 | /* |
| 868 | * Capture headers from message starting at <som> according to header list |
| 869 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 870 | */ |
| 871 | void capture_headers(char *som, struct hdr_idx *idx, |
| 872 | char **cap, struct cap_hdr *cap_hdr) |
| 873 | { |
| 874 | char *eol, *sol, *col, *sov; |
| 875 | int cur_idx; |
| 876 | struct cap_hdr *h; |
| 877 | int len; |
| 878 | |
| 879 | sol = som + hdr_idx_first_pos(idx); |
| 880 | cur_idx = hdr_idx_first_idx(idx); |
| 881 | |
| 882 | while (cur_idx) { |
| 883 | eol = sol + idx->v[cur_idx].len; |
| 884 | |
| 885 | col = sol; |
| 886 | while (col < eol && *col != ':') |
| 887 | col++; |
| 888 | |
| 889 | sov = col + 1; |
| 890 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 891 | sov++; |
| 892 | |
| 893 | for (h = cap_hdr; h; h = h->next) { |
| 894 | if ((h->namelen == col - sol) && |
| 895 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 896 | if (cap[h->index] == NULL) |
| 897 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 898 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 899 | |
| 900 | if (cap[h->index] == NULL) { |
| 901 | Alert("HTTP capture : out of memory.\n"); |
| 902 | continue; |
| 903 | } |
| 904 | |
| 905 | len = eol - sov; |
| 906 | if (len > h->len) |
| 907 | len = h->len; |
| 908 | |
| 909 | memcpy(cap[h->index], sov, len); |
| 910 | cap[h->index][len]=0; |
| 911 | } |
| 912 | } |
| 913 | sol = eol + idx->v[cur_idx].cr + 1; |
| 914 | cur_idx = idx->v[cur_idx].next; |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 919 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 920 | */ |
| 921 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 922 | |
| 923 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 924 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 925 | */ |
| 926 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 927 | ptr++; \ |
| 928 | if (likely(ptr < end)) \ |
| 929 | goto good; \ |
| 930 | else { \ |
| 931 | state = (st); \ |
| 932 | goto http_msg_ood; \ |
| 933 | } \ |
| 934 | } while (0) |
| 935 | |
| 936 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 937 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 938 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 939 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 940 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 941 | * will give undefined results. |
| 942 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 943 | * and that msg->sol points to the beginning of the response. |
| 944 | * If a complete line is found (which implies that at least one CR or LF is |
| 945 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 946 | * returned indicating an incomplete line (which does not mean that parts have |
| 947 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 948 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 949 | * upon next call. |
| 950 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 951 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 952 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 953 | * 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] | 954 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 955 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 956 | const char *http_parse_stsline(struct http_msg *msg, |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 957 | unsigned int state, const char *ptr, const char *end, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 958 | unsigned int *ret_ptr, unsigned int *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 959 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 960 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 961 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 962 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 963 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 964 | http_msg_rpver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 965 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 966 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 967 | |
| 968 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 969 | msg->sl.st.v_l = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 970 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 971 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 972 | state = HTTP_MSG_ERROR; |
| 973 | break; |
| 974 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 975 | case HTTP_MSG_RPVER_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 976 | http_msg_rpver_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 977 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 978 | msg->sl.st.c = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 979 | goto http_msg_rpcode; |
| 980 | } |
| 981 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 982 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 983 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 984 | state = HTTP_MSG_ERROR; |
| 985 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 986 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 987 | case HTTP_MSG_RPCODE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 988 | http_msg_rpcode: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 989 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 990 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 991 | |
| 992 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 993 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 994 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 995 | } |
| 996 | |
| 997 | /* so it's a CR/LF, so there is no reason phrase */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 998 | msg->sl.st.c_l = ptr - msg_start - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 999 | http_msg_rsp_reason: |
| 1000 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1001 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1002 | msg->sl.st.r_l = 0; |
| 1003 | goto http_msg_rpline_eol; |
| 1004 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1005 | case HTTP_MSG_RPCODE_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1006 | http_msg_rpcode_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1007 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1008 | msg->sl.st.r = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1009 | goto http_msg_rpreason; |
| 1010 | } |
| 1011 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1012 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1013 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1014 | goto http_msg_rsp_reason; |
| 1015 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1016 | case HTTP_MSG_RPREASON: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1017 | http_msg_rpreason: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1018 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1019 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1020 | msg->sl.st.r_l = ptr - msg_start - msg->sl.st.r; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1021 | http_msg_rpline_eol: |
| 1022 | /* We have seen the end of line. Note that we do not |
| 1023 | * necessarily have the \n yet, but at least we know that we |
| 1024 | * have EITHER \r OR \n, otherwise the response would not be |
| 1025 | * complete. We can then record the response length and return |
| 1026 | * to the caller which will be able to register it. |
| 1027 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1028 | msg->sl.st.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1029 | return ptr; |
| 1030 | |
| 1031 | #ifdef DEBUG_FULL |
| 1032 | default: |
| 1033 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1034 | exit(1); |
| 1035 | #endif |
| 1036 | } |
| 1037 | |
| 1038 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1039 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1040 | if (ret_state) |
| 1041 | *ret_state = state; |
| 1042 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1043 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1044 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1045 | } |
| 1046 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1047 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1048 | * This function parses a request line between <ptr> and <end>, starting with |
| 1049 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1050 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1051 | * will give undefined results. |
| 1052 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1053 | * and that msg->sol points to the beginning of the request. |
| 1054 | * If a complete line is found (which implies that at least one CR or LF is |
| 1055 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1056 | * returned indicating an incomplete line (which does not mean that parts have |
| 1057 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1058 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1059 | * upon next call. |
| 1060 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1061 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1062 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1063 | * 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] | 1064 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1065 | */ |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1066 | const char *http_parse_reqline(struct http_msg *msg, |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1067 | unsigned int state, const char *ptr, const char *end, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1068 | unsigned int *ret_ptr, unsigned int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1069 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1070 | const char *msg_start = msg->chn->buf->p; |
Willy Tarreau | 62f791e | 2012-03-09 11:32:30 +0100 | [diff] [blame] | 1071 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1072 | switch (state) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1073 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1074 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1075 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1076 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1077 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1078 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1079 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1080 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1081 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1082 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1083 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1084 | /* HTTP 0.9 request */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1085 | msg->sl.rq.m_l = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1086 | http_msg_req09_uri: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1087 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1088 | http_msg_req09_uri_e: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1089 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1090 | http_msg_req09_ver: |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1091 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1092 | msg->sl.rq.v_l = 0; |
| 1093 | goto http_msg_rqline_eol; |
| 1094 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1095 | state = HTTP_MSG_ERROR; |
| 1096 | break; |
| 1097 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1098 | case HTTP_MSG_RQMETH_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1099 | http_msg_rqmeth_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1100 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1101 | msg->sl.rq.u = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1102 | goto http_msg_rquri; |
| 1103 | } |
| 1104 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1105 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1106 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1107 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1108 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1109 | case HTTP_MSG_RQURI: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1110 | http_msg_rquri: |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1111 | if (likely((unsigned char)(*ptr - 33) <= 93)) /* 33 to 126 included */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1112 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1113 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1114 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1115 | msg->sl.rq.u_l = ptr - msg_start - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1116 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1117 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1118 | |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1119 | if (likely((unsigned char)*ptr >= 128)) { |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1120 | /* non-ASCII chars are forbidden unless option |
| 1121 | * accept-invalid-http-request is enabled in the frontend. |
| 1122 | * In any case, we capture the faulty char. |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1123 | */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1124 | if (msg->err_pos < -1) |
| 1125 | goto invalid_char; |
| 1126 | if (msg->err_pos == -1) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1127 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1128 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
| 1129 | } |
| 1130 | |
| 1131 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1132 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1133 | goto http_msg_req09_uri_e; |
| 1134 | } |
| 1135 | |
| 1136 | /* OK forbidden chars, 0..31 or 127 */ |
Willy Tarreau | 422246e | 2012-01-07 23:54:13 +0100 | [diff] [blame] | 1137 | invalid_char: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1138 | msg->err_pos = ptr - msg_start; |
Willy Tarreau | 2e9506d | 2012-01-07 23:22:31 +0100 | [diff] [blame] | 1139 | state = HTTP_MSG_ERROR; |
| 1140 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1141 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1142 | case HTTP_MSG_RQURI_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1143 | http_msg_rquri_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1144 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1145 | msg->sl.rq.v = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1146 | goto http_msg_rqver; |
| 1147 | } |
| 1148 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1149 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1150 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1151 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1152 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1153 | case HTTP_MSG_RQVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1154 | http_msg_rqver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1155 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1156 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1157 | |
| 1158 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1159 | msg->sl.rq.v_l = ptr - msg_start - msg->sl.rq.v; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1160 | http_msg_rqline_eol: |
| 1161 | /* We have seen the end of line. Note that we do not |
| 1162 | * necessarily have the \n yet, but at least we know that we |
| 1163 | * have EITHER \r OR \n, otherwise the request would not be |
| 1164 | * complete. We can then record the request length and return |
| 1165 | * to the caller which will be able to register it. |
| 1166 | */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1167 | msg->sl.rq.l = ptr - msg_start - msg->sol; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1168 | return ptr; |
| 1169 | } |
| 1170 | |
| 1171 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1172 | state = HTTP_MSG_ERROR; |
| 1173 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1174 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1175 | #ifdef DEBUG_FULL |
| 1176 | default: |
| 1177 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1178 | exit(1); |
| 1179 | #endif |
| 1180 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1181 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1182 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1183 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1184 | if (ret_state) |
| 1185 | *ret_state = state; |
| 1186 | if (ret_ptr) |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1187 | *ret_ptr = ptr - msg_start; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1188 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1189 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1190 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1191 | /* |
| 1192 | * Returns the data from Authorization header. Function may be called more |
| 1193 | * than once so data is stored in txn->auth_data. When no header is found |
| 1194 | * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid |
| 1195 | * searching again for something we are unable to find anyway. |
| 1196 | */ |
| 1197 | |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1198 | char *get_http_auth_buff; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1199 | |
| 1200 | int |
| 1201 | get_http_auth(struct session *s) |
| 1202 | { |
| 1203 | |
| 1204 | struct http_txn *txn = &s->txn; |
| 1205 | struct chunk auth_method; |
| 1206 | struct hdr_ctx ctx; |
| 1207 | char *h, *p; |
| 1208 | int len; |
| 1209 | |
| 1210 | #ifdef DEBUG_AUTH |
| 1211 | printf("Auth for session %p: %d\n", s, txn->auth.method); |
| 1212 | #endif |
| 1213 | |
| 1214 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1215 | return 0; |
| 1216 | |
| 1217 | if (txn->auth.method) |
| 1218 | return 1; |
| 1219 | |
| 1220 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1221 | |
| 1222 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1223 | |
| 1224 | if (txn->flags & TX_USE_PX_CONN) { |
| 1225 | h = "Proxy-Authorization"; |
| 1226 | len = strlen(h); |
| 1227 | } else { |
| 1228 | h = "Authorization"; |
| 1229 | len = strlen(h); |
| 1230 | } |
| 1231 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1232 | 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] | 1233 | return 0; |
| 1234 | |
| 1235 | h = ctx.line + ctx.val; |
| 1236 | |
| 1237 | p = memchr(h, ' ', ctx.vlen); |
| 1238 | if (!p || p == h) |
| 1239 | return 0; |
| 1240 | |
| 1241 | chunk_initlen(&auth_method, h, 0, p-h); |
| 1242 | chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1); |
| 1243 | |
| 1244 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
| 1245 | |
| 1246 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
Willy Tarreau | 7e2c647 | 2012-10-29 20:44:36 +0100 | [diff] [blame] | 1247 | get_http_auth_buff, global.tune.bufsize - 1); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1248 | |
| 1249 | if (len < 0) |
| 1250 | return 0; |
| 1251 | |
| 1252 | |
| 1253 | get_http_auth_buff[len] = '\0'; |
| 1254 | |
| 1255 | p = strchr(get_http_auth_buff, ':'); |
| 1256 | |
| 1257 | if (!p) |
| 1258 | return 0; |
| 1259 | |
| 1260 | txn->auth.user = get_http_auth_buff; |
| 1261 | *p = '\0'; |
| 1262 | txn->auth.pass = p+1; |
| 1263 | |
| 1264 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1265 | return 1; |
| 1266 | } |
| 1267 | |
| 1268 | return 0; |
| 1269 | } |
| 1270 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1271 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1272 | /* |
| 1273 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | 8b1323e | 2012-03-09 14:46:19 +0100 | [diff] [blame] | 1274 | * depending on the initial msg->msg_state. The caller is responsible for |
| 1275 | * ensuring that the message does not wrap. The function can be preempted |
| 1276 | * everywhere when data are missing and recalled at the exact same location |
| 1277 | * with no information loss. The message may even be realigned between two |
| 1278 | * calls. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1279 | * 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] | 1280 | * fields. Note that msg->sol will be initialized after completing the first |
| 1281 | * state, so that none of the msg pointers has to be initialized prior to the |
| 1282 | * first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1283 | */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 1284 | void http_msg_analyzer(struct http_msg *msg, struct hdr_idx *idx) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1285 | { |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1286 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1287 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1288 | struct buffer *buf; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1289 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1290 | state = msg->msg_state; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1291 | buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1292 | ptr = buf->p + msg->next; |
| 1293 | end = buf->p + buf->i; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1294 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1295 | if (unlikely(ptr >= end)) |
| 1296 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1297 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1298 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1299 | /* |
| 1300 | * First, states that are specific to the response only. |
| 1301 | * We check them first so that request and headers are |
| 1302 | * closer to each other (accessed more often). |
| 1303 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1304 | case HTTP_MSG_RPBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1305 | http_msg_rpbefore: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1306 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1307 | /* we have a start of message, but we have to check |
| 1308 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1309 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1310 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1311 | if (unlikely(ptr != buf->p)) { |
| 1312 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1313 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1314 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1315 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1316 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1317 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1318 | msg->sl.st.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1319 | hdr_idx_init(idx); |
| 1320 | state = HTTP_MSG_RPVER; |
| 1321 | goto http_msg_rpver; |
| 1322 | } |
| 1323 | |
| 1324 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1325 | goto http_msg_invalid; |
| 1326 | |
| 1327 | if (unlikely(*ptr == '\n')) |
| 1328 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1329 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1330 | /* stop here */ |
| 1331 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1332 | case HTTP_MSG_RPBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1333 | http_msg_rpbefore_cr: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1334 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1335 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1336 | /* stop here */ |
| 1337 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1338 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1339 | http_msg_rpver: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1340 | case HTTP_MSG_RPVER_SP: |
| 1341 | case HTTP_MSG_RPCODE: |
| 1342 | case HTTP_MSG_RPCODE_SP: |
| 1343 | case HTTP_MSG_RPREASON: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1344 | ptr = (char *)http_parse_stsline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1345 | state, ptr, end, |
| 1346 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1347 | if (unlikely(!ptr)) |
| 1348 | return; |
| 1349 | |
| 1350 | /* we have a full response and we know that we have either a CR |
| 1351 | * or an LF at <ptr>. |
| 1352 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1353 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1354 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1355 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1356 | if (likely(*ptr == '\r')) |
| 1357 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1358 | goto http_msg_rpline_end; |
| 1359 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1360 | case HTTP_MSG_RPLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1361 | http_msg_rpline_end: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1362 | /* msg->sol must point to the first of CR or LF. */ |
| 1363 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1364 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1365 | /* stop here */ |
| 1366 | |
| 1367 | /* |
| 1368 | * Second, states that are specific to the request only |
| 1369 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1370 | case HTTP_MSG_RQBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1371 | http_msg_rqbefore: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1372 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1373 | /* we have a start of message, but we have to check |
| 1374 | * first if we need to remove some CRLF. We can only |
Willy Tarreau | 2e046c6 | 2012-03-01 16:08:30 +0100 | [diff] [blame] | 1375 | * do this when o=0. |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1376 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1377 | if (likely(ptr != buf->p)) { |
| 1378 | if (buf->o) |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1379 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1380 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1381 | bi_fast_delete(buf, ptr - buf->p); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1382 | } |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1383 | msg->sol = 0; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 1384 | msg->sl.rq.l = 0; /* used in debug mode */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1385 | state = HTTP_MSG_RQMETH; |
| 1386 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1387 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1388 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1389 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1390 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1391 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1392 | if (unlikely(*ptr == '\n')) |
| 1393 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1394 | 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] | 1395 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1396 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1397 | case HTTP_MSG_RQBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1398 | http_msg_rqbefore_cr: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1399 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1400 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1401 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1402 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1403 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1404 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1405 | case HTTP_MSG_RQMETH_SP: |
| 1406 | case HTTP_MSG_RQURI: |
| 1407 | case HTTP_MSG_RQURI_SP: |
| 1408 | case HTTP_MSG_RQVER: |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1409 | ptr = (char *)http_parse_reqline(msg, |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1410 | state, ptr, end, |
| 1411 | &msg->next, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1412 | if (unlikely(!ptr)) |
| 1413 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1414 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1415 | /* we have a full request and we know that we have either a CR |
| 1416 | * or an LF at <ptr>. |
| 1417 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1418 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1419 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1420 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1421 | if (likely(*ptr == '\r')) |
| 1422 | 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] | 1423 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1424 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1425 | case HTTP_MSG_RQLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1426 | http_msg_rqline_end: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1427 | /* check for HTTP/0.9 request : no version information available. |
| 1428 | * msg->sol must point to the first of CR or LF. |
| 1429 | */ |
| 1430 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1431 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1432 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1433 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1434 | 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] | 1435 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1436 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1437 | /* |
| 1438 | * Common states below |
| 1439 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1440 | case HTTP_MSG_HDR_FIRST: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1441 | http_msg_hdr_first: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1442 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1443 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1444 | goto http_msg_hdr_name; |
| 1445 | } |
| 1446 | |
| 1447 | if (likely(*ptr == '\r')) |
| 1448 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1449 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1450 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1451 | case HTTP_MSG_HDR_NAME: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1452 | http_msg_hdr_name: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1453 | /* assumes msg->sol points to the first char */ |
| 1454 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1455 | 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] | 1456 | |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1457 | if (likely(*ptr == ':')) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1458 | 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] | 1459 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1460 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1461 | goto http_msg_invalid; |
| 1462 | |
| 1463 | if (msg->err_pos == -1) /* capture error pointer */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1464 | msg->err_pos = ptr - buf->p; /* >= 0 now */ |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1465 | |
| 1466 | /* and we still accept this non-token character */ |
| 1467 | 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] | 1468 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1469 | case HTTP_MSG_HDR_L1_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1470 | http_msg_hdr_l1_sp: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1471 | /* assumes msg->sol points to the first char */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1472 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1473 | 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] | 1474 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1475 | /* header value can be basically anything except CR/LF */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1476 | msg->sov = ptr - buf->p; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1477 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1478 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1479 | goto http_msg_hdr_val; |
| 1480 | } |
| 1481 | |
| 1482 | if (likely(*ptr == '\r')) |
| 1483 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1484 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1485 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1486 | case HTTP_MSG_HDR_L1_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1487 | http_msg_hdr_l1_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1488 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1489 | 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] | 1490 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1491 | case HTTP_MSG_HDR_L1_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1492 | http_msg_hdr_l1_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1493 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1494 | /* replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1495 | for (; buf->p + msg->sov < ptr; msg->sov++) |
| 1496 | buf->p[msg->sov] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1497 | goto http_msg_hdr_l1_sp; |
| 1498 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1499 | /* we had a header consisting only in spaces ! */ |
Willy Tarreau | 12e48b3 | 2012-03-05 16:57:34 +0100 | [diff] [blame] | 1500 | msg->eol = msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1501 | goto http_msg_complete_header; |
| 1502 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1503 | case HTTP_MSG_HDR_VAL: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1504 | http_msg_hdr_val: |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1505 | /* assumes msg->sol points to the first char, and msg->sov |
| 1506 | * points to the first character of the value. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1507 | */ |
| 1508 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1509 | 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] | 1510 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1511 | msg->eol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1512 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1513 | * real header end in case it ends with lots of LWS, but is this |
| 1514 | * really needed ? |
| 1515 | */ |
| 1516 | if (likely(*ptr == '\r')) |
| 1517 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1518 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1519 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1520 | case HTTP_MSG_HDR_L2_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1521 | http_msg_hdr_l2_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1522 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1523 | 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] | 1524 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1525 | case HTTP_MSG_HDR_L2_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1526 | http_msg_hdr_l2_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1527 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1528 | /* LWS: replace HT,CR,LF with spaces */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1529 | for (; buf->p + msg->eol < ptr; msg->eol++) |
| 1530 | buf->p[msg->eol] = ' '; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1531 | goto http_msg_hdr_val; |
| 1532 | } |
| 1533 | http_msg_complete_header: |
| 1534 | /* |
| 1535 | * It was a new header, so the last one is finished. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 1536 | * Assumes msg->sol points to the first char, msg->sov points |
| 1537 | * to the first character of the value and msg->eol to the |
| 1538 | * first CR or LF so we know how the line ends. We insert last |
| 1539 | * header into the index. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1540 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1541 | 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] | 1542 | idx, idx->tail) < 0)) |
| 1543 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1544 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1545 | msg->sol = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1546 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1547 | goto http_msg_hdr_name; |
| 1548 | } |
| 1549 | |
| 1550 | if (likely(*ptr == '\r')) |
| 1551 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1552 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1553 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1554 | case HTTP_MSG_LAST_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1555 | http_msg_last_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1556 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1557 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1558 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1559 | msg->sov = msg->next = ptr - buf->p; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 1560 | msg->eoh = msg->sol; |
| 1561 | msg->sol = 0; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1562 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1563 | return; |
Willy Tarreau | b56928a | 2012-04-16 14:51:55 +0200 | [diff] [blame] | 1564 | |
| 1565 | case HTTP_MSG_ERROR: |
| 1566 | /* this may only happen if we call http_msg_analyser() twice with an error */ |
| 1567 | break; |
| 1568 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1569 | #ifdef DEBUG_FULL |
| 1570 | default: |
| 1571 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1572 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1573 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1574 | } |
| 1575 | http_msg_ood: |
| 1576 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1577 | msg->msg_state = state; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1578 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1579 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1580 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1581 | http_msg_invalid: |
| 1582 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1583 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1584 | msg->next = ptr - buf->p; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1585 | return; |
| 1586 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1587 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1588 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1589 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1590 | * nothing is done and 1 is returned. |
| 1591 | */ |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1592 | static int http_upgrade_v09_to_v10(struct http_txn *txn) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1593 | { |
| 1594 | int delta; |
| 1595 | char *cur_end; |
Willy Tarreau | 418bfcc | 2012-03-09 13:56:20 +0100 | [diff] [blame] | 1596 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1597 | |
| 1598 | if (msg->sl.rq.v_l != 0) |
| 1599 | return 1; |
| 1600 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1601 | cur_end = msg->chn->buf->p + msg->sl.rq.l; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1602 | delta = 0; |
| 1603 | |
| 1604 | if (msg->sl.rq.u_l == 0) { |
| 1605 | /* if no URI was set, add "/" */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1606 | delta = buffer_replace2(msg->chn->buf, cur_end, cur_end, " /", 2); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1607 | cur_end += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1608 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1609 | } |
| 1610 | /* add HTTP version */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1611 | 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] | 1612 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1613 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 1614 | cur_end = (char *)http_parse_reqline(msg, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1615 | HTTP_MSG_RQMETH, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1616 | msg->chn->buf->p, cur_end + 1, |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1617 | NULL, NULL); |
| 1618 | if (unlikely(!cur_end)) |
| 1619 | return 0; |
| 1620 | |
| 1621 | /* we have a full HTTP/1.0 request now and we know that |
| 1622 | * we have either a CR or an LF at <ptr>. |
| 1623 | */ |
| 1624 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1625 | return 1; |
| 1626 | } |
| 1627 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1628 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1629 | * and "keep-alive" values. If we already know that some headers may safely |
| 1630 | * 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] | 1631 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1632 | * - 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] | 1633 | * Presence of the "Upgrade" token is also checked and reported. |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1634 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1635 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1636 | * harmless combinations may be removed. Do not call that after changes have |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1637 | * been processed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1638 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1639 | 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] | 1640 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1641 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1642 | const char *hdr_val = "Connection"; |
| 1643 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1644 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1645 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1646 | return; |
| 1647 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1648 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1649 | hdr_val = "Proxy-Connection"; |
| 1650 | hdr_len = 16; |
| 1651 | } |
| 1652 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1653 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1654 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1655 | 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] | 1656 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1657 | txn->flags |= TX_HDR_CONN_KAL; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1658 | if (to_del & 2) |
| 1659 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1660 | else |
| 1661 | txn->flags |= TX_CON_KAL_SET; |
| 1662 | } |
| 1663 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1664 | txn->flags |= TX_HDR_CONN_CLO; |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1665 | if (to_del & 1) |
| 1666 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1667 | else |
| 1668 | txn->flags |= TX_CON_CLO_SET; |
| 1669 | } |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 1670 | else if (ctx.vlen >= 7 && word_match(ctx.line + ctx.val, ctx.vlen, "upgrade", 7)) { |
| 1671 | txn->flags |= TX_HDR_CONN_UPG; |
| 1672 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1673 | } |
| 1674 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1675 | txn->flags |= TX_HDR_CONN_PRS; |
| 1676 | return; |
| 1677 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1678 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1679 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 1680 | * added depending on the <wanted> flags, which are exclusively composed of |
| 1681 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 1682 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 1683 | */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1684 | 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] | 1685 | { |
| 1686 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1687 | const char *hdr_val = "Connection"; |
| 1688 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1689 | |
| 1690 | ctx.idx = 0; |
| 1691 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1692 | |
| 1693 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1694 | hdr_val = "Proxy-Connection"; |
| 1695 | hdr_len = 16; |
| 1696 | } |
| 1697 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1698 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1699 | 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] | 1700 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1701 | if (wanted & TX_CON_KAL_SET) |
| 1702 | txn->flags |= TX_CON_KAL_SET; |
| 1703 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1704 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1705 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1706 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1707 | if (wanted & TX_CON_CLO_SET) |
| 1708 | txn->flags |= TX_CON_CLO_SET; |
| 1709 | else |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1710 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 1711 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1712 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1713 | |
| 1714 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 1715 | return; |
| 1716 | |
| 1717 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 1718 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1719 | hdr_val = "Connection: close"; |
| 1720 | hdr_len = 17; |
| 1721 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1722 | hdr_val = "Proxy-Connection: close"; |
| 1723 | hdr_len = 23; |
| 1724 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1725 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1726 | } |
| 1727 | |
| 1728 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 1729 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1730 | hdr_val = "Connection: keep-alive"; |
| 1731 | hdr_len = 22; |
| 1732 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1733 | hdr_val = "Proxy-Connection: keep-alive"; |
| 1734 | hdr_len = 28; |
| 1735 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 1736 | http_header_add_tail2(msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1737 | } |
| 1738 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1739 | } |
| 1740 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1741 | /* 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] | 1742 | * 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] | 1743 | * so that we know we can forward between ->sol and ->sov. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1744 | * 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] | 1745 | * 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] | 1746 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 1747 | static inline int http_parse_chunk_size(struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1748 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1749 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1750 | const char *ptr = b_ptr(buf, msg->next); |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1751 | const char *ptr_old = ptr; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1752 | const char *end = buf->data + buf->size; |
| 1753 | const char *stop = bi_end(buf); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1754 | unsigned int chunk = 0; |
| 1755 | |
| 1756 | /* The chunk size is in the following form, though we are only |
| 1757 | * interested in the size and CRLF : |
| 1758 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 1759 | */ |
| 1760 | while (1) { |
| 1761 | int c; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1762 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1763 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1764 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1765 | if (c < 0) /* not a hex digit anymore */ |
| 1766 | break; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1767 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1768 | ptr = buf->data; |
Willy Tarreau | 431946e | 2012-02-24 19:20:12 +0100 | [diff] [blame] | 1769 | if (chunk & 0xF8000000) /* integer overflow will occur if result >= 2GB */ |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1770 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1771 | chunk = (chunk << 4) + c; |
| 1772 | } |
| 1773 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1774 | /* empty size not allowed */ |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1775 | if (ptr == ptr_old) |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1776 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1777 | |
| 1778 | while (http_is_spht[(unsigned char)*ptr]) { |
| 1779 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1780 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1781 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1782 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1783 | } |
| 1784 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1785 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 1786 | * for the end of chunk size. |
| 1787 | */ |
| 1788 | while (1) { |
| 1789 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1790 | /* we now have a CR or an LF at ptr */ |
| 1791 | if (likely(*ptr == '\r')) { |
| 1792 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1793 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1794 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1795 | return 0; |
| 1796 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1797 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1798 | if (*ptr != '\n') |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1799 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1800 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1801 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1802 | /* done */ |
| 1803 | break; |
| 1804 | } |
| 1805 | else if (*ptr == ';') { |
| 1806 | /* chunk extension, ends at next CRLF */ |
| 1807 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1808 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1809 | if (ptr == stop) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1810 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1811 | |
| 1812 | while (!HTTP_IS_CRLF(*ptr)) { |
| 1813 | if (++ptr >= end) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1814 | ptr = buf->data; |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1815 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1816 | return 0; |
| 1817 | } |
| 1818 | /* we have a CRLF now, loop above */ |
| 1819 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1820 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1821 | else |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1822 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1823 | } |
| 1824 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1825 | /* 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] | 1826 | * 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] | 1827 | * ->sov. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1828 | */ |
Willy Tarreau | b8ffd37 | 2012-09-27 15:08:56 +0200 | [diff] [blame] | 1829 | if (ptr < ptr_old) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1830 | msg->sov += buf->size; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1831 | msg->sov += ptr - ptr_old; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1832 | msg->next = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 1833 | msg->chunk_len = chunk; |
| 1834 | msg->body_len += chunk; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1835 | msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1836 | return 1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1837 | error: |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1838 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1839 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1840 | } |
| 1841 | |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1842 | /* This function skips trailers in the buffer associated with HTTP |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1843 | * 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] | 1844 | * the trailers is found, it is automatically scheduled to be forwarded, |
| 1845 | * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0. |
| 1846 | * If not enough data are available, the function does not change anything |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1847 | * 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] | 1848 | * 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] | 1849 | * 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] | 1850 | * must already be in HTTP_MSG_TRAILERS state before calling this function, |
| 1851 | * which implies that all non-trailers data have already been scheduled for |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1852 | * forwarding, and that the difference between msg->sol and msg->sov exactly |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1853 | * matches the length of trailers already parsed and not forwarded. It is also |
| 1854 | * important to note that this function is designed to be able to parse wrapped |
| 1855 | * headers at end of buffer. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1856 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 1857 | static int http_forward_trailers(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1858 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1859 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1860 | |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 1861 | /* we have msg->next which points to next line. Look for CRLF. */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1862 | while (1) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1863 | const char *p1 = NULL, *p2 = NULL; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1864 | const char *ptr = b_ptr(buf, msg->next); |
| 1865 | const char *stop = bi_end(buf); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1866 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1867 | |
| 1868 | /* scan current line and stop at LF or CRLF */ |
| 1869 | while (1) { |
Willy Tarreau | 363a5bb | 2012-03-02 20:14:45 +0100 | [diff] [blame] | 1870 | if (ptr == stop) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1871 | return 0; |
| 1872 | |
| 1873 | if (*ptr == '\n') { |
| 1874 | if (!p1) |
| 1875 | p1 = ptr; |
| 1876 | p2 = ptr; |
| 1877 | break; |
| 1878 | } |
| 1879 | |
| 1880 | if (*ptr == '\r') { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1881 | if (p1) { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1882 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1883 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1884 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1885 | p1 = ptr; |
| 1886 | } |
| 1887 | |
| 1888 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1889 | if (ptr >= buf->data + buf->size) |
| 1890 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1891 | } |
| 1892 | |
| 1893 | /* after LF; point to beginning of next line */ |
| 1894 | p2++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1895 | if (p2 >= buf->data + buf->size) |
| 1896 | p2 = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1897 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1898 | bytes = p2 - b_ptr(buf, msg->next); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1899 | if (bytes < 0) |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1900 | bytes += buf->size; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1901 | |
| 1902 | /* schedule this line for forwarding */ |
| 1903 | msg->sov += bytes; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1904 | if (msg->sov >= buf->size) |
| 1905 | msg->sov -= buf->size; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1906 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1907 | if (p1 == b_ptr(buf, msg->next)) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 1908 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 1909 | * Everything was scheduled for forwarding, there's nothing |
| 1910 | * left from this message. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 1911 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1912 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1913 | msg->msg_state = HTTP_MSG_DONE; |
| 1914 | return 1; |
| 1915 | } |
| 1916 | /* OK, next line then */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1917 | msg->next = buffer_count(buf, buf->p, p2); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1918 | } |
| 1919 | } |
| 1920 | |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 1921 | /* 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] | 1922 | * 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] | 1923 | * ->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] | 1924 | * 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] | 1925 | * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If |
| 1926 | * not enough data are available, the function does not change anything and |
| 1927 | * returns zero. If a parse error is encountered, the function returns < 0 and |
| 1928 | * does not change anything. Note: this function is designed to parse wrapped |
| 1929 | * CRLF at the end of the buffer. |
| 1930 | */ |
Willy Tarreau | 24e6d97 | 2012-10-26 00:49:52 +0200 | [diff] [blame] | 1931 | static inline int http_skip_chunk_crlf(struct http_msg *msg) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1932 | { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 1933 | const struct buffer *buf = msg->chn->buf; |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 1934 | const char *ptr; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1935 | int bytes; |
| 1936 | |
| 1937 | /* NB: we'll check data availabilty at the end. It's not a |
| 1938 | * problem because whatever we match first will be checked |
| 1939 | * against the correct length. |
| 1940 | */ |
| 1941 | bytes = 1; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1942 | ptr = buf->p; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1943 | if (*ptr == '\r') { |
| 1944 | bytes++; |
| 1945 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1946 | if (ptr >= buf->data + buf->size) |
| 1947 | ptr = buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1948 | } |
| 1949 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1950 | if (bytes > buf->i) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1951 | return 0; |
| 1952 | |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1953 | if (*ptr != '\n') { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1954 | msg->err_pos = buffer_count(buf, buf->p, ptr); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1955 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 1956 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1957 | |
| 1958 | ptr++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 1959 | if (ptr >= buf->data + buf->size) |
| 1960 | ptr = buf->data; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 1961 | /* prepare the CRLF to be forwarded (between ->sol and ->sov) */ |
| 1962 | msg->sol = 0; |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 1963 | msg->sov = msg->next = bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1964 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 1965 | return 1; |
| 1966 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1967 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 1968 | |
| 1969 | /* |
| 1970 | * Selects a compression algorithm depending on the client request. |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 1971 | */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 1972 | int select_compression_request_header(struct session *s, struct buffer *req) |
| 1973 | { |
| 1974 | struct http_txn *txn = &s->txn; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 1975 | struct http_msg *msg = &txn->req; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 1976 | struct hdr_ctx ctx; |
| 1977 | struct comp_algo *comp_algo = NULL; |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 1978 | struct comp_algo *comp_algo_back = NULL; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 1979 | |
Finn Arne Gangstad | cbb9a4b | 2012-10-29 21:43:01 +0100 | [diff] [blame] | 1980 | /* Disable compression for older user agents announcing themselves as "Mozilla/4" |
| 1981 | * 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] | 1982 | * See http://zoompf.com/2012/02/lose-the-wait-http-compression for more details. |
| 1983 | */ |
| 1984 | ctx.idx = 0; |
| 1985 | if (http_find_header2("User-Agent", 10, req->p, &txn->hdr_idx, &ctx) && |
| 1986 | ctx.vlen >= 9 && |
Finn Arne Gangstad | cbb9a4b | 2012-10-29 21:43:01 +0100 | [diff] [blame] | 1987 | memcmp(ctx.line + ctx.val, "Mozilla/4", 9) == 0 && |
| 1988 | (ctx.vlen < 31 || |
| 1989 | memcmp(ctx.line + ctx.val + 25, "MSIE ", 5) != 0 || |
| 1990 | ctx.line[ctx.val + 30] < '6' || |
| 1991 | (ctx.line[ctx.val + 30] == '6' && |
| 1992 | (ctx.vlen < 54 || memcmp(ctx.line + 51, "SV1", 3) != 0)))) { |
| 1993 | s->comp_algo = NULL; |
| 1994 | return 0; |
Willy Tarreau | 05d8460 | 2012-10-26 02:11:25 +0200 | [diff] [blame] | 1995 | } |
| 1996 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 1997 | ctx.idx = 0; |
| 1998 | /* no compression when Cache-Control: no-transform found */ |
| 1999 | while (http_find_header2("Cache-Control", 13, req->p, &txn->hdr_idx, &ctx)) { |
| 2000 | if (word_match(ctx.line + ctx.val, ctx.vlen, "no-transform", 12)) { |
| 2001 | s->comp_algo = NULL; |
| 2002 | return 0; |
| 2003 | } |
| 2004 | } |
| 2005 | |
| 2006 | /* search for the algo in the backend in priority or the frontend */ |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2007 | 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] | 2008 | ctx.idx = 0; |
| 2009 | 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] | 2010 | 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] | 2011 | if (word_match(ctx.line + ctx.val, ctx.vlen, comp_algo->name, comp_algo->name_len)) { |
| 2012 | s->comp_algo = comp_algo; |
Willy Tarreau | 70737d1 | 2012-10-27 00:34:28 +0200 | [diff] [blame] | 2013 | |
| 2014 | /* remove all occurrences of the header when "compression offload" is set */ |
| 2015 | |
| 2016 | if ((s->be->comp && s->be->comp->offload) || |
| 2017 | (s->fe->comp && s->fe->comp->offload)) { |
| 2018 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2019 | ctx.idx = 0; |
| 2020 | while (http_find_header2("Accept-Encoding", 15, req->p, &txn->hdr_idx, &ctx)) { |
| 2021 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2022 | } |
| 2023 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2024 | return 1; |
| 2025 | } |
| 2026 | } |
| 2027 | } |
| 2028 | } |
| 2029 | |
| 2030 | /* identity is implicit does not require headers */ |
Willy Tarreau | 3c7b97b | 2012-10-26 14:50:26 +0200 | [diff] [blame] | 2031 | if ((s->be->comp && (comp_algo_back = s->be->comp->algos)) || (s->fe->comp && (comp_algo_back = s->fe->comp->algos))) { |
| 2032 | 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] | 2033 | if (comp_algo->add_data == identity_add_data) { |
| 2034 | s->comp_algo = comp_algo; |
| 2035 | return 1; |
| 2036 | } |
| 2037 | } |
| 2038 | } |
| 2039 | |
| 2040 | s->comp_algo = NULL; |
| 2041 | |
| 2042 | return 0; |
| 2043 | } |
| 2044 | |
| 2045 | /* |
| 2046 | * Selects a comression algorithm depending of the server response. |
| 2047 | */ |
| 2048 | int select_compression_response_header(struct session *s, struct buffer *res) |
| 2049 | { |
| 2050 | struct http_txn *txn = &s->txn; |
| 2051 | struct http_msg *msg = &txn->rsp; |
| 2052 | struct hdr_ctx ctx; |
| 2053 | struct comp_type *comp_type; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2054 | |
| 2055 | /* no common compression algorithm was found in request header */ |
| 2056 | if (s->comp_algo == NULL) |
| 2057 | goto fail; |
| 2058 | |
| 2059 | /* HTTP < 1.1 should not be compressed */ |
| 2060 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
| 2061 | goto fail; |
| 2062 | |
William Lallemand | d300261 | 2012-11-26 14:34:47 +0100 | [diff] [blame] | 2063 | /* 200 only */ |
| 2064 | if (txn->status != 200) |
| 2065 | goto fail; |
| 2066 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2067 | /* Content-Length is null */ |
| 2068 | if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0) |
| 2069 | goto fail; |
| 2070 | |
| 2071 | /* content is already compressed */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2072 | ctx.idx = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2073 | if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx)) |
| 2074 | goto fail; |
| 2075 | |
| 2076 | comp_type = NULL; |
| 2077 | |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2078 | /* we don't want to compress multipart content-types, nor content-types that are |
| 2079 | * not listed in the "compression type" directive if any. If no content-type was |
| 2080 | * found but configuration requires one, we don't compress either. Backend has |
| 2081 | * the priority. |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2082 | */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2083 | ctx.idx = 0; |
| 2084 | if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) { |
| 2085 | if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0) |
| 2086 | goto fail; |
| 2087 | |
| 2088 | if ((s->be->comp && (comp_type = s->be->comp->types)) || |
| 2089 | (s->fe->comp && (comp_type = s->fe->comp->types))) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2090 | for (; comp_type; comp_type = comp_type->next) { |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2091 | if (ctx.vlen >= comp_type->name_len && |
| 2092 | 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] | 2093 | /* this Content-Type should be compressed */ |
| 2094 | break; |
| 2095 | } |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2096 | /* this Content-Type should not be compressed */ |
| 2097 | if (comp_type == NULL) |
| 2098 | goto fail; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2099 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2100 | } |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2101 | else { /* no content-type header */ |
| 2102 | if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types)) |
| 2103 | goto fail; /* a content-type was required */ |
William Lallemand | d300261 | 2012-11-26 14:34:47 +0100 | [diff] [blame] | 2104 | } |
| 2105 | |
William Lallemand | d85f917 | 2012-11-09 17:05:39 +0100 | [diff] [blame] | 2106 | /* limit compression rate */ |
| 2107 | if (global.comp_rate_lim > 0) |
| 2108 | if (read_freq_ctr(&global.comp_bps_in) > global.comp_rate_lim) |
| 2109 | goto fail; |
| 2110 | |
William Lallemand | 072a2bf | 2012-11-20 17:01:01 +0100 | [diff] [blame] | 2111 | /* limit cpu usage */ |
| 2112 | if (idle_pct < compress_min_idle) |
| 2113 | goto fail; |
| 2114 | |
William Lallemand | 4c49fae | 2012-11-07 15:00:23 +0100 | [diff] [blame] | 2115 | /* initialize compression */ |
William Lallemand | f374783 | 2012-11-09 12:33:10 +0100 | [diff] [blame] | 2116 | 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] | 2117 | goto fail; |
| 2118 | |
William Lallemand | ec3e389 | 2012-11-12 17:02:18 +0100 | [diff] [blame] | 2119 | s->flags |= SN_COMP_READY; |
| 2120 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2121 | /* remove Content-Length header */ |
Willy Tarreau | 0a80a8d | 2012-11-26 16:33:37 +0100 | [diff] [blame] | 2122 | ctx.idx = 0; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2123 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx)) |
| 2124 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 2125 | |
| 2126 | /* add Transfer-Encoding header */ |
| 2127 | if (!(msg->flags & HTTP_MSGF_TE_CHNK)) |
| 2128 | http_header_add_tail2(&txn->rsp, &txn->hdr_idx, "Transfer-Encoding: chunked", 26); |
| 2129 | |
| 2130 | /* |
| 2131 | * Add Content-Encoding header when it's not identity encoding. |
| 2132 | * RFC 2616 : Identity encoding: This content-coding is used only in the |
| 2133 | * Accept-Encoding header, and SHOULD NOT be used in the Content-Encoding |
| 2134 | * header. |
| 2135 | */ |
| 2136 | if (s->comp_algo->add_data != identity_add_data) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2137 | trash.len = 18; |
| 2138 | memcpy(trash.str, "Content-Encoding: ", trash.len); |
| 2139 | memcpy(trash.str + trash.len, s->comp_algo->name, s->comp_algo->name_len); |
| 2140 | trash.len += s->comp_algo->name_len; |
| 2141 | trash.str[trash.len] = '\0'; |
| 2142 | 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] | 2143 | } |
| 2144 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2145 | return 1; |
| 2146 | |
| 2147 | fail: |
Willy Tarreau | b97b619 | 2012-11-19 14:55:02 +0100 | [diff] [blame] | 2148 | if (s->flags & SN_COMP_READY) |
William Lallemand | 1c2d622 | 2012-10-30 15:52:53 +0100 | [diff] [blame] | 2149 | s->comp_algo->end(&s->comp_ctx); |
Willy Tarreau | b97b619 | 2012-11-19 14:55:02 +0100 | [diff] [blame] | 2150 | s->comp_algo = NULL; |
| 2151 | s->flags &= ~SN_COMP_READY; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 2152 | return 0; |
| 2153 | } |
| 2154 | |
| 2155 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2156 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2157 | * processing can continue on next analysers, or zero if it either needs more |
| 2158 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 2159 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers |
| 2160 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2161 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2162 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 2163 | 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] | 2164 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2165 | /* |
| 2166 | * We will parse the partial (or complete) lines. |
| 2167 | * We will check the request syntax, and also join multi-line |
| 2168 | * headers. An index of all the lines will be elaborated while |
| 2169 | * parsing. |
| 2170 | * |
| 2171 | * For the parsing, we use a 28 states FSM. |
| 2172 | * |
| 2173 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2174 | * req->buf->p = beginning of request |
| 2175 | * req->buf->p + msg->eoh = end of processed headers / start of current one |
| 2176 | * req->buf->p + req->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 2177 | * msg->eol = end of current header or line (LF or CRLF) |
| 2178 | * msg->next = first non-visited byte |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2179 | * |
| 2180 | * At end of parsing, we may perform a capture of the error (if any), and |
| 2181 | * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE). |
| 2182 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2183 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2184 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2185 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2186 | int cur_idx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2187 | int use_close_only; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2188 | struct http_txn *txn = &s->txn; |
| 2189 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2190 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2191 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2192 | 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] | 2193 | now_ms, __FUNCTION__, |
| 2194 | s, |
| 2195 | req, |
| 2196 | req->rex, req->wex, |
| 2197 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2198 | req->buf->i, |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2199 | req->analysers); |
| 2200 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2201 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2202 | s->srv_error = http_return_srv_error; |
| 2203 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2204 | /* There's a protected area at the end of the buffer for rewriting |
| 2205 | * purposes. We don't want to start to parse the request if the |
| 2206 | * protected area is affected, because we may have to move processed |
| 2207 | * data later, which is much more complicated. |
| 2208 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2209 | if (buffer_not_empty(req->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2210 | if ((txn->flags & TX_NOT_FIRST) && |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 2211 | unlikely(channel_full(req) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2212 | bi_end(req->buf) < b_ptr(req->buf, msg->next) || |
| 2213 | bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite)) { |
| 2214 | if (req->buf->o) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2215 | 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] | 2216 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2217 | /* 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] | 2218 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2219 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2220 | return 0; |
| 2221 | } |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2222 | if (bi_end(req->buf) < b_ptr(req->buf, msg->next) || |
| 2223 | bi_end(req->buf) > req->buf->data + req->buf->size - global.tune.maxrewrite) |
| 2224 | buffer_slow_realign(msg->chn->buf); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2225 | } |
| 2226 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2227 | /* Note that we have the same problem with the response ; we |
| 2228 | * may want to send a redirect, error or anything which requires |
| 2229 | * some spare space. So we'll ensure that we have at least |
| 2230 | * maxrewrite bytes available in the response buffer before |
| 2231 | * processing that one. This will only affect pipelined |
| 2232 | * keep-alive requests. |
| 2233 | */ |
| 2234 | if ((txn->flags & TX_NOT_FIRST) && |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 2235 | unlikely(channel_full(s->rep) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2236 | bi_end(s->rep->buf) < b_ptr(s->rep->buf, txn->rsp.next) || |
| 2237 | bi_end(s->rep->buf) > s->rep->buf->data + s->rep->buf->size - global.tune.maxrewrite)) { |
| 2238 | if (s->rep->buf->o) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2239 | 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] | 2240 | goto failed_keep_alive; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2241 | /* don't let a connection request be initiated */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2242 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2243 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 0499e35 | 2010-12-17 07:13:42 +0100 | [diff] [blame] | 2244 | s->rep->analysers |= an_bit; /* wake us up once it changes */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2245 | return 0; |
| 2246 | } |
| 2247 | } |
| 2248 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2249 | if (likely(msg->next < req->buf->i)) /* some unparsed data are available */ |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 2250 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2251 | } |
| 2252 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2253 | /* 1: we might have to print this header in debug mode */ |
| 2254 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2255 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2256 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2257 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2258 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2259 | sol = req->buf->p; |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 2260 | /* this is a bit complex : in case of error on the request line, |
| 2261 | * we know that rq.l is still zero, so we display only the part |
| 2262 | * up to the end of the line (truncated by debug_hdr). |
| 2263 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2264 | 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] | 2265 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2266 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2267 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2268 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2269 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2270 | while (cur_idx) { |
| 2271 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2272 | debug_hdr("clihdr", s, sol, eol); |
| 2273 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2274 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2275 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2276 | } |
| 2277 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2278 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2279 | /* |
| 2280 | * Now we quickly check if we have found a full valid request. |
| 2281 | * If not so, we check the FD and buffer states before leaving. |
| 2282 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2283 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2284 | * requests are checked first. When waiting for a second request |
| 2285 | * on a keep-alive session, if we encounter and error, close, t/o, |
| 2286 | * we note the error in the session flags but don't set any state. |
| 2287 | * Since the error will be noted there, it will not be counted by |
| 2288 | * process_session() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2289 | * Last, we may increase some tracked counters' http request errors on |
| 2290 | * the cases that are deliberately the client's fault. For instance, |
| 2291 | * a timeout or connection reset is not counted as an error. However |
| 2292 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2293 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2294 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2295 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2296 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2297 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2298 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2299 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2300 | session_inc_http_req_ctr(s); |
| 2301 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2302 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2303 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2304 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2305 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2306 | /* 1: Since we are in header mode, if there's no space |
| 2307 | * left for headers, we won't be able to free more |
| 2308 | * later, so the session will never terminate. We |
| 2309 | * must terminate it now. |
| 2310 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2311 | if (unlikely(buffer_full(req->buf, global.tune.maxrewrite))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2312 | /* FIXME: check if URI is set and return Status |
| 2313 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2314 | */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2315 | session_inc_http_req_ctr(s); |
| 2316 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2317 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 2318 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2319 | msg->err_pos = req->buf->i; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2320 | goto return_bad_req; |
| 2321 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2322 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2323 | /* 2: have we encountered a read error ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2324 | else if (req->flags & CF_READ_ERROR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2325 | if (!(s->flags & SN_ERR_MASK)) |
| 2326 | s->flags |= SN_ERR_CLICL; |
| 2327 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2328 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2329 | goto failed_keep_alive; |
| 2330 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2331 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2332 | if (msg->err_pos >= 0) { |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2333 | 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] | 2334 | session_inc_http_err_ctr(s); |
| 2335 | } |
| 2336 | |
Willy Tarreau | dc979f2 | 2012-12-04 10:39:01 +0100 | [diff] [blame] | 2337 | txn->status = 400; |
| 2338 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2339 | msg->msg_state = HTTP_MSG_ERROR; |
| 2340 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2341 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2342 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2343 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2344 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2345 | if (s->listener->counters) |
| 2346 | s->listener->counters->failed_req++; |
| 2347 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2348 | if (!(s->flags & SN_FINST_MASK)) |
| 2349 | s->flags |= SN_FINST_R; |
| 2350 | return 0; |
| 2351 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2352 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2353 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2354 | 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] | 2355 | if (!(s->flags & SN_ERR_MASK)) |
| 2356 | s->flags |= SN_ERR_CLITO; |
| 2357 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2358 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2359 | goto failed_keep_alive; |
| 2360 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2361 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2362 | if (msg->err_pos >= 0) { |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2363 | 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] | 2364 | session_inc_http_err_ctr(s); |
| 2365 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2366 | txn->status = 408; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2367 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2368 | msg->msg_state = HTTP_MSG_ERROR; |
| 2369 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2370 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2371 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2372 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2373 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2374 | if (s->listener->counters) |
| 2375 | s->listener->counters->failed_req++; |
| 2376 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2377 | if (!(s->flags & SN_FINST_MASK)) |
| 2378 | s->flags |= SN_FINST_R; |
| 2379 | return 0; |
| 2380 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2381 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2382 | /* 4: have we encountered a close ? */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2383 | else if (req->flags & CF_SHUTR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2384 | if (!(s->flags & SN_ERR_MASK)) |
| 2385 | s->flags |= SN_ERR_CLICL; |
| 2386 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2387 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2388 | goto failed_keep_alive; |
| 2389 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2390 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2391 | 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] | 2392 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2393 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2394 | msg->msg_state = HTTP_MSG_ERROR; |
| 2395 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2396 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2397 | session_inc_http_err_ctr(s); |
| 2398 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2399 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2400 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2401 | if (s->listener->counters) |
| 2402 | s->listener->counters->failed_req++; |
| 2403 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2404 | if (!(s->flags & SN_FINST_MASK)) |
| 2405 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2406 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2407 | } |
| 2408 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2409 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2410 | req->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 2411 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2412 | #ifdef TCP_QUICKACK |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2413 | if (s->listener->options & LI_O_NOQUICKACK && req->buf->i) { |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 2414 | /* We need more data, we have to re-enable quick-ack in case we |
| 2415 | * previously disabled it, otherwise we might cause the client |
| 2416 | * to delay next data. |
| 2417 | */ |
Willy Tarreau | 7f7ad91 | 2012-11-11 19:27:15 +0100 | [diff] [blame] | 2418 | 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] | 2419 | } |
| 2420 | #endif |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2421 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2422 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2423 | /* If the client starts to talk, let's fall back to |
| 2424 | * request timeout processing. |
| 2425 | */ |
| 2426 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2427 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2428 | } |
| 2429 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2430 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2431 | if (!tick_isset(req->analyse_exp)) { |
| 2432 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2433 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2434 | tick_isset(s->be->timeout.httpka)) |
| 2435 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2436 | else |
| 2437 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2438 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2439 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2440 | /* we're not ready yet */ |
| 2441 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2442 | |
| 2443 | failed_keep_alive: |
| 2444 | /* Here we process low-level errors for keep-alive requests. In |
| 2445 | * short, if the request is not the first one and it experiences |
| 2446 | * a timeout, read error or shutdown, we just silently close so |
| 2447 | * that the client can try again. |
| 2448 | */ |
| 2449 | txn->status = 0; |
| 2450 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 2451 | req->analysers = 0; |
| 2452 | s->logs.logwait = 0; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 2453 | s->rep->flags &= ~CF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 2454 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2455 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2456 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2457 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2458 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2459 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2460 | * 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] | 2461 | * 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] | 2462 | * 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] | 2463 | * byte after the last LF. msg->sov points to the first byte of data. |
| 2464 | * msg->eol cannot be trusted because it may have been left uninitialized |
| 2465 | * (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2466 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2467 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2468 | session_inc_http_req_ctr(s); |
Willy Tarreau | d9b587f | 2010-02-26 10:05:55 +0100 | [diff] [blame] | 2469 | proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */ |
| 2470 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2471 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2472 | /* kill the pending keep-alive timeout */ |
| 2473 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2474 | req->analyse_exp = TICK_ETERNITY; |
| 2475 | } |
| 2476 | |
| 2477 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2478 | /* Maybe we found in invalid header name while we were configured not |
| 2479 | * to block on that, so we have to capture it now. |
| 2480 | */ |
| 2481 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2482 | 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] | 2483 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2484 | /* |
| 2485 | * 1: identify the method |
| 2486 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2487 | 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] | 2488 | |
| 2489 | /* we can make use of server redirect on GET and HEAD */ |
| 2490 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2491 | s->flags |= SN_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2492 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2493 | /* |
| 2494 | * 2: check if the URI matches the monitor_uri. |
| 2495 | * We have to do this for every request which gets in, because |
| 2496 | * the monitor-uri is defined by the frontend. |
| 2497 | */ |
| 2498 | if (unlikely((s->fe->monitor_uri_len != 0) && |
| 2499 | (s->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2500 | !memcmp(req->buf->p + msg->sl.rq.u, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2501 | s->fe->monitor_uri, |
| 2502 | s->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2503 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2504 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2505 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2506 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2507 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2508 | s->flags |= SN_MONITOR; |
Willy Tarreau | eabea07 | 2011-09-10 23:29:44 +0200 | [diff] [blame] | 2509 | s->fe->fe_counters.intercepted_req++; |
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 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2512 | list_for_each_entry(cond, &s->fe->mon_fail_cond, list) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 2513 | 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] | 2514 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2515 | ret = acl_pass(ret); |
| 2516 | if (cond->pol == ACL_COND_UNLESS) |
| 2517 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2518 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2519 | if (ret) { |
| 2520 | /* we fail this request, let's return 503 service unavail */ |
| 2521 | txn->status = 503; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2522 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_503)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2523 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2524 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2525 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2526 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2527 | /* nothing to fail, let's reply normaly */ |
| 2528 | txn->status = 200; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2529 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_200)); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2530 | goto return_prx_cond; |
| 2531 | } |
| 2532 | |
| 2533 | /* |
| 2534 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2535 | * Note: we cannot log anymore if the request has been |
| 2536 | * classified as invalid. |
| 2537 | */ |
| 2538 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2539 | /* we have a complete HTTP request that we must log */ |
| 2540 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2541 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2542 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2543 | if (urilen >= REQURI_LEN) |
| 2544 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2545 | memcpy(txn->uri, req->buf->p, urilen); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2546 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2547 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2548 | if (!(s->logs.logwait &= ~LW_REQ)) |
| 2549 | s->do_log(s); |
| 2550 | } else { |
| 2551 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2552 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2553 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2554 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 2555 | if (!LIST_ISEMPTY(&s->fe->format_unique_id)) { |
| 2556 | s->unique_id = pool_alloc2(pool2_uniqueid); |
| 2557 | } |
| 2558 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2559 | /* 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] | 2560 | 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] | 2561 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2562 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2563 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2564 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2565 | ((req->buf->p[msg->sl.rq.v + 5] > '1') || |
| 2566 | ((req->buf->p[msg->sl.rq.v + 5] == '1') && |
| 2567 | (req->buf->p[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2568 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2569 | |
| 2570 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 2571 | 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] | 2572 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2573 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 2574 | * we have what looks like a proxied connection instead of a connection, |
| 2575 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 2576 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 2577 | * happen to do that despite being non-standard :-( |
| 2578 | * We consider that a request not beginning with either '/' or '*' is |
| 2579 | * a proxied connection, which covers both "scheme://location" and |
| 2580 | * CONNECT ip:port. |
| 2581 | */ |
| 2582 | if ((s->fe->options2 & PR_O2_USE_PXHDR) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2583 | 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] | 2584 | txn->flags |= TX_USE_PX_CONN; |
| 2585 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2586 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2587 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2588 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2589 | /* 5: we may need to capture headers */ |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 2590 | if (unlikely((s->logs.logwait & LW_REQHDR) && txn->req.cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2591 | capture_headers(req->buf->p, &txn->hdr_idx, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2592 | txn->req.cap, s->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2593 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2594 | /* 6: determine the transfer-length. |
| 2595 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 2596 | * the presence of a message-body in a REQUEST and its transfer length |
| 2597 | * must be determined that way (in order of precedence) : |
| 2598 | * 1. The presence of a message-body in a request is signaled by the |
| 2599 | * inclusion of a Content-Length or Transfer-Encoding header field |
| 2600 | * in the request's header fields. When a request message contains |
| 2601 | * both a message-body of non-zero length and a method that does |
| 2602 | * not define any semantics for that request message-body, then an |
| 2603 | * origin server SHOULD either ignore the message-body or respond |
| 2604 | * with an appropriate error message (e.g., 413). A proxy or |
| 2605 | * gateway, when presented the same request, SHOULD either forward |
| 2606 | * the request inbound with the message- body or ignore the |
| 2607 | * message-body when determining a response. |
| 2608 | * |
| 2609 | * 2. If a Transfer-Encoding header field (Section 9.7) is present |
| 2610 | * and the "chunked" transfer-coding (Section 6.2) is used, the |
| 2611 | * transfer-length is defined by the use of this transfer-coding. |
| 2612 | * If a Transfer-Encoding header field is present and the "chunked" |
| 2613 | * transfer-coding is not present, the transfer-length is defined |
| 2614 | * by the sender closing the connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2615 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2616 | * 3. If a Content-Length header field is present, its decimal value in |
| 2617 | * OCTETs represents both the entity-length and the transfer-length. |
| 2618 | * If a message is received with both a Transfer-Encoding header |
| 2619 | * field and a Content-Length header field, the latter MUST be ignored. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2620 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2621 | * 4. By the server closing the connection. (Closing the connection |
| 2622 | * cannot be used to indicate the end of a request body, since that |
| 2623 | * would leave no possibility for the server to send back a response.) |
| 2624 | * |
| 2625 | * Whenever a transfer-coding is applied to a message-body, the set of |
| 2626 | * transfer-codings MUST include "chunked", unless the message indicates |
| 2627 | * it is terminated by closing the connection. When the "chunked" |
| 2628 | * transfer-coding is used, it MUST be the last transfer-coding applied |
| 2629 | * to the message-body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2630 | */ |
| 2631 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2632 | use_close_only = 0; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2633 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2634 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2635 | while ((msg->flags & HTTP_MSGF_VER_11) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2636 | 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] | 2637 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2638 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 2639 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2640 | /* bad transfer-encoding (chunked followed by something else) */ |
| 2641 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2642 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2643 | break; |
| 2644 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2645 | } |
| 2646 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2647 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2648 | while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2649 | 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] | 2650 | signed long long cl; |
| 2651 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2652 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2653 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2654 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2655 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2656 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2657 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2658 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2659 | goto return_bad_req; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2660 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2661 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2662 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2663 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2664 | goto return_bad_req; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2665 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2666 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2667 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2668 | msg->err_pos = ctx.line + ctx.val - req->buf->p; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2669 | goto return_bad_req; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 2670 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2671 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2672 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2673 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2674 | } |
| 2675 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2676 | /* bodyless requests have a known length */ |
| 2677 | if (!use_close_only) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 2678 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2679 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2680 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2681 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2682 | req->analyse_exp = TICK_ETERNITY; |
| 2683 | return 1; |
| 2684 | |
| 2685 | return_bad_req: |
| 2686 | /* We centralize bad requests processing here */ |
| 2687 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2688 | /* we detected a parsing error. We want to archive this request |
| 2689 | * in the dedicated proxy area for later troubleshooting. |
| 2690 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 2691 | 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] | 2692 | } |
| 2693 | |
| 2694 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2695 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 2696 | 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] | 2697 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2698 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2699 | if (s->listener->counters) |
| 2700 | s->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2701 | |
| 2702 | return_prx_cond: |
| 2703 | if (!(s->flags & SN_ERR_MASK)) |
| 2704 | s->flags |= SN_ERR_PRXCOND; |
| 2705 | if (!(s->flags & SN_FINST_MASK)) |
| 2706 | s->flags |= SN_FINST_R; |
| 2707 | |
| 2708 | req->analysers = 0; |
| 2709 | req->analyse_exp = TICK_ETERNITY; |
| 2710 | return 0; |
| 2711 | } |
| 2712 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2713 | /* We reached the stats page through a POST request. |
| 2714 | * Parse the posted data and enable/disable servers if necessary. |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2715 | * 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] | 2716 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 2717 | 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] | 2718 | { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2719 | struct proxy *px = NULL; |
| 2720 | struct server *sv = NULL; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2721 | |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2722 | char key[LINESIZE]; |
| 2723 | int action = ST_ADM_ACTION_NONE; |
| 2724 | int reprocess = 0; |
| 2725 | |
| 2726 | int total_servers = 0; |
| 2727 | int altered_servers = 0; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2728 | |
| 2729 | char *first_param, *cur_param, *next_param, *end_params; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 2730 | char *st_cur_param = NULL; |
| 2731 | char *st_next_param = NULL; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2732 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2733 | first_param = req->buf->p + txn->req.eoh + 2; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2734 | end_params = first_param + txn->req.body_len; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2735 | |
| 2736 | cur_param = next_param = end_params; |
| 2737 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2738 | if (end_params >= req->buf->data + req->buf->size - global.tune.maxrewrite) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2739 | /* Prevent buffer overflow */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2740 | si->applet.ctx.stats.st_code = STAT_STATUS_EXCD; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2741 | return 1; |
| 2742 | } |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2743 | else if (end_params > req->buf->p + req->buf->i) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2744 | /* we need more data */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2745 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2746 | return 0; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2747 | } |
| 2748 | |
| 2749 | *end_params = '\0'; |
| 2750 | |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2751 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2752 | |
| 2753 | /* |
| 2754 | * Parse the parameters in reverse order to only store the last value. |
| 2755 | * From the html form, the backend and the action are at the end. |
| 2756 | */ |
| 2757 | while (cur_param > first_param) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2758 | char *value; |
| 2759 | int poffset, plen; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2760 | |
| 2761 | cur_param--; |
| 2762 | if ((*cur_param == '&') || (cur_param == first_param)) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2763 | reprocess_servers: |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2764 | /* Parse the key */ |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2765 | poffset = (cur_param != first_param ? 1 : 0); |
| 2766 | plen = next_param - cur_param + (cur_param == first_param ? 1 : 0); |
| 2767 | if ((plen > 0) && (plen <= sizeof(key))) { |
| 2768 | strncpy(key, cur_param + poffset, plen); |
| 2769 | key[plen - 1] = '\0'; |
| 2770 | } else { |
| 2771 | si->applet.ctx.stats.st_code = STAT_STATUS_EXCD; |
| 2772 | goto out; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2773 | } |
| 2774 | |
| 2775 | /* Parse the value */ |
| 2776 | value = key; |
| 2777 | while (*value != '\0' && *value != '=') { |
| 2778 | value++; |
| 2779 | } |
| 2780 | if (*value == '=') { |
| 2781 | /* Ok, a value is found, we can mark the end of the key */ |
| 2782 | *value++ = '\0'; |
| 2783 | } |
| 2784 | |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 2785 | if (!url_decode(key) || !url_decode(value)) |
| 2786 | break; |
| 2787 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2788 | /* Now we can check the key to see what to do */ |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2789 | if (!px && (strcmp(key, "b") == 0)) { |
| 2790 | if ((px = findproxy(value, PR_CAP_BE)) == NULL) { |
| 2791 | /* the backend name is unknown or ambiguous (duplicate names) */ |
| 2792 | si->applet.ctx.stats.st_code = STAT_STATUS_ERRP; |
| 2793 | goto out; |
| 2794 | } |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2795 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2796 | else if (!action && (strcmp(key, "action") == 0)) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2797 | if (strcmp(value, "disable") == 0) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2798 | action = ST_ADM_ACTION_DISABLE; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2799 | } |
| 2800 | else if (strcmp(value, "enable") == 0) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2801 | action = ST_ADM_ACTION_ENABLE; |
| 2802 | } |
Willy Tarreau | d728224 | 2012-06-04 00:22:44 +0200 | [diff] [blame] | 2803 | else if (strcmp(value, "stop") == 0) { |
| 2804 | action = ST_ADM_ACTION_STOP; |
| 2805 | } |
| 2806 | else if (strcmp(value, "start") == 0) { |
| 2807 | action = ST_ADM_ACTION_START; |
| 2808 | } |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 2809 | else if (strcmp(value, "shutdown") == 0) { |
| 2810 | action = ST_ADM_ACTION_SHUTDOWN; |
| 2811 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2812 | else { |
| 2813 | si->applet.ctx.stats.st_code = STAT_STATUS_ERRP; |
| 2814 | goto out; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2815 | } |
| 2816 | } |
| 2817 | else if (strcmp(key, "s") == 0) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2818 | if (!(px && action)) { |
| 2819 | /* |
| 2820 | * Indicates that we'll need to reprocess the parameters |
| 2821 | * as soon as backend and action are known |
| 2822 | */ |
| 2823 | if (!reprocess) { |
| 2824 | st_cur_param = cur_param; |
| 2825 | st_next_param = next_param; |
| 2826 | } |
| 2827 | reprocess = 1; |
| 2828 | } |
| 2829 | else if ((sv = findserver(px, value)) != NULL) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2830 | switch (action) { |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2831 | case ST_ADM_ACTION_DISABLE: |
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 | /* Not already in maintenance, we can change the server state */ |
| 2834 | sv->state |= SRV_MAINTAIN; |
| 2835 | set_server_down(sv); |
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 | } |
| 2839 | break; |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2840 | case ST_ADM_ACTION_ENABLE: |
Cyril Bonté | 1e2a170 | 2011-03-03 21:05:17 +0100 | [diff] [blame] | 2841 | if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2842 | /* Already in maintenance, we can change the server state */ |
| 2843 | set_server_up(sv); |
Willy Tarreau | 7046130 | 2010-10-22 14:39:02 +0200 | [diff] [blame] | 2844 | sv->health = sv->rise; /* up, but will fall down at first failure */ |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2845 | altered_servers++; |
| 2846 | total_servers++; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2847 | } |
Willy Tarreau | d728224 | 2012-06-04 00:22:44 +0200 | [diff] [blame] | 2848 | break; |
| 2849 | case ST_ADM_ACTION_STOP: |
| 2850 | case ST_ADM_ACTION_START: |
| 2851 | if (action == ST_ADM_ACTION_START) |
| 2852 | sv->uweight = sv->iweight; |
| 2853 | else |
| 2854 | sv->uweight = 0; |
| 2855 | |
| 2856 | if (px->lbprm.algo & BE_LB_PROP_DYN) { |
| 2857 | /* we must take care of not pushing the server to full throttle during slow starts */ |
| 2858 | if ((sv->state & SRV_WARMINGUP) && (px->lbprm.algo & BE_LB_PROP_DYN)) |
| 2859 | sv->eweight = (BE_WEIGHT_SCALE * (now.tv_sec - sv->last_change) + sv->slowstart - 1) / sv->slowstart; |
| 2860 | else |
| 2861 | sv->eweight = BE_WEIGHT_SCALE; |
| 2862 | sv->eweight *= sv->uweight; |
| 2863 | } else { |
| 2864 | sv->eweight = sv->uweight; |
| 2865 | } |
| 2866 | |
| 2867 | /* static LB algorithms are a bit harder to update */ |
| 2868 | if (px->lbprm.update_server_eweight) |
| 2869 | px->lbprm.update_server_eweight(sv); |
| 2870 | else if (sv->eweight) { |
| 2871 | if (px->lbprm.set_server_status_up) |
| 2872 | px->lbprm.set_server_status_up(sv); |
| 2873 | } |
| 2874 | else { |
| 2875 | if (px->lbprm.set_server_status_down) |
| 2876 | px->lbprm.set_server_status_down(sv); |
| 2877 | } |
| 2878 | altered_servers++; |
| 2879 | total_servers++; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2880 | break; |
Willy Tarreau | 4f8a83c | 2012-06-04 00:26:23 +0200 | [diff] [blame] | 2881 | case ST_ADM_ACTION_SHUTDOWN: |
| 2882 | if (px->state != PR_STSTOPPED) { |
| 2883 | struct session *sess, *sess_bck; |
| 2884 | |
| 2885 | list_for_each_entry_safe(sess, sess_bck, &sv->actconns, by_srv) |
| 2886 | if (sess->srv_conn == sv) |
| 2887 | session_shutdown(sess, SN_ERR_KILLED); |
| 2888 | |
| 2889 | altered_servers++; |
| 2890 | total_servers++; |
| 2891 | } |
| 2892 | break; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2893 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2894 | } else { |
| 2895 | /* the server name is unknown or ambiguous (duplicate names) */ |
| 2896 | total_servers++; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2897 | } |
| 2898 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2899 | if (reprocess && px && action) { |
| 2900 | /* Now, we know the backend and the action chosen by the user. |
| 2901 | * We can safely restart from the first server parameter |
| 2902 | * to reprocess them |
| 2903 | */ |
| 2904 | cur_param = st_cur_param; |
| 2905 | next_param = st_next_param; |
| 2906 | reprocess = 0; |
| 2907 | goto reprocess_servers; |
| 2908 | } |
| 2909 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2910 | next_param = cur_param; |
| 2911 | } |
| 2912 | } |
Cyril Bonté | cf8d9ae | 2012-04-04 12:57:18 +0200 | [diff] [blame] | 2913 | |
| 2914 | if (total_servers == 0) { |
| 2915 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
| 2916 | } |
| 2917 | else if (altered_servers == 0) { |
| 2918 | si->applet.ctx.stats.st_code = STAT_STATUS_ERRP; |
| 2919 | } |
| 2920 | else if (altered_servers == total_servers) { |
| 2921 | si->applet.ctx.stats.st_code = STAT_STATUS_DONE; |
| 2922 | } |
| 2923 | else { |
| 2924 | si->applet.ctx.stats.st_code = STAT_STATUS_PART; |
| 2925 | } |
| 2926 | out: |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2927 | return 1; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2928 | } |
| 2929 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2930 | /* returns a pointer to the first rule which forbids access (deny or http_auth), |
| 2931 | * or NULL if everything's OK. |
| 2932 | */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2933 | static inline struct http_req_rule * |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2934 | http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn) |
| 2935 | { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2936 | struct http_req_rule *rule; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2937 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2938 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2939 | int ret = 1; |
| 2940 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2941 | if (rule->action >= HTTP_REQ_ACT_MAX) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2942 | continue; |
| 2943 | |
| 2944 | /* check condition, but only if attached */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2945 | if (rule->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 2946 | 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] | 2947 | ret = acl_pass(ret); |
| 2948 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2949 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2950 | ret = !ret; |
| 2951 | } |
| 2952 | |
| 2953 | if (ret) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2954 | if (rule->action == HTTP_REQ_ACT_ALLOW) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2955 | return NULL; /* no problem */ |
| 2956 | else |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2957 | return rule; /* most likely a deny or auth rule */ |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2958 | } |
| 2959 | } |
| 2960 | return NULL; |
| 2961 | } |
| 2962 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2963 | /* This stream analyser runs all HTTP request processing which is common to |
| 2964 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 2965 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2966 | * 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] | 2967 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 2968 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2969 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 2970 | 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] | 2971 | { |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2972 | struct http_txn *txn = &s->txn; |
| 2973 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2974 | struct acl_cond *cond; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2975 | struct http_req_rule *http_req_last_rule = NULL; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2976 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 2977 | struct cond_wordlist *wl; |
Simon Horman | 70735c9 | 2011-06-07 11:07:50 +0900 | [diff] [blame] | 2978 | int do_stats; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2979 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2980 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2981 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 2982 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2983 | return 0; |
| 2984 | } |
| 2985 | |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2986 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2987 | req->analyse_exp = TICK_ETERNITY; |
| 2988 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 2989 | 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] | 2990 | now_ms, __FUNCTION__, |
| 2991 | s, |
| 2992 | req, |
| 2993 | req->rex, req->wex, |
| 2994 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 2995 | req->buf->i, |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2996 | req->analysers); |
| 2997 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2998 | /* first check whether we have some ACLs set to block this request */ |
| 2999 | list_for_each_entry(cond, &px->block_cond, list) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3000 | 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] | 3001 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3002 | ret = acl_pass(ret); |
| 3003 | if (cond->pol == ACL_COND_UNLESS) |
| 3004 | ret = !ret; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 3005 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3006 | if (ret) { |
| 3007 | txn->status = 403; |
| 3008 | /* let's log the request time */ |
| 3009 | s->logs.tv_request = now; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3010 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3011 | session_inc_http_err_ctr(s); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3012 | goto return_prx_cond; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3013 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3014 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3015 | |
Willy Tarreau | 5d5b5d8 | 2012-12-09 12:00:04 +0100 | [diff] [blame] | 3016 | /* just in case we have some per-backend tracking */ |
| 3017 | session_inc_be_http_req_ctr(s); |
| 3018 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3019 | /* evaluate http-request rules */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3020 | http_req_last_rule = http_check_access_rule(px, &px->http_req_rules, s, txn); |
Willy Tarreau | 5142594 | 2010-02-01 10:40:19 +0100 | [diff] [blame] | 3021 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3022 | /* evaluate stats http-request rules only if http-request is OK */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3023 | if (!http_req_last_rule) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3024 | do_stats = stats_check_uri(s->rep->prod, txn, px); |
| 3025 | if (do_stats) |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3026 | http_req_last_rule = http_check_access_rule(px, &px->uri_auth->http_req_rules, s, txn); |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3027 | } |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3028 | else |
| 3029 | do_stats = 0; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3030 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3031 | /* return a 403 if either rule has blocked */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3032 | if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_DENY) { |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3033 | txn->status = 403; |
| 3034 | s->logs.tv_request = now; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3035 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3036 | session_inc_http_err_ctr(s); |
Willy Tarreau | 6da0f6d | 2011-01-06 18:19:50 +0100 | [diff] [blame] | 3037 | s->fe->fe_counters.denied_req++; |
| 3038 | if (an_bit == AN_REQ_HTTP_PROCESS_BE) |
| 3039 | s->be->be_counters.denied_req++; |
| 3040 | if (s->listener->counters) |
| 3041 | s->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3042 | goto return_prx_cond; |
| 3043 | } |
| 3044 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3045 | /* try headers filters */ |
| 3046 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 3047 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3048 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3049 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3050 | /* has the request been denied ? */ |
| 3051 | if (txn->flags & TX_CLDENY) { |
| 3052 | /* no need to go further */ |
| 3053 | txn->status = 403; |
| 3054 | /* let's log the request time */ |
| 3055 | s->logs.tv_request = now; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3056 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3057 | session_inc_http_err_ctr(s); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3058 | goto return_prx_cond; |
| 3059 | } |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3060 | |
| 3061 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 3062 | * which may be the same as the connect timeout if unspecified. |
| 3063 | * If unset, then set it to zero because we really want it to |
| 3064 | * eventually expire. We build the tarpit as an analyser. |
| 3065 | */ |
| 3066 | if (txn->flags & TX_CLTARPIT) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3067 | channel_erase(s->req); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3068 | /* wipe the request out so that we can drop the connection early |
| 3069 | * if the client closes first. |
| 3070 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3071 | channel_dont_connect(req); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3072 | req->analysers = 0; /* remove switching rules etc... */ |
| 3073 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 3074 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 3075 | if (!req->analyse_exp) |
| 3076 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3077 | session_inc_http_err_ctr(s); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3078 | return 1; |
| 3079 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3080 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3081 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3082 | /* Until set to anything else, the connection mode is set as TUNNEL. It will |
| 3083 | * only change if both the request and the config reference something else. |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3084 | * Option httpclose by itself does not set a mode, it remains a tunnel mode |
| 3085 | * 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] | 3086 | * affect it (eg: server-close/keep-alive + httpclose = close). Note that we |
| 3087 | * avoid to redo the same work if FE and BE have the same settings (common). |
| 3088 | * The method consists in checking if options changed between the two calls |
| 3089 | * (implying that either one is non-null, or one of them is non-null and we |
| 3090 | * are there for the first time. |
Willy Tarreau | 4273664 | 2009-10-18 21:04:35 +0200 | [diff] [blame] | 3091 | */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3092 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 3093 | if ((!(txn->flags & TX_HDR_CONN_PRS) && |
| 3094 | (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) || |
| 3095 | ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) != |
| 3096 | (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] | 3097 | int tmp = TX_CON_WANT_TUN; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3098 | |
Cyril Bonté | 9ea2b9a | 2010-12-29 09:36:56 +0100 | [diff] [blame] | 3099 | if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE || |
| 3100 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3101 | tmp = TX_CON_WANT_KAL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 3102 | if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO) |
| 3103 | tmp = TX_CON_WANT_SCL; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3104 | if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3105 | tmp = TX_CON_WANT_CLO; |
| 3106 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3107 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 3108 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3109 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3110 | if (!(txn->flags & TX_HDR_CONN_PRS)) { |
| 3111 | /* parse the Connection header and possibly clean it */ |
| 3112 | int to_del = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3113 | if ((msg->flags & HTTP_MSGF_VER_11) || |
Willy Tarreau | 8a8e1d9 | 2010-04-05 16:15:16 +0200 | [diff] [blame] | 3114 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
| 3115 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3116 | to_del |= 2; /* remove "keep-alive" */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3117 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3118 | to_del |= 1; /* remove "close" */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3119 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3120 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3121 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3122 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 3123 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 3124 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 3125 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3126 | (!(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] | 3127 | ((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] | 3128 | !(msg->flags & HTTP_MSGF_XFER_LEN) || /* no length known => close */ |
Willy Tarreau | c3e8b25 | 2010-01-28 15:01:20 +0100 | [diff] [blame] | 3129 | s->fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3130 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 3131 | } |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 3132 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3133 | /* we can be blocked here because the request needs to be authenticated, |
| 3134 | * either to pass or to access stats. |
| 3135 | */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3136 | if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_HTTP_AUTH) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3137 | char *realm = http_req_last_rule->http_auth.realm; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3138 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3139 | if (!realm) |
| 3140 | realm = do_stats?STATS_DEFAULT_REALM:px->id; |
| 3141 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3142 | 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] | 3143 | txn->status = 401; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3144 | stream_int_retnclose(req->prod, &trash); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3145 | /* on 401 we still count one error, because normal browsing |
| 3146 | * won't significantly increase the counter but brute force |
| 3147 | * attempts will. |
| 3148 | */ |
| 3149 | session_inc_http_err_ctr(s); |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3150 | goto return_prx_cond; |
| 3151 | } |
| 3152 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3153 | /* add request headers from the rule sets in the same order */ |
| 3154 | list_for_each_entry(wl, &px->req_add, list) { |
| 3155 | if (wl->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3156 | 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] | 3157 | ret = acl_pass(ret); |
| 3158 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 3159 | ret = !ret; |
| 3160 | if (!ret) |
| 3161 | continue; |
| 3162 | } |
| 3163 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3164 | 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] | 3165 | goto return_bad_req; |
| 3166 | } |
| 3167 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3168 | if (do_stats) { |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 3169 | struct stats_admin_rule *stats_admin_rule; |
| 3170 | |
| 3171 | /* We need to provide stats for this request. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3172 | * FIXME!!! that one is rather dangerous, we want to |
| 3173 | * make it follow standard rules (eg: clear req->analysers). |
| 3174 | */ |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3175 | |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 3176 | /* now check whether we have some admin rules for this request */ |
| 3177 | list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) { |
| 3178 | int ret = 1; |
| 3179 | |
| 3180 | if (stats_admin_rule->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3181 | ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL); |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 3182 | ret = acl_pass(ret); |
| 3183 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 3184 | ret = !ret; |
| 3185 | } |
| 3186 | |
| 3187 | if (ret) { |
| 3188 | /* no rule, or the rule matches */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 3189 | s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN; |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 3190 | break; |
| 3191 | } |
| 3192 | } |
| 3193 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 3194 | /* Was the status page requested with a POST ? */ |
| 3195 | if (txn->meth == HTTP_METH_POST) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 3196 | if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 3197 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 3198 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 3199 | * send an HTTP/1.1 100 Continue intermediate response. |
| 3200 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3201 | if (msg->flags & HTTP_MSGF_VER_11) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 3202 | struct hdr_ctx ctx; |
| 3203 | ctx.idx = 0; |
| 3204 | /* Expect is allowed in 1.1, look for it */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3205 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &ctx) && |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 3206 | 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] | 3207 | bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len); |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 3208 | } |
| 3209 | } |
| 3210 | msg->msg_state = HTTP_MSG_100_SENT; |
| 3211 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 3212 | } |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 3213 | if (!http_process_req_stat_post(s->rep->prod, txn, req)) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 3214 | /* we need more data */ |
| 3215 | req->analysers |= an_bit; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3216 | channel_dont_connect(req); |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 3217 | return 0; |
| 3218 | } |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 3219 | } else { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 3220 | s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY; |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 3221 | } |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 3222 | } |
| 3223 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3224 | s->logs.tv_request = now; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3225 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | b24281b | 2011-02-13 13:16:36 +0100 | [diff] [blame] | 3226 | stream_int_register_handler(s->rep->prod, &http_stats_applet); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 3227 | s->target = s->rep->prod->conn->target; // for logging only |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3228 | s->rep->prod->conn->xprt_ctx = s; |
Willy Tarreau | bc4af05 | 2011-02-13 13:25:14 +0100 | [diff] [blame] | 3229 | s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3230 | req->analysers = 0; |
Willy Tarreau | eabea07 | 2011-09-10 23:29:44 +0200 | [diff] [blame] | 3231 | if (s->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 3232 | s->fe->fe_counters.intercepted_req++; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3233 | |
| 3234 | return 0; |
| 3235 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3236 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 3237 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3238 | /* check whether we have some ACLs set to redirect this request */ |
| 3239 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3240 | int ret = ACL_PAT_PASS; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3241 | |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3242 | if (rule->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 3243 | 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] | 3244 | ret = acl_pass(ret); |
| 3245 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3246 | ret = !ret; |
| 3247 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3248 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3249 | if (ret) { |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3250 | const char *msg_fmt; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3251 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3252 | /* build redirect message */ |
| 3253 | switch(rule->code) { |
| 3254 | case 303: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3255 | msg_fmt = HTTP_303; |
| 3256 | break; |
| 3257 | case 301: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3258 | msg_fmt = HTTP_301; |
| 3259 | break; |
| 3260 | case 302: |
| 3261 | default: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3262 | msg_fmt = HTTP_302; |
| 3263 | break; |
| 3264 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3265 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3266 | if (unlikely(!chunk_strcpy(&trash, msg_fmt))) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3267 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3268 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3269 | switch(rule->type) { |
Willy Tarreau | 2e1dca8 | 2012-09-12 08:43:15 +0200 | [diff] [blame] | 3270 | case REDIRECT_TYPE_SCHEME: { |
| 3271 | const char *path; |
| 3272 | const char *host; |
| 3273 | struct hdr_ctx ctx; |
| 3274 | int pathlen; |
| 3275 | int hostlen; |
| 3276 | |
| 3277 | host = ""; |
| 3278 | hostlen = 0; |
| 3279 | ctx.idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3280 | if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 2e1dca8 | 2012-09-12 08:43:15 +0200 | [diff] [blame] | 3281 | host = ctx.line + ctx.val; |
| 3282 | hostlen = ctx.vlen; |
| 3283 | } |
| 3284 | |
| 3285 | path = http_get_path(txn); |
| 3286 | /* build message using path */ |
| 3287 | if (path) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3288 | pathlen = txn->req.sl.rq.u_l + (req->buf->p + txn->req.sl.rq.u) - path; |
Willy Tarreau | 2e1dca8 | 2012-09-12 08:43:15 +0200 | [diff] [blame] | 3289 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3290 | int qs = 0; |
| 3291 | while (qs < pathlen) { |
| 3292 | if (path[qs] == '?') { |
| 3293 | pathlen = qs; |
| 3294 | break; |
| 3295 | } |
| 3296 | qs++; |
| 3297 | } |
| 3298 | } |
| 3299 | } else { |
| 3300 | path = "/"; |
| 3301 | pathlen = 1; |
| 3302 | } |
| 3303 | |
| 3304 | /* check if we can add scheme + "://" + host + path */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3305 | if (trash.len + rule->rdr_len + 3 + hostlen + pathlen > trash.size - 4) |
Willy Tarreau | 2e1dca8 | 2012-09-12 08:43:15 +0200 | [diff] [blame] | 3306 | goto return_bad_req; |
| 3307 | |
| 3308 | /* add scheme */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3309 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3310 | trash.len += rule->rdr_len; |
Willy Tarreau | 2e1dca8 | 2012-09-12 08:43:15 +0200 | [diff] [blame] | 3311 | |
| 3312 | /* add "://" */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3313 | memcpy(trash.str + trash.len, "://", 3); |
| 3314 | trash.len += 3; |
Willy Tarreau | 2e1dca8 | 2012-09-12 08:43:15 +0200 | [diff] [blame] | 3315 | |
| 3316 | /* add host */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3317 | memcpy(trash.str + trash.len, host, hostlen); |
| 3318 | trash.len += hostlen; |
Willy Tarreau | 2e1dca8 | 2012-09-12 08:43:15 +0200 | [diff] [blame] | 3319 | |
| 3320 | /* add path */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3321 | memcpy(trash.str + trash.len, path, pathlen); |
| 3322 | trash.len += pathlen; |
Willy Tarreau | 2e1dca8 | 2012-09-12 08:43:15 +0200 | [diff] [blame] | 3323 | |
| 3324 | /* append a slash at the end of the location is needed and missing */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3325 | if (trash.len && trash.str[trash.len - 1] != '/' && |
Willy Tarreau | 2e1dca8 | 2012-09-12 08:43:15 +0200 | [diff] [blame] | 3326 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3327 | if (trash.len > trash.size - 5) |
Willy Tarreau | 2e1dca8 | 2012-09-12 08:43:15 +0200 | [diff] [blame] | 3328 | goto return_bad_req; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3329 | trash.str[trash.len] = '/'; |
| 3330 | trash.len++; |
Willy Tarreau | 2e1dca8 | 2012-09-12 08:43:15 +0200 | [diff] [blame] | 3331 | } |
| 3332 | |
| 3333 | break; |
| 3334 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3335 | case REDIRECT_TYPE_PREFIX: { |
| 3336 | const char *path; |
| 3337 | int pathlen; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3338 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3339 | path = http_get_path(txn); |
| 3340 | /* build message using path */ |
| 3341 | if (path) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3342 | pathlen = txn->req.sl.rq.u_l + (req->buf->p + txn->req.sl.rq.u) - path; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3343 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3344 | int qs = 0; |
| 3345 | while (qs < pathlen) { |
| 3346 | if (path[qs] == '?') { |
| 3347 | pathlen = qs; |
| 3348 | break; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3349 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3350 | qs++; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3351 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3352 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3353 | } else { |
| 3354 | path = "/"; |
| 3355 | pathlen = 1; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3356 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3357 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3358 | if (trash.len + rule->rdr_len + pathlen > trash.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3359 | goto return_bad_req; |
| 3360 | |
| 3361 | /* add prefix. Note that if prefix == "/", we don't want to |
| 3362 | * add anything, otherwise it makes it hard for the user to |
| 3363 | * configure a self-redirection. |
| 3364 | */ |
| 3365 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3366 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3367 | trash.len += rule->rdr_len; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3368 | } |
| 3369 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3370 | /* add path */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3371 | memcpy(trash.str + trash.len, path, pathlen); |
| 3372 | trash.len += pathlen; |
Willy Tarreau | 81e3b4f | 2010-01-10 00:42:19 +0100 | [diff] [blame] | 3373 | |
| 3374 | /* append a slash at the end of the location is needed and missing */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3375 | if (trash.len && trash.str[trash.len - 1] != '/' && |
Willy Tarreau | 81e3b4f | 2010-01-10 00:42:19 +0100 | [diff] [blame] | 3376 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3377 | if (trash.len > trash.size - 5) |
Willy Tarreau | 81e3b4f | 2010-01-10 00:42:19 +0100 | [diff] [blame] | 3378 | goto return_bad_req; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3379 | trash.str[trash.len] = '/'; |
| 3380 | trash.len++; |
Willy Tarreau | 81e3b4f | 2010-01-10 00:42:19 +0100 | [diff] [blame] | 3381 | } |
| 3382 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3383 | break; |
| 3384 | } |
| 3385 | case REDIRECT_TYPE_LOCATION: |
| 3386 | default: |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3387 | if (trash.len + rule->rdr_len > trash.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3388 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3389 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3390 | /* add location */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3391 | memcpy(trash.str + trash.len, rule->rdr_str, rule->rdr_len); |
| 3392 | trash.len += rule->rdr_len; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3393 | break; |
| 3394 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3395 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3396 | if (rule->cookie_len) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3397 | memcpy(trash.str + trash.len, "\r\nSet-Cookie: ", 14); |
| 3398 | trash.len += 14; |
| 3399 | memcpy(trash.str + trash.len, rule->cookie_str, rule->cookie_len); |
| 3400 | trash.len += rule->cookie_len; |
| 3401 | memcpy(trash.str + trash.len, "\r\n", 2); |
| 3402 | trash.len += 2; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3403 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3404 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3405 | /* add end of headers and the keep-alive/close status. |
| 3406 | * We may choose to set keep-alive if the Location begins |
| 3407 | * with a slash, because the client will come back to the |
| 3408 | * same server. |
| 3409 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3410 | txn->status = rule->code; |
| 3411 | /* let's log the request time */ |
| 3412 | s->logs.tv_request = now; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3413 | |
| 3414 | if (rule->rdr_len >= 1 && *rule->rdr_str == '/' && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3415 | (msg->flags & HTTP_MSGF_XFER_LEN) && |
| 3416 | !(msg->flags & HTTP_MSGF_TE_CHNK) && !txn->req.body_len && |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3417 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 3418 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 3419 | /* keep-alive possible */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3420 | if (!(msg->flags & HTTP_MSGF_VER_11)) { |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3421 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3422 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: keep-alive", 30); |
| 3423 | trash.len += 30; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3424 | } else { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3425 | memcpy(trash.str + trash.len, "\r\nConnection: keep-alive", 24); |
| 3426 | trash.len += 24; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3427 | } |
Willy Tarreau | 7566145 | 2010-01-10 10:35:01 +0100 | [diff] [blame] | 3428 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3429 | memcpy(trash.str + trash.len, "\r\n\r\n", 4); |
| 3430 | trash.len += 4; |
| 3431 | bo_inject(req->prod->ob, trash.str, trash.len); |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3432 | /* "eat" the request */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3433 | bi_fast_delete(req->buf, msg->sov); |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 3434 | msg->sov = 0; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3435 | req->analysers = AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 9300fb2 | 2010-01-05 00:58:24 +0100 | [diff] [blame] | 3436 | s->rep->analysers = AN_RES_HTTP_XFER_BODY; |
| 3437 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3438 | txn->rsp.msg_state = HTTP_MSG_DONE; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3439 | break; |
| 3440 | } else { |
| 3441 | /* keep-alive not possible */ |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3442 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3443 | memcpy(trash.str + trash.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 3444 | trash.len += 29; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3445 | } else { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3446 | memcpy(trash.str + trash.len, "\r\nConnection: close\r\n\r\n", 23); |
| 3447 | trash.len += 23; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3448 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3449 | stream_int_retnclose(req->prod, &trash); |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3450 | goto return_prx_cond; |
| 3451 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3452 | } |
| 3453 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3454 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3455 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 3456 | * If this happens, then the data will not come immediately, so we must |
| 3457 | * send all what we have without waiting. Note that due to the small gain |
| 3458 | * 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] | 3459 | * 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] | 3460 | * itself once used. |
| 3461 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3462 | req->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3463 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3464 | /* that's OK for us now, let's move on to next analysers */ |
| 3465 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 3466 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3467 | return_bad_req: |
| 3468 | /* We centralize bad requests processing here */ |
| 3469 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3470 | /* we detected a parsing error. We want to archive this request |
| 3471 | * in the dedicated proxy area for later troubleshooting. |
| 3472 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 3473 | 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] | 3474 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3475 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3476 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3477 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3478 | 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] | 3479 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3480 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3481 | if (s->listener->counters) |
| 3482 | s->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3483 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3484 | return_prx_cond: |
| 3485 | if (!(s->flags & SN_ERR_MASK)) |
| 3486 | s->flags |= SN_ERR_PRXCOND; |
| 3487 | if (!(s->flags & SN_FINST_MASK)) |
| 3488 | s->flags |= SN_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 3489 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3490 | req->analysers = 0; |
| 3491 | req->analyse_exp = TICK_ETERNITY; |
| 3492 | return 0; |
| 3493 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3494 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3495 | /* This function performs all the processing enabled for the current request. |
| 3496 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 3497 | * needs more data, encounters an error, or wants to immediately abort the |
| 3498 | * request. It relies on buffers flags, and updates s->req->analysers. |
| 3499 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 3500 | 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] | 3501 | { |
| 3502 | struct http_txn *txn = &s->txn; |
| 3503 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3504 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3505 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3506 | /* we need more data */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3507 | channel_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3508 | return 0; |
| 3509 | } |
| 3510 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 3511 | 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] | 3512 | now_ms, __FUNCTION__, |
| 3513 | s, |
| 3514 | req, |
| 3515 | req->rex, req->wex, |
| 3516 | req->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3517 | req->buf->i, |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3518 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3519 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 3520 | if (s->fe->comp || s->be->comp) |
| 3521 | select_compression_request_header(s, req->buf); |
| 3522 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3523 | /* |
| 3524 | * Right now, we know that we have processed the entire headers |
| 3525 | * and that unwanted requests have been filtered out. We can do |
| 3526 | * whatever we want with the remaining request. Also, now we |
| 3527 | * may have separate values for ->fe, ->be. |
| 3528 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3529 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3530 | /* |
| 3531 | * If HTTP PROXY is set we simply get remote server address |
| 3532 | * parsing incoming request. |
| 3533 | */ |
| 3534 | 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] | 3535 | 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] | 3536 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3537 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3538 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3539 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3540 | * Note that doing so might move headers in the request, but |
| 3541 | * the fields will stay coherent and the URI will not move. |
| 3542 | * This should only be performed in the backend. |
| 3543 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 3544 | 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] | 3545 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 3546 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 3547 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3548 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3549 | * 8: the appsession cookie was looked up very early in 1.2, |
| 3550 | * so let's do the same now. |
| 3551 | */ |
| 3552 | |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 3553 | /* It needs to look into the URI unless persistence must be ignored */ |
| 3554 | 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] | 3555 | 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] | 3556 | } |
| 3557 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3558 | /* add unique-id if "header-unique-id" is specified */ |
| 3559 | |
| 3560 | if (!LIST_ISEMPTY(&s->fe->format_unique_id)) |
| 3561 | build_logline(s, s->unique_id, UNIQUEID_LEN, &s->fe->format_unique_id); |
| 3562 | |
| 3563 | if (s->fe->header_unique_id && s->unique_id) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3564 | chunk_printf(&trash, "%s: %s", s->fe->header_unique_id, s->unique_id); |
| 3565 | if (trash.len < 0) |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 3566 | goto return_bad_req; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3567 | 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] | 3568 | goto return_bad_req; |
| 3569 | } |
| 3570 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3571 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3572 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 3573 | * asks for it. |
| 3574 | */ |
| 3575 | if ((s->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3576 | struct hdr_ctx ctx = { .idx = 0 }; |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3577 | if (!((s->fe->options | s->be->options) & PR_O_FF_ALWAYS) && |
Cyril Bonté | a32d275 | 2012-05-29 23:27:41 +0200 | [diff] [blame] | 3578 | http_find_header2(s->be->fwdfor_hdr_len ? s->be->fwdfor_hdr_name : s->fe->fwdfor_hdr_name, |
| 3579 | 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] | 3580 | req->buf->p, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | 87cf514 | 2011-08-19 22:57:24 +0200 | [diff] [blame] | 3581 | /* The header is set to be added only if none is present |
| 3582 | * and we found it, so don't do anything. |
| 3583 | */ |
| 3584 | } |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3585 | else if (s->req->prod->conn->addr.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3586 | /* Add an X-Forwarded-For header unless the source IP is |
| 3587 | * in the 'except' network range. |
| 3588 | */ |
| 3589 | if ((!s->fe->except_mask.s_addr || |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3590 | (((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] | 3591 | != s->fe->except_net.s_addr) && |
| 3592 | (!s->be->except_mask.s_addr || |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3593 | (((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] | 3594 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3595 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3596 | unsigned char *pn; |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3597 | 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] | 3598 | |
| 3599 | /* Note: we rely on the backend to get the header name to be used for |
| 3600 | * x-forwarded-for, because the header is really meant for the backends. |
| 3601 | * However, if the backend did not specify any option, we have to rely |
| 3602 | * on the frontend's header name. |
| 3603 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3604 | if (s->be->fwdfor_hdr_len) { |
| 3605 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3606 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3607 | } else { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3608 | len = s->fe->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3609 | memcpy(trash.str, s->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3610 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3611 | 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] | 3612 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3613 | 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] | 3614 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3615 | } |
| 3616 | } |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3617 | else if (s->req->prod->conn->addr.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3618 | /* FIXME: for the sake of completeness, we should also support |
| 3619 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3620 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3621 | int len; |
| 3622 | char pn[INET6_ADDRSTRLEN]; |
| 3623 | inet_ntop(AF_INET6, |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3624 | (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] | 3625 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3626 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3627 | /* Note: we rely on the backend to get the header name to be used for |
| 3628 | * x-forwarded-for, because the header is really meant for the backends. |
| 3629 | * However, if the backend did not specify any option, we have to rely |
| 3630 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3631 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3632 | if (s->be->fwdfor_hdr_len) { |
| 3633 | len = s->be->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3634 | memcpy(trash.str, s->be->fwdfor_hdr_name, len); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3635 | } else { |
| 3636 | len = s->fe->fwdfor_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3637 | memcpy(trash.str, s->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3638 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3639 | len += sprintf(trash.str + len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3640 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3641 | 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] | 3642 | goto return_bad_req; |
| 3643 | } |
| 3644 | } |
| 3645 | |
| 3646 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3647 | * 10: add X-Original-To if either the frontend or the backend |
| 3648 | * asks for it. |
| 3649 | */ |
| 3650 | if ((s->fe->options | s->be->options) & PR_O_ORGTO) { |
| 3651 | |
| 3652 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3653 | if (s->req->prod->conn->addr.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3654 | /* Add an X-Original-To header unless the destination IP is |
| 3655 | * in the 'except' network range. |
| 3656 | */ |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3657 | conn_get_to_addr(s->req->prod->conn); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3658 | |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3659 | if (s->req->prod->conn->addr.to.ss_family == AF_INET && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 3660 | ((!s->fe->except_mask_to.s_addr || |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3661 | (((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] | 3662 | != s->fe->except_to.s_addr) && |
| 3663 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3664 | (((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] | 3665 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3666 | int len; |
| 3667 | unsigned char *pn; |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 3668 | 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] | 3669 | |
| 3670 | /* Note: we rely on the backend to get the header name to be used for |
| 3671 | * x-original-to, because the header is really meant for the backends. |
| 3672 | * However, if the backend did not specify any option, we have to rely |
| 3673 | * on the frontend's header name. |
| 3674 | */ |
| 3675 | if (s->be->orgto_hdr_len) { |
| 3676 | len = s->be->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3677 | memcpy(trash.str, s->be->orgto_hdr_name, len); |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3678 | } else { |
| 3679 | len = s->fe->orgto_hdr_len; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3680 | memcpy(trash.str, s->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3681 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3682 | 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] | 3683 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3684 | 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] | 3685 | goto return_bad_req; |
| 3686 | } |
| 3687 | } |
| 3688 | } |
| 3689 | |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 3690 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. |
| 3691 | * If an "Upgrade" token is found, the header is left untouched in order not to have |
| 3692 | * to deal with some servers bugs : some of them fail an Upgrade if anything but |
| 3693 | * "Upgrade" is present in the Connection header. |
| 3694 | */ |
| 3695 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 3696 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
| 3697 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE))) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3698 | unsigned int want_flags = 0; |
| 3699 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3700 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3701 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
| 3702 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) && |
| 3703 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3704 | want_flags |= TX_CON_CLO_SET; |
| 3705 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3706 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
| 3707 | !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) || |
| 3708 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3709 | want_flags |= TX_CON_KAL_SET; |
| 3710 | } |
| 3711 | |
| 3712 | 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] | 3713 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3714 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3715 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3716 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3717 | /* If we have no server assigned yet and we're balancing on url_param |
| 3718 | * with a POST request, we may be interested in checking the body for |
| 3719 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3720 | */ |
| 3721 | if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 3722 | s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3723 | s->be->url_param_post_limit != 0 && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3724 | (msg->flags & (HTTP_MSGF_CNT_LEN|HTTP_MSGF_TE_CHNK))) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3725 | channel_dont_connect(req); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3726 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3727 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3728 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3729 | if (msg->flags & HTTP_MSGF_XFER_LEN) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3730 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 5e20552 | 2011-12-17 16:34:27 +0100 | [diff] [blame] | 3731 | #ifdef TCP_QUICKACK |
| 3732 | /* We expect some data from the client. Unless we know for sure |
| 3733 | * we already have a full request, we have to re-enable quick-ack |
| 3734 | * in case we previously disabled it, otherwise we might cause |
| 3735 | * the client to delay further data. |
| 3736 | */ |
| 3737 | if ((s->listener->options & LI_O_NOQUICKACK) && |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3738 | ((msg->flags & HTTP_MSGF_TE_CHNK) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3739 | (msg->body_len > req->buf->i - txn->req.eoh - 2))) |
Willy Tarreau | 7f7ad91 | 2012-11-11 19:27:15 +0100 | [diff] [blame] | 3740 | 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] | 3741 | #endif |
| 3742 | } |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 3743 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3744 | /************************************************************* |
| 3745 | * OK, that's finished for the headers. We have done what we * |
| 3746 | * could. Let's switch to the DATA state. * |
| 3747 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3748 | req->analyse_exp = TICK_ETERNITY; |
| 3749 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3750 | |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 3751 | /* if the server closes the connection, we want to immediately react |
| 3752 | * and close the socket to save packets and syscalls. |
| 3753 | */ |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 3754 | if (!(req->analysers & AN_REQ_HTTP_XFER_BODY)) |
| 3755 | req->cons->flags |= SI_FL_NOHALF; |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 3756 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3757 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3758 | /* OK let's go on with the BODY now */ |
| 3759 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3760 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3761 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3762 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3763 | /* we detected a parsing error. We want to archive this request |
| 3764 | * in the dedicated proxy area for later troubleshooting. |
| 3765 | */ |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 3766 | 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] | 3767 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3768 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3769 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3770 | txn->status = 400; |
| 3771 | req->analysers = 0; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3772 | 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] | 3773 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3774 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3775 | if (s->listener->counters) |
| 3776 | s->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3777 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3778 | if (!(s->flags & SN_ERR_MASK)) |
| 3779 | s->flags |= SN_ERR_PRXCOND; |
| 3780 | if (!(s->flags & SN_FINST_MASK)) |
| 3781 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3782 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3783 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3784 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3785 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 3786 | * returns zero, at the beginning because it prevents any other processing |
| 3787 | * from occurring, and at the end because it terminates the request. |
| 3788 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 3789 | 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] | 3790 | { |
| 3791 | struct http_txn *txn = &s->txn; |
| 3792 | |
| 3793 | /* This connection is being tarpitted. The CLIENT side has |
| 3794 | * already set the connect expiration date to the right |
| 3795 | * timeout. We just have to check that the client is still |
| 3796 | * there and that the timeout has not expired. |
| 3797 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3798 | channel_dont_connect(req); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3799 | if ((req->flags & (CF_SHUTR|CF_READ_ERROR)) == 0 && |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3800 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 3801 | return 0; |
| 3802 | |
| 3803 | /* We will set the queue timer to the time spent, just for |
| 3804 | * logging purposes. We fake a 500 server error, so that the |
| 3805 | * attacker will not suspect his connection has been tarpitted. |
| 3806 | * It will not cause trouble to the logs because we can exclude |
| 3807 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 3808 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3809 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3810 | |
| 3811 | txn->status = 500; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3812 | if (!(req->flags & CF_READ_ERROR)) |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3813 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_500)); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3814 | |
| 3815 | req->analysers = 0; |
| 3816 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3817 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3818 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3819 | if (s->listener->counters) |
| 3820 | s->listener->counters->failed_req++; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3821 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3822 | if (!(s->flags & SN_ERR_MASK)) |
| 3823 | s->flags |= SN_ERR_PRXCOND; |
| 3824 | if (!(s->flags & SN_FINST_MASK)) |
| 3825 | s->flags |= SN_FINST_T; |
| 3826 | return 0; |
| 3827 | } |
| 3828 | |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3829 | /* This function is an analyser which processes the HTTP request body. It looks |
| 3830 | * for parameters to be used for the load balancing algorithm (url_param). It |
| 3831 | * must only be called after the standard HTTP request processing has occurred, |
| 3832 | * because it expects the request to be parsed. It returns zero if it needs to |
| 3833 | * read more data, or 1 once it has completed its analysis. |
| 3834 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 3835 | 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] | 3836 | { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3837 | struct http_txn *txn = &s->txn; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3838 | struct http_msg *msg = &s->txn.req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3839 | long long limit = s->be->url_param_post_limit; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3840 | |
| 3841 | /* We have to parse the HTTP request body to find any required data. |
| 3842 | * "balance url_param check_post" should have been the only way to get |
| 3843 | * into this. We were brought here after HTTP header analysis, so all |
| 3844 | * related structures are ready. |
| 3845 | */ |
| 3846 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3847 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 3848 | goto missing_data; |
| 3849 | |
| 3850 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 3851 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 3852 | * send an HTTP/1.1 100 Continue intermediate response. |
| 3853 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3854 | if (msg->flags & HTTP_MSGF_VER_11) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3855 | struct hdr_ctx ctx; |
| 3856 | ctx.idx = 0; |
| 3857 | /* Expect is allowed in 1.1, look for it */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3858 | 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] | 3859 | 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] | 3860 | bo_inject(s->rep, http_100_chunk.str, http_100_chunk.len); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3861 | } |
| 3862 | } |
| 3863 | msg->msg_state = HTTP_MSG_100_SENT; |
| 3864 | } |
| 3865 | |
| 3866 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 3867 | /* 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] | 3868 | * 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] | 3869 | * is still null. We must save the body in msg->next because it |
| 3870 | * survives buffer re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3871 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 3872 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 3873 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 3874 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3875 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 3876 | else |
| 3877 | msg->msg_state = HTTP_MSG_DATA; |
| 3878 | } |
| 3879 | |
| 3880 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3881 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 3882 | * 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] | 3883 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3884 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 3885 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3886 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3887 | if (!ret) |
| 3888 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3889 | else if (ret < 0) { |
| 3890 | session_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3891 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3892 | } |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3893 | } |
| 3894 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3895 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 3896 | * We have the first data byte is in msg->sov. We're waiting for at |
| 3897 | * least <url_param_post_limit> bytes after msg->sov. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3898 | */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3899 | |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3900 | if (msg->body_len < limit) |
| 3901 | limit = msg->body_len; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3902 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3903 | if (req->buf->i - msg->sov >= limit) /* we have enough bytes now */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3904 | goto http_end; |
| 3905 | |
| 3906 | missing_data: |
| 3907 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3908 | if (buffer_full(req->buf, global.tune.maxrewrite)) { |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3909 | session_inc_http_err_ctr(s); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3910 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3911 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3912 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 3913 | 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] | 3914 | txn->status = 408; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3915 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 3916 | |
| 3917 | if (!(s->flags & SN_ERR_MASK)) |
| 3918 | s->flags |= SN_ERR_CLITO; |
| 3919 | if (!(s->flags & SN_FINST_MASK)) |
| 3920 | s->flags |= SN_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3921 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3922 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3923 | |
| 3924 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3925 | 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] | 3926 | /* Not enough data. We'll re-use the http-request |
| 3927 | * timeout here. Ideally, we should set the timeout |
| 3928 | * relative to the accept() date. We just set the |
| 3929 | * request timeout once at the beginning of the |
| 3930 | * request. |
| 3931 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 3932 | channel_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3933 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 3934 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3935 | return 0; |
| 3936 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3937 | |
| 3938 | http_end: |
| 3939 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 3940 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 3941 | req->analysers &= ~an_bit; |
| 3942 | req->analyse_exp = TICK_ETERNITY; |
| 3943 | return 1; |
| 3944 | |
| 3945 | return_bad_req: /* let's centralize all bad requests */ |
| 3946 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3947 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 3948 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3949 | |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 3950 | if (!(s->flags & SN_ERR_MASK)) |
| 3951 | s->flags |= SN_ERR_PRXCOND; |
| 3952 | if (!(s->flags & SN_FINST_MASK)) |
| 3953 | s->flags |= SN_FINST_R; |
| 3954 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3955 | return_err_msg: |
| 3956 | req->analysers = 0; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3957 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3958 | if (s->listener->counters) |
| 3959 | s->listener->counters->failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3960 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3961 | } |
| 3962 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3963 | /* send a server's name with an outgoing request over an established connection. |
| 3964 | * Note: this function is designed to be called once the request has been scheduled |
| 3965 | * for being forwarded. This is the reason why it rewinds the buffer before |
| 3966 | * proceeding. |
| 3967 | */ |
Willy Tarreau | 45c0d98 | 2012-03-09 12:11:57 +0100 | [diff] [blame] | 3968 | 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] | 3969 | |
| 3970 | struct hdr_ctx ctx; |
| 3971 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3972 | char *hdr_name = be->server_id_hdr_name; |
| 3973 | int hdr_name_len = be->server_id_hdr_len; |
Willy Tarreau | 394db37 | 2012-10-12 22:40:39 +0200 | [diff] [blame] | 3974 | struct channel *chn = txn->req.chn; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3975 | char *hdr_val; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3976 | unsigned int old_o, old_i; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3977 | |
William Lallemand | d9e9066 | 2012-01-30 17:27:17 +0100 | [diff] [blame] | 3978 | ctx.idx = 0; |
| 3979 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3980 | old_o = chn->buf->o; |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3981 | if (old_o) { |
| 3982 | /* The request was already skipped, let's restore it */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3983 | b_rew(chn->buf, old_o); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 3984 | } |
| 3985 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 3986 | old_i = chn->buf->i; |
| 3987 | 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] | 3988 | /* remove any existing values from the header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 3989 | http_remove_header2(&txn->req, &txn->hdr_idx, &ctx); |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3990 | } |
| 3991 | |
| 3992 | /* Add the new header requested with the server value */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3993 | hdr_val = trash.str; |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 3994 | memcpy(hdr_val, hdr_name, hdr_name_len); |
| 3995 | hdr_val += hdr_name_len; |
| 3996 | *hdr_val++ = ':'; |
| 3997 | *hdr_val++ = ' '; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3998 | hdr_val += strlcpy2(hdr_val, srv_name, trash.str + trash.size - hdr_val); |
| 3999 | 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] | 4000 | |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4001 | if (old_o) { |
| 4002 | /* If this was a forwarded request, we must readjust the amount of |
| 4003 | * data to be forwarded in order to take into account the size |
| 4004 | * variations. |
| 4005 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4006 | b_adv(chn->buf, old_o + chn->buf->i - old_i); |
Willy Tarreau | d1de8af | 2012-05-18 22:12:14 +0200 | [diff] [blame] | 4007 | } |
| 4008 | |
Mark Lamourine | c2247f0 | 2012-01-04 13:02:01 -0500 | [diff] [blame] | 4009 | return 0; |
| 4010 | } |
| 4011 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4012 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 4013 | * right now but it works. |
| 4014 | */ |
| 4015 | void http_end_txn_clean_session(struct session *s) |
| 4016 | { |
| 4017 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 4018 | * server's connections. We need a safer way to reinitialize buffer |
| 4019 | * flags. We also need a more accurate method for computing per-request |
| 4020 | * data. |
| 4021 | */ |
| 4022 | http_silent_debug(__LINE__, s); |
| 4023 | |
Willy Tarreau | 7bb68ab | 2012-05-13 14:48:59 +0200 | [diff] [blame] | 4024 | s->req->cons->flags |= SI_FL_NOLINGER | SI_FL_NOHALF; |
Willy Tarreau | 73b013b | 2012-05-21 16:31:45 +0200 | [diff] [blame] | 4025 | si_shutr(s->req->cons); |
| 4026 | si_shutw(s->req->cons); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4027 | |
| 4028 | http_silent_debug(__LINE__, s); |
| 4029 | |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 4030 | if (s->flags & SN_BE_ASSIGNED) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4031 | s->be->beconn--; |
Willy Tarreau | 2d5cd47 | 2012-03-01 23:34:37 +0100 | [diff] [blame] | 4032 | if (unlikely(s->srv_conn)) |
| 4033 | sess_change_server(s, NULL); |
| 4034 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4035 | |
| 4036 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 4037 | session_process_counters(s); |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 4038 | session_stop_backend_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4039 | |
| 4040 | if (s->txn.status) { |
| 4041 | int n; |
| 4042 | |
| 4043 | n = s->txn.status / 100; |
| 4044 | if (n < 1 || n > 5) |
| 4045 | n = 0; |
| 4046 | |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4047 | if (s->fe->mode == PR_MODE_HTTP) { |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4048 | s->fe->fe_counters.p.http.rsp[n]++; |
Willy Tarreau | 8139b99 | 2012-11-27 07:35:31 +0100 | [diff] [blame] | 4049 | if (s->comp_algo && (s->flags & SN_COMP_READY)) |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4050 | s->fe->fe_counters.p.http.comp_rsp++; |
| 4051 | } |
Willy Tarreau | 2465779 | 2010-02-26 10:30:28 +0100 | [diff] [blame] | 4052 | if ((s->flags & SN_BE_ASSIGNED) && |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4053 | (s->be->mode == PR_MODE_HTTP)) { |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4054 | s->be->be_counters.p.http.rsp[n]++; |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4055 | s->be->be_counters.p.http.cum_req++; |
Willy Tarreau | 8139b99 | 2012-11-27 07:35:31 +0100 | [diff] [blame] | 4056 | if (s->comp_algo && (s->flags & SN_COMP_READY)) |
Willy Tarreau | 5e16cbc | 2012-11-24 14:54:13 +0100 | [diff] [blame] | 4057 | s->be->be_counters.p.http.comp_rsp++; |
| 4058 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4059 | } |
| 4060 | |
| 4061 | /* don't count other requests' data */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4062 | s->logs.bytes_in -= s->req->buf->i; |
| 4063 | s->logs.bytes_out -= s->rep->buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4064 | |
| 4065 | /* let's do a final log if we need it */ |
| 4066 | if (s->logs.logwait && |
| 4067 | !(s->flags & SN_MONITOR) && |
| 4068 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 4069 | s->do_log(s); |
| 4070 | } |
| 4071 | |
| 4072 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 4073 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 4074 | tv_zero(&s->logs.tv_request); |
| 4075 | s->logs.t_queue = -1; |
| 4076 | s->logs.t_connect = -1; |
| 4077 | s->logs.t_data = -1; |
| 4078 | s->logs.t_close = 0; |
| 4079 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 4080 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 4081 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4082 | s->logs.bytes_in = s->req->total = s->req->buf->i; |
| 4083 | s->logs.bytes_out = s->rep->total = s->rep->buf->i; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4084 | |
| 4085 | if (s->pend_pos) |
| 4086 | pendconn_free(s->pend_pos); |
| 4087 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4088 | if (objt_server(s->target)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4089 | if (s->flags & SN_CURR_SESS) { |
| 4090 | s->flags &= ~SN_CURR_SESS; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4091 | objt_server(s->target)->cur_sess--; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4092 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4093 | if (may_dequeue_tasks(objt_server(s->target), s->be)) |
| 4094 | process_srv_queue(objt_server(s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4095 | } |
| 4096 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4097 | s->target = NULL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4098 | |
| 4099 | s->req->cons->state = s->req->cons->prev_state = SI_ST_INI; |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 4100 | s->req->cons->conn->t.sock.fd = -1; /* just to help with debugging */ |
| 4101 | s->req->cons->conn->flags = CO_FL_NONE; |
Willy Tarreau | 14cba4b | 2012-11-30 17:33:05 +0100 | [diff] [blame] | 4102 | s->req->cons->conn->err_code = CO_ER_NONE; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4103 | s->req->cons->err_type = SI_ET_NONE; |
Willy Tarreau | 0b3a411 | 2011-03-27 19:16:56 +0200 | [diff] [blame] | 4104 | s->req->cons->conn_retries = 0; /* used for logging too */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4105 | s->req->cons->err_loc = NULL; |
| 4106 | s->req->cons->exp = TICK_ETERNITY; |
| 4107 | s->req->cons->flags = SI_FL_NONE; |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4108 | s->req->flags &= ~(CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CONNECT|CF_WRITE_ERROR|CF_STREAMER|CF_STREAMER_FAST|CF_NEVER_WAIT); |
| 4109 | 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] | 4110 | 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] | 4111 | s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE); |
Willy Tarreau | 543db62 | 2012-11-15 16:41:22 +0100 | [diff] [blame] | 4112 | |
| 4113 | if (s->flags & SN_COMP_READY) |
| 4114 | s->comp_algo->end(&s->comp_ctx); |
| 4115 | s->comp_algo = NULL; |
| 4116 | s->flags &= ~SN_COMP_READY; |
| 4117 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4118 | s->txn.meth = 0; |
| 4119 | http_reset_txn(s); |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 4120 | s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | ee55dc0 | 2010-06-01 10:56:34 +0200 | [diff] [blame] | 4121 | if (s->fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4122 | s->req->cons->flags |= SI_FL_INDEP_STR; |
| 4123 | |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 4124 | if (s->fe->options2 & PR_O2_NODELAY) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4125 | s->req->flags |= CF_NEVER_WAIT; |
| 4126 | s->rep->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 96e3121 | 2011-05-30 18:10:30 +0200 | [diff] [blame] | 4127 | } |
| 4128 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4129 | /* if the request buffer is not empty, it means we're |
| 4130 | * about to process another request, so send pending |
| 4131 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 4132 | * Just don't do this if the buffer is close to be full, |
| 4133 | * because the request will wait for it to flush a little |
| 4134 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4135 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4136 | if (s->req->buf->i) { |
| 4137 | if (s->rep->buf->o && |
| 4138 | !buffer_full(s->rep->buf, global.tune.maxrewrite) && |
| 4139 | 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] | 4140 | s->rep->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 4141 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4142 | |
| 4143 | /* we're removing the analysers, we MUST re-enable events detection */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4144 | channel_auto_read(s->req); |
| 4145 | channel_auto_close(s->req); |
| 4146 | channel_auto_read(s->rep); |
| 4147 | channel_auto_close(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4148 | |
Willy Tarreau | 342b11c | 2010-11-24 16:22:09 +0100 | [diff] [blame] | 4149 | s->req->analysers = s->listener->analysers; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4150 | s->rep->analysers = 0; |
| 4151 | |
| 4152 | http_silent_debug(__LINE__, s); |
| 4153 | } |
| 4154 | |
| 4155 | |
| 4156 | /* This function updates the request state machine according to the response |
| 4157 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4158 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4159 | * it is only used to find when a request/response couple is complete. Both |
| 4160 | * this function and its equivalent should loop until both return zero. It |
| 4161 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4162 | */ |
| 4163 | int http_sync_req_state(struct session *s) |
| 4164 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4165 | struct channel *chn = s->req; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4166 | struct http_txn *txn = &s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4167 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4168 | unsigned int old_state = txn->req.msg_state; |
| 4169 | |
| 4170 | http_silent_debug(__LINE__, s); |
| 4171 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 4172 | return 0; |
| 4173 | |
| 4174 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4175 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4176 | * We can shut the read side unless we want to abort_on_close, |
| 4177 | * or we have a POST request. The issue with POST requests is |
| 4178 | * that some browsers still send a CRLF after the request, and |
| 4179 | * this CRLF must be read so that it does not remain in the kernel |
| 4180 | * buffers, otherwise a close could cause an RST on some systems |
| 4181 | * (eg: Linux). |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4182 | */ |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4183 | 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] | 4184 | channel_dont_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4185 | |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4186 | /* if the server closes the connection, we want to immediately react |
| 4187 | * and close the socket to save packets and syscalls. |
| 4188 | */ |
| 4189 | chn->cons->flags |= SI_FL_NOHALF; |
| 4190 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4191 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 4192 | goto wait_other_side; |
| 4193 | |
| 4194 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 4195 | /* The server has not finished to respond, so we |
| 4196 | * don't want to move in order not to upset it. |
| 4197 | */ |
| 4198 | goto wait_other_side; |
| 4199 | } |
| 4200 | |
| 4201 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 4202 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4203 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4204 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4205 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4206 | goto wait_other_side; |
| 4207 | } |
| 4208 | |
| 4209 | /* When we get here, it means that both the request and the |
| 4210 | * response have finished receiving. Depending on the connection |
| 4211 | * mode, we'll have to wait for the last bytes to leave in either |
| 4212 | * direction, and sometimes for a close to be effective. |
| 4213 | */ |
| 4214 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4215 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4216 | /* Server-close mode : queue a connection close to the server */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4217 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) |
| 4218 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4219 | } |
| 4220 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4221 | /* Option forceclose is set, or either side wants to close, |
| 4222 | * let's enforce it now that we're not expecting any new |
| 4223 | * data to come. The caller knows the session is complete |
| 4224 | * once both states are CLOSED. |
| 4225 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4226 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4227 | channel_shutr_now(chn); |
| 4228 | channel_shutw_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4229 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4230 | } |
| 4231 | else { |
| 4232 | /* The last possible modes are keep-alive and tunnel. Since tunnel |
| 4233 | * mode does not set the body analyser, we can't reach this place |
| 4234 | * in tunnel mode, so we're left with keep-alive only. |
| 4235 | * This mode is currently not implemented, we switch to tunnel mode. |
| 4236 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4237 | channel_auto_read(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4238 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4239 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4240 | } |
| 4241 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4242 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4243 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4244 | chn->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4245 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4246 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4247 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 4248 | goto http_msg_closing; |
| 4249 | } |
| 4250 | else { |
| 4251 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4252 | goto http_msg_closed; |
| 4253 | } |
| 4254 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4255 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4256 | } |
| 4257 | |
| 4258 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 4259 | http_msg_closing: |
| 4260 | /* nothing else to forward, just waiting for the output buffer |
| 4261 | * to be empty and for the shutw_now to take effect. |
| 4262 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4263 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4264 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4265 | goto http_msg_closed; |
| 4266 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4267 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4268 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4269 | goto wait_other_side; |
| 4270 | } |
| 4271 | } |
| 4272 | |
| 4273 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 4274 | http_msg_closed: |
| 4275 | goto wait_other_side; |
| 4276 | } |
| 4277 | |
| 4278 | wait_other_side: |
| 4279 | http_silent_debug(__LINE__, s); |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4280 | return txn->req.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4281 | } |
| 4282 | |
| 4283 | |
| 4284 | /* This function updates the response state machine according to the request |
| 4285 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4286 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4287 | * it is only used to find when a request/response couple is complete. Both |
| 4288 | * this function and its equivalent should loop until both return zero. It |
| 4289 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4290 | */ |
| 4291 | int http_sync_res_state(struct session *s) |
| 4292 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4293 | struct channel *chn = s->rep; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4294 | struct http_txn *txn = &s->txn; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4295 | unsigned int old_flags = chn->flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4296 | unsigned int old_state = txn->rsp.msg_state; |
| 4297 | |
| 4298 | http_silent_debug(__LINE__, s); |
| 4299 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 4300 | return 0; |
| 4301 | |
| 4302 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 4303 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4304 | * still monitor the server connection for a possible close |
| 4305 | * while the request is being uploaded, so we don't disable |
| 4306 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4307 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4308 | /* channel_dont_read(chn); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4309 | |
| 4310 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 4311 | goto wait_other_side; |
| 4312 | |
| 4313 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 4314 | /* The client seems to still be sending data, probably |
| 4315 | * because we got an error response during an upload. |
| 4316 | * We have the choice of either breaking the connection |
| 4317 | * or letting it pass through. Let's do the later. |
| 4318 | */ |
| 4319 | goto wait_other_side; |
| 4320 | } |
| 4321 | |
| 4322 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 4323 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4324 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4325 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4326 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4327 | goto wait_other_side; |
| 4328 | } |
| 4329 | |
| 4330 | /* When we get here, it means that both the request and the |
| 4331 | * response have finished receiving. Depending on the connection |
| 4332 | * mode, we'll have to wait for the last bytes to leave in either |
| 4333 | * direction, and sometimes for a close to be effective. |
| 4334 | */ |
| 4335 | |
| 4336 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4337 | /* Server-close mode : shut read and wait for the request |
| 4338 | * side to close its output buffer. The caller will detect |
| 4339 | * when we're in DONE and the other is in CLOSED and will |
| 4340 | * catch that for the final cleanup. |
| 4341 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4342 | if (!(chn->flags & (CF_SHUTR|CF_SHUTR_NOW))) |
| 4343 | channel_shutr_now(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4344 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4345 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4346 | /* Option forceclose is set, or either side wants to close, |
| 4347 | * let's enforce it now that we're not expecting any new |
| 4348 | * data to come. The caller knows the session is complete |
| 4349 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4350 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4351 | if (!(chn->flags & (CF_SHUTW|CF_SHUTW_NOW))) { |
| 4352 | channel_shutr_now(chn); |
| 4353 | channel_shutw_now(chn); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4354 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4355 | } |
| 4356 | else { |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4357 | /* The last possible modes are keep-alive and tunnel. Since tunnel |
| 4358 | * mode does not set the body analyser, we can't reach this place |
| 4359 | * in tunnel mode, so we're left with keep-alive only. |
| 4360 | * This mode is currently not implemented, we switch to tunnel mode. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4361 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4362 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4363 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4364 | chn->flags |= CF_NEVER_WAIT; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4365 | } |
| 4366 | |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4367 | if (chn->flags & (CF_SHUTW|CF_SHUTW_NOW)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4368 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4369 | if (!channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4370 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 4371 | goto http_msg_closing; |
| 4372 | } |
| 4373 | else { |
| 4374 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4375 | goto http_msg_closed; |
| 4376 | } |
| 4377 | } |
| 4378 | goto wait_other_side; |
| 4379 | } |
| 4380 | |
| 4381 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 4382 | http_msg_closing: |
| 4383 | /* nothing else to forward, just waiting for the output buffer |
| 4384 | * to be empty and for the shutw_now to take effect. |
| 4385 | */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4386 | if (channel_is_empty(chn)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4387 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4388 | goto http_msg_closed; |
| 4389 | } |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4390 | else if (chn->flags & CF_SHUTW) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4391 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4392 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4393 | if (objt_server(s->target)) |
| 4394 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4395 | goto wait_other_side; |
| 4396 | } |
| 4397 | } |
| 4398 | |
| 4399 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 4400 | http_msg_closed: |
| 4401 | /* drop any pending data */ |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4402 | bi_erase(chn); |
| 4403 | channel_auto_close(chn); |
| 4404 | channel_auto_read(chn); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4405 | goto wait_other_side; |
| 4406 | } |
| 4407 | |
| 4408 | wait_other_side: |
| 4409 | http_silent_debug(__LINE__, s); |
Willy Tarreau | fc47f91 | 2012-10-20 10:38:09 +0200 | [diff] [blame] | 4410 | /* We force the response to leave immediately if we're waiting for the |
| 4411 | * other side, since there is no pending shutdown to push it out. |
| 4412 | */ |
| 4413 | if (!channel_is_empty(chn)) |
| 4414 | chn->flags |= CF_SEND_DONTWAIT; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 4415 | return txn->rsp.msg_state != old_state || chn->flags != old_flags; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4416 | } |
| 4417 | |
| 4418 | |
| 4419 | /* Resync the request and response state machines. Return 1 if either state |
| 4420 | * changes. |
| 4421 | */ |
| 4422 | int http_resync_states(struct session *s) |
| 4423 | { |
| 4424 | struct http_txn *txn = &s->txn; |
| 4425 | int old_req_state = txn->req.msg_state; |
| 4426 | int old_res_state = txn->rsp.msg_state; |
| 4427 | |
| 4428 | http_silent_debug(__LINE__, s); |
| 4429 | http_sync_req_state(s); |
| 4430 | while (1) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4431 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4432 | if (!http_sync_res_state(s)) |
| 4433 | break; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4434 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4435 | if (!http_sync_req_state(s)) |
| 4436 | break; |
| 4437 | } |
| 4438 | http_silent_debug(__LINE__, s); |
| 4439 | /* OK, both state machines agree on a compatible state. |
| 4440 | * There are a few cases we're interested in : |
| 4441 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 4442 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 4443 | * directions, so let's simply disable both analysers. |
| 4444 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 4445 | * request. |
| 4446 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 4447 | * with server-close mode means we've completed one request and we |
| 4448 | * must re-initialize the server connection. |
| 4449 | */ |
| 4450 | |
| 4451 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 4452 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 4453 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4454 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
| 4455 | s->req->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4456 | channel_auto_close(s->req); |
| 4457 | channel_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4458 | s->rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4459 | channel_auto_close(s->rep); |
| 4460 | channel_auto_read(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4461 | } |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4462 | else if ((txn->req.msg_state >= HTTP_MSG_DONE && |
| 4463 | (txn->rsp.msg_state == HTTP_MSG_CLOSED || (s->rep->flags & CF_SHUTW))) || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4464 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 40f151a | 2012-12-20 12:10:09 +0100 | [diff] [blame] | 4465 | txn->req.msg_state == HTTP_MSG_ERROR) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4466 | s->rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4467 | channel_auto_close(s->rep); |
| 4468 | channel_auto_read(s->rep); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4469 | s->req->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4470 | channel_abort(s->req); |
| 4471 | channel_auto_close(s->req); |
| 4472 | channel_auto_read(s->req); |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 4473 | bi_erase(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4474 | } |
| 4475 | else if (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4476 | txn->rsp.msg_state == HTTP_MSG_DONE && |
| 4477 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) { |
| 4478 | /* server-close: terminate this server connection and |
| 4479 | * reinitialize a fresh-new transaction. |
| 4480 | */ |
| 4481 | http_end_txn_clean_session(s); |
| 4482 | } |
| 4483 | |
| 4484 | http_silent_debug(__LINE__, s); |
| 4485 | return txn->req.msg_state != old_req_state || |
| 4486 | txn->rsp.msg_state != old_res_state; |
| 4487 | } |
| 4488 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4489 | /* This function is an analyser which forwards request body (including chunk |
| 4490 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 4491 | * zero byte. The only situation where it must not be called is when we're in |
| 4492 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 4493 | * remaining data and to resync after end of body. It expects the msg_state to |
| 4494 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 4495 | * 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] | 4496 | * 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] | 4497 | * bytes of pending data + the headers if not already done (between sol and sov). |
| 4498 | * 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] | 4499 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 4500 | 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] | 4501 | { |
| 4502 | struct http_txn *txn = &s->txn; |
| 4503 | struct http_msg *msg = &s->txn.req; |
| 4504 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4505 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4506 | return 0; |
| 4507 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4508 | 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] | 4509 | ((req->flags & CF_SHUTW) && (req->to_forward || req->buf->o))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4510 | /* Output closed while we were sending data. We must abort and |
| 4511 | * wake the other side up. |
| 4512 | */ |
| 4513 | msg->msg_state = HTTP_MSG_ERROR; |
| 4514 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 4515 | return 1; |
| 4516 | } |
| 4517 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4518 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4519 | channel_auto_close(req); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4520 | |
| 4521 | /* Note that we don't have to send 100-continue back because we don't |
| 4522 | * need the data to complete our job, and it's up to the server to |
| 4523 | * decide whether to return 100, 417 or anything else in return of |
| 4524 | * an "Expect: 100-continue" header. |
| 4525 | */ |
| 4526 | |
| 4527 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 4528 | /* 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] | 4529 | * 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] | 4530 | * is still null. We must save the body in msg->next because it |
| 4531 | * survives buffer re-alignments. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4532 | */ |
Willy Tarreau | ea1175a | 2012-03-05 15:52:30 +0100 | [diff] [blame] | 4533 | msg->next = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4534 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4535 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4536 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 4537 | else |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4538 | msg->msg_state = HTTP_MSG_DATA; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4539 | } |
| 4540 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4541 | while (1) { |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 4542 | unsigned int bytes; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4543 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4544 | http_silent_debug(__LINE__, s); |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 4545 | /* we may have some data pending between sol and sov */ |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 4546 | bytes = msg->sov - msg->sol; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4547 | if (msg->chunk_len || bytes) { |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 4548 | msg->sol = msg->sov; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4549 | msg->next -= bytes; /* will be forwarded */ |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 4550 | msg->chunk_len += bytes; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4551 | msg->chunk_len -= channel_forward(req, msg->chunk_len); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4552 | } |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 4553 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4554 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 4555 | /* must still forward */ |
| 4556 | if (req->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4557 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4558 | |
| 4559 | /* nothing left to forward */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4560 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 4561 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4562 | else |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4563 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4564 | } |
| 4565 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4566 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 4567 | * 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] | 4568 | * TRAILERS state. |
| 4569 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4570 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4571 | |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 4572 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4573 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4574 | else if (ret < 0) { |
| 4575 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4576 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4577 | 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] | 4578 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4579 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4580 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4581 | } |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 4582 | else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4583 | /* we want the CRLF after the data */ |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 4584 | int ret = http_skip_chunk_crlf(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4585 | |
| 4586 | if (ret == 0) |
| 4587 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4588 | else if (ret < 0) { |
| 4589 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4590 | if (msg->err_pos >= 0) |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 4591 | 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] | 4592 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4593 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4594 | /* we're in MSG_CHUNK_SIZE now */ |
| 4595 | } |
| 4596 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 4597 | int ret = http_forward_trailers(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4598 | |
| 4599 | if (ret == 0) |
| 4600 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4601 | else if (ret < 0) { |
| 4602 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4603 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4604 | 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] | 4605 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4606 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4607 | /* we're in HTTP_MSG_DONE now */ |
| 4608 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4609 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4610 | int old_state = msg->msg_state; |
| 4611 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4612 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4613 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4614 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 4615 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4616 | channel_dont_close(req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4617 | if (http_resync_states(s)) { |
| 4618 | /* some state changes occurred, maybe the analyser |
| 4619 | * was disabled too. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 4620 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4621 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4622 | if (req->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4623 | /* request errors are most likely due to |
| 4624 | * the server aborting the transfer. |
| 4625 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4626 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4627 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4628 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4629 | 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] | 4630 | goto return_bad_req; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4631 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4632 | return 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4633 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4634 | |
| 4635 | /* If "option abortonclose" is set on the backend, we |
| 4636 | * want to monitor the client's connection and forward |
| 4637 | * any shutdown notification to the server, which will |
| 4638 | * decide whether to close or to go on processing the |
| 4639 | * request. |
| 4640 | */ |
| 4641 | if (s->be->options & PR_O_ABRT_CLOSE) { |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4642 | channel_auto_read(req); |
| 4643 | channel_auto_close(req); |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4644 | } |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4645 | else if (s->txn.meth == HTTP_METH_POST) { |
| 4646 | /* POST requests may require to read extra CRLF |
| 4647 | * sent by broken browsers and which could cause |
| 4648 | * an RST to be sent upon close on some systems |
| 4649 | * (eg: Linux). |
| 4650 | */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4651 | channel_auto_read(req); |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4652 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4653 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4654 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4655 | } |
| 4656 | } |
| 4657 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4658 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4659 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4660 | if (req->flags & CF_SHUTR) { |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4661 | if (!(s->flags & SN_ERR_MASK)) |
| 4662 | s->flags |= SN_ERR_CLICL; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4663 | if (!(s->flags & SN_FINST_MASK)) { |
| 4664 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4665 | s->flags |= SN_FINST_H; |
| 4666 | else |
| 4667 | s->flags |= SN_FINST_D; |
| 4668 | } |
| 4669 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4670 | s->fe->fe_counters.cli_aborts++; |
| 4671 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4672 | if (objt_server(s->target)) |
| 4673 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4674 | |
| 4675 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4676 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4677 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4678 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4679 | if (req->flags & CF_SHUTW) |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4680 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4681 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4682 | /* 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] | 4683 | * 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] | 4684 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4685 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4686 | channel_dont_close(req); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4687 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4688 | /* 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] | 4689 | * 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] | 4690 | * 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] | 4691 | * modes are already handled by the stream sock layer. We must not do |
| 4692 | * this in content-length mode because it could present the MSG_MORE |
| 4693 | * flag with the last block of forwarded data, which would cause an |
| 4694 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4695 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 4696 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4697 | req->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 4698 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4699 | http_silent_debug(__LINE__, s); |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4700 | return 0; |
| 4701 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4702 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4703 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4704 | if (s->listener->counters) |
| 4705 | s->listener->counters->failed_req++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4706 | return_bad_req_stats_ok: |
| 4707 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4708 | if (txn->status) { |
| 4709 | /* Note: we don't send any error if some data were already sent */ |
| 4710 | stream_int_retnclose(req->prod, NULL); |
| 4711 | } else { |
| 4712 | txn->status = 400; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4713 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_400)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4714 | } |
| 4715 | req->analysers = 0; |
| 4716 | 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] | 4717 | |
| 4718 | if (!(s->flags & SN_ERR_MASK)) |
| 4719 | s->flags |= SN_ERR_PRXCOND; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4720 | if (!(s->flags & SN_FINST_MASK)) { |
| 4721 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4722 | s->flags |= SN_FINST_H; |
| 4723 | else |
| 4724 | s->flags |= SN_FINST_D; |
| 4725 | } |
| 4726 | return 0; |
| 4727 | |
| 4728 | aborted_xfer: |
| 4729 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4730 | if (txn->status) { |
| 4731 | /* Note: we don't send any error if some data were already sent */ |
| 4732 | stream_int_retnclose(req->prod, NULL); |
| 4733 | } else { |
| 4734 | txn->status = 502; |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4735 | stream_int_retnclose(req->prod, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4736 | } |
| 4737 | req->analysers = 0; |
| 4738 | s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 4739 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4740 | s->fe->fe_counters.srv_aborts++; |
| 4741 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4742 | if (objt_server(s->target)) |
| 4743 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4744 | |
| 4745 | if (!(s->flags & SN_ERR_MASK)) |
| 4746 | s->flags |= SN_ERR_SRVCL; |
| 4747 | if (!(s->flags & SN_FINST_MASK)) { |
| 4748 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4749 | s->flags |= SN_FINST_H; |
| 4750 | else |
| 4751 | s->flags |= SN_FINST_D; |
| 4752 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4753 | return 0; |
| 4754 | } |
| 4755 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4756 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 4757 | * processing can continue on next analysers, or zero if it either needs more |
| 4758 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 4759 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers |
| 4760 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 4761 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4762 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 4763 | 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] | 4764 | { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4765 | struct http_txn *txn = &s->txn; |
| 4766 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4767 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4768 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4769 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4770 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4771 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 4772 | 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] | 4773 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4774 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4775 | rep, |
| 4776 | rep->rex, rep->wex, |
| 4777 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4778 | rep->buf->i, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4779 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 4780 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4781 | /* |
| 4782 | * Now parse the partial (or complete) lines. |
| 4783 | * We will check the response syntax, and also join multi-line |
| 4784 | * headers. An index of all the lines will be elaborated while |
| 4785 | * parsing. |
| 4786 | * |
| 4787 | * For the parsing, we use a 28 states FSM. |
| 4788 | * |
| 4789 | * Here is the information we currently have : |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4790 | * rep->buf->p = beginning of response |
| 4791 | * rep->buf->p + msg->eoh = end of processed headers / start of current one |
| 4792 | * rep->buf->p + rep->buf->i = end of input data |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 4793 | * msg->eol = end of current header or line (LF or CRLF) |
| 4794 | * msg->next = first non-visited byte |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4795 | */ |
| 4796 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4797 | /* There's a protected area at the end of the buffer for rewriting |
| 4798 | * purposes. We don't want to start to parse the request if the |
| 4799 | * protected area is affected, because we may have to move processed |
| 4800 | * data later, which is much more complicated. |
| 4801 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4802 | if (buffer_not_empty(rep->buf) && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 4803 | if (unlikely(channel_full(rep) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4804 | bi_end(rep->buf) < b_ptr(rep->buf, msg->next) || |
| 4805 | bi_end(rep->buf) > rep->buf->data + rep->buf->size - global.tune.maxrewrite)) { |
| 4806 | if (rep->buf->o) { |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4807 | /* 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] | 4808 | 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] | 4809 | goto abort_response; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4810 | channel_dont_close(rep); |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4811 | rep->flags |= CF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4812 | return 0; |
| 4813 | } |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4814 | if (rep->buf->i <= rep->buf->size - global.tune.maxrewrite) |
| 4815 | buffer_slow_realign(msg->chn->buf); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4816 | } |
| 4817 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4818 | if (likely(msg->next < rep->buf->i)) |
Willy Tarreau | a560c21 | 2012-03-09 13:50:57 +0100 | [diff] [blame] | 4819 | http_msg_analyzer(msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4820 | } |
| 4821 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4822 | /* 1: we might have to print this header in debug mode */ |
| 4823 | if (unlikely((global.mode & MODE_DEBUG) && |
| 4824 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4825 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4826 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4827 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4828 | sol = rep->buf->p; |
| 4829 | 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] | 4830 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4831 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4832 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 4833 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4834 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4835 | while (cur_idx) { |
| 4836 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 4837 | debug_hdr("srvhdr", s, sol, eol); |
| 4838 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 4839 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 4840 | } |
| 4841 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4842 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4843 | /* |
| 4844 | * Now we quickly check if we have found a full valid response. |
| 4845 | * If not so, we check the FD and buffer states before leaving. |
| 4846 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4847 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4848 | * responses are checked first. |
| 4849 | * |
| 4850 | * Depending on whether the client is still there or not, we |
| 4851 | * may send an error response back or not. Note that normally |
| 4852 | * we should only check for HTTP status there, and check I/O |
| 4853 | * errors somewhere else. |
| 4854 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4855 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4856 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4857 | /* Invalid response */ |
| 4858 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4859 | /* we detected a parsing error. We want to archive this response |
| 4860 | * in the dedicated proxy area for later troubleshooting. |
| 4861 | */ |
| 4862 | hdr_response_bad: |
| 4863 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4864 | 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] | 4865 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4866 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4867 | if (objt_server(s->target)) { |
| 4868 | objt_server(s->target)->counters.failed_resp++; |
| 4869 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4870 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 4871 | abort_response: |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4872 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4873 | rep->analysers = 0; |
| 4874 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4875 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 4876 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4877 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4878 | |
| 4879 | if (!(s->flags & SN_ERR_MASK)) |
| 4880 | s->flags |= SN_ERR_PRXCOND; |
| 4881 | if (!(s->flags & SN_FINST_MASK)) |
| 4882 | s->flags |= SN_FINST_H; |
| 4883 | |
| 4884 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4885 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4886 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4887 | /* too large response does not fit in buffer. */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4888 | else if (buffer_full(rep->buf, global.tune.maxrewrite)) { |
Willy Tarreau | fec4d89 | 2011-09-02 20:04:57 +0200 | [diff] [blame] | 4889 | if (msg->err_pos < 0) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 4890 | msg->err_pos = rep->buf->i; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4891 | goto hdr_response_bad; |
| 4892 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4893 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4894 | /* read error */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4895 | else if (rep->flags & CF_READ_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4896 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4897 | 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] | 4898 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4899 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4900 | if (objt_server(s->target)) { |
| 4901 | objt_server(s->target)->counters.failed_resp++; |
| 4902 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4903 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 4904 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4905 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4906 | rep->analysers = 0; |
| 4907 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4908 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 4909 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4910 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 4911 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4912 | if (!(s->flags & SN_ERR_MASK)) |
| 4913 | s->flags |= SN_ERR_SRVCL; |
| 4914 | if (!(s->flags & SN_FINST_MASK)) |
| 4915 | s->flags |= SN_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 4916 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4917 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4918 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4919 | /* read timeout : return a 504 to the client. */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4920 | else if (rep->flags & CF_READ_TIMEOUT) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4921 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4922 | 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] | 4923 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4924 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4925 | if (objt_server(s->target)) { |
| 4926 | objt_server(s->target)->counters.failed_resp++; |
| 4927 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4928 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4929 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4930 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4931 | rep->analysers = 0; |
| 4932 | txn->status = 504; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4933 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 4934 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4935 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4936 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4937 | if (!(s->flags & SN_ERR_MASK)) |
| 4938 | s->flags |= SN_ERR_SRVTO; |
| 4939 | if (!(s->flags & SN_FINST_MASK)) |
| 4940 | s->flags |= SN_FINST_H; |
| 4941 | return 0; |
| 4942 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 4943 | |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 4944 | /* client abort with an abortonclose */ |
| 4945 | else if ((rep->flags & CF_SHUTR) && ((s->req->flags & (CF_SHUTR|CF_SHUTW)) == (CF_SHUTR|CF_SHUTW))) { |
| 4946 | s->fe->fe_counters.cli_aborts++; |
| 4947 | s->be->be_counters.cli_aborts++; |
| 4948 | if (objt_server(s->target)) |
| 4949 | objt_server(s->target)->counters.cli_aborts++; |
| 4950 | |
| 4951 | rep->analysers = 0; |
| 4952 | channel_auto_close(rep); |
| 4953 | |
| 4954 | txn->status = 400; |
| 4955 | bi_erase(rep); |
| 4956 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_400)); |
| 4957 | |
| 4958 | if (!(s->flags & SN_ERR_MASK)) |
| 4959 | s->flags |= SN_ERR_CLICL; |
| 4960 | if (!(s->flags & SN_FINST_MASK)) |
| 4961 | s->flags |= SN_FINST_H; |
| 4962 | |
| 4963 | /* process_session() will take care of the error */ |
| 4964 | return 0; |
| 4965 | } |
| 4966 | |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 4967 | /* 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] | 4968 | else if (rep->flags & CF_SHUTR) { |
Willy Tarreau | 3b8c08a | 2011-09-02 20:16:24 +0200 | [diff] [blame] | 4969 | if (msg->msg_state >= HTTP_MSG_RPVER || msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4970 | 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] | 4971 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4972 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4973 | if (objt_server(s->target)) { |
| 4974 | objt_server(s->target)->counters.failed_resp++; |
| 4975 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4976 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4977 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4978 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4979 | rep->analysers = 0; |
| 4980 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4981 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 4982 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 4983 | stream_int_retnclose(rep->cons, http_error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4984 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4985 | if (!(s->flags & SN_ERR_MASK)) |
| 4986 | s->flags |= SN_ERR_SRVCL; |
| 4987 | if (!(s->flags & SN_FINST_MASK)) |
| 4988 | s->flags |= SN_FINST_H; |
| 4989 | return 0; |
| 4990 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4991 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4992 | /* write error to client (we don't send any message then) */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 4993 | else if (rep->flags & CF_WRITE_ERROR) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4994 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 4995 | 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] | 4996 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4997 | s->be->be_counters.failed_resp++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4998 | rep->analysers = 0; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 4999 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5000 | |
| 5001 | if (!(s->flags & SN_ERR_MASK)) |
| 5002 | s->flags |= SN_ERR_CLICL; |
| 5003 | if (!(s->flags & SN_FINST_MASK)) |
| 5004 | s->flags |= SN_FINST_H; |
| 5005 | |
| 5006 | /* process_session() will take care of the error */ |
| 5007 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5008 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5009 | |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5010 | channel_dont_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5011 | return 0; |
| 5012 | } |
| 5013 | |
| 5014 | /* More interesting part now : we know that we have a complete |
| 5015 | * response which at least looks like HTTP. We have an indicator |
| 5016 | * of each header's length, so we can parse them quickly. |
| 5017 | */ |
| 5018 | |
| 5019 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5020 | 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] | 5021 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5022 | /* |
| 5023 | * 1: get the status code |
| 5024 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5025 | n = rep->buf->p[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5026 | if (n < 1 || n > 5) |
| 5027 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 5028 | /* when the client triggers a 4xx from the server, it's most often due |
| 5029 | * to a missing object or permission. These events should be tracked |
| 5030 | * because if they happen often, it may indicate a brute force or a |
| 5031 | * vulnerability scan. |
| 5032 | */ |
| 5033 | if (n == 4) |
| 5034 | session_inc_http_err_ctr(s); |
| 5035 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5036 | if (objt_server(s->target)) |
| 5037 | objt_server(s->target)->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5038 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5039 | /* check if the response is HTTP/1.1 or above */ |
| 5040 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5041 | ((rep->buf->p[5] > '1') || |
| 5042 | ((rep->buf->p[5] == '1') && (rep->buf->p[7] >= '1')))) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5043 | msg->flags |= HTTP_MSGF_VER_11; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5044 | |
| 5045 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 5046 | 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] | 5047 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5048 | /* transfer length unknown*/ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5049 | msg->flags &= ~HTTP_MSGF_XFER_LEN; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5050 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5051 | 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] | 5052 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 5053 | /* Adjust server's health based on status code. Note: status codes 501 |
| 5054 | * and 505 are triggered on demand by client request, so we must not |
| 5055 | * count them as server failures. |
| 5056 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5057 | if (objt_server(s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5058 | 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] | 5059 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5060 | else |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5061 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 5062 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5063 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5064 | /* |
| 5065 | * 2: check for cacheability. |
| 5066 | */ |
| 5067 | |
| 5068 | switch (txn->status) { |
| 5069 | case 200: |
| 5070 | case 203: |
| 5071 | case 206: |
| 5072 | case 300: |
| 5073 | case 301: |
| 5074 | case 410: |
| 5075 | /* RFC2616 @13.4: |
| 5076 | * "A response received with a status code of |
| 5077 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 5078 | * by a cache (...) unless a cache-control |
| 5079 | * directive prohibits caching." |
| 5080 | * |
| 5081 | * RFC2616 @9.5: POST method : |
| 5082 | * "Responses to this method are not cacheable, |
| 5083 | * unless the response includes appropriate |
| 5084 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5085 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5086 | if (likely(txn->meth != HTTP_METH_POST) && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5087 | ((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] | 5088 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 5089 | break; |
| 5090 | default: |
| 5091 | break; |
| 5092 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5093 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5094 | /* |
| 5095 | * 3: we may need to capture headers |
| 5096 | */ |
| 5097 | s->logs.logwait &= ~LW_RESP; |
Willy Tarreau | 42f7d89 | 2012-03-24 08:28:09 +0100 | [diff] [blame] | 5098 | if (unlikely((s->logs.logwait & LW_RSPHDR) && txn->rsp.cap)) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5099 | capture_headers(rep->buf->p, &txn->hdr_idx, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5100 | txn->rsp.cap, s->fe->rsp_cap); |
| 5101 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5102 | /* 4: determine the transfer-length. |
| 5103 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 5104 | * the presence of a message-body in a RESPONSE and its transfer length |
| 5105 | * must be determined that way : |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5106 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5107 | * All responses to the HEAD request method MUST NOT include a |
| 5108 | * message-body, even though the presence of entity-header fields |
| 5109 | * might lead one to believe they do. All 1xx (informational), 204 |
| 5110 | * (No Content), and 304 (Not Modified) responses MUST NOT include a |
| 5111 | * message-body. All other responses do include a message-body, |
| 5112 | * although it MAY be of zero length. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5113 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5114 | * 1. Any response which "MUST NOT" include a message-body (such as the |
| 5115 | * 1xx, 204 and 304 responses and any response to a HEAD request) is |
| 5116 | * always terminated by the first empty line after the header fields, |
| 5117 | * regardless of the entity-header fields present in the message. |
| 5118 | * |
| 5119 | * 2. If a Transfer-Encoding header field (Section 9.7) is present and |
| 5120 | * the "chunked" transfer-coding (Section 6.2) is used, the |
| 5121 | * transfer-length is defined by the use of this transfer-coding. |
| 5122 | * If a Transfer-Encoding header field is present and the "chunked" |
| 5123 | * transfer-coding is not present, the transfer-length is defined by |
| 5124 | * the sender closing the connection. |
| 5125 | * |
| 5126 | * 3. If a Content-Length header field is present, its decimal value in |
| 5127 | * OCTETs represents both the entity-length and the transfer-length. |
| 5128 | * If a message is received with both a Transfer-Encoding header |
| 5129 | * field and a Content-Length header field, the latter MUST be ignored. |
| 5130 | * |
| 5131 | * 4. If the message uses the media type "multipart/byteranges", and |
| 5132 | * the transfer-length is not otherwise specified, then this self- |
| 5133 | * delimiting media type defines the transfer-length. This media |
| 5134 | * type MUST NOT be used unless the sender knows that the recipient |
| 5135 | * can parse it; the presence in a request of a Range header with |
| 5136 | * multiple byte-range specifiers from a 1.1 client implies that the |
| 5137 | * client can parse multipart/byteranges responses. |
| 5138 | * |
| 5139 | * 5. By the server closing the connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5140 | */ |
| 5141 | |
| 5142 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5143 | * 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] | 5144 | * 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] | 5145 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 5146 | */ |
| 5147 | if (txn->meth == HTTP_METH_HEAD || |
| 5148 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5149 | txn->status == 204 || txn->status == 304) { |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5150 | msg->flags |= HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 9101535 | 2012-11-27 07:31:33 +0100 | [diff] [blame] | 5151 | s->comp_algo = NULL; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5152 | goto skip_content_length; |
| 5153 | } |
| 5154 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5155 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5156 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5157 | while ((msg->flags & HTTP_MSGF_VER_11) && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5158 | 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] | 5159 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5160 | msg->flags |= (HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
| 5161 | else if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5162 | /* bad transfer-encoding (chunked followed by something else) */ |
| 5163 | use_close_only = 1; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5164 | msg->flags &= ~(HTTP_MSGF_TE_CHNK | HTTP_MSGF_XFER_LEN); |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5165 | break; |
| 5166 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5167 | } |
| 5168 | |
| 5169 | /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */ |
| 5170 | ctx.idx = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5171 | while (!(msg->flags & HTTP_MSGF_TE_CHNK) && !use_close_only && |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5172 | 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] | 5173 | signed long long cl; |
| 5174 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5175 | if (!ctx.vlen) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5176 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5177 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5178 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5179 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5180 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5181 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5182 | goto hdr_response_bad; /* parse failure */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5183 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5184 | |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5185 | if (cl < 0) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5186 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5187 | goto hdr_response_bad; |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5188 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5189 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5190 | if ((msg->flags & HTTP_MSGF_CNT_LEN) && (msg->chunk_len != cl)) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5191 | msg->err_pos = ctx.line + ctx.val - rep->buf->p; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5192 | goto hdr_response_bad; /* already specified, was different */ |
Willy Tarreau | ad14f75 | 2011-09-02 20:33:27 +0200 | [diff] [blame] | 5193 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5194 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5195 | msg->flags |= HTTP_MSGF_CNT_LEN | HTTP_MSGF_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5196 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5197 | } |
| 5198 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5199 | if (s->fe->comp || s->be->comp) |
| 5200 | select_compression_response_header(s, rep->buf); |
| 5201 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5202 | /* FIXME: we should also implement the multipart/byterange method. |
| 5203 | * For now on, we resort to close mode in this case (unknown length). |
| 5204 | */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5205 | skip_content_length: |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5206 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5207 | /* end of job, return OK */ |
| 5208 | rep->analysers &= ~an_bit; |
| 5209 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5210 | channel_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5211 | return 1; |
| 5212 | } |
| 5213 | |
| 5214 | /* This function performs all the processing enabled for the current response. |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5215 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 5216 | * and updates t->rep->analysers. It might make sense to explode it into several |
| 5217 | * other functions. It works like process_request (see indications above). |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5218 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 5219 | 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] | 5220 | { |
| 5221 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5222 | struct http_msg *msg = &txn->rsp; |
| 5223 | struct proxy *cur_proxy; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 5224 | struct cond_wordlist *wl; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5225 | |
Willy Tarreau | 02d6cfc | 2012-03-01 18:19:58 +0100 | [diff] [blame] | 5226 | 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] | 5227 | now_ms, __FUNCTION__, |
| 5228 | t, |
| 5229 | rep, |
| 5230 | rep->rex, rep->wex, |
| 5231 | rep->flags, |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5232 | rep->buf->i, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5233 | rep->analysers); |
| 5234 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5235 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5236 | return 0; |
| 5237 | |
| 5238 | rep->analysers &= ~an_bit; |
| 5239 | rep->analyse_exp = TICK_ETERNITY; |
| 5240 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5241 | /* Now we have to check if we need to modify the Connection header. |
| 5242 | * This is more difficult on the response than it is on the request, |
| 5243 | * because we can have two different HTTP versions and we don't know |
| 5244 | * how the client will interprete a response. For instance, let's say |
| 5245 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 5246 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 5247 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 5248 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 5249 | * the lack of header as a keep-alive. Thus we will use two flags |
| 5250 | * indicating how a request MAY be understood by the client. In case |
| 5251 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 5252 | * ambiguous cases such as both close and keepalive are seen, then we |
| 5253 | * will fall back to explicit close. Note that we won't take risks with |
| 5254 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5255 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5256 | */ |
| 5257 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5258 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 5259 | txn->status == 101)) { |
| 5260 | /* Either we've established an explicit tunnel, or we're |
| 5261 | * switching the protocol. In both cases, we're very unlikely |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5262 | * to understand the next protocols. We have to switch to tunnel |
| 5263 | * mode, so that we transfer the request and responses then let |
| 5264 | * this protocol pass unmodified. When we later implement specific |
| 5265 | * parsers for such protocols, we'll want to check the Upgrade |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5266 | * header which contains information about that protocol for |
| 5267 | * responses with status 101 (eg: see RFC2817 about TLS). |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5268 | */ |
| 5269 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 5270 | } |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5271 | else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 5272 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
| 5273 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5274 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5275 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5276 | /* on unknown transfer length, we must close */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5277 | if (!(msg->flags & HTTP_MSGF_XFER_LEN) && |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5278 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 5279 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5280 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5281 | /* now adjust header transformations depending on current state */ |
| 5282 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 5283 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5284 | to_del |= 2; /* remove "keep-alive" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5285 | if (!(msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5286 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5287 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5288 | else { /* SCL / KAL */ |
| 5289 | to_del |= 1; /* remove "close" on any response */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5290 | if (txn->req.flags & msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5291 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5292 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5293 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5294 | /* Parse and remove some headers from the connection header */ |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5295 | http_parse_connection_header(txn, msg, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5296 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5297 | /* Some keep-alive responses are converted to Server-close if |
| 5298 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5299 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5300 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 5301 | if ((txn->flags & TX_HDR_CONN_CLO) || |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5302 | (!(txn->flags & TX_HDR_CONN_KAL) && !(msg->flags & HTTP_MSGF_VER_11))) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5303 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5304 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5305 | } |
| 5306 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5307 | if (1) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5308 | /* |
| 5309 | * 3: we will have to evaluate the filters. |
| 5310 | * As opposed to version 1.2, now they will be evaluated in the |
| 5311 | * filters order and not in the header order. This means that |
| 5312 | * each filter has to be validated among all headers. |
| 5313 | * |
| 5314 | * Filters are tried with ->be first, then with ->fe if it is |
| 5315 | * different from ->be. |
| 5316 | */ |
| 5317 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5318 | cur_proxy = t->be; |
| 5319 | while (1) { |
| 5320 | struct proxy *rule_set = cur_proxy; |
| 5321 | |
| 5322 | /* try headers filters */ |
| 5323 | if (rule_set->rsp_exp != NULL) { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 5324 | if (apply_filters_to_response(t, rep, rule_set) < 0) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5325 | return_bad_resp: |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5326 | if (objt_server(t->target)) { |
| 5327 | objt_server(t->target)->counters.failed_resp++; |
| 5328 | health_adjust(objt_server(t->target), HANA_STATUS_HTTP_RSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5329 | } |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5330 | t->be->be_counters.failed_resp++; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5331 | return_srv_prx_502: |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 5332 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5333 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5334 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 9dab5fc | 2012-05-07 11:56:55 +0200 | [diff] [blame] | 5335 | bi_erase(rep); |
Willy Tarreau | 783f258 | 2012-09-04 12:19:04 +0200 | [diff] [blame] | 5336 | stream_int_retnclose(rep->cons, http_error_message(t, HTTP_ERR_502)); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5337 | if (!(t->flags & SN_ERR_MASK)) |
| 5338 | t->flags |= SN_ERR_PRXCOND; |
| 5339 | if (!(t->flags & SN_FINST_MASK)) |
| 5340 | t->flags |= SN_FINST_H; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 5341 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5342 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5343 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5344 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5345 | /* has the response been denied ? */ |
| 5346 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5347 | if (objt_server(t->target)) |
| 5348 | objt_server(t->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5349 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5350 | t->be->be_counters.denied_resp++; |
| 5351 | t->fe->fe_counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5352 | if (t->listener->counters) |
| 5353 | t->listener->counters->denied_resp++; |
| 5354 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5355 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 5356 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5357 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5358 | /* add response headers from the rule sets in the same order */ |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 5359 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 5360 | if (txn->status < 200) |
| 5361 | break; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 5362 | if (wl->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 5363 | 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] | 5364 | ret = acl_pass(ret); |
| 5365 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 5366 | ret = !ret; |
| 5367 | if (!ret) |
| 5368 | continue; |
| 5369 | } |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5370 | 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] | 5371 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 5372 | } |
| 5373 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5374 | /* check whether we're already working on the frontend */ |
| 5375 | if (cur_proxy == t->fe) |
| 5376 | break; |
| 5377 | cur_proxy = t->fe; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5378 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5379 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5380 | /* |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5381 | * We may be facing a 100-continue response, in which case this |
| 5382 | * is not the right response, and we're waiting for the next one. |
| 5383 | * 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] | 5384 | * next one. |
| 5385 | */ |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5386 | if (unlikely(txn->status == 100)) { |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5387 | hdr_idx_init(&txn->hdr_idx); |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5388 | msg->next -= channel_forward(rep, msg->next); |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5389 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 5390 | txn->status = 0; |
| 5391 | rep->analysers |= AN_RES_WAIT_HTTP | an_bit; |
| 5392 | return 1; |
| 5393 | } |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5394 | else if (unlikely(txn->status < 200)) |
| 5395 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5396 | |
| 5397 | /* we don't have any 1xx status code now */ |
| 5398 | |
| 5399 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5400 | * 4: check for server cookie. |
| 5401 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5402 | if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name || |
| 5403 | (t->be->options & PR_O_CHK_CACHE)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5404 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5405 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5406 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5407 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5408 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5409 | */ |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5410 | 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] | 5411 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5412 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5413 | /* |
| 5414 | * 6: add server cookie in the response if needed |
| 5415 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5416 | if (objt_server(t->target) && (t->be->ck_opts & PR_CK_INS) && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5417 | !((txn->flags & TX_SCK_FOUND) && (t->be->ck_opts & PR_CK_PSV)) && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5418 | (!(t->flags & SN_DIRECT) || |
| 5419 | ((t->be->cookie_maxidle || txn->cookie_last_date) && |
| 5420 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 5421 | (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 5422 | (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5423 | (!(t->be->ck_opts & PR_CK_POST) || (txn->meth == HTTP_METH_POST)) && |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 5424 | !(t->flags & SN_IGNORE_PRST)) { |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5425 | /* the server is known, it's not the one the client requested, or the |
| 5426 | * cookie's last seen date needs to be refreshed. We have to |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5427 | * insert a set-cookie here, except if we want to insert only on POST |
| 5428 | * requests and this one isn't. Note that servers which don't have cookies |
| 5429 | * (eg: some backup servers) will return a full cookie removal request. |
| 5430 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5431 | if (!objt_server(t->target)->cookie) { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5432 | chunk_printf(&trash, |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5433 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 5434 | t->be->cookie_name); |
| 5435 | } |
| 5436 | else { |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5437 | 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] | 5438 | |
| 5439 | if (t->be->cookie_maxidle || t->be->cookie_maxlife) { |
| 5440 | /* emit last_date, which is mandatory */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5441 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
| 5442 | s30tob64((date.tv_sec+3) >> 2, trash.str + trash.len); |
| 5443 | trash.len += 5; |
| 5444 | |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5445 | if (t->be->cookie_maxlife) { |
| 5446 | /* emit first_date, which is either the original one or |
| 5447 | * the current date. |
| 5448 | */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5449 | trash.str[trash.len++] = COOKIE_DELIM_DATE; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5450 | s30tob64(txn->cookie_first_date ? |
| 5451 | txn->cookie_first_date >> 2 : |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5452 | (date.tv_sec+3) >> 2, trash.str + trash.len); |
| 5453 | trash.len += 5; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5454 | } |
| 5455 | } |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5456 | chunk_appendf(&trash, "; path=/"); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5457 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5458 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5459 | if (t->be->cookie_domain) |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5460 | chunk_appendf(&trash, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5461 | |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 5462 | if (t->be->ck_opts & PR_CK_HTTPONLY) |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5463 | chunk_appendf(&trash, "; HttpOnly"); |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 5464 | |
| 5465 | if (t->be->ck_opts & PR_CK_SECURE) |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5466 | chunk_appendf(&trash, "; Secure"); |
Willy Tarreau | 4992dd2 | 2012-05-31 21:02:17 +0200 | [diff] [blame] | 5467 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5468 | 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] | 5469 | goto return_bad_resp; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5470 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 5471 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5472 | if (objt_server(t->target)->cookie && (t->flags & SN_DIRECT)) |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5473 | /* the server did not change, only the date was updated */ |
| 5474 | txn->flags |= TX_SCK_UPDATED; |
| 5475 | else |
| 5476 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5477 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5478 | /* Here, we will tell an eventual cache on the client side that we don't |
| 5479 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 5480 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 5481 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 5482 | */ |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 5483 | if ((t->be->ck_opts & PR_CK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5484 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5485 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 5486 | |
Willy Tarreau | 6acf7c9 | 2012-03-09 13:30:45 +0100 | [diff] [blame] | 5487 | if (unlikely(http_header_add_tail2(&txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 5488 | "Cache-control: private", 22) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5489 | goto return_bad_resp; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5490 | } |
| 5491 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5492 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5493 | /* |
| 5494 | * 7: check if result will be cacheable with a cookie. |
| 5495 | * We'll block the response if security checks have caught |
| 5496 | * nasty things such as a cacheable cookie. |
| 5497 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 5498 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 5499 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5500 | (t->be->options & PR_O_CHK_CACHE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5501 | |
| 5502 | /* we're in presence of a cacheable response containing |
| 5503 | * a set-cookie header. We'll block it as requested by |
| 5504 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5505 | */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5506 | if (objt_server(t->target)) |
| 5507 | objt_server(t->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5508 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5509 | t->be->be_counters.denied_resp++; |
| 5510 | t->fe->fe_counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5511 | if (t->listener->counters) |
| 5512 | t->listener->counters->denied_resp++; |
| 5513 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5514 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5515 | 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] | 5516 | send_log(t->be, LOG_ALERT, |
| 5517 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5518 | 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] | 5519 | goto return_srv_prx_502; |
| 5520 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5521 | |
| 5522 | /* |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5523 | * 8: adjust "Connection: close" or "Connection: keep-alive" if needed. |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 5524 | * If an "Upgrade" token is found, the header is left untouched in order |
| 5525 | * not to have to deal with some client bugs : some of them fail an upgrade |
| 5526 | * if anything but "Upgrade" is present in the Connection header. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5527 | */ |
Willy Tarreau | 50fc777 | 2012-11-11 22:19:57 +0100 | [diff] [blame] | 5528 | if (!(txn->flags & TX_HDR_CONN_UPG) && |
| 5529 | (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
| 5530 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5531 | unsigned int want_flags = 0; |
| 5532 | |
| 5533 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5534 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5535 | /* we want a keep-alive response here. Keep-alive header |
| 5536 | * required if either side is not 1.1. |
| 5537 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5538 | if (!(txn->req.flags & msg->flags & HTTP_MSGF_VER_11)) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5539 | want_flags |= TX_CON_KAL_SET; |
| 5540 | } |
| 5541 | else { |
| 5542 | /* we want a close response here. Close header required if |
| 5543 | * the server is 1.1, regardless of the client. |
| 5544 | */ |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5545 | if (msg->flags & HTTP_MSGF_VER_11) |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5546 | want_flags |= TX_CON_CLO_SET; |
| 5547 | } |
| 5548 | |
| 5549 | 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] | 5550 | http_change_connection_header(txn, msg, want_flags); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5551 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5552 | |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5553 | skip_header_mangling: |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5554 | if ((msg->flags & HTTP_MSGF_XFER_LEN) || |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5555 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5556 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 5557 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5558 | /************************************************************* |
| 5559 | * OK, that's finished for the headers. We have done what we * |
| 5560 | * could. Let's switch to the DATA state. * |
| 5561 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5562 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5563 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5564 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5565 | /* if the user wants to log as soon as possible, without counting |
| 5566 | * bytes from the server, then this is the right moment. We have |
| 5567 | * to temporarily assign bytes_out to log what we currently have. |
| 5568 | */ |
| 5569 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 5570 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 5571 | t->logs.bytes_out = txn->rsp.eoh; |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 5572 | t->do_log(t); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5573 | t->logs.bytes_out = 0; |
| 5574 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5575 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5576 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 5577 | * otherwise we would not let the client side wake up. |
| 5578 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5579 | |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5580 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5581 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5582 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5583 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5584 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5585 | /* This function is an analyser which forwards response body (including chunk |
| 5586 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5587 | * zero byte. The only situation where it must not be called is when we're in |
| 5588 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5589 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5590 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 5591 | * 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] | 5592 | * 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] | 5593 | * bytes of pending data + the headers if not already done (between sol and sov). |
| 5594 | * 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] | 5595 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 5596 | 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] | 5597 | { |
| 5598 | struct http_txn *txn = &s->txn; |
| 5599 | struct http_msg *msg = &s->txn.rsp; |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 5600 | unsigned int bytes; |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5601 | static struct buffer *tmpbuf = NULL; |
| 5602 | int compressing = 0; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 5603 | int consumed_data = 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5604 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5605 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5606 | return 0; |
| 5607 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5608 | 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] | 5609 | ((res->flags & CF_SHUTW) && (res->to_forward || res->buf->o)) || |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 5610 | !s->req->analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5611 | /* Output closed while we were sending data. We must abort and |
| 5612 | * wake the other side up. |
| 5613 | */ |
| 5614 | msg->msg_state = HTTP_MSG_ERROR; |
| 5615 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5616 | return 1; |
| 5617 | } |
| 5618 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5619 | /* in most states, we should abort in case of early close */ |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5620 | channel_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5621 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5622 | /* this is the first time we need the compression buffer */ |
| 5623 | if (s->comp_algo != NULL && tmpbuf == NULL) { |
| 5624 | if ((tmpbuf = pool_alloc2(pool2_buffer)) == NULL) |
| 5625 | goto aborted_xfer; /* no memory */ |
| 5626 | } |
| 5627 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5628 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | fa4a03c | 2012-03-09 21:28:54 +0100 | [diff] [blame] | 5629 | /* 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] | 5630 | * rep->buf.p still points to the beginning of the message and msg->sol |
| 5631 | * is still null. We forward the headers, we don't need them. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5632 | */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5633 | channel_forward(res, msg->sov); |
| 5634 | msg->next = 0; |
| 5635 | msg->sov = 0; |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5636 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 5637 | if (msg->flags & HTTP_MSGF_TE_CHNK) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5638 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5639 | else |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5640 | msg->msg_state = HTTP_MSG_DATA; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5641 | } |
| 5642 | |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5643 | if (s->comp_algo != NULL) { |
| 5644 | int ret = http_compression_buffer_init(s, res->buf, tmpbuf); /* init a buffer with headers */ |
| 5645 | if (ret < 0) |
| 5646 | goto missing_data; /* not enough spaces in buffers */ |
| 5647 | compressing = 1; |
| 5648 | } |
| 5649 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5650 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5651 | http_silent_debug(__LINE__, s); |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 5652 | /* we may have some data pending between sol and sov */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5653 | if (s->comp_algo == NULL) { |
| 5654 | bytes = msg->sov - msg->sol; |
| 5655 | if (msg->chunk_len || bytes) { |
| 5656 | msg->sol = msg->sov; |
| 5657 | msg->next -= bytes; /* will be forwarded */ |
| 5658 | msg->chunk_len += bytes; |
| 5659 | msg->chunk_len -= channel_forward(res, msg->chunk_len); |
| 5660 | } |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 5661 | } |
| 5662 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5663 | if (msg->msg_state == HTTP_MSG_DATA) { |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 5664 | int ret; |
| 5665 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5666 | /* must still forward */ |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 5667 | if (compressing) { |
| 5668 | consumed_data += ret = http_compression_buffer_add_data(s, res->buf, tmpbuf); |
| 5669 | if (ret < 0) |
| 5670 | goto aborted_xfer; |
| 5671 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5672 | |
| 5673 | if (res->to_forward || msg->chunk_len) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5674 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5675 | |
| 5676 | /* nothing left to forward */ |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 5677 | if (msg->flags & HTTP_MSGF_TE_CHNK) { |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5678 | msg->msg_state = HTTP_MSG_CHUNK_CRLF; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 5679 | } else { |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5680 | msg->msg_state = HTTP_MSG_DONE; |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 5681 | if (compressing && consumed_data) { |
| 5682 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 5683 | compressing = 0; |
| 5684 | } |
| 5685 | } |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5686 | } |
| 5687 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5688 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | a458b67 | 2012-03-05 11:17:50 +0100 | [diff] [blame] | 5689 | * set ->sov and ->next to point to the body and switch to DATA or |
| 5690 | * TRAILERS state. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5691 | */ |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5692 | int ret = http_parse_chunk_size(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5693 | |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5694 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5695 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5696 | else if (ret < 0) { |
| 5697 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5698 | 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] | 5699 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5700 | } |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 5701 | if (compressing) { |
| 5702 | if (likely(msg->chunk_len > 0)) { |
| 5703 | /* skipping data if we are in compression mode */ |
| 5704 | b_adv(res->buf, msg->next); |
| 5705 | msg->next = 0; |
| 5706 | msg->sov = 0; |
| 5707 | msg->sol = 0; |
| 5708 | } else { |
| 5709 | if (consumed_data) { |
| 5710 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 1); |
| 5711 | compressing = 0; |
| 5712 | } |
| 5713 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5714 | } |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 5715 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5716 | } |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5717 | else if (msg->msg_state == HTTP_MSG_CHUNK_CRLF) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5718 | /* we want the CRLF after the data */ |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5719 | int ret = http_skip_chunk_crlf(msg); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5720 | |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5721 | if (ret == 0) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5722 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5723 | else if (ret < 0) { |
| 5724 | if (msg->err_pos >= 0) |
Willy Tarreau | 54d23df | 2012-10-25 19:04:45 +0200 | [diff] [blame] | 5725 | 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] | 5726 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5727 | } |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5728 | /* skipping data in buffer for compression */ |
| 5729 | if (compressing) { |
| 5730 | b_adv(res->buf, msg->next); |
| 5731 | msg->next = 0; |
| 5732 | msg->sov = 0; |
| 5733 | msg->sol = 0; |
| 5734 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5735 | /* we're in MSG_CHUNK_SIZE now */ |
| 5736 | } |
| 5737 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
Willy Tarreau | 4baf44b | 2012-03-09 14:10:20 +0100 | [diff] [blame] | 5738 | int ret = http_forward_trailers(msg); |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5739 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5740 | if (ret == 0) |
| 5741 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5742 | else if (ret < 0) { |
| 5743 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5744 | 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] | 5745 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5746 | } |
William Lallemand | 00bf1de | 2012-11-22 17:55:14 +0100 | [diff] [blame] | 5747 | if (s->comp_algo != NULL) { |
| 5748 | /* forwarding trailers */ |
| 5749 | channel_forward(res, msg->next); |
| 5750 | msg->next = 0; |
| 5751 | } |
| 5752 | /* we're in HTTP_MSG_DONE now */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5753 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5754 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5755 | int old_state = msg->msg_state; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5756 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5757 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5758 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5759 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5760 | channel_dont_close(res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5761 | if (http_resync_states(s)) { |
| 5762 | http_silent_debug(__LINE__, s); |
| 5763 | /* some state changes occurred, maybe the analyser |
| 5764 | * was disabled too. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5765 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5766 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5767 | if (res->flags & CF_SHUTW) { |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5768 | /* response errors are most likely due to |
| 5769 | * the client aborting the transfer. |
| 5770 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5771 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5772 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5773 | if (msg->err_pos >= 0) |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 5774 | 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] | 5775 | goto return_bad_res; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5776 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5777 | return 1; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5778 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5779 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5780 | } |
| 5781 | } |
| 5782 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5783 | missing_data: |
William Lallemand | bf3ae61 | 2012-11-19 12:35:37 +0100 | [diff] [blame] | 5784 | if (compressing && consumed_data) { |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5785 | http_compression_buffer_end(s, &res->buf, &tmpbuf, 0); |
| 5786 | compressing = 0; |
| 5787 | } |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5788 | |
| 5789 | if (res->flags & CF_SHUTW) |
| 5790 | goto aborted_xfer; |
| 5791 | |
| 5792 | /* stop waiting for data if the input is closed before the end. If the |
| 5793 | * client side was already closed, it means that the client has aborted, |
| 5794 | * so we don't want to count this as a server abort. Otherwise it's a |
| 5795 | * server abort. |
| 5796 | */ |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5797 | if (res->flags & CF_SHUTR) { |
Willy Tarreau | f003d37 | 2012-11-26 13:35:37 +0100 | [diff] [blame] | 5798 | if ((res->flags & CF_SHUTW_NOW) || (s->req->flags & CF_SHUTR)) |
| 5799 | goto aborted_xfer; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5800 | if (!(s->flags & SN_ERR_MASK)) |
| 5801 | s->flags |= SN_ERR_SRVCL; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5802 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5803 | if (objt_server(s->target)) |
| 5804 | objt_server(s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5805 | goto return_bad_res_stats_ok; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5806 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5807 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5808 | /* 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] | 5809 | if (!s->req->analysers) |
| 5810 | goto return_bad_res; |
| 5811 | |
Willy Tarreau | ea95316 | 2012-05-18 23:41:28 +0200 | [diff] [blame] | 5812 | /* forward any data pending between sol and sov */ |
William Lallemand | 82fe75c | 2012-10-23 10:25:10 +0200 | [diff] [blame] | 5813 | if (s->comp_algo == NULL) { |
| 5814 | bytes = msg->sov - msg->sol; |
| 5815 | if (msg->chunk_len || bytes) { |
| 5816 | msg->sol = msg->sov; |
| 5817 | msg->next -= bytes; /* will be forwarded */ |
| 5818 | msg->chunk_len += bytes; |
| 5819 | msg->chunk_len -= channel_forward(res, msg->chunk_len); |
| 5820 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5821 | } |
| 5822 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5823 | /* 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] | 5824 | * 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] | 5825 | * Similarly, with keep-alive on the client side, we don't want to forward a |
| 5826 | * close. |
| 5827 | */ |
Willy Tarreau | 08b4d79 | 2012-10-27 01:36:34 +0200 | [diff] [blame] | 5828 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo || |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5829 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5830 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
Willy Tarreau | 8263d2b | 2012-08-28 00:06:31 +0200 | [diff] [blame] | 5831 | channel_dont_close(res); |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5832 | |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5833 | /* 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] | 5834 | * 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] | 5835 | * 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] | 5836 | * modes are already handled by the stream sock layer. We must not do |
| 5837 | * this in content-length mode because it could present the MSG_MORE |
| 5838 | * flag with the last block of forwarded data, which would cause an |
| 5839 | * additional delay to be observed by the receiver. |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5840 | */ |
Willy Tarreau | 08b4d79 | 2012-10-27 01:36:34 +0200 | [diff] [blame] | 5841 | if ((msg->flags & HTTP_MSGF_TE_CHNK) || s->comp_algo) |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 5842 | res->flags |= CF_EXPECT_MORE; |
Willy Tarreau | 5c62092 | 2011-05-11 19:56:11 +0200 | [diff] [blame] | 5843 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5844 | /* the session handler will take care of timeouts and errors */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5845 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5846 | return 0; |
| 5847 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5848 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5849 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5850 | if (objt_server(s->target)) |
| 5851 | objt_server(s->target)->counters.failed_resp++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5852 | |
| 5853 | return_bad_res_stats_ok: |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5854 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 5855 | /* don't send any error message as we're in the body */ |
| 5856 | stream_int_retnclose(res->cons, NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5857 | res->analysers = 0; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5858 | 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] | 5859 | if (objt_server(s->target)) |
| 5860 | health_adjust(objt_server(s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5861 | |
| 5862 | if (!(s->flags & SN_ERR_MASK)) |
| 5863 | s->flags |= SN_ERR_PRXCOND; |
| 5864 | if (!(s->flags & SN_FINST_MASK)) |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5865 | s->flags |= SN_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5866 | return 0; |
| 5867 | |
| 5868 | aborted_xfer: |
| 5869 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 5870 | /* don't send any error message as we're in the body */ |
| 5871 | stream_int_retnclose(res->cons, NULL); |
| 5872 | res->analysers = 0; |
| 5873 | s->req->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 5874 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5875 | s->fe->fe_counters.cli_aborts++; |
| 5876 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5877 | if (objt_server(s->target)) |
| 5878 | objt_server(s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5879 | |
| 5880 | if (!(s->flags & SN_ERR_MASK)) |
| 5881 | s->flags |= SN_ERR_CLICL; |
| 5882 | if (!(s->flags & SN_FINST_MASK)) |
| 5883 | s->flags |= SN_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5884 | return 0; |
| 5885 | } |
| 5886 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5887 | /* Iterate the same filter through all request headers. |
| 5888 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5889 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5890 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5891 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 5892 | 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] | 5893 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5894 | char term; |
| 5895 | char *cur_ptr, *cur_end, *cur_next; |
| 5896 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5897 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5898 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5899 | int delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 5900 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5901 | last_hdr = 0; |
| 5902 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5903 | cur_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5904 | old_idx = 0; |
| 5905 | |
| 5906 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5907 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5908 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5909 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5910 | (exp->action == ACT_ALLOW || |
| 5911 | exp->action == ACT_DENY || |
| 5912 | exp->action == ACT_TARPIT)) |
| 5913 | return 0; |
| 5914 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5915 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5916 | if (!cur_idx) |
| 5917 | break; |
| 5918 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5919 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5920 | cur_ptr = cur_next; |
| 5921 | cur_end = cur_ptr + cur_hdr->len; |
| 5922 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5923 | |
| 5924 | /* Now we have one header between cur_ptr and cur_end, |
| 5925 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5926 | */ |
| 5927 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5928 | /* The annoying part is that pattern matching needs |
| 5929 | * that we modify the contents to null-terminate all |
| 5930 | * strings before testing them. |
| 5931 | */ |
| 5932 | |
| 5933 | term = *cur_end; |
| 5934 | *cur_end = '\0'; |
| 5935 | |
| 5936 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5937 | switch (exp->action) { |
| 5938 | case ACT_SETBE: |
| 5939 | /* It is not possible to jump a second time. |
| 5940 | * FIXME: should we return an HTTP/500 here so that |
| 5941 | * the admin knows there's a problem ? |
| 5942 | */ |
| 5943 | if (t->be != t->fe) |
| 5944 | break; |
| 5945 | |
| 5946 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 5947 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5948 | last_hdr = 1; |
| 5949 | break; |
| 5950 | |
| 5951 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5952 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5953 | last_hdr = 1; |
| 5954 | break; |
| 5955 | |
| 5956 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5957 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5958 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5959 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5960 | t->fe->fe_counters.denied_req++; |
| 5961 | if (t->fe != t->be) |
| 5962 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5963 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5964 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5965 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5966 | break; |
| 5967 | |
| 5968 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5969 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5970 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5971 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5972 | t->fe->fe_counters.denied_req++; |
| 5973 | if (t->fe != t->be) |
| 5974 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5975 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5976 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5977 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5978 | break; |
| 5979 | |
| 5980 | case ACT_REPLACE: |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 5981 | trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch); |
| 5982 | 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] | 5983 | /* FIXME: if the user adds a newline in the replacement, the |
| 5984 | * index will not be recalculated for now, and the new line |
| 5985 | * will not be counted as a new header. |
| 5986 | */ |
| 5987 | |
| 5988 | cur_end += delta; |
| 5989 | cur_next += delta; |
| 5990 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5991 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5992 | break; |
| 5993 | |
| 5994 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 5995 | delta = buffer_replace2(req->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5996 | cur_next += delta; |
| 5997 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5998 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5999 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6000 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6001 | cur_hdr->len = 0; |
| 6002 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6003 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6004 | break; |
| 6005 | |
| 6006 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6007 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6008 | if (cur_end) |
| 6009 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6010 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6011 | /* keep the link from this header to next one in case of later |
| 6012 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6013 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6014 | old_idx = cur_idx; |
| 6015 | } |
| 6016 | return 0; |
| 6017 | } |
| 6018 | |
| 6019 | |
| 6020 | /* Apply the filter to the request line. |
| 6021 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 6022 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6023 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 6024 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6025 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6026 | 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] | 6027 | { |
| 6028 | char term; |
| 6029 | char *cur_ptr, *cur_end; |
| 6030 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6031 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6032 | int delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6033 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6034 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6035 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6036 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6037 | (exp->action == ACT_ALLOW || |
| 6038 | exp->action == ACT_DENY || |
| 6039 | exp->action == ACT_TARPIT)) |
| 6040 | return 0; |
| 6041 | else if (exp->action == ACT_REMOVE) |
| 6042 | return 0; |
| 6043 | |
| 6044 | done = 0; |
| 6045 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6046 | cur_ptr = req->buf->p; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6047 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6048 | |
| 6049 | /* Now we have the request line between cur_ptr and cur_end */ |
| 6050 | |
| 6051 | /* The annoying part is that pattern matching needs |
| 6052 | * that we modify the contents to null-terminate all |
| 6053 | * strings before testing them. |
| 6054 | */ |
| 6055 | |
| 6056 | term = *cur_end; |
| 6057 | *cur_end = '\0'; |
| 6058 | |
| 6059 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6060 | switch (exp->action) { |
| 6061 | case ACT_SETBE: |
| 6062 | /* It is not possible to jump a second time. |
| 6063 | * FIXME: should we return an HTTP/500 here so that |
| 6064 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6065 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6066 | if (t->be != t->fe) |
| 6067 | break; |
| 6068 | |
| 6069 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 6070 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6071 | done = 1; |
| 6072 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6073 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6074 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6075 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6076 | done = 1; |
| 6077 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6078 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6079 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6080 | txn->flags |= TX_CLDENY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6081 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6082 | t->fe->fe_counters.denied_req++; |
| 6083 | if (t->fe != t->be) |
| 6084 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6085 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 6086 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6087 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6088 | done = 1; |
| 6089 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6090 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6091 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6092 | txn->flags |= TX_CLTARPIT; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6093 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 6094 | t->fe->fe_counters.denied_req++; |
| 6095 | if (t->fe != t->be) |
| 6096 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6097 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 6098 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 6099 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6100 | done = 1; |
| 6101 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6102 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6103 | case ACT_REPLACE: |
| 6104 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6105 | trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch); |
| 6106 | 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] | 6107 | /* FIXME: if the user adds a newline in the replacement, the |
| 6108 | * index will not be recalculated for now, and the new line |
| 6109 | * will not be counted as a new header. |
| 6110 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6111 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6112 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6113 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 6114 | cur_end = (char *)http_parse_reqline(&txn->req, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6115 | HTTP_MSG_RQMETH, |
| 6116 | cur_ptr, cur_end + 1, |
| 6117 | NULL, NULL); |
| 6118 | if (unlikely(!cur_end)) |
| 6119 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 6120 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6121 | /* we have a full request and we know that we have either a CR |
| 6122 | * or an LF at <ptr>. |
| 6123 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6124 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 6125 | 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] | 6126 | /* there is no point trying this regex on headers */ |
| 6127 | return 1; |
| 6128 | } |
| 6129 | } |
| 6130 | *cur_end = term; /* restore the string terminator */ |
| 6131 | return done; |
| 6132 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 6133 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6134 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6135 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6136 | /* |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6137 | * 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] | 6138 | * 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] | 6139 | * unparsable request. Since it can manage the switch to another backend, it |
| 6140 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6141 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6142 | 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] | 6143 | { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6144 | struct http_txn *txn = &s->txn; |
| 6145 | struct hdr_exp *exp; |
| 6146 | |
| 6147 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6148 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6149 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6150 | /* |
| 6151 | * The interleaving of transformations and verdicts |
| 6152 | * makes it difficult to decide to continue or stop |
| 6153 | * the evaluation. |
| 6154 | */ |
| 6155 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6156 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 6157 | break; |
| 6158 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6159 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6160 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6161 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6162 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6163 | |
| 6164 | /* if this filter had a condition, evaluate it now and skip to |
| 6165 | * next filter if the condition does not match. |
| 6166 | */ |
| 6167 | if (exp->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 6168 | 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] | 6169 | ret = acl_pass(ret); |
| 6170 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 6171 | ret = !ret; |
| 6172 | |
| 6173 | if (!ret) |
| 6174 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6175 | } |
| 6176 | |
| 6177 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6178 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6179 | if (unlikely(ret < 0)) |
| 6180 | return -1; |
| 6181 | |
| 6182 | if (likely(ret == 0)) { |
| 6183 | /* The filter did not match the request, it can be |
| 6184 | * iterated through all headers. |
| 6185 | */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 6186 | apply_filter_to_req_headers(s, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6187 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6188 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6189 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6190 | } |
| 6191 | |
| 6192 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6193 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6194 | /* |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6195 | * Try to retrieve the server associated to the appsession. |
| 6196 | * If the server is found, it's assigned to the session. |
| 6197 | */ |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6198 | 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] | 6199 | struct http_txn *txn = &t->txn; |
| 6200 | appsess *asession = NULL; |
| 6201 | char *sessid_temp = NULL; |
| 6202 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6203 | if (len > t->be->appsession_len) { |
| 6204 | len = t->be->appsession_len; |
| 6205 | } |
| 6206 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6207 | if (t->be->options2 & PR_O2_AS_REQL) { |
| 6208 | /* 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] | 6209 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6210 | /* 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] | 6211 | pool_free2(apools.sessid, txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6212 | } |
| 6213 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6214 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6215 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 6216 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 6217 | return; |
| 6218 | } |
| 6219 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6220 | memcpy(txn->sessid, buf, len); |
| 6221 | txn->sessid[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6222 | } |
| 6223 | |
| 6224 | if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) { |
| 6225 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 6226 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 6227 | return; |
| 6228 | } |
| 6229 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6230 | memcpy(sessid_temp, buf, len); |
| 6231 | sessid_temp[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6232 | |
| 6233 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp); |
| 6234 | /* free previously allocated memory */ |
| 6235 | pool_free2(apools.sessid, sessid_temp); |
| 6236 | |
| 6237 | if (asession != NULL) { |
| 6238 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 6239 | if (!(t->be->options2 & PR_O2_AS_REQL)) |
| 6240 | asession->request_count++; |
| 6241 | |
| 6242 | if (asession->serverid != NULL) { |
| 6243 | struct server *srv = t->be->srv; |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 6244 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6245 | while (srv) { |
| 6246 | if (strcmp(srv->id, asession->serverid) == 0) { |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 6247 | if ((srv->state & SRV_RUNNING) || |
| 6248 | (t->be->options & PR_O_PERSIST) || |
| 6249 | (t->flags & SN_FORCE_PRST)) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6250 | /* we found the server and it's usable */ |
| 6251 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 2a6d88d | 2010-01-24 13:10:43 +0100 | [diff] [blame] | 6252 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6253 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6254 | t->target = &srv->obj_type; |
Willy Tarreau | 664beb8 | 2011-03-10 11:38:29 +0100 | [diff] [blame] | 6255 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6256 | break; |
| 6257 | } else { |
| 6258 | txn->flags &= ~TX_CK_MASK; |
| 6259 | txn->flags |= TX_CK_DOWN; |
| 6260 | } |
| 6261 | } |
| 6262 | srv = srv->next; |
| 6263 | } |
| 6264 | } |
| 6265 | } |
| 6266 | } |
| 6267 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6268 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 6269 | * same way as with headers above except that the semi-colon also ends a token. |
| 6270 | * See RFC2965 for more information. Note that it requires a valid header to |
| 6271 | * return a valid result. |
| 6272 | */ |
| 6273 | char *find_cookie_value_end(char *s, const char *e) |
| 6274 | { |
| 6275 | int quoted, qdpair; |
| 6276 | |
| 6277 | quoted = qdpair = 0; |
| 6278 | for (; s < e; s++) { |
| 6279 | if (qdpair) qdpair = 0; |
| 6280 | else if (quoted) { |
| 6281 | if (*s == '\\') qdpair = 1; |
| 6282 | else if (*s == '"') quoted = 0; |
| 6283 | } |
| 6284 | else if (*s == '"') quoted = 1; |
| 6285 | else if (*s == ',' || *s == ';') return s; |
| 6286 | } |
| 6287 | return s; |
| 6288 | } |
| 6289 | |
| 6290 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 6291 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 6292 | * the first delimiter is updated if required. The function tries as much as |
| 6293 | * possible to respect the following principles : |
| 6294 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 6295 | * colon, in which case <next> is simply removed |
| 6296 | * - set exactly one space character after the new first delimiter, unless |
| 6297 | * there are not enough characters in the block being moved to do so. |
| 6298 | * - remove unneeded spaces before the previous delimiter and after the new |
| 6299 | * one. |
| 6300 | * |
| 6301 | * It is the caller's responsibility to ensure that : |
| 6302 | * - <from> points to a valid delimiter or the colon ; |
| 6303 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 6304 | * - there are non-space chars before <from> ; |
| 6305 | * - there is a CR/LF at or after <next>. |
| 6306 | */ |
Willy Tarreau | af81935 | 2012-08-27 22:08:00 +0200 | [diff] [blame] | 6307 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6308 | { |
| 6309 | char *prev = *from; |
| 6310 | |
| 6311 | if (*prev == ':') { |
| 6312 | /* We're removing the first value, preserve the colon and add a |
| 6313 | * space if possible. |
| 6314 | */ |
| 6315 | if (!http_is_crlf[(unsigned char)*next]) |
| 6316 | next++; |
| 6317 | prev++; |
| 6318 | if (prev < next) |
| 6319 | *prev++ = ' '; |
| 6320 | |
| 6321 | while (http_is_spht[(unsigned char)*next]) |
| 6322 | next++; |
| 6323 | } else { |
| 6324 | /* Remove useless spaces before the old delimiter. */ |
| 6325 | while (http_is_spht[(unsigned char)*(prev-1)]) |
| 6326 | prev--; |
| 6327 | *from = prev; |
| 6328 | |
| 6329 | /* copy the delimiter and if possible a space if we're |
| 6330 | * not at the end of the line. |
| 6331 | */ |
| 6332 | if (!http_is_crlf[(unsigned char)*next]) { |
| 6333 | *prev++ = *next++; |
| 6334 | if (prev + 1 < next) |
| 6335 | *prev++ = ' '; |
| 6336 | while (http_is_spht[(unsigned char)*next]) |
| 6337 | next++; |
| 6338 | } |
| 6339 | } |
| 6340 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 6341 | } |
| 6342 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6343 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 6344 | * 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] | 6345 | * desirable to call it only when needed. This code is quite complex because |
| 6346 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 6347 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6348 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6349 | void manage_client_side_cookies(struct session *t, struct channel *req) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6350 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6351 | struct http_txn *txn = &t->txn; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6352 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6353 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6354 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 6355 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6356 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6357 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 6358 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6359 | hdr_next = req->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6360 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6361 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6362 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6363 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6364 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6365 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6366 | hdr_beg = hdr_next; |
| 6367 | hdr_end = hdr_beg + cur_hdr->len; |
| 6368 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6369 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6370 | /* We have one full header between hdr_beg and hdr_end, and the |
| 6371 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6372 | * "Cookie:" headers. |
| 6373 | */ |
| 6374 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6375 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6376 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6377 | old_idx = cur_idx; |
| 6378 | continue; |
| 6379 | } |
| 6380 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6381 | del_from = NULL; /* nothing to be deleted */ |
| 6382 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 6383 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6384 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 6385 | * attributes whose name begin with a '$', and associate them with |
| 6386 | * the right cookie, if we want to delete this cookie. |
| 6387 | * So there are 3 cases for each cookie read : |
| 6388 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 6389 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 6390 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 6391 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 6392 | * "special" cookie. |
| 6393 | * At the end of loop, if a "special" cookie remains, we may have to |
| 6394 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6395 | * *MUST* delete it. |
| 6396 | * |
| 6397 | * Note: RFC2965 is unclear about the processing of spaces around |
| 6398 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 6399 | * the RFC explicitly allows spaces before it, and not within the |
| 6400 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 6401 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 6402 | * after the equal sign too, resulting in some (rare) buggy |
| 6403 | * implementations trying to do that. So let's do what servers do. |
| 6404 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 6405 | * allowed quoted strings in values, with any possible character |
| 6406 | * after a backslash, including control chars and delimitors, which |
| 6407 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 6408 | * within values even without quotes. |
| 6409 | * |
| 6410 | * We have to keep multiple pointers in order to support cookie |
| 6411 | * removal at the beginning, middle or end of header without |
| 6412 | * corrupting the header. All of these headers are valid : |
| 6413 | * |
| 6414 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 6415 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 6416 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 6417 | * | | | | | | | | | |
| 6418 | * | | | | | | | | hdr_end <--+ |
| 6419 | * | | | | | | | +--> next |
| 6420 | * | | | | | | +----> val_end |
| 6421 | * | | | | | +-----------> val_beg |
| 6422 | * | | | | +--------------> equal |
| 6423 | * | | | +----------------> att_end |
| 6424 | * | | +---------------------> att_beg |
| 6425 | * | +--------------------------> prev |
| 6426 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6427 | */ |
| 6428 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6429 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 6430 | /* Iterate through all cookies on this line */ |
| 6431 | |
| 6432 | /* find att_beg */ |
| 6433 | att_beg = prev + 1; |
| 6434 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 6435 | att_beg++; |
| 6436 | |
| 6437 | /* find att_end : this is the first character after the last non |
| 6438 | * space before the equal. It may be equal to hdr_end. |
| 6439 | */ |
| 6440 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6441 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6442 | while (equal < hdr_end) { |
| 6443 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6444 | break; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6445 | if (http_is_spht[(unsigned char)*equal++]) |
| 6446 | continue; |
| 6447 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6448 | } |
| 6449 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6450 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6451 | * is between <att_beg> and <equal>, both may be identical. |
| 6452 | */ |
| 6453 | |
| 6454 | /* look for end of cookie if there is an equal sign */ |
| 6455 | if (equal < hdr_end && *equal == '=') { |
| 6456 | /* look for the beginning of the value */ |
| 6457 | val_beg = equal + 1; |
| 6458 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 6459 | val_beg++; |
| 6460 | |
| 6461 | /* find the end of the value, respecting quotes */ |
| 6462 | next = find_cookie_value_end(val_beg, hdr_end); |
| 6463 | |
| 6464 | /* make val_end point to the first white space or delimitor after the value */ |
| 6465 | val_end = next; |
| 6466 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 6467 | val_end--; |
| 6468 | } else { |
| 6469 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6470 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6471 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6472 | /* We have nothing to do with attributes beginning with '$'. However, |
| 6473 | * they will automatically be removed if a header before them is removed, |
| 6474 | * since they're supposed to be linked together. |
| 6475 | */ |
| 6476 | if (*att_beg == '$') |
| 6477 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6478 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6479 | /* Ignore cookies with no equal sign */ |
| 6480 | if (equal == next) { |
| 6481 | /* This is not our cookie, so we must preserve it. But if we already |
| 6482 | * scheduled another cookie for removal, we cannot remove the |
| 6483 | * complete header, but we can remove the previous block itself. |
| 6484 | */ |
| 6485 | preserve_hdr = 1; |
| 6486 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6487 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6488 | val_end += delta; |
| 6489 | next += delta; |
| 6490 | hdr_end += delta; |
| 6491 | hdr_next += delta; |
| 6492 | cur_hdr->len += delta; |
| 6493 | http_msg_move_end(&txn->req, delta); |
| 6494 | prev = del_from; |
| 6495 | del_from = NULL; |
| 6496 | } |
| 6497 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6498 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6499 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6500 | /* if there are spaces around the equal sign, we need to |
| 6501 | * strip them otherwise we'll get trouble for cookie captures, |
| 6502 | * or even for rewrites. Since this happens extremely rarely, |
| 6503 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6504 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6505 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 6506 | int stripped_before = 0; |
| 6507 | int stripped_after = 0; |
| 6508 | |
| 6509 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6510 | stripped_before = buffer_replace2(req->buf, att_end, equal, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6511 | equal += stripped_before; |
| 6512 | val_beg += stripped_before; |
| 6513 | } |
| 6514 | |
| 6515 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6516 | stripped_after = buffer_replace2(req->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6517 | val_beg += stripped_after; |
| 6518 | stripped_before += stripped_after; |
| 6519 | } |
| 6520 | |
| 6521 | val_end += stripped_before; |
| 6522 | next += stripped_before; |
| 6523 | hdr_end += stripped_before; |
| 6524 | hdr_next += stripped_before; |
| 6525 | cur_hdr->len += stripped_before; |
| 6526 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6527 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6528 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6529 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6530 | /* First, let's see if we want to capture this cookie. We check |
| 6531 | * that we don't already have a client side cookie, because we |
| 6532 | * can only capture one. Also as an optimisation, we ignore |
| 6533 | * cookies shorter than the declared name. |
| 6534 | */ |
| 6535 | if (t->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 6536 | (val_end - att_beg >= t->fe->capture_namelen) && |
| 6537 | memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
| 6538 | int log_len = val_end - att_beg; |
| 6539 | |
| 6540 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
| 6541 | Alert("HTTP logging : out of memory.\n"); |
| 6542 | } else { |
| 6543 | if (log_len > t->fe->capture_len) |
| 6544 | log_len = t->fe->capture_len; |
| 6545 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 6546 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6547 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6548 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6549 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6550 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 6551 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6552 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6553 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6554 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6555 | * For cookies in prefix mode, the form is : |
| 6556 | * |
| 6557 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6558 | */ |
| 6559 | if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 6560 | (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) { |
| 6561 | struct server *srv = t->be->srv; |
| 6562 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6563 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6564 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 6565 | * have the server ID between val_beg and delim, and the original cookie between |
| 6566 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 6567 | * |
| 6568 | * Cookie: NAME=SRV; # in all but prefix modes |
| 6569 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 6570 | * | || || | |+-> next |
| 6571 | * | || || | +--> val_end |
| 6572 | * | || || +---------> delim |
| 6573 | * | || |+------------> val_beg |
| 6574 | * | || +-------------> att_end = equal |
| 6575 | * | |+-----------------> att_beg |
| 6576 | * | +------------------> prev |
| 6577 | * +-------------------------> hdr_beg |
| 6578 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6579 | |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 6580 | if (t->be->ck_opts & PR_CK_PFX) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6581 | for (delim = val_beg; delim < val_end; delim++) |
| 6582 | if (*delim == COOKIE_DELIM) |
| 6583 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6584 | } else { |
| 6585 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6586 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6587 | /* Now check if the cookie contains a date field, which would |
| 6588 | * appear after a vertical bar ('|') just after the server name |
| 6589 | * and before the delimiter. |
| 6590 | */ |
| 6591 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 6592 | if (vbar1) { |
| 6593 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6594 | * right is the last seen date. It is a base64 encoded |
| 6595 | * 30-bit value representing the UNIX date since the |
| 6596 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6597 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6598 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6599 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6600 | if (val_end - vbar1 >= 5) { |
| 6601 | val = b64tos30(vbar1); |
| 6602 | if (val > 0) |
| 6603 | txn->cookie_last_date = val << 2; |
| 6604 | } |
| 6605 | /* look for a second vertical bar */ |
| 6606 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 6607 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 6608 | val = b64tos30(vbar1 + 1); |
| 6609 | if (val > 0) |
| 6610 | txn->cookie_first_date = val << 2; |
| 6611 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6612 | } |
| 6613 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6614 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6615 | /* if the cookie has an expiration date and the proxy wants to check |
| 6616 | * it, then we do that now. We first check if the cookie is too old, |
| 6617 | * then only if it has expired. We detect strict overflow because the |
| 6618 | * time resolution here is not great (4 seconds). Cookies with dates |
| 6619 | * in the future are ignored if their offset is beyond one day. This |
| 6620 | * allows an admin to fix timezone issues without expiring everyone |
| 6621 | * and at the same time avoids keeping unwanted side effects for too |
| 6622 | * long. |
| 6623 | */ |
| 6624 | if (txn->cookie_first_date && t->be->cookie_maxlife && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6625 | (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) || |
| 6626 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6627 | txn->flags &= ~TX_CK_MASK; |
| 6628 | txn->flags |= TX_CK_OLD; |
| 6629 | delim = val_beg; // let's pretend we have not found the cookie |
| 6630 | txn->cookie_first_date = 0; |
| 6631 | txn->cookie_last_date = 0; |
| 6632 | } |
| 6633 | else if (txn->cookie_last_date && t->be->cookie_maxidle && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6634 | (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) || |
| 6635 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6636 | txn->flags &= ~TX_CK_MASK; |
| 6637 | txn->flags |= TX_CK_EXPIRED; |
| 6638 | delim = val_beg; // let's pretend we have not found the cookie |
| 6639 | txn->cookie_first_date = 0; |
| 6640 | txn->cookie_last_date = 0; |
| 6641 | } |
| 6642 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6643 | /* Here, we'll look for the first running server which supports the cookie. |
| 6644 | * This allows to share a same cookie between several servers, for example |
| 6645 | * to dedicate backup servers to specific servers only. |
| 6646 | * However, to prevent clients from sticking to cookie-less backup server |
| 6647 | * when they have incidentely learned an empty cookie, we simply ignore |
| 6648 | * empty cookies and mark them as invalid. |
| 6649 | * The same behaviour is applied when persistence must be ignored. |
| 6650 | */ |
Willy Tarreau | 4a5cade | 2012-04-05 21:09:48 +0200 | [diff] [blame] | 6651 | if ((delim == val_beg) || (t->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6652 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6653 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6654 | while (srv) { |
| 6655 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 6656 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
| 6657 | if ((srv->state & SRV_RUNNING) || |
| 6658 | (t->be->options & PR_O_PERSIST) || |
| 6659 | (t->flags & SN_FORCE_PRST)) { |
| 6660 | /* we found the server and we can use it */ |
| 6661 | txn->flags &= ~TX_CK_MASK; |
| 6662 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
| 6663 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6664 | t->target = &srv->obj_type; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6665 | break; |
| 6666 | } else { |
| 6667 | /* we found a server, but it's down, |
| 6668 | * mark it as such and go on in case |
| 6669 | * another one is available. |
| 6670 | */ |
| 6671 | txn->flags &= ~TX_CK_MASK; |
| 6672 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6673 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6674 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6675 | srv = srv->next; |
| 6676 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6677 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6678 | 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] | 6679 | /* no server matched this cookie or we deliberately skipped it */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6680 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | c89ccb6 | 2012-04-05 21:18:22 +0200 | [diff] [blame] | 6681 | if ((t->flags & (SN_IGNORE_PRST | SN_ASSIGNED))) |
| 6682 | txn->flags |= TX_CK_UNUSED; |
| 6683 | else |
| 6684 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6685 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6686 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6687 | /* depending on the cookie mode, we may have to either : |
| 6688 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 6689 | * the server never sees it ; |
| 6690 | * - remove the server id from the cookie value, and tag the cookie as an |
| 6691 | * application cookie so that it does not get accidentely removed later, |
| 6692 | * if we're in cookie prefix mode |
| 6693 | */ |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 6694 | if ((t->be->ck_opts & PR_CK_PFX) && (delim != val_end)) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6695 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6696 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6697 | delta = buffer_replace2(req->buf, val_beg, delim + 1, NULL, 0); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6698 | val_end += delta; |
| 6699 | next += delta; |
| 6700 | hdr_end += delta; |
| 6701 | hdr_next += delta; |
| 6702 | cur_hdr->len += delta; |
| 6703 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6704 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6705 | del_from = NULL; |
| 6706 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 6707 | } |
| 6708 | else if (del_from == NULL && |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 6709 | (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] | 6710 | del_from = prev; |
| 6711 | } |
| 6712 | } else { |
| 6713 | /* This is not our cookie, so we must preserve it. But if we already |
| 6714 | * scheduled another cookie for removal, we cannot remove the |
| 6715 | * complete header, but we can remove the previous block itself. |
| 6716 | */ |
| 6717 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6718 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6719 | if (del_from != NULL) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6720 | int delta = del_hdr_value(req->buf, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 6721 | if (att_beg >= del_from) |
| 6722 | att_beg += delta; |
| 6723 | if (att_end >= del_from) |
| 6724 | att_end += delta; |
| 6725 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6726 | val_end += delta; |
| 6727 | next += delta; |
| 6728 | hdr_end += delta; |
| 6729 | hdr_next += delta; |
| 6730 | cur_hdr->len += delta; |
| 6731 | http_msg_move_end(&txn->req, delta); |
| 6732 | prev = del_from; |
| 6733 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6734 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6735 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6736 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6737 | /* Look for the appsession cookie unless persistence must be ignored */ |
| 6738 | if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) { |
| 6739 | int cmp_len, value_len; |
| 6740 | char *value_begin; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 6741 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6742 | if (t->be->options2 & PR_O2_AS_PFX) { |
| 6743 | cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len); |
| 6744 | value_begin = att_beg + t->be->appsession_name_len; |
| 6745 | value_len = val_end - att_beg - t->be->appsession_name_len; |
| 6746 | } else { |
| 6747 | cmp_len = att_end - att_beg; |
| 6748 | value_begin = val_beg; |
| 6749 | value_len = val_end - val_beg; |
| 6750 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6751 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6752 | /* let's see if the cookie is our appcookie */ |
| 6753 | if (cmp_len == t->be->appsession_name_len && |
| 6754 | memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) { |
| 6755 | manage_client_side_appsession(t, value_begin, value_len); |
| 6756 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6757 | } |
| 6758 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6759 | /* continue with next cookie on this header line */ |
| 6760 | att_beg = next; |
| 6761 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6762 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6763 | /* There are no more cookies on this line. |
| 6764 | * We may still have one (or several) marked for deletion at the |
| 6765 | * end of the line. We must do this now in two ways : |
| 6766 | * - if some cookies must be preserved, we only delete from the |
| 6767 | * mark to the end of line ; |
| 6768 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6769 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6770 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6771 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6772 | if (preserve_hdr) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6773 | delta = del_hdr_value(req->buf, &del_from, hdr_end); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6774 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6775 | cur_hdr->len += delta; |
| 6776 | } else { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6777 | delta = buffer_replace2(req->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6778 | |
| 6779 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6780 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6781 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6782 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6783 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6784 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6785 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6786 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6787 | } |
| 6788 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6789 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6790 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6791 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6792 | } |
| 6793 | |
| 6794 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6795 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 6796 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 6797 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6798 | 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] | 6799 | { |
| 6800 | char term; |
| 6801 | char *cur_ptr, *cur_end, *cur_next; |
| 6802 | int cur_idx, old_idx, last_hdr; |
| 6803 | struct http_txn *txn = &t->txn; |
| 6804 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6805 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6806 | |
| 6807 | last_hdr = 0; |
| 6808 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6809 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6810 | old_idx = 0; |
| 6811 | |
| 6812 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6813 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6814 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6815 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6816 | (exp->action == ACT_ALLOW || |
| 6817 | exp->action == ACT_DENY)) |
| 6818 | return 0; |
| 6819 | |
| 6820 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 6821 | if (!cur_idx) |
| 6822 | break; |
| 6823 | |
| 6824 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 6825 | cur_ptr = cur_next; |
| 6826 | cur_end = cur_ptr + cur_hdr->len; |
| 6827 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6828 | |
| 6829 | /* Now we have one header between cur_ptr and cur_end, |
| 6830 | * and the next header starts at cur_next. |
| 6831 | */ |
| 6832 | |
| 6833 | /* The annoying part is that pattern matching needs |
| 6834 | * that we modify the contents to null-terminate all |
| 6835 | * strings before testing them. |
| 6836 | */ |
| 6837 | |
| 6838 | term = *cur_end; |
| 6839 | *cur_end = '\0'; |
| 6840 | |
| 6841 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6842 | switch (exp->action) { |
| 6843 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6844 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6845 | last_hdr = 1; |
| 6846 | break; |
| 6847 | |
| 6848 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6849 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6850 | last_hdr = 1; |
| 6851 | break; |
| 6852 | |
| 6853 | case ACT_REPLACE: |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6854 | trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch); |
| 6855 | 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] | 6856 | /* FIXME: if the user adds a newline in the replacement, the |
| 6857 | * index will not be recalculated for now, and the new line |
| 6858 | * will not be counted as a new header. |
| 6859 | */ |
| 6860 | |
| 6861 | cur_end += delta; |
| 6862 | cur_next += delta; |
| 6863 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6864 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6865 | break; |
| 6866 | |
| 6867 | case ACT_REMOVE: |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6868 | delta = buffer_replace2(rtr->buf, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6869 | cur_next += delta; |
| 6870 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6871 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6872 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6873 | txn->hdr_idx.used--; |
| 6874 | cur_hdr->len = 0; |
| 6875 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6876 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6877 | break; |
| 6878 | |
| 6879 | } |
| 6880 | } |
| 6881 | if (cur_end) |
| 6882 | *cur_end = term; /* restore the string terminator */ |
| 6883 | |
| 6884 | /* keep the link from this header to next one in case of later |
| 6885 | * removal of next header. |
| 6886 | */ |
| 6887 | old_idx = cur_idx; |
| 6888 | } |
| 6889 | return 0; |
| 6890 | } |
| 6891 | |
| 6892 | |
| 6893 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 6894 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 6895 | * or -1 if a replacement resulted in an invalid status line. |
| 6896 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6897 | 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] | 6898 | { |
| 6899 | char term; |
| 6900 | char *cur_ptr, *cur_end; |
| 6901 | int done; |
| 6902 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6903 | int delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6904 | |
| 6905 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6906 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6907 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6908 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6909 | (exp->action == ACT_ALLOW || |
| 6910 | exp->action == ACT_DENY)) |
| 6911 | return 0; |
| 6912 | else if (exp->action == ACT_REMOVE) |
| 6913 | return 0; |
| 6914 | |
| 6915 | done = 0; |
| 6916 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6917 | cur_ptr = rtr->buf->p; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 6918 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6919 | |
| 6920 | /* Now we have the status line between cur_ptr and cur_end */ |
| 6921 | |
| 6922 | /* The annoying part is that pattern matching needs |
| 6923 | * that we modify the contents to null-terminate all |
| 6924 | * strings before testing them. |
| 6925 | */ |
| 6926 | |
| 6927 | term = *cur_end; |
| 6928 | *cur_end = '\0'; |
| 6929 | |
| 6930 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6931 | switch (exp->action) { |
| 6932 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6933 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6934 | done = 1; |
| 6935 | break; |
| 6936 | |
| 6937 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6938 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6939 | done = 1; |
| 6940 | break; |
| 6941 | |
| 6942 | case ACT_REPLACE: |
| 6943 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 6944 | trash.len = exp_replace(trash.str, cur_ptr, exp->replace, pmatch); |
| 6945 | 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] | 6946 | /* FIXME: if the user adds a newline in the replacement, the |
| 6947 | * index will not be recalculated for now, and the new line |
| 6948 | * will not be counted as a new header. |
| 6949 | */ |
| 6950 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6951 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6952 | cur_end += delta; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 6953 | cur_end = (char *)http_parse_stsline(&txn->rsp, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 6954 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6955 | cur_ptr, cur_end + 1, |
| 6956 | NULL, NULL); |
| 6957 | if (unlikely(!cur_end)) |
| 6958 | return -1; |
| 6959 | |
| 6960 | /* we have a full respnse and we know that we have either a CR |
| 6961 | * or an LF at <ptr>. |
| 6962 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 6963 | 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] | 6964 | 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] | 6965 | /* there is no point trying this regex on headers */ |
| 6966 | return 1; |
| 6967 | } |
| 6968 | } |
| 6969 | *cur_end = term; /* restore the string terminator */ |
| 6970 | return done; |
| 6971 | } |
| 6972 | |
| 6973 | |
| 6974 | |
| 6975 | /* |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6976 | * 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] | 6977 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 6978 | * unparsable response. |
| 6979 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 6980 | 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] | 6981 | { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6982 | struct http_txn *txn = &s->txn; |
| 6983 | struct hdr_exp *exp; |
| 6984 | |
| 6985 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6986 | int ret; |
| 6987 | |
| 6988 | /* |
| 6989 | * The interleaving of transformations and verdicts |
| 6990 | * makes it difficult to decide to continue or stop |
| 6991 | * the evaluation. |
| 6992 | */ |
| 6993 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6994 | if (txn->flags & TX_SVDENY) |
| 6995 | break; |
| 6996 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6997 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6998 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 6999 | exp->action == ACT_PASS)) { |
| 7000 | exp = exp->next; |
| 7001 | continue; |
| 7002 | } |
| 7003 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7004 | /* if this filter had a condition, evaluate it now and skip to |
| 7005 | * next filter if the condition does not match. |
| 7006 | */ |
| 7007 | if (exp->cond) { |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7008 | 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] | 7009 | ret = acl_pass(ret); |
| 7010 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 7011 | ret = !ret; |
| 7012 | if (!ret) |
| 7013 | continue; |
| 7014 | } |
| 7015 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7016 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7017 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7018 | if (unlikely(ret < 0)) |
| 7019 | return -1; |
| 7020 | |
| 7021 | if (likely(ret == 0)) { |
| 7022 | /* The filter did not match the response, it can be |
| 7023 | * iterated through all headers. |
| 7024 | */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 7025 | apply_filter_to_resp_headers(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7026 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7027 | } |
| 7028 | return 0; |
| 7029 | } |
| 7030 | |
| 7031 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7032 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 7033 | * 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] | 7034 | * desirable to call it only when needed. This function is also used when we |
| 7035 | * 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] | 7036 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7037 | void manage_server_side_cookies(struct session *t, struct channel *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7038 | { |
| 7039 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7040 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7041 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7042 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7043 | char *hdr_beg, *hdr_end, *hdr_next; |
| 7044 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7045 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7046 | /* Iterate through the headers. |
| 7047 | * we start with the start line. |
| 7048 | */ |
| 7049 | old_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7050 | hdr_next = res->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7051 | |
| 7052 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 7053 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7054 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7055 | |
| 7056 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7057 | hdr_beg = hdr_next; |
| 7058 | hdr_end = hdr_beg + cur_hdr->len; |
| 7059 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7060 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7061 | /* We have one full header between hdr_beg and hdr_end, and the |
| 7062 | * next header starts at hdr_next. We're only interested in |
| 7063 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7064 | */ |
| 7065 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7066 | is_cookie2 = 0; |
| 7067 | prev = hdr_beg + 10; |
| 7068 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7069 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7070 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 7071 | if (!val) { |
| 7072 | old_idx = cur_idx; |
| 7073 | continue; |
| 7074 | } |
| 7075 | is_cookie2 = 1; |
| 7076 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7077 | } |
| 7078 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7079 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 7080 | * <prev> points to the colon. |
| 7081 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7082 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7083 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7084 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 7085 | * check-cache is enabled) and we are not interested in checking |
| 7086 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 7087 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 7088 | if (t->be->cookie_name == NULL && |
| 7089 | t->be->appsession_name == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 7090 | t->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7091 | return; |
| 7092 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7093 | /* OK so now we know we have to process this response cookie. |
| 7094 | * The format of the Set-Cookie header is slightly different |
| 7095 | * from the format of the Cookie header in that it does not |
| 7096 | * support the comma as a cookie delimiter (thus the header |
| 7097 | * cannot be folded) because the Expires attribute described in |
| 7098 | * the original Netscape's spec may contain an unquoted date |
| 7099 | * with a comma inside. We have to live with this because |
| 7100 | * many browsers don't support Max-Age and some browsers don't |
| 7101 | * support quoted strings. However the Set-Cookie2 header is |
| 7102 | * clean. |
| 7103 | * |
| 7104 | * We have to keep multiple pointers in order to support cookie |
| 7105 | * removal at the beginning, middle or end of header without |
| 7106 | * corrupting the header (in case of set-cookie2). A special |
| 7107 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 7108 | * fields after the first semi-colon. The <next> pointer points |
| 7109 | * either to the end of line (set-cookie) or next unquoted comma |
| 7110 | * (set-cookie2). All of these headers are valid : |
| 7111 | * |
| 7112 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 7113 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 7114 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 7115 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 7116 | * | | | | | | | | | | |
| 7117 | * | | | | | | | | +-> next hdr_end <--+ |
| 7118 | * | | | | | | | +------------> scav |
| 7119 | * | | | | | | +--------------> val_end |
| 7120 | * | | | | | +--------------------> val_beg |
| 7121 | * | | | | +----------------------> equal |
| 7122 | * | | | +------------------------> att_end |
| 7123 | * | | +----------------------------> att_beg |
| 7124 | * | +------------------------------> prev |
| 7125 | * +-----------------------------------------> hdr_beg |
| 7126 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7127 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7128 | for (; prev < hdr_end; prev = next) { |
| 7129 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7130 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7131 | /* find att_beg */ |
| 7132 | att_beg = prev + 1; |
| 7133 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 7134 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7135 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7136 | /* find att_end : this is the first character after the last non |
| 7137 | * space before the equal. It may be equal to hdr_end. |
| 7138 | */ |
| 7139 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7140 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7141 | while (equal < hdr_end) { |
| 7142 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 7143 | break; |
| 7144 | if (http_is_spht[(unsigned char)*equal++]) |
| 7145 | continue; |
| 7146 | att_end = equal; |
| 7147 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7148 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7149 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 7150 | * is between <att_beg> and <equal>, both may be identical. |
| 7151 | */ |
| 7152 | |
| 7153 | /* look for end of cookie if there is an equal sign */ |
| 7154 | if (equal < hdr_end && *equal == '=') { |
| 7155 | /* look for the beginning of the value */ |
| 7156 | val_beg = equal + 1; |
| 7157 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 7158 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7159 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7160 | /* find the end of the value, respecting quotes */ |
| 7161 | next = find_cookie_value_end(val_beg, hdr_end); |
| 7162 | |
| 7163 | /* make val_end point to the first white space or delimitor after the value */ |
| 7164 | val_end = next; |
| 7165 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 7166 | val_end--; |
| 7167 | } else { |
| 7168 | /* <equal> points to next comma, semi-colon or EOL */ |
| 7169 | val_beg = val_end = next = equal; |
| 7170 | } |
| 7171 | |
| 7172 | if (next < hdr_end) { |
| 7173 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 7174 | * a colon or semi-colon before the end. So skip all attr-value |
| 7175 | * pairs and look for the next comma. For Set-Cookie, since |
| 7176 | * commas are permitted in values, skip to the end. |
| 7177 | */ |
| 7178 | if (is_cookie2) |
| 7179 | next = find_hdr_value_end(next, hdr_end); |
| 7180 | else |
| 7181 | next = hdr_end; |
| 7182 | } |
| 7183 | |
| 7184 | /* Now everything is as on the diagram above */ |
| 7185 | |
| 7186 | /* Ignore cookies with no equal sign */ |
| 7187 | if (equal == val_end) |
| 7188 | continue; |
| 7189 | |
| 7190 | /* If there are spaces around the equal sign, we need to |
| 7191 | * strip them otherwise we'll get trouble for cookie captures, |
| 7192 | * or even for rewrites. Since this happens extremely rarely, |
| 7193 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7194 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7195 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 7196 | int stripped_before = 0; |
| 7197 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7198 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7199 | if (att_end != equal) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7200 | stripped_before = buffer_replace2(res->buf, att_end, equal, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7201 | equal += stripped_before; |
| 7202 | val_beg += stripped_before; |
| 7203 | } |
| 7204 | |
| 7205 | if (val_beg > equal + 1) { |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7206 | stripped_after = buffer_replace2(res->buf, equal + 1, val_beg, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7207 | val_beg += stripped_after; |
| 7208 | stripped_before += stripped_after; |
| 7209 | } |
| 7210 | |
| 7211 | val_end += stripped_before; |
| 7212 | next += stripped_before; |
| 7213 | hdr_end += stripped_before; |
| 7214 | hdr_next += stripped_before; |
| 7215 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 7216 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7217 | } |
| 7218 | |
| 7219 | /* First, let's see if we want to capture this cookie. We check |
| 7220 | * that we don't already have a server side cookie, because we |
| 7221 | * can only capture one. Also as an optimisation, we ignore |
| 7222 | * cookies shorter than the declared name. |
| 7223 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 7224 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 7225 | txn->srv_cookie == NULL && |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7226 | (val_end - att_beg >= t->fe->capture_namelen) && |
| 7227 | memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
| 7228 | int log_len = val_end - att_beg; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 7229 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7230 | Alert("HTTP logging : out of memory.\n"); |
| 7231 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 7232 | else { |
| 7233 | if (log_len > t->fe->capture_len) |
| 7234 | log_len = t->fe->capture_len; |
| 7235 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 7236 | txn->srv_cookie[log_len] = 0; |
| 7237 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7238 | } |
| 7239 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7240 | srv = objt_server(t->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7241 | /* now check if we need to process it for persistence */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7242 | if (!(t->flags & SN_IGNORE_PRST) && |
| 7243 | (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 7244 | (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7245 | /* assume passive cookie by default */ |
| 7246 | txn->flags &= ~TX_SCK_MASK; |
| 7247 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7248 | |
| 7249 | /* If the cookie is in insert mode on a known server, we'll delete |
| 7250 | * this occurrence because we'll insert another one later. |
| 7251 | * 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] | 7252 | * a direct access. |
| 7253 | */ |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 7254 | if (t->be->ck_opts & PR_CK_PSV) { |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 7255 | /* The "preserve" flag was set, we don't want to touch the |
| 7256 | * server's cookie. |
| 7257 | */ |
| 7258 | } |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 7259 | else if ((srv && (t->be->ck_opts & PR_CK_INS)) || |
| 7260 | ((t->flags & SN_DIRECT) && (t->be->ck_opts & PR_CK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7261 | /* this cookie must be deleted */ |
| 7262 | if (*prev == ':' && next == hdr_end) { |
| 7263 | /* whole header */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7264 | delta = buffer_replace2(res->buf, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7265 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7266 | txn->hdr_idx.used--; |
| 7267 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7268 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7269 | hdr_next += delta; |
| 7270 | http_msg_move_end(&txn->rsp, delta); |
| 7271 | /* note: while both invalid now, <next> and <hdr_end> |
| 7272 | * are still equal, so the for() will stop as expected. |
| 7273 | */ |
| 7274 | } else { |
| 7275 | /* just remove the value */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7276 | int delta = del_hdr_value(res->buf, &prev, next); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7277 | next = prev; |
| 7278 | hdr_end += delta; |
| 7279 | hdr_next += delta; |
| 7280 | cur_hdr->len += delta; |
| 7281 | http_msg_move_end(&txn->rsp, delta); |
| 7282 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7283 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7284 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7285 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7286 | } |
Willy Tarreau | 6740213 | 2012-05-31 20:40:20 +0200 | [diff] [blame] | 7287 | else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7288 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7289 | * with this server since we know it. |
| 7290 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7291 | 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] | 7292 | next += delta; |
| 7293 | hdr_end += delta; |
| 7294 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7295 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7296 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7297 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7298 | txn->flags &= ~TX_SCK_MASK; |
| 7299 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7300 | } |
Willy Tarreau | a059031 | 2012-06-06 16:07:00 +0200 | [diff] [blame] | 7301 | else if (srv && srv->cookie && (t->be->ck_opts & PR_CK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7302 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7303 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7304 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7305 | 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] | 7306 | next += delta; |
| 7307 | hdr_end += delta; |
| 7308 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7309 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7310 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7311 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7312 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7313 | txn->flags &= ~TX_SCK_MASK; |
| 7314 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7315 | } |
| 7316 | } |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 7317 | /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */ |
| 7318 | else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7319 | int cmp_len, value_len; |
| 7320 | char *value_begin; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7321 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7322 | if (t->be->options2 & PR_O2_AS_PFX) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7323 | cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len); |
| 7324 | value_begin = att_beg + t->be->appsession_name_len; |
| 7325 | 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] | 7326 | } else { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7327 | cmp_len = att_end - att_beg; |
| 7328 | value_begin = val_beg; |
| 7329 | value_len = MIN(t->be->appsession_len, val_end - val_beg); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7330 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7331 | |
Cyril Bonté | 17530c3 | 2010-04-06 21:11:10 +0200 | [diff] [blame] | 7332 | if ((cmp_len == t->be->appsession_name_len) && |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7333 | (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 7334 | /* free a possibly previously allocated memory */ |
| 7335 | pool_free2(apools.sessid, txn->sessid); |
| 7336 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7337 | /* Store the sessid in the session for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7338 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7339 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 7340 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 7341 | return; |
| 7342 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7343 | memcpy(txn->sessid, value_begin, value_len); |
| 7344 | txn->sessid[value_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7345 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7346 | } |
| 7347 | /* that's done for this cookie, check the next one on the same |
| 7348 | * line when next != hdr_end (only if is_cookie2). |
| 7349 | */ |
| 7350 | } |
| 7351 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7352 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7353 | } |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7354 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7355 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7356 | appsess *asession = NULL; |
| 7357 | /* only do insert, if lookup fails */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7358 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7359 | if (asession == NULL) { |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 7360 | size_t server_id_len; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7361 | if ((asession = pool_alloc2(pool2_appsess)) == NULL) { |
| 7362 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 7363 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 7364 | return; |
| 7365 | } |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 7366 | asession->serverid = NULL; /* to avoid a double free in case of allocation error */ |
| 7367 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7368 | if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 7369 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 7370 | 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] | 7371 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7372 | return; |
| 7373 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7374 | memcpy(asession->sessid, txn->sessid, t->be->appsession_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7375 | asession->sessid[t->be->appsession_len] = 0; |
| 7376 | |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7377 | server_id_len = strlen(objt_server(t->target)->id) + 1; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7378 | if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 7379 | Alert("Not enough Memory process_srv():asession->serverid:malloc().\n"); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7380 | 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] | 7381 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7382 | return; |
| 7383 | } |
| 7384 | asession->serverid[0] = '\0'; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7385 | memcpy(asession->serverid, objt_server(t->target)->id, server_id_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7386 | |
| 7387 | asession->request_count = 0; |
| 7388 | appsession_hash_insert(&(t->be->htbl_proxy), asession); |
| 7389 | } |
| 7390 | |
| 7391 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 7392 | asession->request_count++; |
| 7393 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7394 | } |
| 7395 | |
| 7396 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7397 | /* |
| 7398 | * Check if response is cacheable or not. Updates t->flags. |
| 7399 | */ |
Willy Tarreau | 7421efb | 2012-07-02 15:11:27 +0200 | [diff] [blame] | 7400 | void check_response_for_cacheability(struct session *t, struct channel *rtr) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7401 | { |
| 7402 | struct http_txn *txn = &t->txn; |
| 7403 | char *p1, *p2; |
| 7404 | |
| 7405 | char *cur_ptr, *cur_end, *cur_next; |
| 7406 | int cur_idx; |
| 7407 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 7408 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7409 | return; |
| 7410 | |
| 7411 | /* Iterate through the headers. |
| 7412 | * we start with the start line. |
| 7413 | */ |
| 7414 | cur_idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7415 | cur_next = rtr->buf->p + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7416 | |
| 7417 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 7418 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7419 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7420 | |
| 7421 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7422 | cur_ptr = cur_next; |
| 7423 | cur_end = cur_ptr + cur_hdr->len; |
| 7424 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7425 | |
| 7426 | /* We have one full header between cur_ptr and cur_end, and the |
| 7427 | * next header starts at cur_next. We're only interested in |
| 7428 | * "Cookie:" headers. |
| 7429 | */ |
| 7430 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7431 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 7432 | if (val) { |
| 7433 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 7434 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 7435 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7436 | return; |
| 7437 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7438 | } |
| 7439 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7440 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 7441 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7442 | continue; |
| 7443 | |
| 7444 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 7445 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7446 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7447 | |
| 7448 | if (p1 >= cur_end) /* no more info */ |
| 7449 | continue; |
| 7450 | |
| 7451 | /* p1 is at the beginning of the value */ |
| 7452 | p2 = p1; |
| 7453 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 7454 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7455 | p2++; |
| 7456 | |
| 7457 | /* we have a complete value between p1 and p2 */ |
| 7458 | if (p2 < cur_end && *p2 == '=') { |
| 7459 | /* we have something of the form no-cache="set-cookie" */ |
| 7460 | if ((cur_end - p1 >= 21) && |
| 7461 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 7462 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7463 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7464 | continue; |
| 7465 | } |
| 7466 | |
| 7467 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 7468 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 7469 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 7470 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 7471 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7472 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7473 | return; |
| 7474 | } |
| 7475 | |
| 7476 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7477 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7478 | continue; |
| 7479 | } |
| 7480 | } |
| 7481 | } |
| 7482 | |
| 7483 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7484 | /* |
| 7485 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 7486 | * If the server is found, it's assigned to the session. |
| 7487 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7488 | 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] | 7489 | { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7490 | char *end_params, *first_param, *cur_param, *next_param; |
| 7491 | char separator; |
| 7492 | int value_len; |
| 7493 | |
| 7494 | int mode = t->be->options2 & PR_O2_AS_M_ANY; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7495 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 7496 | if (t->be->appsession_name == NULL || |
Cyril Bonté | 17530c3 | 2010-04-06 21:11:10 +0200 | [diff] [blame] | 7497 | (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] | 7498 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7499 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7500 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7501 | first_param = NULL; |
| 7502 | switch (mode) { |
| 7503 | case PR_O2_AS_M_PP: |
| 7504 | first_param = memchr(begin, ';', len); |
| 7505 | break; |
| 7506 | case PR_O2_AS_M_QS: |
| 7507 | first_param = memchr(begin, '?', len); |
| 7508 | break; |
| 7509 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7510 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7511 | if (first_param == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7512 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7513 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7514 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7515 | switch (mode) { |
| 7516 | case PR_O2_AS_M_PP: |
| 7517 | if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) { |
| 7518 | end_params = (char *) begin + len; |
| 7519 | } |
| 7520 | separator = ';'; |
| 7521 | break; |
| 7522 | case PR_O2_AS_M_QS: |
| 7523 | end_params = (char *) begin + len; |
| 7524 | separator = '&'; |
| 7525 | break; |
| 7526 | default: |
| 7527 | /* unknown mode, shouldn't happen */ |
| 7528 | return; |
| 7529 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7530 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7531 | cur_param = next_param = end_params; |
| 7532 | while (cur_param > first_param) { |
| 7533 | cur_param--; |
| 7534 | if ((cur_param[0] == separator) || (cur_param == first_param)) { |
| 7535 | /* let's see if this is the appsession parameter */ |
| 7536 | if ((cur_param + t->be->appsession_name_len + 1 < next_param) && |
| 7537 | ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') && |
| 7538 | (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 7539 | /* Cool... it's the right one */ |
| 7540 | cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2); |
| 7541 | value_len = MIN(t->be->appsession_len, next_param - cur_param); |
| 7542 | if (value_len > 0) { |
| 7543 | manage_client_side_appsession(t, cur_param, value_len); |
| 7544 | } |
| 7545 | break; |
| 7546 | } |
| 7547 | next_param = cur_param; |
| 7548 | } |
| 7549 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7550 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 7551 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 7552 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7553 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7554 | } |
| 7555 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7556 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7557 | * 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] | 7558 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7559 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7560 | * 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] | 7561 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7562 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7563 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7564 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7565 | 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] | 7566 | { |
| 7567 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7568 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7569 | const char *uri = msg->chn->buf->p+ msg->sl.rq.u; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7570 | const char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7571 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7572 | if (!uri_auth) |
| 7573 | return 0; |
| 7574 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7575 | 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] | 7576 | return 0; |
| 7577 | |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7578 | memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats)); |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 7579 | si->applet.ctx.stats.st_code = STAT_STATUS_INIT; |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 7580 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7581 | /* check URI size */ |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7582 | if (uri_auth->uri_len > msg->sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7583 | return 0; |
| 7584 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7585 | h = uri; |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 7586 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7587 | return 0; |
| 7588 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7589 | h += uri_auth->uri_len; |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7590 | while (h <= uri + msg->sl.rq.u_l - 3) { |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7591 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7592 | si->applet.ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7593 | break; |
| 7594 | } |
| 7595 | h++; |
| 7596 | } |
| 7597 | |
| 7598 | if (uri_auth->refresh) { |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7599 | h = uri + uri_auth->uri_len; |
| 7600 | while (h <= uri + msg->sl.rq.u_l - 10) { |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7601 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7602 | si->applet.ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7603 | break; |
| 7604 | } |
| 7605 | h++; |
| 7606 | } |
| 7607 | } |
| 7608 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7609 | h = uri + uri_auth->uri_len; |
| 7610 | while (h <= uri + msg->sl.rq.u_l - 4) { |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 7611 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7612 | si->applet.ctx.stats.flags |= STAT_FMT_CSV; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 7613 | break; |
| 7614 | } |
| 7615 | h++; |
| 7616 | } |
| 7617 | |
Willy Tarreau | 3a215be | 2012-03-09 21:39:51 +0100 | [diff] [blame] | 7618 | h = uri + uri_auth->uri_len; |
| 7619 | while (h <= uri + msg->sl.rq.u_l - 8) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7620 | if (memcmp(h, ";st=", 4) == 0) { |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 7621 | int i; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7622 | h += 4; |
Cyril Bonté | 20a804a | 2012-05-10 19:42:52 +0200 | [diff] [blame] | 7623 | si->applet.ctx.stats.st_code = STAT_STATUS_UNKN; |
Cyril Bonté | 19979e1 | 2012-04-04 12:57:21 +0200 | [diff] [blame] | 7624 | for (i = STAT_STATUS_INIT + 1; i < STAT_STATUS_SIZE; i++) { |
| 7625 | if (strncmp(stat_status_codes[i], h, 4) == 0) { |
| 7626 | si->applet.ctx.stats.st_code = i; |
| 7627 | break; |
| 7628 | } |
| 7629 | } |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7630 | break; |
| 7631 | } |
| 7632 | h++; |
| 7633 | } |
| 7634 | |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7635 | si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO; |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 7636 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7637 | return 1; |
| 7638 | } |
| 7639 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7640 | /* |
| 7641 | * 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] | 7642 | * By default it tries to report the error position as msg->err_pos. However if |
| 7643 | * this one is not set, it will then report msg->next, which is the last known |
| 7644 | * 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] | 7645 | * displays buffers as a contiguous area starting at buf->p. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7646 | */ |
| 7647 | void http_capture_bad_message(struct error_snapshot *es, struct session *s, |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 7648 | struct http_msg *msg, |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 7649 | int state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7650 | { |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 7651 | struct channel *chn = msg->chn; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7652 | int len1, len2; |
Willy Tarreau | 8a0cef2 | 2012-03-09 13:39:23 +0100 | [diff] [blame] | 7653 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7654 | es->len = MIN(chn->buf->i, sizeof(es->buf)); |
| 7655 | len1 = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7656 | len1 = MIN(len1, es->len); |
| 7657 | len2 = es->len - len1; /* remaining data if buffer wraps */ |
| 7658 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7659 | memcpy(es->buf, chn->buf->p, len1); |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7660 | if (len2) |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7661 | memcpy(es->buf + len1, chn->buf->data, len2); |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7662 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7663 | if (msg->err_pos >= 0) |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7664 | es->pos = msg->err_pos; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7665 | else |
Willy Tarreau | 69d8c5d | 2012-05-08 09:44:41 +0200 | [diff] [blame] | 7666 | es->pos = msg->next; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7667 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7668 | es->when = date; // user-visible date |
| 7669 | es->sid = s->uniq_id; |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7670 | es->srv = objt_server(s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7671 | es->oe = other_end; |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 7672 | es->src = s->req->prod->conn->addr.from; |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 7673 | es->state = state; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 7674 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 7675 | es->b_flags = chn->flags; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 7676 | es->s_flags = s->flags; |
| 7677 | es->t_flags = s->txn.flags; |
| 7678 | es->m_flags = msg->flags; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7679 | es->b_out = chn->buf->o; |
| 7680 | es->b_wrap = chn->buf->data + chn->buf->size - chn->buf->p; |
Willy Tarreau | cdbdd52 | 2012-10-12 22:51:15 +0200 | [diff] [blame] | 7681 | es->b_tot = chn->total; |
Willy Tarreau | d04b1bc | 2012-05-08 11:03:10 +0200 | [diff] [blame] | 7682 | es->m_clen = msg->chunk_len; |
| 7683 | es->m_blen = msg->body_len; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7684 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7685 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7686 | /* Return in <vptr> and <vlen> the pointer and length of occurrence <occ> of |
| 7687 | * header whose name is <hname> of length <hlen>. If <ctx> is null, lookup is |
| 7688 | * performed over the whole headers. Otherwise it must contain a valid header |
| 7689 | * context, initialised with ctx->idx=0 for the first lookup in a series. If |
| 7690 | * <occ> is positive or null, occurrence #occ from the beginning (or last ctx) |
| 7691 | * is returned. Occ #0 and #1 are equivalent. If <occ> is negative (and no less |
| 7692 | * than -MAX_HDR_HISTORY), the occurrence is counted from the last one which is |
| 7693 | * -1. |
| 7694 | * 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] | 7695 | */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 7696 | 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] | 7697 | struct hdr_idx *idx, int occ, |
| 7698 | struct hdr_ctx *ctx, char **vptr, int *vlen) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7699 | { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7700 | struct hdr_ctx local_ctx; |
| 7701 | char *ptr_hist[MAX_HDR_HISTORY]; |
| 7702 | int len_hist[MAX_HDR_HISTORY]; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7703 | unsigned int hist_ptr; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7704 | int found; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7705 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7706 | if (!ctx) { |
| 7707 | local_ctx.idx = 0; |
| 7708 | ctx = &local_ctx; |
| 7709 | } |
| 7710 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7711 | if (occ >= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7712 | /* search from the beginning */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7713 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7714 | occ--; |
| 7715 | if (occ <= 0) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7716 | *vptr = ctx->line + ctx->val; |
| 7717 | *vlen = ctx->vlen; |
| 7718 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7719 | } |
| 7720 | } |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7721 | return 0; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7722 | } |
| 7723 | |
| 7724 | /* negative occurrence, we scan all the list then walk back */ |
| 7725 | if (-occ > MAX_HDR_HISTORY) |
| 7726 | return 0; |
| 7727 | |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7728 | found = hist_ptr = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7729 | while (http_find_header2(hname, hlen, msg->chn->buf->p, idx, ctx)) { |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7730 | ptr_hist[hist_ptr] = ctx->line + ctx->val; |
| 7731 | len_hist[hist_ptr] = ctx->vlen; |
| 7732 | if (++hist_ptr >= MAX_HDR_HISTORY) |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7733 | hist_ptr = 0; |
| 7734 | found++; |
| 7735 | } |
| 7736 | if (-occ > found) |
| 7737 | return 0; |
| 7738 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
| 7739 | * find occurrence -occ, so we have to check [hist_ptr+occ]. |
| 7740 | */ |
| 7741 | hist_ptr += occ; |
| 7742 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 7743 | hist_ptr -= MAX_HDR_HISTORY; |
Willy Tarreau | 294c473 | 2011-12-16 21:35:50 +0100 | [diff] [blame] | 7744 | *vptr = ptr_hist[hist_ptr]; |
| 7745 | *vlen = len_hist[hist_ptr]; |
| 7746 | return 1; |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7747 | } |
| 7748 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 7749 | /* |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 7750 | * Print a debug line with a header. Always stop at the first CR or LF char, |
| 7751 | * so it is safe to pass it a full buffer if needed. If <err> is not NULL, an |
| 7752 | * arrow is printed after the line which contains the pointer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7753 | */ |
| 7754 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 7755 | { |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7756 | int max; |
| 7757 | 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] | 7758 | dir, (unsigned short)t->req->prod->conn->t.sock.fd, |
| 7759 | (unsigned short)t->req->cons->conn->t.sock.fd); |
Willy Tarreau | e92693a | 2012-09-24 21:13:39 +0200 | [diff] [blame] | 7760 | |
| 7761 | for (max = 0; start + max < end; max++) |
| 7762 | if (start[max] == '\r' || start[max] == '\n') |
| 7763 | break; |
| 7764 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 7765 | UBOUND(max, trash.size - trash.len - 3); |
| 7766 | trash.len += strlcpy2(trash.str + trash.len, start, max + 1); |
| 7767 | trash.str[trash.len++] = '\n'; |
| 7768 | if (write(1, trash.str, trash.len) < 0) /* shut gcc warning */; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7769 | } |
| 7770 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7771 | /* |
| 7772 | * Initialize a new HTTP transaction for session <s>. It is assumed that all |
| 7773 | * the required fields are properly allocated and that we only need to (re)init |
| 7774 | * them. This should be used before processing any new request. |
| 7775 | */ |
| 7776 | void http_init_txn(struct session *s) |
| 7777 | { |
| 7778 | struct http_txn *txn = &s->txn; |
| 7779 | struct proxy *fe = s->fe; |
| 7780 | |
| 7781 | txn->flags = 0; |
| 7782 | txn->status = -1; |
| 7783 | |
William Lallemand | 5f23240 | 2012-04-05 18:02:55 +0200 | [diff] [blame] | 7784 | global.req_count++; |
| 7785 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7786 | txn->cookie_first_date = 0; |
| 7787 | txn->cookie_last_date = 0; |
| 7788 | |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 7789 | txn->req.flags = 0; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 7790 | 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] | 7791 | txn->req.next = 0; |
Willy Tarreau | a36fc4d | 2012-02-17 17:39:37 +0100 | [diff] [blame] | 7792 | txn->rsp.flags = 0; |
Willy Tarreau | 2692736 | 2012-05-18 23:22:52 +0200 | [diff] [blame] | 7793 | 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] | 7794 | txn->rsp.next = 0; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 7795 | txn->req.chunk_len = 0LL; |
| 7796 | txn->req.body_len = 0LL; |
| 7797 | txn->rsp.chunk_len = 0LL; |
| 7798 | txn->rsp.body_len = 0LL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7799 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 7800 | 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] | 7801 | txn->req.chn = s->req; |
| 7802 | txn->rsp.chn = s->rep; |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7803 | |
| 7804 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7805 | |
| 7806 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 7807 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 7808 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 7809 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7810 | if (txn->req.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7811 | memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *)); |
| 7812 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7813 | if (txn->rsp.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7814 | memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
| 7815 | |
| 7816 | if (txn->hdr_idx.v) |
| 7817 | hdr_idx_init(&txn->hdr_idx); |
| 7818 | } |
| 7819 | |
| 7820 | /* to be used at the end of a transaction */ |
| 7821 | void http_end_txn(struct session *s) |
| 7822 | { |
| 7823 | struct http_txn *txn = &s->txn; |
| 7824 | |
| 7825 | /* these ones will have been dynamically allocated */ |
| 7826 | pool_free2(pool2_requri, txn->uri); |
| 7827 | pool_free2(pool2_capture, txn->cli_cookie); |
| 7828 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7829 | pool_free2(apools.sessid, txn->sessid); |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 7830 | pool_free2(pool2_uniqueid, s->unique_id); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7831 | |
William Lallemand | a73203e | 2012-03-12 12:48:57 +0100 | [diff] [blame] | 7832 | s->unique_id = NULL; |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7833 | txn->sessid = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7834 | txn->uri = NULL; |
| 7835 | txn->srv_cookie = NULL; |
| 7836 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7837 | |
| 7838 | if (txn->req.cap) { |
| 7839 | struct cap_hdr *h; |
| 7840 | for (h = s->fe->req_cap; h; h = h->next) |
| 7841 | pool_free2(h->pool, txn->req.cap[h->index]); |
| 7842 | memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *)); |
| 7843 | } |
| 7844 | |
| 7845 | if (txn->rsp.cap) { |
| 7846 | struct cap_hdr *h; |
| 7847 | for (h = s->fe->rsp_cap; h; h = h->next) |
| 7848 | pool_free2(h->pool, txn->rsp.cap[h->index]); |
| 7849 | memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *)); |
| 7850 | } |
| 7851 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7852 | } |
| 7853 | |
| 7854 | /* to be used at the end of a transaction to prepare a new one */ |
| 7855 | void http_reset_txn(struct session *s) |
| 7856 | { |
| 7857 | http_end_txn(s); |
| 7858 | http_init_txn(s); |
| 7859 | |
| 7860 | s->be = s->fe; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7861 | s->logs.logwait = s->fe->to_log; |
Simon Horman | af51495 | 2011-06-21 14:34:57 +0900 | [diff] [blame] | 7862 | session_del_srv_conn(s); |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 7863 | s->target = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 7864 | /* re-init store persistence */ |
| 7865 | s->store_count = 0; |
| 7866 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7867 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7868 | |
Willy Tarreau | 03cdb7c | 2012-08-27 23:14:58 +0200 | [diff] [blame] | 7869 | s->req->flags |= CF_READ_DONTWAIT; /* one read is usually enough */ |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7870 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 7871 | /* We must trim any excess data from the response buffer, because we |
| 7872 | * may have blocked an invalid response from a server that we don't |
| 7873 | * want to accidentely forward once we disable the analysers, nor do |
| 7874 | * we want those data to come along with next response. A typical |
| 7875 | * example of such data would be from a buggy server responding to |
| 7876 | * a HEAD with some data, or sending more than the advertised |
| 7877 | * content-length. |
| 7878 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 7879 | if (unlikely(s->rep->buf->i)) |
| 7880 | s->rep->buf->i = 0; |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 7881 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7882 | s->req->rto = s->fe->timeout.client; |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 7883 | s->req->wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7884 | |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 7885 | s->rep->rto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7886 | s->rep->wto = s->fe->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7887 | |
| 7888 | s->req->rex = TICK_ETERNITY; |
| 7889 | s->req->wex = TICK_ETERNITY; |
| 7890 | s->req->analyse_exp = TICK_ETERNITY; |
| 7891 | s->rep->rex = TICK_ETERNITY; |
| 7892 | s->rep->wex = TICK_ETERNITY; |
| 7893 | s->rep->analyse_exp = TICK_ETERNITY; |
| 7894 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7895 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 7896 | void free_http_req_rules(struct list *r) { |
| 7897 | struct http_req_rule *tr, *pr; |
| 7898 | |
| 7899 | list_for_each_entry_safe(pr, tr, r, list) { |
| 7900 | LIST_DEL(&pr->list); |
| 7901 | if (pr->action == HTTP_REQ_ACT_HTTP_AUTH) |
| 7902 | free(pr->http_auth.realm); |
| 7903 | |
| 7904 | free(pr); |
| 7905 | } |
| 7906 | } |
| 7907 | |
| 7908 | struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 7909 | { |
| 7910 | struct http_req_rule *rule; |
| 7911 | int cur_arg; |
| 7912 | |
| 7913 | rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule)); |
| 7914 | if (!rule) { |
| 7915 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 7916 | return NULL; |
| 7917 | } |
| 7918 | |
| 7919 | if (!*args[0]) { |
| 7920 | goto req_error_parsing; |
| 7921 | } else if (!strcmp(args[0], "allow")) { |
| 7922 | rule->action = HTTP_REQ_ACT_ALLOW; |
| 7923 | cur_arg = 1; |
| 7924 | } else if (!strcmp(args[0], "deny")) { |
| 7925 | rule->action = HTTP_REQ_ACT_DENY; |
| 7926 | cur_arg = 1; |
| 7927 | } else if (!strcmp(args[0], "auth")) { |
| 7928 | rule->action = HTTP_REQ_ACT_HTTP_AUTH; |
| 7929 | cur_arg = 1; |
| 7930 | |
| 7931 | while(*args[cur_arg]) { |
| 7932 | if (!strcmp(args[cur_arg], "realm")) { |
| 7933 | rule->http_auth.realm = strdup(args[cur_arg + 1]); |
| 7934 | cur_arg+=2; |
| 7935 | continue; |
| 7936 | } else |
| 7937 | break; |
| 7938 | } |
| 7939 | } else { |
| 7940 | req_error_parsing: |
| 7941 | Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n", |
| 7942 | file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]); |
| 7943 | return NULL; |
| 7944 | } |
| 7945 | |
| 7946 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 7947 | struct acl_cond *cond; |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 7948 | char *errmsg = NULL; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 7949 | |
Willy Tarreau | b7451bb | 2012-04-27 12:38:15 +0200 | [diff] [blame] | 7950 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg, &errmsg)) == NULL) { |
| 7951 | Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition : %s.\n", |
| 7952 | file, linenum, args[0], errmsg); |
| 7953 | free(errmsg); |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 7954 | return NULL; |
| 7955 | } |
| 7956 | rule->cond = cond; |
| 7957 | } |
| 7958 | else if (*args[cur_arg]) { |
| 7959 | Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 7960 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 7961 | file, linenum, args[0], args[cur_arg]); |
| 7962 | return NULL; |
| 7963 | } |
| 7964 | |
| 7965 | return rule; |
| 7966 | } |
| 7967 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7968 | /************************************************************************/ |
| 7969 | /* The code below is dedicated to ACL parsing and matching */ |
| 7970 | /************************************************************************/ |
| 7971 | |
| 7972 | |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7973 | /* This function ensures that the prerequisites for an L7 fetch are ready, |
| 7974 | * which means that a request or response is ready. If some data is missing, |
| 7975 | * 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] | 7976 | * to extract data from L7. If <req_vol> is non-null during a request prefetch, |
| 7977 | * another test is made to ensure the required information is not gone. |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7978 | * |
| 7979 | * The function returns : |
| 7980 | * 0 if some data is missing or if the requested data cannot be fetched |
| 7981 | * -1 if it is certain that we'll never have any HTTP message there |
| 7982 | * 1 if an HTTP message is ready |
| 7983 | */ |
| 7984 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 7985 | 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] | 7986 | const struct arg *args, struct sample *smp, int req_vol) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 7987 | { |
| 7988 | struct http_txn *txn = l7; |
| 7989 | struct http_msg *msg = &txn->req; |
| 7990 | |
| 7991 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 7992 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 7993 | */ |
| 7994 | |
| 7995 | if (unlikely(!s || !txn)) |
| 7996 | return 0; |
| 7997 | |
| 7998 | /* Check for a dependency on a request */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 7999 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8000 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8001 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8002 | if (unlikely(!s->req)) |
| 8003 | return 0; |
| 8004 | |
| 8005 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 8006 | if ((msg->msg_state == HTTP_MSG_ERROR) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8007 | buffer_full(s->req->buf, global.tune.maxrewrite)) { |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 8008 | smp->data.uint = 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8009 | return -1; |
| 8010 | } |
| 8011 | |
| 8012 | /* Try to decode HTTP request */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8013 | if (likely(msg->next < s->req->buf->i)) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8014 | http_msg_analyzer(msg, &txn->hdr_idx); |
| 8015 | |
| 8016 | /* Still no valid request ? */ |
| 8017 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 3bf1b2b | 2012-08-27 20:46:07 +0200 | [diff] [blame] | 8018 | if ((msg->msg_state == HTTP_MSG_ERROR) || |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8019 | buffer_full(s->req->buf, global.tune.maxrewrite)) { |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 8020 | smp->data.uint = 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8021 | return -1; |
| 8022 | } |
| 8023 | /* wait for final state */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8024 | smp->flags |= SMP_F_MAY_CHANGE; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8025 | return 0; |
| 8026 | } |
| 8027 | |
| 8028 | /* OK we just got a valid HTTP request. We have some minor |
| 8029 | * preparation to perform so that further checks can rely |
| 8030 | * on HTTP tests. |
| 8031 | */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8032 | 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] | 8033 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 8034 | s->flags |= SN_REDIRECTABLE; |
| 8035 | |
| 8036 | 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] | 8037 | smp->data.uint = 0; |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8038 | return -1; |
| 8039 | } |
| 8040 | } |
| 8041 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8042 | if (req_vol && txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8043 | return 0; /* data might have moved and indexes changed */ |
| 8044 | |
| 8045 | /* otherwise everything's ready for the request */ |
| 8046 | } |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8047 | else { |
| 8048 | /* Check for a dependency on a response */ |
Willy Tarreau | 14174bc | 2012-04-16 14:34:04 +0200 | [diff] [blame] | 8049 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 8050 | return 0; |
| 8051 | } |
| 8052 | |
| 8053 | /* everything's OK */ |
| 8054 | return 1; |
| 8055 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8056 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8057 | #define CHECK_HTTP_MESSAGE_FIRST() \ |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8058 | 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] | 8059 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8060 | #define CHECK_HTTP_MESSAGE_FIRST_PERM() \ |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8061 | 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] | 8062 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8063 | |
| 8064 | /* 1. Check on METHOD |
| 8065 | * We use the pre-parsed method if it is known, and store its number as an |
| 8066 | * integer. If it is unknown, we use the pointer and the length. |
| 8067 | */ |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 8068 | 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] | 8069 | { |
| 8070 | int len, meth; |
| 8071 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 8072 | len = strlen(*text); |
| 8073 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8074 | |
| 8075 | pattern->val.i = meth; |
| 8076 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 8077 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 8078 | if (!pattern->ptr.str) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 8079 | memprintf(err, "out of memory while loading pattern"); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8080 | return 0; |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 8081 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8082 | pattern->len = len; |
| 8083 | } |
| 8084 | return 1; |
| 8085 | } |
| 8086 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 8087 | /* This function fetches the method of current HTTP request and stores |
| 8088 | * it in the global pattern struct as a chunk. There are two possibilities : |
| 8089 | * - if the method is known (not HTTP_METH_OTHER), its identifier is stored |
| 8090 | * in <len> and <ptr> is NULL ; |
| 8091 | * - if the method is unknown (HTTP_METH_OTHER), <ptr> points to the text and |
| 8092 | * <len> to its length. |
| 8093 | * This is intended to be used with acl_match_meth() only. |
| 8094 | */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 8095 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8096 | 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] | 8097 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8098 | { |
| 8099 | int meth; |
| 8100 | struct http_txn *txn = l7; |
| 8101 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8102 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8103 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8104 | meth = txn->meth; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8105 | smp->type = SMP_T_UINT; |
| 8106 | smp->data.uint = meth; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8107 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8108 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 8109 | /* ensure the indexes are not affected */ |
| 8110 | return 0; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8111 | smp->type = SMP_T_CSTR; |
| 8112 | smp->data.str.len = txn->req.sl.rq.m_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8113 | smp->data.str.str = txn->req.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8114 | } |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 8115 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8116 | return 1; |
| 8117 | } |
| 8118 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 8119 | /* See above how the method is stored in the global pattern */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8120 | static int acl_match_meth(struct sample *smp, struct acl_pattern *pattern) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8121 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 8122 | int icase; |
| 8123 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 8124 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8125 | if (smp->type == SMP_T_UINT) { |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 8126 | /* well-known method */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8127 | if (smp->data.uint == pattern->val.i) |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 8128 | return ACL_PAT_PASS; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 8129 | return ACL_PAT_FAIL; |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 8130 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8131 | |
Willy Tarreau | 8e5e955 | 2011-12-16 15:38:49 +0100 | [diff] [blame] | 8132 | /* Uncommon method, only HTTP_METH_OTHER is accepted now */ |
| 8133 | if (pattern->val.i != HTTP_METH_OTHER) |
| 8134 | return ACL_PAT_FAIL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8135 | |
| 8136 | /* Other method, we must compare the strings */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8137 | if (pattern->len != smp->data.str.len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 8138 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 8139 | |
| 8140 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8141 | if ((icase && strncasecmp(pattern->ptr.str, smp->data.str.str, smp->data.str.len) != 0) || |
| 8142 | (!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] | 8143 | return ACL_PAT_FAIL; |
| 8144 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8145 | } |
| 8146 | |
| 8147 | /* 2. Check on Request/Status Version |
| 8148 | * We simply compare strings here. |
| 8149 | */ |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 8150 | 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] | 8151 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 8152 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 8153 | if (!pattern->ptr.str) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 8154 | memprintf(err, "out of memory while loading pattern"); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8155 | return 0; |
Willy Tarreau | 7dcb648 | 2012-04-27 17:52:25 +0200 | [diff] [blame] | 8156 | } |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 8157 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8158 | return 1; |
| 8159 | } |
| 8160 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 8161 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8162 | 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] | 8163 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8164 | { |
| 8165 | struct http_txn *txn = l7; |
| 8166 | char *ptr; |
| 8167 | int len; |
| 8168 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8169 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8170 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8171 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8172 | ptr = txn->req.chn->buf->p + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8173 | |
| 8174 | while ((len-- > 0) && (*ptr++ != '/')); |
| 8175 | if (len <= 0) |
| 8176 | return 0; |
| 8177 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8178 | smp->type = SMP_T_CSTR; |
| 8179 | smp->data.str.str = ptr; |
| 8180 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8181 | |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 8182 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8183 | return 1; |
| 8184 | } |
| 8185 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 8186 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8187 | 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] | 8188 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8189 | { |
| 8190 | struct http_txn *txn = l7; |
| 8191 | char *ptr; |
| 8192 | int len; |
| 8193 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8194 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8195 | |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 8196 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 8197 | return 0; |
| 8198 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8199 | len = txn->rsp.sl.st.v_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8200 | ptr = txn->rsp.chn->buf->p; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8201 | |
| 8202 | while ((len-- > 0) && (*ptr++ != '/')); |
| 8203 | if (len <= 0) |
| 8204 | return 0; |
| 8205 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8206 | smp->type = SMP_T_CSTR; |
| 8207 | smp->data.str.str = ptr; |
| 8208 | smp->data.str.len = len; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8209 | |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 8210 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8211 | return 1; |
| 8212 | } |
| 8213 | |
| 8214 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 8215 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8216 | 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] | 8217 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8218 | { |
| 8219 | struct http_txn *txn = l7; |
| 8220 | char *ptr; |
| 8221 | int len; |
| 8222 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8223 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8224 | |
Willy Tarreau | f26b252 | 2012-12-14 08:33:14 +0100 | [diff] [blame] | 8225 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
| 8226 | return 0; |
| 8227 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8228 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8229 | ptr = txn->rsp.chn->buf->p + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8230 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8231 | smp->type = SMP_T_UINT; |
| 8232 | smp->data.uint = __strl2ui(ptr, len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8233 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8234 | return 1; |
| 8235 | } |
| 8236 | |
| 8237 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 8238 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 8239 | 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] | 8240 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8241 | { |
| 8242 | struct http_txn *txn = l7; |
| 8243 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8244 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8245 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8246 | smp->type = SMP_T_CSTR; |
| 8247 | smp->data.str.len = txn->req.sl.rq.u_l; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8248 | 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] | 8249 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8250 | return 1; |
| 8251 | } |
| 8252 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 8253 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 8254 | 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] | 8255 | const struct arg *args, struct sample *smp) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 8256 | { |
| 8257 | struct http_txn *txn = l7; |
| 8258 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8259 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 8260 | |
| 8261 | /* Parse HTTP request */ |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 8262 | url2sa(txn->req.chn->buf->p + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->conn->addr.to); |
| 8263 | 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] | 8264 | return 0; |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8265 | smp->type = SMP_T_IPV4; |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 8266 | 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] | 8267 | |
| 8268 | /* |
| 8269 | * If we are parsing url in frontend space, we prepare backend stage |
| 8270 | * to not parse again the same url ! optimization lazyness... |
| 8271 | */ |
| 8272 | if (px->options & PR_O_HTTP_PROXY) |
| 8273 | l4->flags |= SN_ADDR_SET; |
| 8274 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8275 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 8276 | return 1; |
| 8277 | } |
| 8278 | |
| 8279 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 8280 | 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] | 8281 | const struct arg *args, struct sample *smp) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 8282 | { |
| 8283 | struct http_txn *txn = l7; |
| 8284 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8285 | CHECK_HTTP_MESSAGE_FIRST(); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 8286 | |
| 8287 | /* Same optimization as url_ip */ |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 8288 | 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] | 8289 | smp->type = SMP_T_UINT; |
Willy Tarreau | f2943dc | 2012-10-26 20:10:28 +0200 | [diff] [blame] | 8290 | 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] | 8291 | |
| 8292 | if (px->options & PR_O_HTTP_PROXY) |
| 8293 | l4->flags |= SN_ADDR_SET; |
| 8294 | |
Willy Tarreau | 21e5b0e | 2012-04-23 19:25:44 +0200 | [diff] [blame] | 8295 | smp->flags = 0; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 8296 | return 1; |
| 8297 | } |
| 8298 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8299 | /* Fetch an HTTP header. A pointer to the beginning of the value is returned. |
| 8300 | * Accepts an optional argument of type string containing the header field name, |
| 8301 | * and an optional argument of type signed or unsigned integer to request an |
| 8302 | * explicit occurrence of the header. Note that in the event of a missing name, |
| 8303 | * headers are considered from the first one. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8304 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8305 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8306 | 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] | 8307 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8308 | { |
| 8309 | struct http_txn *txn = l7; |
| 8310 | struct hdr_idx *idx = &txn->hdr_idx; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8311 | struct hdr_ctx *ctx = (struct hdr_ctx *)smp->ctx.a; |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8312 | 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] | 8313 | int occ = 0; |
| 8314 | const char *name_str = NULL; |
| 8315 | int name_len = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8316 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8317 | if (args) { |
| 8318 | if (args[0].type != ARGT_STR) |
| 8319 | return 0; |
| 8320 | name_str = args[0].data.str.str; |
| 8321 | name_len = args[0].data.str.len; |
| 8322 | |
| 8323 | if (args[1].type == ARGT_UINT || args[1].type == ARGT_SINT) |
| 8324 | occ = args[1].data.uint; |
| 8325 | } |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 8326 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8327 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8328 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8329 | if (ctx && !(smp->flags & SMP_F_NOT_LAST)) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8330 | /* search for header from the beginning */ |
| 8331 | ctx->idx = 0; |
| 8332 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8333 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 8334 | /* no explicit occurrence and single fetch => last header by default */ |
| 8335 | occ = -1; |
| 8336 | |
| 8337 | if (!occ) |
| 8338 | /* prepare to report multiple occurrences for ACL fetches */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8339 | smp->flags |= SMP_F_NOT_LAST; |
Willy Tarreau | 664092c | 2011-12-16 19:11:42 +0100 | [diff] [blame] | 8340 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8341 | smp->type = SMP_T_CSTR; |
| 8342 | smp->flags |= SMP_F_VOL_HDR; |
| 8343 | 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] | 8344 | return 1; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8345 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8346 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8347 | return 0; |
| 8348 | } |
| 8349 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8350 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 8351 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8352 | */ |
| 8353 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8354 | 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] | 8355 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8356 | { |
| 8357 | struct http_txn *txn = l7; |
| 8358 | struct hdr_idx *idx = &txn->hdr_idx; |
| 8359 | struct hdr_ctx ctx; |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8360 | 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] | 8361 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8362 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8363 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 8364 | return 0; |
| 8365 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8366 | CHECK_HTTP_MESSAGE_FIRST(); |
| 8367 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8368 | ctx.idx = 0; |
| 8369 | cnt = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8370 | 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] | 8371 | cnt++; |
| 8372 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8373 | smp->type = SMP_T_UINT; |
| 8374 | smp->data.uint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8375 | smp->flags = SMP_F_VOL_HDR; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8376 | return 1; |
| 8377 | } |
| 8378 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8379 | /* Fetch an HTTP header's integer value. The integer value is returned. It |
| 8380 | * takes a mandatory argument of type string and an optional one of type int |
| 8381 | * to designate a specific occurrence. It returns an unsigned integer, which |
| 8382 | * may or may not be appropriate for everything. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8383 | */ |
| 8384 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8385 | 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] | 8386 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8387 | { |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8388 | int ret = smp_fetch_hdr(px, l4, l7, opt, args, smp); |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8389 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8390 | if (ret > 0) { |
| 8391 | smp->type = SMP_T_UINT; |
| 8392 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 8393 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8394 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 8395 | return ret; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8396 | } |
| 8397 | |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 8398 | /* Fetch an HTTP header's IP value. takes a mandatory argument of type string |
| 8399 | * and an optional one of type int to designate a specific occurrence. |
| 8400 | * It returns an IPv4 or IPv6 address. |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8401 | */ |
| 8402 | static int |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8403 | 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] | 8404 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8405 | { |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 8406 | int ret; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8407 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8408 | while ((ret = smp_fetch_hdr(px, l4, l7, opt, args, smp)) > 0) { |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 8409 | if (url2ipv4((char *)smp->data.str.str, &smp->data.ipv4)) { |
| 8410 | smp->type = SMP_T_IPV4; |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 8411 | break; |
Cyril Bonté | 69fa992 | 2012-10-25 00:01:06 +0200 | [diff] [blame] | 8412 | } else { |
| 8413 | struct chunk *temp = sample_get_trash_chunk(); |
| 8414 | if (smp->data.str.len < temp->size - 1) { |
| 8415 | memcpy(temp->str, smp->data.str.str, smp->data.str.len); |
| 8416 | temp->str[smp->data.str.len] = '\0'; |
| 8417 | if (inet_pton(AF_INET6, temp->str, &smp->data.ipv6)) { |
| 8418 | smp->type = SMP_T_IPV6; |
| 8419 | break; |
| 8420 | } |
| 8421 | } |
| 8422 | } |
| 8423 | |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 8424 | /* if the header doesn't match an IP address, fetch next one */ |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 8425 | if (!(smp->flags & SMP_F_NOT_LAST)) |
| 8426 | return 0; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8427 | } |
Willy Tarreau | d53e242 | 2012-04-16 17:21:11 +0200 | [diff] [blame] | 8428 | return ret; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8429 | } |
| 8430 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8431 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 8432 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 8433 | */ |
| 8434 | static int |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 8435 | 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] | 8436 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8437 | { |
| 8438 | struct http_txn *txn = l7; |
| 8439 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8440 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8441 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8442 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8443 | 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] | 8444 | ptr = http_get_path(txn); |
| 8445 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8446 | return 0; |
| 8447 | |
| 8448 | /* OK, we got the '/' ! */ |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8449 | smp->type = SMP_T_CSTR; |
| 8450 | smp->data.str.str = ptr; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8451 | |
| 8452 | while (ptr < end && *ptr != '?') |
| 8453 | ptr++; |
| 8454 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8455 | smp->data.str.len = ptr - smp->data.str.str; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8456 | smp->flags = SMP_F_VOL_1ST; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8457 | return 1; |
| 8458 | } |
| 8459 | |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 8460 | /* This produces a concatenation of the first occurrence of the Host header |
| 8461 | * followed by the path component if it begins with a slash ('/'). This means |
| 8462 | * that '*' will not be added, resulting in exactly the first Host entry. |
| 8463 | * If no Host header is found, then the path is returned as-is. The returned |
| 8464 | * value is stored in the trash so it does not need to be marked constant. |
| 8465 | */ |
| 8466 | static int |
| 8467 | smp_fetch_base(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 8468 | const struct arg *args, struct sample *smp) |
| 8469 | { |
| 8470 | struct http_txn *txn = l7; |
| 8471 | char *ptr, *end, *beg; |
| 8472 | struct hdr_ctx ctx; |
| 8473 | |
| 8474 | CHECK_HTTP_MESSAGE_FIRST(); |
| 8475 | |
| 8476 | ctx.idx = 0; |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8477 | 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] | 8478 | !ctx.vlen) |
| 8479 | return smp_fetch_path(px, l4, l7, opt, args, smp); |
| 8480 | |
| 8481 | /* 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] | 8482 | memcpy(trash.str, ctx.line + ctx.val, ctx.vlen); |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 8483 | smp->type = SMP_T_STR; |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 8484 | smp->data.str.str = trash.str; |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 8485 | smp->data.str.len = ctx.vlen; |
| 8486 | |
| 8487 | /* now retrieve the path */ |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8488 | 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] | 8489 | beg = http_get_path(txn); |
| 8490 | if (!beg) |
| 8491 | beg = end; |
| 8492 | |
| 8493 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 8494 | |
| 8495 | if (beg < ptr && *beg == '/') { |
| 8496 | memcpy(smp->data.str.str + smp->data.str.len, beg, ptr - beg); |
| 8497 | smp->data.str.len += ptr - beg; |
| 8498 | } |
| 8499 | |
| 8500 | smp->flags = SMP_F_VOL_1ST; |
| 8501 | return 1; |
| 8502 | } |
| 8503 | |
Willy Tarreau | ab1f7b7 | 2012-12-09 13:38:54 +0100 | [diff] [blame] | 8504 | /* This produces a 32-bit hash of the concatenation of the first occurrence of |
| 8505 | * the Host header followed by the path component if it begins with a slash ('/'). |
| 8506 | * This means that '*' will not be added, resulting in exactly the first Host |
| 8507 | * entry. If no Host header is found, then the path is used. The resulting value |
| 8508 | * is hashed using the url hash followed by a full avalanche hash and provides a |
| 8509 | * 32-bit integer value. This fetch is useful for tracking per-URL activity on |
| 8510 | * high-traffic sites without having to store whole paths. |
| 8511 | */ |
| 8512 | static int |
| 8513 | smp_fetch_base32(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 8514 | const struct arg *args, struct sample *smp) |
| 8515 | { |
| 8516 | struct http_txn *txn = l7; |
| 8517 | struct hdr_ctx ctx; |
| 8518 | unsigned int hash = 0; |
| 8519 | char *ptr, *beg, *end; |
| 8520 | int len; |
| 8521 | |
| 8522 | CHECK_HTTP_MESSAGE_FIRST(); |
| 8523 | |
| 8524 | ctx.idx = 0; |
| 8525 | if (http_find_header2("Host", 4, txn->req.chn->buf->p + txn->req.sol, &txn->hdr_idx, &ctx)) { |
| 8526 | /* OK we have the header value in ctx.line+ctx.val for ctx.vlen bytes */ |
| 8527 | ptr = ctx.line + ctx.val; |
| 8528 | len = ctx.vlen; |
| 8529 | while (len--) |
| 8530 | hash = *(ptr++) + (hash << 6) + (hash << 16) - hash; |
| 8531 | } |
| 8532 | |
| 8533 | /* now retrieve the path */ |
| 8534 | end = txn->req.chn->buf->p + txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 8535 | beg = http_get_path(txn); |
| 8536 | if (!beg) |
| 8537 | beg = end; |
| 8538 | |
| 8539 | for (ptr = beg; ptr < end && *ptr != '?'; ptr++); |
| 8540 | |
| 8541 | if (beg < ptr && *beg == '/') { |
| 8542 | while (beg < ptr) |
| 8543 | hash = *(beg++) + (hash << 6) + (hash << 16) - hash; |
| 8544 | } |
| 8545 | hash = full_hash(hash); |
| 8546 | |
| 8547 | smp->type = SMP_T_UINT; |
| 8548 | smp->data.uint = hash; |
| 8549 | smp->flags = SMP_F_VOL_1ST; |
| 8550 | return 1; |
| 8551 | } |
| 8552 | |
Willy Tarreau | 4a55060 | 2012-12-09 14:53:32 +0100 | [diff] [blame] | 8553 | /* This concatenates the source address with the 32-bit hash of the Host and |
| 8554 | * URL as returned by smp_fetch_base32(). The idea is to have per-source and |
| 8555 | * per-url counters. The result is a binary block from 8 to 20 bytes depending |
| 8556 | * on the source address length. The URL hash is stored before the address so |
| 8557 | * that in environments where IPv6 is insignificant, truncating the output to |
| 8558 | * 8 bytes would still work. |
| 8559 | */ |
| 8560 | static int |
| 8561 | smp_fetch_base32_src(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 8562 | const struct arg *args, struct sample *smp) |
| 8563 | { |
| 8564 | struct chunk *temp = sample_get_trash_chunk(); |
| 8565 | |
| 8566 | if (!smp_fetch_base32(px, l4, l7, opt, args, smp)) |
| 8567 | return 0; |
| 8568 | |
| 8569 | memcpy(temp->str + temp->len, &smp->data.uint, sizeof(smp->data.uint)); |
| 8570 | temp->len += sizeof(smp->data.uint); |
| 8571 | |
| 8572 | switch (l4->si[0].conn->addr.from.ss_family) { |
| 8573 | case AF_INET: |
| 8574 | memcpy(temp->str + temp->len, &((struct sockaddr_in *)&l4->si[0].conn->addr.from)->sin_addr, 4); |
| 8575 | temp->len += 4; |
| 8576 | break; |
| 8577 | case AF_INET6: |
| 8578 | memcpy(temp->str + temp->len, &((struct sockaddr_in6 *)(&l4->si[0].conn->addr.from))->sin6_addr, 16); |
| 8579 | temp->len += 16; |
| 8580 | break; |
| 8581 | default: |
| 8582 | return 0; |
| 8583 | } |
| 8584 | |
| 8585 | smp->data.str = *temp; |
| 8586 | smp->type = SMP_T_BIN; |
| 8587 | return 1; |
| 8588 | } |
| 8589 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8590 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8591 | 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] | 8592 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8593 | { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8594 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 8595 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 8596 | */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8597 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8598 | CHECK_HTTP_MESSAGE_FIRST_PERM(); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8599 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8600 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 8601 | smp->data.uint = 1; |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8602 | return 1; |
| 8603 | } |
| 8604 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 8605 | /* return a valid test if the current request is the first one on the connection */ |
| 8606 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8607 | 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] | 8608 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 8609 | { |
| 8610 | if (!s) |
| 8611 | return 0; |
| 8612 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8613 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 197e10a | 2012-04-23 19:18:42 +0200 | [diff] [blame] | 8614 | smp->data.uint = !(s->txn.flags & TX_NOT_FIRST); |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 8615 | return 1; |
| 8616 | } |
| 8617 | |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 8618 | /* Accepts exactly 1 argument of type userlist */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8619 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8620 | 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] | 8621 | const struct arg *args, struct sample *smp) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8622 | { |
| 8623 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8624 | if (!args || args->type != ARGT_USR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 8625 | return 0; |
| 8626 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8627 | CHECK_HTTP_MESSAGE_FIRST(); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8628 | |
Willy Tarreau | c0239e0 | 2012-04-16 14:42:55 +0200 | [diff] [blame] | 8629 | if (!get_http_auth(l4)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8630 | return 0; |
| 8631 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8632 | smp->type = SMP_T_BOOL; |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8633 | 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] | 8634 | return 1; |
| 8635 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8636 | |
Willy Tarreau | 4a3fd4c | 2012-05-10 23:18:26 +0200 | [diff] [blame] | 8637 | /* Accepts exactly 1 argument of type userlist */ |
| 8638 | static int |
| 8639 | acl_fetch_http_auth_grp(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 8640 | const struct arg *args, struct sample *smp) |
| 8641 | { |
| 8642 | |
| 8643 | if (!args || args->type != ARGT_USR) |
| 8644 | return 0; |
| 8645 | |
| 8646 | CHECK_HTTP_MESSAGE_FIRST(); |
| 8647 | |
| 8648 | if (!get_http_auth(l4)) |
| 8649 | return 0; |
| 8650 | |
| 8651 | /* acl_match_auth() will need several information at once */ |
| 8652 | smp->ctx.a[0] = args->data.usr; /* user list */ |
| 8653 | smp->ctx.a[1] = l4->txn.auth.user; /* user name */ |
| 8654 | smp->ctx.a[2] = l4->txn.auth.pass; /* password */ |
| 8655 | |
| 8656 | /* if the user does not belong to the userlist or has a wrong password, |
| 8657 | * report that it unconditionally does not match. Otherwise we return |
| 8658 | * a non-zero integer which will be ignored anyway since all the params |
| 8659 | * that acl_match_auth() will use are in test->ctx.a[0,1,2]. |
| 8660 | */ |
| 8661 | smp->type = SMP_T_BOOL; |
| 8662 | smp->data.uint = check_user(args->data.usr, 0, l4->txn.auth.user, l4->txn.auth.pass); |
| 8663 | if (smp->data.uint) |
| 8664 | smp->type = SMP_T_UINT; |
| 8665 | |
| 8666 | return 1; |
| 8667 | } |
| 8668 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8669 | /* Try to find the next occurrence of a cookie name in a cookie header value. |
| 8670 | * The lookup begins at <hdr>. The pointer and size of the next occurrence of |
| 8671 | * the cookie value is returned into *value and *value_l, and the function |
| 8672 | * returns a pointer to the next pointer to search from if the value was found. |
| 8673 | * Otherwise if the cookie was not found, NULL is returned and neither value |
| 8674 | * nor value_l are touched. The input <hdr> string should first point to the |
| 8675 | * header's value, and the <hdr_end> pointer must point to the first character |
| 8676 | * not part of the value. <list> must be non-zero if value may represent a list |
| 8677 | * of values (cookie headers). This makes it faster to abort parsing when no |
| 8678 | * list is expected. |
| 8679 | */ |
| 8680 | static char * |
| 8681 | extract_cookie_value(char *hdr, const char *hdr_end, |
| 8682 | char *cookie_name, size_t cookie_name_l, int list, |
Willy Tarreau | 3fb818c | 2012-04-11 17:21:08 +0200 | [diff] [blame] | 8683 | char **value, int *value_l) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8684 | { |
| 8685 | char *equal, *att_end, *att_beg, *val_beg, *val_end; |
| 8686 | char *next; |
| 8687 | |
| 8688 | /* we search at least a cookie name followed by an equal, and more |
| 8689 | * generally something like this : |
| 8690 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 8691 | */ |
| 8692 | for (att_beg = hdr; att_beg + cookie_name_l + 1 < hdr_end; att_beg = next + 1) { |
| 8693 | /* Iterate through all cookies on this line */ |
| 8694 | |
| 8695 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 8696 | att_beg++; |
| 8697 | |
| 8698 | /* find att_end : this is the first character after the last non |
| 8699 | * space before the equal. It may be equal to hdr_end. |
| 8700 | */ |
| 8701 | equal = att_end = att_beg; |
| 8702 | |
| 8703 | while (equal < hdr_end) { |
| 8704 | if (*equal == '=' || *equal == ';' || (list && *equal == ',')) |
| 8705 | break; |
| 8706 | if (http_is_spht[(unsigned char)*equal++]) |
| 8707 | continue; |
| 8708 | att_end = equal; |
| 8709 | } |
| 8710 | |
| 8711 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 8712 | * is between <att_beg> and <equal>, both may be identical. |
| 8713 | */ |
| 8714 | |
| 8715 | /* look for end of cookie if there is an equal sign */ |
| 8716 | if (equal < hdr_end && *equal == '=') { |
| 8717 | /* look for the beginning of the value */ |
| 8718 | val_beg = equal + 1; |
| 8719 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 8720 | val_beg++; |
| 8721 | |
| 8722 | /* find the end of the value, respecting quotes */ |
| 8723 | next = find_cookie_value_end(val_beg, hdr_end); |
| 8724 | |
| 8725 | /* make val_end point to the first white space or delimitor after the value */ |
| 8726 | val_end = next; |
| 8727 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 8728 | val_end--; |
| 8729 | } else { |
| 8730 | val_beg = val_end = next = equal; |
| 8731 | } |
| 8732 | |
| 8733 | /* We have nothing to do with attributes beginning with '$'. However, |
| 8734 | * they will automatically be removed if a header before them is removed, |
| 8735 | * since they're supposed to be linked together. |
| 8736 | */ |
| 8737 | if (*att_beg == '$') |
| 8738 | continue; |
| 8739 | |
| 8740 | /* Ignore cookies with no equal sign */ |
| 8741 | if (equal == next) |
| 8742 | continue; |
| 8743 | |
| 8744 | /* Now we have the cookie name between att_beg and att_end, and |
| 8745 | * its value between val_beg and val_end. |
| 8746 | */ |
| 8747 | |
| 8748 | if (att_end - att_beg == cookie_name_l && |
| 8749 | memcmp(att_beg, cookie_name, cookie_name_l) == 0) { |
| 8750 | /* let's return this value and indicate where to go on from */ |
| 8751 | *value = val_beg; |
| 8752 | *value_l = val_end - val_beg; |
| 8753 | return next + 1; |
| 8754 | } |
| 8755 | |
| 8756 | /* Set-Cookie headers only have the name in the first attr=value part */ |
| 8757 | if (!list) |
| 8758 | break; |
| 8759 | } |
| 8760 | |
| 8761 | return NULL; |
| 8762 | } |
| 8763 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8764 | /* 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] | 8765 | * smp->ctx.a[0] for the in-header position, smp->ctx.a[1] for the |
| 8766 | * 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] | 8767 | * 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] | 8768 | * 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] | 8769 | * Accepts exactly 1 argument of type string. If the input options indicate |
| 8770 | * 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] | 8771 | */ |
| 8772 | static int |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 8773 | smp_fetch_cookie(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 8774 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8775 | { |
| 8776 | struct http_txn *txn = l7; |
| 8777 | struct hdr_idx *idx = &txn->hdr_idx; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8778 | struct hdr_ctx *ctx = (struct hdr_ctx *)&smp->ctx.a[2]; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8779 | const struct http_msg *msg; |
| 8780 | const char *hdr_name; |
| 8781 | int hdr_name_len; |
| 8782 | char *sol; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 8783 | int occ = 0; |
| 8784 | int found = 0; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8785 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8786 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 8787 | return 0; |
| 8788 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8789 | CHECK_HTTP_MESSAGE_FIRST(); |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8790 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8791 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8792 | msg = &txn->req; |
| 8793 | hdr_name = "Cookie"; |
| 8794 | hdr_name_len = 6; |
| 8795 | } else { |
| 8796 | msg = &txn->rsp; |
| 8797 | hdr_name = "Set-Cookie"; |
| 8798 | hdr_name_len = 10; |
| 8799 | } |
| 8800 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 8801 | if (!occ && !(opt & SMP_OPT_ITERATE)) |
| 8802 | /* no explicit occurrence and single fetch => last cookie by default */ |
| 8803 | occ = -1; |
| 8804 | |
| 8805 | /* OK so basically here, either we want only one value and it's the |
| 8806 | * last one, or we want to iterate over all of them and we fetch the |
| 8807 | * next one. |
| 8808 | */ |
| 8809 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8810 | sol = msg->chn->buf->p; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8811 | if (!(smp->flags & SMP_F_NOT_LAST)) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8812 | /* search for the header from the beginning, we must first initialize |
| 8813 | * the search parameters. |
| 8814 | */ |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8815 | smp->ctx.a[0] = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8816 | ctx->idx = 0; |
| 8817 | } |
| 8818 | |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 8819 | smp->flags |= SMP_F_VOL_HDR; |
| 8820 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8821 | while (1) { |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8822 | /* Note: smp->ctx.a[0] == NULL every time we need to fetch a new header */ |
| 8823 | if (!smp->ctx.a[0]) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8824 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, ctx)) |
| 8825 | goto out; |
| 8826 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8827 | if (ctx->vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8828 | continue; |
| 8829 | |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8830 | smp->ctx.a[0] = ctx->line + ctx->val; |
| 8831 | smp->ctx.a[1] = smp->ctx.a[0] + ctx->vlen; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8832 | } |
| 8833 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8834 | smp->type = SMP_T_CSTR; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8835 | 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] | 8836 | args->data.str.str, args->data.str.len, |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8837 | (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8838 | &smp->data.str.str, |
| 8839 | &smp->data.str.len); |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8840 | if (smp->ctx.a[0]) { |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 8841 | found = 1; |
| 8842 | if (occ >= 0) { |
| 8843 | /* one value was returned into smp->data.str.{str,len} */ |
| 8844 | smp->flags |= SMP_F_NOT_LAST; |
| 8845 | return 1; |
| 8846 | } |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8847 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 8848 | /* if we're looking for last occurrence, let's loop */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8849 | } |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 8850 | /* all cookie headers and values were scanned. If we're looking for the |
| 8851 | * last occurrence, we may return it now. |
| 8852 | */ |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8853 | out: |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8854 | smp->flags &= ~SMP_F_NOT_LAST; |
Willy Tarreau | 28376d6 | 2012-04-26 21:26:10 +0200 | [diff] [blame] | 8855 | return found; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8856 | } |
| 8857 | |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8858 | /* 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] | 8859 | * 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] | 8860 | * multiple cookies may be parsed on the same line. |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 8861 | * Accepts exactly 1 argument of type string. |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8862 | */ |
| 8863 | static int |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8864 | 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] | 8865 | const struct arg *args, struct sample *smp) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8866 | { |
| 8867 | struct http_txn *txn = l7; |
| 8868 | struct hdr_idx *idx = &txn->hdr_idx; |
| 8869 | struct hdr_ctx ctx; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8870 | const struct http_msg *msg; |
| 8871 | const char *hdr_name; |
| 8872 | int hdr_name_len; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8873 | int cnt; |
| 8874 | char *val_beg, *val_end; |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8875 | char *sol; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8876 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8877 | if (!args || args->type != ARGT_STR) |
Willy Tarreau | 34db108 | 2012-04-19 17:16:54 +0200 | [diff] [blame] | 8878 | return 0; |
| 8879 | |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8880 | CHECK_HTTP_MESSAGE_FIRST(); |
| 8881 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8882 | if ((opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) { |
Willy Tarreau | e333ec9 | 2012-04-16 16:26:40 +0200 | [diff] [blame] | 8883 | msg = &txn->req; |
| 8884 | hdr_name = "Cookie"; |
| 8885 | hdr_name_len = 6; |
| 8886 | } else { |
| 8887 | msg = &txn->rsp; |
| 8888 | hdr_name = "Set-Cookie"; |
| 8889 | hdr_name_len = 10; |
| 8890 | } |
| 8891 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 8892 | sol = msg->chn->buf->p; |
Willy Tarreau | 46787ed | 2012-04-11 17:28:40 +0200 | [diff] [blame] | 8893 | val_end = val_beg = NULL; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8894 | ctx.idx = 0; |
| 8895 | cnt = 0; |
| 8896 | |
| 8897 | while (1) { |
| 8898 | /* Note: val_beg == NULL every time we need to fetch a new header */ |
| 8899 | if (!val_beg) { |
| 8900 | if (!http_find_header2(hdr_name, hdr_name_len, sol, idx, &ctx)) |
| 8901 | break; |
| 8902 | |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8903 | if (ctx.vlen < args->data.str.len + 1) |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8904 | continue; |
| 8905 | |
| 8906 | val_beg = ctx.line + ctx.val; |
| 8907 | val_end = val_beg + ctx.vlen; |
| 8908 | } |
| 8909 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8910 | smp->type = SMP_T_CSTR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8911 | while ((val_beg = extract_cookie_value(val_beg, val_end, |
Willy Tarreau | 24e32d8 | 2012-04-23 23:55:44 +0200 | [diff] [blame] | 8912 | args->data.str.str, args->data.str.len, |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 8913 | (opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ, |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8914 | &smp->data.str.str, |
| 8915 | &smp->data.str.len))) { |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8916 | cnt++; |
| 8917 | } |
| 8918 | } |
| 8919 | |
Willy Tarreau | f853c46 | 2012-04-23 18:53:56 +0200 | [diff] [blame] | 8920 | smp->data.uint = cnt; |
Willy Tarreau | 3740635 | 2012-04-23 16:16:37 +0200 | [diff] [blame] | 8921 | smp->flags |= SMP_F_VOL_HDR; |
Willy Tarreau | 04aa6a9 | 2012-04-06 18:57:55 +0200 | [diff] [blame] | 8922 | return 1; |
| 8923 | } |
| 8924 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 8925 | /* Fetch an cookie's integer value. The integer value is returned. It |
| 8926 | * takes a mandatory argument of type string. It relies on smp_fetch_cookie(). |
| 8927 | */ |
| 8928 | static int |
| 8929 | smp_fetch_cookie_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 8930 | const struct arg *args, struct sample *smp) |
| 8931 | { |
| 8932 | int ret = smp_fetch_cookie(px, l4, l7, opt, args, smp); |
| 8933 | |
| 8934 | if (ret > 0) { |
| 8935 | smp->type = SMP_T_UINT; |
| 8936 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 8937 | } |
| 8938 | |
| 8939 | return ret; |
| 8940 | } |
| 8941 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8942 | /************************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 8943 | /* The code below is dedicated to sample fetches */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8944 | /************************************************************************/ |
| 8945 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8946 | /* |
| 8947 | * Given a path string and its length, find the position of beginning of the |
| 8948 | * query string. Returns NULL if no query string is found in the path. |
| 8949 | * |
| 8950 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 8951 | * |
| 8952 | * find_query_string(path, n) points to "yo=mama;ye=daddy" string. |
| 8953 | */ |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 8954 | 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] | 8955 | { |
| 8956 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 8957 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 8958 | p = memchr(path, delim, path_l); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8959 | return p ? p + 1 : NULL; |
| 8960 | } |
| 8961 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 8962 | static inline int is_param_delimiter(char c, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8963 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 8964 | return c == '&' || c == ';' || c == delim; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8965 | } |
| 8966 | |
| 8967 | /* |
| 8968 | * Given a url parameter, find the starting position of the first occurence, |
| 8969 | * or NULL if the parameter is not found. |
| 8970 | * |
| 8971 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 8972 | * the function will return query_string+8. |
| 8973 | */ |
| 8974 | static char* |
| 8975 | find_url_param_pos(char* query_string, size_t query_string_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 8976 | char* url_param_name, size_t url_param_name_l, |
| 8977 | char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8978 | { |
| 8979 | char *pos, *last; |
| 8980 | |
| 8981 | pos = query_string; |
| 8982 | last = query_string + query_string_l - url_param_name_l - 1; |
| 8983 | |
| 8984 | while (pos <= last) { |
| 8985 | if (pos[url_param_name_l] == '=') { |
| 8986 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) |
| 8987 | return pos; |
| 8988 | pos += url_param_name_l + 1; |
| 8989 | } |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 8990 | while (pos <= last && !is_param_delimiter(*pos, delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8991 | pos++; |
| 8992 | pos++; |
| 8993 | } |
| 8994 | return NULL; |
| 8995 | } |
| 8996 | |
| 8997 | /* |
| 8998 | * Given a url parameter name, returns its value and size into *value and |
| 8999 | * *value_l respectively, and returns non-zero. If the parameter is not found, |
| 9000 | * zero is returned and value/value_l are not touched. |
| 9001 | */ |
| 9002 | static int |
| 9003 | find_url_param_value(char* path, size_t path_l, |
| 9004 | char* url_param_name, size_t url_param_name_l, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9005 | char** value, int* value_l, char delim) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9006 | { |
| 9007 | char *query_string, *qs_end; |
| 9008 | char *arg_start; |
| 9009 | char *value_start, *value_end; |
| 9010 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9011 | query_string = find_param_list(path, path_l, delim); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9012 | if (!query_string) |
| 9013 | return 0; |
| 9014 | |
| 9015 | qs_end = path + path_l; |
| 9016 | arg_start = find_url_param_pos(query_string, qs_end - query_string, |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9017 | url_param_name, url_param_name_l, |
| 9018 | delim); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9019 | if (!arg_start) |
| 9020 | return 0; |
| 9021 | |
| 9022 | value_start = arg_start + url_param_name_l + 1; |
| 9023 | value_end = value_start; |
| 9024 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9025 | while ((value_end < qs_end) && !is_param_delimiter(*value_end, delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9026 | value_end++; |
| 9027 | |
| 9028 | *value = value_start; |
| 9029 | *value_l = value_end - value_start; |
Willy Tarreau | 0013433 | 2011-01-04 14:57:34 +0100 | [diff] [blame] | 9030 | return value_end != value_start; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9031 | } |
| 9032 | |
| 9033 | static int |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 9034 | smp_fetch_url_param(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 9035 | const struct arg *args, struct sample *smp) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9036 | { |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9037 | char delim = '?'; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9038 | struct http_txn *txn = l7; |
| 9039 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 9040 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9041 | if (!args || args[0].type != ARGT_STR || |
| 9042 | (args[1].type && args[1].type != ARGT_STR)) |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 9043 | return 0; |
| 9044 | |
| 9045 | CHECK_HTTP_MESSAGE_FIRST(); |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9046 | |
bedis | 4c75cca | 2012-10-05 08:38:24 +0200 | [diff] [blame] | 9047 | if (args[1].type) |
| 9048 | delim = *args[1].data.str.str; |
| 9049 | |
Willy Tarreau | 9b28e03 | 2012-10-12 23:49:43 +0200 | [diff] [blame] | 9050 | 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] | 9051 | args->data.str.str, args->data.str.len, |
| 9052 | &smp->data.str.str, &smp->data.str.len, |
| 9053 | delim)) |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9054 | return 0; |
| 9055 | |
Willy Tarreau | b8c8f1f | 2012-04-23 22:38:26 +0200 | [diff] [blame] | 9056 | smp->type = SMP_T_CSTR; |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 9057 | smp->flags = SMP_F_VOL_1ST; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 9058 | return 1; |
| 9059 | } |
| 9060 | |
Willy Tarreau | a9fddca | 2012-07-31 07:51:48 +0200 | [diff] [blame] | 9061 | /* Return the signed integer value for the specified url parameter (see url_param |
| 9062 | * above). |
| 9063 | */ |
| 9064 | static int |
| 9065 | smp_fetch_url_param_val(struct proxy *px, struct session *l4, void *l7, unsigned int opt, |
| 9066 | const struct arg *args, struct sample *smp) |
| 9067 | { |
| 9068 | int ret = smp_fetch_url_param(px, l4, l7, opt, args, smp); |
| 9069 | |
| 9070 | if (ret > 0) { |
| 9071 | smp->type = SMP_T_UINT; |
| 9072 | smp->data.uint = strl2ic(smp->data.str.str, smp->data.str.len); |
| 9073 | } |
| 9074 | |
| 9075 | return ret; |
| 9076 | } |
| 9077 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9078 | /* This function is used to validate the arguments passed to any "hdr" fetch |
| 9079 | * keyword. These keywords support an optional positive or negative occurrence |
| 9080 | * number. We must ensure that the number is greater than -MAX_HDR_HISTORY. It |
| 9081 | * is assumed that the types are already the correct ones. Returns 0 on error, |
| 9082 | * non-zero if OK. If <err> is not NULL, it will be filled with a pointer to an |
| 9083 | * error message in case of error, that the caller is responsible for freeing. |
| 9084 | * The initial location must either be freeable or NULL. |
| 9085 | */ |
| 9086 | static int val_hdr(struct arg *arg, char **err_msg) |
| 9087 | { |
| 9088 | 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] | 9089 | memprintf(err_msg, "header occurrence must be >= %d", -MAX_HDR_HISTORY); |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9090 | return 0; |
| 9091 | } |
| 9092 | return 1; |
| 9093 | } |
| 9094 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 9095 | /************************************************************************/ |
Willy Tarreau | 25c1ebc | 2012-04-25 16:21:44 +0200 | [diff] [blame] | 9096 | /* All supported ACL keywords must be declared here. */ |
| 9097 | /************************************************************************/ |
| 9098 | |
| 9099 | /* Note: must not be declared <const> as its list will be overwritten. |
| 9100 | * Please take care of keeping this list alphabetically sorted. |
| 9101 | */ |
| 9102 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | a7ad50c | 2012-04-29 15:39:40 +0200 | [diff] [blame] | 9103 | { "base", acl_parse_str, smp_fetch_base, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 }, |
| 9104 | { "base_beg", acl_parse_str, smp_fetch_base, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9105 | { "base_dir", acl_parse_str, smp_fetch_base, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9106 | { "base_dom", acl_parse_str, smp_fetch_base, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9107 | { "base_end", acl_parse_str, smp_fetch_base, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9108 | { "base_len", acl_parse_int, smp_fetch_base, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9109 | { "base_reg", acl_parse_reg, smp_fetch_base, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9110 | { "base_sub", acl_parse_str, smp_fetch_base, acl_match_sub, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9111 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 9112 | { "cook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) }, |
| 9113 | { "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] | 9114 | { "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] | 9115 | { "cook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
| 9116 | { "cook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
| 9117 | { "cook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
| 9118 | { "cook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
| 9119 | { "cook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
| 9120 | { "cook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
| 9121 | { "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] | 9122 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9123 | { "hdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr }, |
| 9124 | { "hdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 9125 | { "hdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7REQ_VOLATILE, ARG1(0,STR) }, |
| 9126 | { "hdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 9127 | { "hdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 9128 | { "hdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 9129 | { "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 }, |
| 9130 | { "hdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 9131 | { "hdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 9132 | { "hdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 9133 | { "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] | 9134 | |
| 9135 | { "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] | 9136 | { "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] | 9137 | { "http_first_req", acl_parse_nothing, acl_fetch_http_first_req, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 }, |
| 9138 | |
| 9139 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT, 0 }, |
| 9140 | |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 9141 | { "path", acl_parse_str, smp_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 }, |
| 9142 | { "path_beg", acl_parse_str, smp_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9143 | { "path_dir", acl_parse_str, smp_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9144 | { "path_dom", acl_parse_str, smp_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9145 | { "path_end", acl_parse_str, smp_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9146 | { "path_len", acl_parse_int, smp_fetch_path, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9147 | { "path_reg", acl_parse_reg, smp_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9148 | { "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] | 9149 | |
| 9150 | { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT, 0 }, |
| 9151 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 }, |
| 9152 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, 0 }, |
| 9153 | |
Willy Tarreau | 5153936 | 2012-05-08 12:46:28 +0200 | [diff] [blame] | 9154 | { "scook", acl_parse_str, smp_fetch_cookie, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG1(0,STR) }, |
| 9155 | { "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] | 9156 | { "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] | 9157 | { "scook_dir", acl_parse_str, smp_fetch_cookie, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
| 9158 | { "scook_dom", acl_parse_str, smp_fetch_cookie, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
| 9159 | { "scook_end", acl_parse_str, smp_fetch_cookie, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
| 9160 | { "scook_len", acl_parse_int, smp_fetch_cookie, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
| 9161 | { "scook_reg", acl_parse_reg, smp_fetch_cookie, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
| 9162 | { "scook_sub", acl_parse_str, smp_fetch_cookie, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
| 9163 | { "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] | 9164 | |
Willy Tarreau | 185b5c4 | 2012-04-26 15:11:51 +0200 | [diff] [blame] | 9165 | { "shdr", acl_parse_str, smp_fetch_hdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP, ARG2(0,STR,SINT), val_hdr }, |
| 9166 | { "shdr_beg", acl_parse_str, smp_fetch_hdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 9167 | { "shdr_cnt", acl_parse_int, smp_fetch_hdr_cnt, acl_match_int, ACL_USE_L7RTR_VOLATILE, ARG1(0,STR) }, |
| 9168 | { "shdr_dir", acl_parse_str, smp_fetch_hdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 9169 | { "shdr_dom", acl_parse_str, smp_fetch_hdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 9170 | { "shdr_end", acl_parse_str, smp_fetch_hdr, acl_match_end, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 9171 | { "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 }, |
| 9172 | { "shdr_len", acl_parse_int, smp_fetch_hdr, acl_match_len, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 9173 | { "shdr_reg", acl_parse_reg, smp_fetch_hdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 9174 | { "shdr_sub", acl_parse_str, smp_fetch_hdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE, ARG2(0,STR,SINT), val_hdr }, |
| 9175 | { "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] | 9176 | |
| 9177 | { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT, 0 }, |
| 9178 | |
Willy Tarreau | 6812bcf | 2012-04-29 09:28:50 +0200 | [diff] [blame] | 9179 | { "url", acl_parse_str, smp_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 }, |
| 9180 | { "url_beg", acl_parse_str, smp_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9181 | { "url_dir", acl_parse_str, smp_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9182 | { "url_dom", acl_parse_str, smp_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9183 | { "url_end", acl_parse_str, smp_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9184 | { "url_ip", acl_parse_ip, smp_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, 0 }, |
| 9185 | { "url_len", acl_parse_int, smp_fetch_url, acl_match_len, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9186 | { "url_port", acl_parse_int, smp_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9187 | { "url_reg", acl_parse_reg, smp_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE, 0 }, |
| 9188 | { "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] | 9189 | |
| 9190 | { "urlp", acl_parse_str, smp_fetch_url_param, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) }, |
| 9191 | { "urlp_beg", acl_parse_str, smp_fetch_url_param, acl_match_beg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
| 9192 | { "urlp_dir", acl_parse_str, smp_fetch_url_param, acl_match_dir, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
| 9193 | { "urlp_dom", acl_parse_str, smp_fetch_url_param, acl_match_dom, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
| 9194 | { "urlp_end", acl_parse_str, smp_fetch_url_param, acl_match_end, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
| 9195 | { "urlp_ip", acl_parse_ip, smp_fetch_url_param, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP, ARG1(1,STR) }, |
| 9196 | { "urlp_len", acl_parse_int, smp_fetch_url_param, acl_match_len, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
| 9197 | { "urlp_reg", acl_parse_reg, smp_fetch_url_param, acl_match_reg, ACL_USE_L7REQ_VOLATILE, ARG1(1,STR) }, |
| 9198 | { "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] | 9199 | { "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] | 9200 | |
| 9201 | { NULL, NULL, NULL, NULL }, |
| 9202 | }}; |
| 9203 | |
| 9204 | /************************************************************************/ |
| 9205 | /* All supported pattern keywords must be declared here. */ |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 9206 | /************************************************************************/ |
| 9207 | /* Note: must not be declared <const> as its list will be overwritten */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 9208 | static struct sample_fetch_kw_list sample_fetch_keywords = {{ },{ |
Willy Tarreau | 1b6c00c | 2012-10-05 22:41:26 +0200 | [diff] [blame] | 9209 | { "hdr", smp_fetch_hdr, ARG2(1,STR,SINT), val_hdr, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ }, |
| 9210 | { "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] | 9211 | { "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] | 9212 | { "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] | 9213 | { "path", smp_fetch_path, 0, NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ }, |
| 9214 | { "url", smp_fetch_url, 0, NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ }, |
| 9215 | { "url_ip", smp_fetch_url_ip, 0, NULL, SMP_T_IPV4, SMP_CAP_L7|SMP_CAP_REQ }, |
| 9216 | { "url_port", smp_fetch_url_port, 0, NULL, SMP_T_UINT, SMP_CAP_L7|SMP_CAP_REQ }, |
| 9217 | { "url_param", smp_fetch_url_param, ARG2(1,STR,STR), NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ }, |
| 9218 | { "cookie", smp_fetch_cookie, ARG1(1,STR), NULL, SMP_T_CSTR, SMP_CAP_L7|SMP_CAP_REQ|SMP_CAP_RES }, |
| 9219 | { "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] | 9220 | { NULL, NULL, 0, 0, 0 }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 9221 | }}; |
| 9222 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9223 | |
| 9224 | __attribute__((constructor)) |
| 9225 | static void __http_protocol_init(void) |
| 9226 | { |
| 9227 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 9228 | sample_register_fetches(&sample_fetch_keywords); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 9229 | } |
| 9230 | |
| 9231 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 9232 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 9233 | * Local variables: |
| 9234 | * c-indent-level: 8 |
| 9235 | * c-basic-offset: 8 |
| 9236 | * End: |
| 9237 | */ |