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 | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 26 | #include <common/appsession.h> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 27 | #include <common/base64.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 28 | #include <common/compat.h> |
| 29 | #include <common/config.h> |
Willy Tarreau | a4cd1f5 | 2006-12-16 19:57:26 +0100 | [diff] [blame] | 30 | #include <common/debug.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 31 | #include <common/memory.h> |
| 32 | #include <common/mini-clist.h> |
| 33 | #include <common/standard.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 34 | #include <common/ticks.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 35 | #include <common/time.h> |
| 36 | #include <common/uri_auth.h> |
| 37 | #include <common/version.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 38 | |
| 39 | #include <types/capture.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 40 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 41 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 42 | #include <proto/acl.h> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 43 | #include <proto/auth.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 44 | #include <proto/backend.h> |
| 45 | #include <proto/buffers.h> |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 46 | #include <proto/checks.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 47 | #include <proto/dumpstats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 48 | #include <proto/fd.h> |
Willy Tarreau | 03fa5df | 2010-05-24 21:02:37 +0200 | [diff] [blame] | 49 | #include <proto/frontend.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 50 | #include <proto/log.h> |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 51 | #include <proto/hdr_idx.h> |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 52 | #include <proto/pattern.h> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 53 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 54 | #include <proto/proto_http.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 55 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 56 | #include <proto/queue.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 57 | #include <proto/server.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 58 | #include <proto/session.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 59 | #include <proto/stream_interface.h> |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 60 | #include <proto/stream_sock.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 61 | #include <proto/task.h> |
| 62 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 63 | const char HTTP_100[] = |
| 64 | "HTTP/1.1 100 Continue\r\n\r\n"; |
| 65 | |
| 66 | const struct chunk http_100_chunk = { |
| 67 | .str = (char *)&HTTP_100, |
| 68 | .len = sizeof(HTTP_100)-1 |
| 69 | }; |
| 70 | |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 71 | /* This is used by remote monitoring */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 72 | const char HTTP_200[] = |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 73 | "HTTP/1.0 200 OK\r\n" |
| 74 | "Cache-Control: no-cache\r\n" |
| 75 | "Connection: close\r\n" |
| 76 | "Content-Type: text/html\r\n" |
| 77 | "\r\n" |
Willy Tarreau | 027a85b | 2011-05-11 16:00:54 +0200 | [diff] [blame] | 78 | "<html><body><h1>200 OK</h1>\nService ready.\n</body></html>\n"; |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 79 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 80 | const struct chunk http_200_chunk = { |
| 81 | .str = (char *)&HTTP_200, |
| 82 | .len = sizeof(HTTP_200)-1 |
| 83 | }; |
| 84 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 85 | /* Warning: no "connection" header is provided with the 3xx messages below */ |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 86 | const char *HTTP_301 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 87 | "HTTP/1.1 301 Moved Permanently\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 88 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 89 | "Content-length: 0\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 90 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 91 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 92 | const char *HTTP_302 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 93 | "HTTP/1.1 302 Found\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 94 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 95 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 96 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 97 | |
| 98 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 99 | const char *HTTP_303 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 100 | "HTTP/1.1 303 See Other\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 101 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 102 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 103 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 104 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 105 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 106 | const char *HTTP_401_fmt = |
| 107 | "HTTP/1.0 401 Unauthorized\r\n" |
| 108 | "Cache-Control: no-cache\r\n" |
| 109 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 110 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 111 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 112 | "\r\n" |
| 113 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 114 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 115 | const char *HTTP_407_fmt = |
| 116 | "HTTP/1.0 407 Unauthorized\r\n" |
| 117 | "Cache-Control: no-cache\r\n" |
| 118 | "Connection: close\r\n" |
| 119 | "Content-Type: text/html\r\n" |
| 120 | "Proxy-Authenticate: Basic realm=\"%s\"\r\n" |
| 121 | "\r\n" |
| 122 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 123 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 124 | |
| 125 | const int http_err_codes[HTTP_ERR_SIZE] = { |
| 126 | [HTTP_ERR_400] = 400, |
| 127 | [HTTP_ERR_403] = 403, |
| 128 | [HTTP_ERR_408] = 408, |
| 129 | [HTTP_ERR_500] = 500, |
| 130 | [HTTP_ERR_502] = 502, |
| 131 | [HTTP_ERR_503] = 503, |
| 132 | [HTTP_ERR_504] = 504, |
| 133 | }; |
| 134 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 135 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 136 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 137 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 138 | "Cache-Control: no-cache\r\n" |
| 139 | "Connection: close\r\n" |
| 140 | "Content-Type: text/html\r\n" |
| 141 | "\r\n" |
| 142 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 143 | |
| 144 | [HTTP_ERR_403] = |
| 145 | "HTTP/1.0 403 Forbidden\r\n" |
| 146 | "Cache-Control: no-cache\r\n" |
| 147 | "Connection: close\r\n" |
| 148 | "Content-Type: text/html\r\n" |
| 149 | "\r\n" |
| 150 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 151 | |
| 152 | [HTTP_ERR_408] = |
| 153 | "HTTP/1.0 408 Request Time-out\r\n" |
| 154 | "Cache-Control: no-cache\r\n" |
| 155 | "Connection: close\r\n" |
| 156 | "Content-Type: text/html\r\n" |
| 157 | "\r\n" |
| 158 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 159 | |
| 160 | [HTTP_ERR_500] = |
| 161 | "HTTP/1.0 500 Server Error\r\n" |
| 162 | "Cache-Control: no-cache\r\n" |
| 163 | "Connection: close\r\n" |
| 164 | "Content-Type: text/html\r\n" |
| 165 | "\r\n" |
| 166 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 167 | |
| 168 | [HTTP_ERR_502] = |
| 169 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 170 | "Cache-Control: no-cache\r\n" |
| 171 | "Connection: close\r\n" |
| 172 | "Content-Type: text/html\r\n" |
| 173 | "\r\n" |
| 174 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 175 | |
| 176 | [HTTP_ERR_503] = |
| 177 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 178 | "Cache-Control: no-cache\r\n" |
| 179 | "Connection: close\r\n" |
| 180 | "Content-Type: text/html\r\n" |
| 181 | "\r\n" |
| 182 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 183 | |
| 184 | [HTTP_ERR_504] = |
| 185 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 186 | "Cache-Control: no-cache\r\n" |
| 187 | "Connection: close\r\n" |
| 188 | "Content-Type: text/html\r\n" |
| 189 | "\r\n" |
| 190 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 191 | |
| 192 | }; |
| 193 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 194 | /* We must put the messages here since GCC cannot initialize consts depending |
| 195 | * on strlen(). |
| 196 | */ |
| 197 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 198 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 199 | #define FD_SETS_ARE_BITFIELDS |
| 200 | #ifdef FD_SETS_ARE_BITFIELDS |
| 201 | /* |
| 202 | * This map is used with all the FD_* macros to check whether a particular bit |
| 203 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 204 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 205 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 206 | * exclusively to the macros. |
| 207 | */ |
| 208 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 209 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 210 | |
| 211 | #else |
| 212 | #error "Check if your OS uses bitfields for fd_sets" |
| 213 | #endif |
| 214 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 215 | void init_proto_http() |
| 216 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 217 | int i; |
| 218 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 219 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 220 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 221 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 222 | if (!http_err_msgs[msg]) { |
| 223 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 224 | abort(); |
| 225 | } |
| 226 | |
| 227 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 228 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 229 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 230 | |
| 231 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 232 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 233 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 234 | * printable characters above. |
| 235 | */ |
| 236 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 237 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 238 | for (i = 0; i < 32; i++) { |
| 239 | FD_SET(i, hdr_encode_map); |
| 240 | FD_SET(i, url_encode_map); |
| 241 | } |
| 242 | for (i = 127; i < 256; i++) { |
| 243 | FD_SET(i, hdr_encode_map); |
| 244 | FD_SET(i, url_encode_map); |
| 245 | } |
| 246 | |
| 247 | tmp = "\"#{|}"; |
| 248 | while (*tmp) { |
| 249 | FD_SET(*tmp, hdr_encode_map); |
| 250 | tmp++; |
| 251 | } |
| 252 | |
| 253 | tmp = "\"#"; |
| 254 | while (*tmp) { |
| 255 | FD_SET(*tmp, url_encode_map); |
| 256 | tmp++; |
| 257 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 258 | |
| 259 | /* memory allocations */ |
| 260 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 261 | pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 262 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 263 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 264 | /* |
| 265 | * We have 26 list of methods (1 per first letter), each of which can have |
| 266 | * up to 3 entries (2 valid, 1 null). |
| 267 | */ |
| 268 | struct http_method_desc { |
| 269 | http_meth_t meth; |
| 270 | int len; |
| 271 | const char text[8]; |
| 272 | }; |
| 273 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 274 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 275 | ['C' - 'A'] = { |
| 276 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 277 | }, |
| 278 | ['D' - 'A'] = { |
| 279 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 280 | }, |
| 281 | ['G' - 'A'] = { |
| 282 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 283 | }, |
| 284 | ['H' - 'A'] = { |
| 285 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 286 | }, |
| 287 | ['P' - 'A'] = { |
| 288 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 289 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 290 | }, |
| 291 | ['T' - 'A'] = { |
| 292 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 293 | }, |
| 294 | /* rest is empty like this : |
| 295 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 296 | */ |
| 297 | }; |
| 298 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 299 | /* 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] | 300 | * 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] | 301 | * RFC2616 for those chars. |
| 302 | */ |
| 303 | |
| 304 | const char http_is_spht[256] = { |
| 305 | [' '] = 1, ['\t'] = 1, |
| 306 | }; |
| 307 | |
| 308 | const char http_is_crlf[256] = { |
| 309 | ['\r'] = 1, ['\n'] = 1, |
| 310 | }; |
| 311 | |
| 312 | const char http_is_lws[256] = { |
| 313 | [' '] = 1, ['\t'] = 1, |
| 314 | ['\r'] = 1, ['\n'] = 1, |
| 315 | }; |
| 316 | |
| 317 | const char http_is_sep[256] = { |
| 318 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 319 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 320 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 321 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 322 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 323 | }; |
| 324 | |
| 325 | const char http_is_ctl[256] = { |
| 326 | [0 ... 31] = 1, |
| 327 | [127] = 1, |
| 328 | }; |
| 329 | |
| 330 | /* |
| 331 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 332 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 333 | * them correctly. Instead, define every non-CTL char's status. |
| 334 | */ |
| 335 | const char http_is_token[256] = { |
| 336 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 337 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 338 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 339 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 340 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 341 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 342 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 343 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 344 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 345 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 346 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 347 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 348 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 349 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 350 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 351 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 352 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 353 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 354 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 355 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 356 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 357 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 358 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 359 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 360 | }; |
| 361 | |
| 362 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 363 | /* |
| 364 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 365 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 366 | */ |
| 367 | const char http_is_ver_token[256] = { |
| 368 | ['.'] = 1, ['/'] = 1, |
| 369 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 370 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| 371 | ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| 372 | }; |
| 373 | |
| 374 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 375 | /* |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 376 | * Silent debug that outputs only in strace, using fd #-1. Trash is modified. |
| 377 | */ |
| 378 | #if defined(DEBUG_FSM) |
| 379 | static void http_silent_debug(int line, struct session *s) |
| 380 | { |
| 381 | int size = 0; |
| 382 | size += snprintf(trash + size, sizeof(trash) - size, |
| 383 | "[%04d] req: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p lr=%p sm=%d fw=%ld tf=%08x\n", |
| 384 | line, |
| 385 | s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers, |
| 386 | s->req->data, s->req->size, s->req->l, s->req->w, s->req->r, s->req->lr, s->req->send_max, s->req->to_forward, s->txn.flags); |
| 387 | write(-1, trash, size); |
| 388 | size = 0; |
| 389 | size += snprintf(trash + size, sizeof(trash) - size, |
| 390 | " %04d rep: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p lr=%p sm=%d fw=%ld\n", |
| 391 | line, |
| 392 | s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers, |
| 393 | s->rep->data, s->rep->size, s->rep->l, s->rep->w, s->rep->r, s->rep->lr, s->rep->send_max, s->rep->to_forward); |
| 394 | |
| 395 | write(-1, trash, size); |
| 396 | } |
| 397 | #else |
| 398 | #define http_silent_debug(l,s) do { } while (0) |
| 399 | #endif |
| 400 | |
| 401 | /* |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 402 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 403 | * CRLF. Text length is measured first, so it cannot be NULL. |
| 404 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 405 | * of headers is automatically adjusted. The number of bytes added is returned |
| 406 | * on success, otherwise <0 is returned indicating an error. |
| 407 | */ |
| 408 | int http_header_add_tail(struct buffer *b, struct http_msg *msg, |
| 409 | struct hdr_idx *hdr_idx, const char *text) |
| 410 | { |
| 411 | int bytes, len; |
| 412 | |
| 413 | len = strlen(text); |
| 414 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 415 | if (!bytes) |
| 416 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 417 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 418 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 419 | } |
| 420 | |
| 421 | /* |
| 422 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 423 | * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then |
| 424 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 425 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 426 | * of headers is automatically adjusted. The number of bytes added is returned |
| 427 | * on success, otherwise <0 is returned indicating an error. |
| 428 | */ |
| 429 | int http_header_add_tail2(struct buffer *b, struct http_msg *msg, |
| 430 | struct hdr_idx *hdr_idx, const char *text, int len) |
| 431 | { |
| 432 | int bytes; |
| 433 | |
| 434 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 435 | if (!bytes) |
| 436 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 437 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 438 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 439 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 440 | |
| 441 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 442 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 443 | * If so, returns the position of the first non-space character relative to |
| 444 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 445 | * to return a pointer to the place after the first space. Returns 0 if the |
| 446 | * header name does not match. Checks are case-insensitive. |
| 447 | */ |
| 448 | int http_header_match2(const char *hdr, const char *end, |
| 449 | const char *name, int len) |
| 450 | { |
| 451 | const char *val; |
| 452 | |
| 453 | if (hdr + len >= end) |
| 454 | return 0; |
| 455 | if (hdr[len] != ':') |
| 456 | return 0; |
| 457 | if (strncasecmp(hdr, name, len) != 0) |
| 458 | return 0; |
| 459 | val = hdr + len + 1; |
| 460 | while (val < end && HTTP_IS_SPHT(*val)) |
| 461 | val++; |
| 462 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 463 | return len + 2; /* we may replace starting from second space */ |
| 464 | return val - hdr; |
| 465 | } |
| 466 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 467 | /* Find the end of the header value contained between <s> and <e>. See RFC2616, |
| 468 | * par 2.2 for more information. Note that it requires a valid header to return |
| 469 | * a valid result. This works for headers defined as comma-separated lists. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 470 | */ |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 471 | char *find_hdr_value_end(char *s, const char *e) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 472 | { |
| 473 | int quoted, qdpair; |
| 474 | |
| 475 | quoted = qdpair = 0; |
| 476 | for (; s < e; s++) { |
| 477 | if (qdpair) qdpair = 0; |
Willy Tarreau | 0f7f51f | 2010-08-30 11:06:34 +0200 | [diff] [blame] | 478 | else if (quoted) { |
| 479 | if (*s == '\\') qdpair = 1; |
| 480 | else if (*s == '"') quoted = 0; |
| 481 | } |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 482 | else if (*s == '"') quoted = 1; |
| 483 | else if (*s == ',') return s; |
| 484 | } |
| 485 | return s; |
| 486 | } |
| 487 | |
| 488 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 489 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 490 | * structure holds everything necessary to use the header and find next |
| 491 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 492 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 493 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 494 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 495 | * is NULL when searching for a new values of a header, the current header is |
| 496 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 497 | */ |
| 498 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 499 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 500 | struct hdr_ctx *ctx) |
| 501 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 502 | char *eol, *sov; |
| 503 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 504 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 505 | cur_idx = ctx->idx; |
| 506 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 507 | /* We have previously returned a value, let's search |
| 508 | * another one on the same line. |
| 509 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 510 | sol = ctx->line; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 511 | ctx->del = ctx->val + ctx->vlen; |
| 512 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 513 | eol = sol + idx->v[cur_idx].len; |
| 514 | |
| 515 | if (sov >= eol) |
| 516 | /* no more values in this header */ |
| 517 | goto next_hdr; |
| 518 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 519 | /* values remaining for this header, skip the comma but save it |
| 520 | * for later use (eg: for header deletion). |
| 521 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 522 | sov++; |
| 523 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 524 | sov++; |
| 525 | |
| 526 | goto return_hdr; |
| 527 | } |
| 528 | |
| 529 | /* first request for this header */ |
| 530 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 531 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 532 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 533 | while (cur_idx) { |
| 534 | eol = sol + idx->v[cur_idx].len; |
| 535 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 536 | if (len == 0) { |
| 537 | /* No argument was passed, we want any header. |
| 538 | * To achieve this, we simply build a fake request. */ |
| 539 | while (sol + len < eol && sol[len] != ':') |
| 540 | len++; |
| 541 | name = sol; |
| 542 | } |
| 543 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 544 | if ((len < eol - sol) && |
| 545 | (sol[len] == ':') && |
| 546 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 547 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 548 | sov = sol + len + 1; |
| 549 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 550 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 551 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 552 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 553 | ctx->prev = old_idx; |
| 554 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 555 | ctx->idx = cur_idx; |
| 556 | ctx->val = sov - sol; |
| 557 | |
| 558 | eol = find_hdr_value_end(sov, eol); |
| 559 | ctx->vlen = eol - sov; |
| 560 | return 1; |
| 561 | } |
| 562 | next_hdr: |
| 563 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 564 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 565 | cur_idx = idx->v[cur_idx].next; |
| 566 | } |
| 567 | return 0; |
| 568 | } |
| 569 | |
| 570 | int http_find_header(const char *name, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 571 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 572 | struct hdr_ctx *ctx) |
| 573 | { |
| 574 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 575 | } |
| 576 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 577 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 578 | * the http_find_header functions. The value is removed, as well as surrounding |
| 579 | * commas if any. If the removed value was alone, the whole header is removed. |
| 580 | * The ctx is always updated accordingly, as well as buffer <buf> and HTTP |
| 581 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 582 | * no more header, so any processing may stop. The ctx is always left in a form |
| 583 | * that can be handled by http_find_header2() to find next occurrence. |
| 584 | */ |
| 585 | int http_remove_header2(struct http_msg *msg, struct buffer *buf, |
| 586 | struct hdr_idx *idx, struct hdr_ctx *ctx) |
| 587 | { |
| 588 | int cur_idx = ctx->idx; |
| 589 | char *sol = ctx->line; |
| 590 | struct hdr_idx_elem *hdr; |
| 591 | int delta, skip_comma; |
| 592 | |
| 593 | if (!cur_idx) |
| 594 | return 0; |
| 595 | |
| 596 | hdr = &idx->v[cur_idx]; |
| 597 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen == hdr->len) { |
| 598 | /* This was the only value of the header, we must now remove it entirely. */ |
| 599 | delta = buffer_replace2(buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0); |
| 600 | http_msg_move_end(msg, delta); |
| 601 | idx->used--; |
| 602 | hdr->len = 0; /* unused entry */ |
| 603 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
Willy Tarreau | 5c4784f | 2011-02-12 13:07:35 +0100 | [diff] [blame] | 604 | if (idx->tail == ctx->idx) |
| 605 | idx->tail = ctx->prev; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 606 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
| 607 | ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1; |
| 608 | ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */ |
| 609 | ctx->vlen = 0; |
| 610 | return ctx->idx; |
| 611 | } |
| 612 | |
| 613 | /* This was not the only value of this header. We have to remove between |
| 614 | * ctx->del+1 and ctx->val+ctx->vlen+1 included. If it is the last entry |
| 615 | * of the list, we remove the last separator. |
| 616 | */ |
| 617 | |
| 618 | skip_comma = (ctx->val + ctx->vlen == hdr->len) ? 0 : 1; |
| 619 | delta = buffer_replace2(buf, sol + ctx->del + skip_comma, |
| 620 | sol + ctx->val + ctx->vlen + skip_comma, |
| 621 | NULL, 0); |
| 622 | hdr->len += delta; |
| 623 | http_msg_move_end(msg, delta); |
| 624 | ctx->val = ctx->del; |
| 625 | ctx->vlen = 0; |
| 626 | return ctx->idx; |
| 627 | } |
| 628 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 629 | /* This function handles a server error at the stream interface level. The |
| 630 | * stream interface is assumed to be already in a closed state. An optional |
| 631 | * message is copied into the input buffer, and an HTTP status code stored. |
| 632 | * 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] | 633 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 634 | */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 635 | static void http_server_error(struct session *t, struct stream_interface *si, |
| 636 | int err, int finst, int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 637 | { |
Willy Tarreau | d5fd51c | 2010-01-22 14:17:47 +0100 | [diff] [blame] | 638 | buffer_auto_read(si->ob); |
| 639 | buffer_abort(si->ob); |
| 640 | buffer_auto_close(si->ob); |
| 641 | buffer_erase(si->ob); |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 642 | buffer_auto_close(si->ib); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 643 | buffer_auto_read(si->ib); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 644 | if (status > 0 && msg) { |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 645 | t->txn.status = status; |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 646 | buffer_write(si->ib, msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 647 | } |
| 648 | if (!(t->flags & SN_ERR_MASK)) |
| 649 | t->flags |= err; |
| 650 | if (!(t->flags & SN_FINST_MASK)) |
| 651 | t->flags |= finst; |
| 652 | } |
| 653 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 654 | /* This function returns the appropriate error location for the given session |
| 655 | * and message. |
| 656 | */ |
| 657 | |
| 658 | struct chunk *error_message(struct session *s, int msgnum) |
| 659 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 660 | if (s->be->errmsg[msgnum].str) |
| 661 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 662 | else if (s->fe->errmsg[msgnum].str) |
| 663 | return &s->fe->errmsg[msgnum]; |
| 664 | else |
| 665 | return &http_err_chunks[msgnum]; |
| 666 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 667 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 668 | /* |
| 669 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 670 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 671 | */ |
| 672 | static http_meth_t find_http_meth(const char *str, const int len) |
| 673 | { |
| 674 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 675 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 676 | |
| 677 | m = ((unsigned)*str - 'A'); |
| 678 | |
| 679 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 680 | for (h = http_methods[m]; h->len > 0; h++) { |
| 681 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 682 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 683 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 684 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 685 | }; |
| 686 | return HTTP_METH_OTHER; |
| 687 | } |
| 688 | return HTTP_METH_NONE; |
| 689 | |
| 690 | } |
| 691 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 692 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 693 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 694 | * It is returned otherwise. |
| 695 | */ |
| 696 | static char * |
| 697 | http_get_path(struct http_txn *txn) |
| 698 | { |
| 699 | char *ptr, *end; |
| 700 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 701 | ptr = txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 702 | end = ptr + txn->req.sl.rq.u_l; |
| 703 | |
| 704 | if (ptr >= end) |
| 705 | return NULL; |
| 706 | |
| 707 | /* RFC2616, par. 5.1.2 : |
| 708 | * Request-URI = "*" | absuri | abspath | authority |
| 709 | */ |
| 710 | |
| 711 | if (*ptr == '*') |
| 712 | return NULL; |
| 713 | |
| 714 | if (isalpha((unsigned char)*ptr)) { |
| 715 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 716 | ptr++; |
| 717 | while (ptr < end && |
| 718 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 719 | ptr++; |
| 720 | /* skip '://' */ |
| 721 | if (ptr == end || *ptr++ != ':') |
| 722 | return NULL; |
| 723 | if (ptr == end || *ptr++ != '/') |
| 724 | return NULL; |
| 725 | if (ptr == end || *ptr++ != '/') |
| 726 | return NULL; |
| 727 | } |
| 728 | /* skip [user[:passwd]@]host[:[port]] */ |
| 729 | |
| 730 | while (ptr < end && *ptr != '/') |
| 731 | ptr++; |
| 732 | |
| 733 | if (ptr == end) |
| 734 | return NULL; |
| 735 | |
| 736 | /* OK, we got the '/' ! */ |
| 737 | return ptr; |
| 738 | } |
| 739 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 740 | /* Returns a 302 for a redirectable request. This may only be called just after |
| 741 | * the stream interface has moved to SI_ST_ASS. Unprocessable requests are |
| 742 | * left unchanged and will follow normal proxy processing. |
| 743 | */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 744 | void perform_http_redirect(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 745 | { |
| 746 | struct http_txn *txn; |
| 747 | struct chunk rdr; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 748 | struct server *srv; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 749 | char *path; |
| 750 | int len; |
| 751 | |
| 752 | /* 1: create the response header */ |
| 753 | rdr.len = strlen(HTTP_302); |
| 754 | rdr.str = trash; |
Willy Tarreau | 59e0b0f | 2010-01-09 21:29:23 +0100 | [diff] [blame] | 755 | rdr.size = sizeof(trash); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 756 | memcpy(rdr.str, HTTP_302, rdr.len); |
| 757 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 758 | srv = target_srv(&s->target); |
| 759 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 760 | /* 2: add the server's prefix */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 761 | if (rdr.len + srv->rdr_len > rdr.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 762 | return; |
| 763 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 764 | /* special prefix "/" means don't change URL */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 765 | if (srv->rdr_len != 1 || *srv->rdr_pfx != '/') { |
| 766 | memcpy(rdr.str + rdr.len, srv->rdr_pfx, srv->rdr_len); |
| 767 | rdr.len += srv->rdr_len; |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 768 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 769 | |
| 770 | /* 3: add the request URI */ |
| 771 | txn = &s->txn; |
| 772 | path = http_get_path(txn); |
| 773 | if (!path) |
| 774 | return; |
| 775 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 776 | len = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path; |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 777 | if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 778 | return; |
| 779 | |
| 780 | memcpy(rdr.str + rdr.len, path, len); |
| 781 | rdr.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 782 | |
| 783 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 784 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 785 | rdr.len += 29; |
| 786 | } else { |
| 787 | memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23); |
| 788 | rdr.len += 23; |
| 789 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 790 | |
| 791 | /* prepare to return without error. */ |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame] | 792 | si->shutr(si); |
| 793 | si->shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 794 | si->err_type = SI_ET_NONE; |
| 795 | si->err_loc = NULL; |
| 796 | si->state = SI_ST_CLO; |
| 797 | |
| 798 | /* send the message */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 799 | http_server_error(s, si, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 800 | |
| 801 | /* 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] | 802 | if (srv) |
| 803 | srv_inc_sess_ctr(srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 804 | } |
| 805 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 806 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 807 | * that the server side is closed. Note that err_type is actually a |
| 808 | * bitmask, where almost only aborts may be cumulated with other |
| 809 | * values. We consider that aborted operations are more important |
| 810 | * than timeouts or errors due to the fact that nobody else in the |
| 811 | * logs might explain incomplete retries. All others should avoid |
| 812 | * being cumulated. It should normally not be possible to have multiple |
| 813 | * aborts at once, but just in case, the first one in sequence is reported. |
| 814 | */ |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 815 | void http_return_srv_error(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 816 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 817 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 818 | |
| 819 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 820 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q, |
| 821 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 822 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 823 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C, |
| 824 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 825 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 826 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q, |
| 827 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 828 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 829 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q, |
| 830 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 831 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 832 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C, |
| 833 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 834 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 835 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C, |
| 836 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 837 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 838 | http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C, |
| 839 | 500, error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 840 | } |
| 841 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 842 | extern const char sess_term_cond[8]; |
| 843 | extern const char sess_fin_state[8]; |
| 844 | extern const char *monthname[12]; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 845 | const char sess_cookie[8] = "NIDVEO67"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie, Expired cookie, Old cookie, unknown */ |
| 846 | const char sess_set_cookie[8] = "NPDIRU67"; /* No set-cookie, Set-cookie found and left unchanged (passive), |
| 847 | Set-cookie Deleted, Set-Cookie Inserted, Set-cookie Rewritten, |
| 848 | Set-cookie Updated, unknown, unknown */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 849 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 850 | struct pool_head *pool2_capture; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 851 | |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 852 | void http_sess_clflog(struct session *s) |
| 853 | { |
Cyril Bonté | acd7d63 | 2010-11-01 19:26:02 +0100 | [diff] [blame] | 854 | char pn[INET6_ADDRSTRLEN]; |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 855 | struct proxy *fe = s->fe; |
| 856 | struct proxy *be = s->be; |
| 857 | struct proxy *prx_log; |
| 858 | struct http_txn *txn = &s->txn; |
| 859 | int tolog, level, err; |
| 860 | char *uri, *h; |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 861 | const char *svid; |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 862 | struct tm tm; |
| 863 | static char tmpline[MAX_SYSLOG_LEN]; |
| 864 | int hdr; |
| 865 | size_t w; |
| 866 | int t_request; |
| 867 | |
| 868 | prx_log = fe; |
| 869 | err = (s->flags & (SN_ERR_MASK | SN_REDISP)) || |
Willy Tarreau | ee28de0 | 2010-06-01 09:51:00 +0200 | [diff] [blame] | 870 | (s->req->cons->conn_retries != be->conn_retries) || |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 871 | txn->status >= 500; |
| 872 | |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 873 | if (s->req->prod->addr.c.from.ss_family == AF_INET) |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 874 | inet_ntop(AF_INET, |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 875 | (const void *)&((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_addr, |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 876 | pn, sizeof(pn)); |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 877 | else if (s->req->prod->addr.c.from.ss_family == AF_INET6) |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 878 | inet_ntop(AF_INET6, |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 879 | (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.c.from))->sin6_addr, |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 880 | pn, sizeof(pn)); |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 881 | else |
| 882 | snprintf(pn, sizeof(pn), "unix:%d", s->listener->luid); |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 883 | |
| 884 | get_gmtime(s->logs.accept_date.tv_sec, &tm); |
| 885 | |
| 886 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 887 | tolog = fe->to_log; |
| 888 | |
| 889 | h = tmpline; |
| 890 | |
| 891 | w = snprintf(h, sizeof(tmpline), |
| 892 | "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]", |
| 893 | pn, |
| 894 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 895 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
| 896 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 897 | goto trunc; |
| 898 | h += w; |
| 899 | |
| 900 | if (h >= tmpline + sizeof(tmpline) - 4) |
| 901 | goto trunc; |
| 902 | |
| 903 | *(h++) = ' '; |
| 904 | *(h++) = '\"'; |
| 905 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 906 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 907 | '#', url_encode_map, uri); |
| 908 | *(h++) = '\"'; |
| 909 | |
| 910 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out); |
| 911 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 912 | goto trunc; |
| 913 | h += w; |
| 914 | |
| 915 | if (h >= tmpline + sizeof(tmpline) - 9) |
| 916 | goto trunc; |
| 917 | memcpy(h, " \"-\" \"-\"", 8); |
| 918 | h += 8; |
| 919 | |
| 920 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 921 | " %d %03d", |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 922 | s->req->prod->addr.c.from.ss_family == AF_UNIX ? s->listener->luid : |
| 923 | ntohs((s->req->prod->addr.c.from.ss_family == AF_INET) ? |
| 924 | ((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_port : |
| 925 | ((struct sockaddr_in6 *)&s->req->prod->addr.c.from)->sin6_port), |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 926 | (int)s->logs.accept_date.tv_usec/1000); |
| 927 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 928 | goto trunc; |
| 929 | h += w; |
| 930 | |
| 931 | w = strlen(fe->id); |
| 932 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 933 | goto trunc; |
| 934 | *(h++) = ' '; |
| 935 | *(h++) = '\"'; |
| 936 | memcpy(h, fe->id, w); |
| 937 | h += w; |
| 938 | *(h++) = '\"'; |
| 939 | |
| 940 | w = strlen(be->id); |
| 941 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 942 | goto trunc; |
| 943 | *(h++) = ' '; |
| 944 | *(h++) = '\"'; |
| 945 | memcpy(h, be->id, w); |
| 946 | h += w; |
| 947 | *(h++) = '\"'; |
| 948 | |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 949 | if (!(tolog & LW_SVID)) |
| 950 | svid = "-"; |
Willy Tarreau | 7b7a8e9 | 2011-03-27 19:53:06 +0200 | [diff] [blame] | 951 | else switch (s->target.type) { |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 952 | case TARG_TYPE_SERVER: |
Willy Tarreau | 7b7a8e9 | 2011-03-27 19:53:06 +0200 | [diff] [blame] | 953 | svid = s->target.ptr.s->id; |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 954 | break; |
| 955 | case TARG_TYPE_APPLET: |
Willy Tarreau | 7b7a8e9 | 2011-03-27 19:53:06 +0200 | [diff] [blame] | 956 | svid = s->target.ptr.a->name; |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 957 | break; |
| 958 | default: |
| 959 | svid = "<NOSRV>"; |
| 960 | break; |
| 961 | } |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 962 | |
| 963 | w = strlen(svid); |
| 964 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 965 | goto trunc; |
| 966 | *(h++) = ' '; |
| 967 | *(h++) = '\"'; |
| 968 | memcpy(h, svid, w); |
| 969 | h += w; |
| 970 | *(h++) = '\"'; |
| 971 | |
| 972 | t_request = -1; |
| 973 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 974 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 975 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 976 | " %d %ld %ld %ld %ld", |
| 977 | t_request, |
| 978 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
| 979 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 980 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 981 | s->logs.t_close); |
| 982 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 983 | goto trunc; |
| 984 | h += w; |
| 985 | |
| 986 | if (h >= tmpline + sizeof(tmpline) - 8) |
| 987 | goto trunc; |
| 988 | *(h++) = ' '; |
| 989 | *(h++) = '\"'; |
| 990 | *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT]; |
| 991 | *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT]; |
| 992 | *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 993 | *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-'; |
| 994 | *(h++) = '\"'; |
| 995 | |
| 996 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 997 | " %d %d %d %d %d %ld %ld", |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 998 | actconn, fe->feconn, be->beconn, target_srv(&s->target) ? target_srv(&s->target)->cur_sess : 0, |
Willy Tarreau | ee28de0 | 2010-06-01 09:51:00 +0200 | [diff] [blame] | 999 | (s->req->cons->conn_retries > 0) ? (be->conn_retries - s->req->cons->conn_retries) : be->conn_retries, |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 1000 | s->logs.srv_queue_size, s->logs.prx_queue_size); |
| 1001 | |
| 1002 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 1003 | goto trunc; |
| 1004 | h += w; |
| 1005 | |
| 1006 | if (txn->cli_cookie) { |
| 1007 | w = strlen(txn->cli_cookie); |
| 1008 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 1009 | goto trunc; |
| 1010 | *(h++) = ' '; |
| 1011 | *(h++) = '\"'; |
| 1012 | memcpy(h, txn->cli_cookie, w); |
| 1013 | h += w; |
| 1014 | *(h++) = '\"'; |
| 1015 | } else { |
| 1016 | if (h >= tmpline + sizeof(tmpline) - 5) |
| 1017 | goto trunc; |
| 1018 | memcpy(h, " \"-\"", 4); |
| 1019 | h += 4; |
| 1020 | } |
| 1021 | |
| 1022 | if (txn->srv_cookie) { |
| 1023 | w = strlen(txn->srv_cookie); |
| 1024 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 1025 | goto trunc; |
| 1026 | *(h++) = ' '; |
| 1027 | *(h++) = '\"'; |
| 1028 | memcpy(h, txn->srv_cookie, w); |
| 1029 | h += w; |
| 1030 | *(h++) = '\"'; |
| 1031 | } else { |
| 1032 | if (h >= tmpline + sizeof(tmpline) - 5) |
| 1033 | goto trunc; |
| 1034 | memcpy(h, " \"-\"", 4); |
| 1035 | h += 4; |
| 1036 | } |
| 1037 | |
| 1038 | if ((fe->to_log & LW_REQHDR) && txn->req.cap) { |
| 1039 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 1040 | if (h >= sizeof (tmpline) + tmpline - 4) |
| 1041 | goto trunc; |
Cyril Bonté | 7f2c539 | 2010-03-13 15:15:07 +0100 | [diff] [blame] | 1042 | if (txn->req.cap[hdr] != NULL) { |
| 1043 | *(h++) = ' '; |
| 1044 | *(h++) = '\"'; |
| 1045 | h = encode_string(h, tmpline + sizeof(tmpline) - 2, |
| 1046 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 1047 | *(h++) = '\"'; |
| 1048 | } else { |
| 1049 | memcpy(h, " \"-\"", 4); |
| 1050 | h += 4; |
| 1051 | } |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 1052 | } |
| 1053 | } |
| 1054 | |
| 1055 | if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) { |
| 1056 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 1057 | if (h >= sizeof (tmpline) + tmpline - 4) |
| 1058 | goto trunc; |
Cyril Bonté | 7f2c539 | 2010-03-13 15:15:07 +0100 | [diff] [blame] | 1059 | if (txn->rsp.cap[hdr] != NULL) { |
| 1060 | *(h++) = ' '; |
| 1061 | *(h++) = '\"'; |
| 1062 | h = encode_string(h, tmpline + sizeof(tmpline) - 2, |
| 1063 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 1064 | *(h++) = '\"'; |
| 1065 | } else { |
| 1066 | memcpy(h, " \"-\"", 4); |
| 1067 | h += 4; |
| 1068 | } |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 1069 | } |
| 1070 | } |
| 1071 | |
| 1072 | trunc: |
| 1073 | *h = '\0'; |
| 1074 | |
| 1075 | level = LOG_INFO; |
| 1076 | if (err && (fe->options2 & PR_O2_LOGERRORS)) |
| 1077 | level = LOG_ERR; |
| 1078 | |
| 1079 | send_log(prx_log, level, "%s\n", tmpline); |
| 1080 | |
| 1081 | s->logs.logwait = 0; |
| 1082 | } |
| 1083 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1084 | /* |
| 1085 | * send a log for the session when we have enough info about it. |
| 1086 | * Will not log if the frontend has no log defined. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1087 | */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1088 | void http_sess_log(struct session *s) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1089 | { |
Cyril Bonté | acd7d63 | 2010-11-01 19:26:02 +0100 | [diff] [blame] | 1090 | char pn[INET6_ADDRSTRLEN]; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1091 | struct proxy *fe = s->fe; |
| 1092 | struct proxy *be = s->be; |
| 1093 | struct proxy *prx_log; |
| 1094 | struct http_txn *txn = &s->txn; |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1095 | int tolog, level, err; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1096 | char *uri, *h; |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 1097 | const char *svid; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1098 | struct tm tm; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1099 | static char tmpline[MAX_SYSLOG_LEN]; |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1100 | int t_request; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1101 | int hdr; |
| 1102 | |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1103 | /* if we don't want to log normal traffic, return now */ |
| 1104 | err = (s->flags & (SN_ERR_MASK | SN_REDISP)) || |
Willy Tarreau | ee28de0 | 2010-06-01 09:51:00 +0200 | [diff] [blame] | 1105 | (s->req->cons->conn_retries != be->conn_retries) || |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1106 | txn->status >= 500; |
| 1107 | if (!err && (fe->options2 & PR_O2_NOLOGNORM)) |
| 1108 | return; |
| 1109 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1110 | if (fe->logfac1 < 0 && fe->logfac2 < 0) |
| 1111 | return; |
| 1112 | prx_log = fe; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1113 | |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 1114 | if (prx_log->options2 & PR_O2_CLFLOG) |
| 1115 | return http_sess_clflog(s); |
| 1116 | |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 1117 | if (s->req->prod->addr.c.from.ss_family == AF_INET) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1118 | inet_ntop(AF_INET, |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 1119 | (const void *)&((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_addr, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1120 | pn, sizeof(pn)); |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 1121 | else if (s->req->prod->addr.c.from.ss_family == AF_INET6) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1122 | inet_ntop(AF_INET6, |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 1123 | (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.c.from))->sin6_addr, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1124 | pn, sizeof(pn)); |
| 1125 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 1126 | get_localtime(s->logs.accept_date.tv_sec, &tm); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1127 | |
| 1128 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 1129 | tolog = fe->to_log; |
| 1130 | |
| 1131 | h = tmpline; |
| 1132 | if (fe->to_log & LW_REQHDR && |
| 1133 | txn->req.cap && |
| 1134 | (h < tmpline + sizeof(tmpline) - 10)) { |
| 1135 | *(h++) = ' '; |
| 1136 | *(h++) = '{'; |
| 1137 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 1138 | if (hdr) |
| 1139 | *(h++) = '|'; |
| 1140 | if (txn->req.cap[hdr] != NULL) |
| 1141 | h = encode_string(h, tmpline + sizeof(tmpline) - 7, |
| 1142 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 1143 | } |
| 1144 | *(h++) = '}'; |
| 1145 | } |
| 1146 | |
| 1147 | if (fe->to_log & LW_RSPHDR && |
| 1148 | txn->rsp.cap && |
| 1149 | (h < tmpline + sizeof(tmpline) - 7)) { |
| 1150 | *(h++) = ' '; |
| 1151 | *(h++) = '{'; |
| 1152 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 1153 | if (hdr) |
| 1154 | *(h++) = '|'; |
| 1155 | if (txn->rsp.cap[hdr] != NULL) |
| 1156 | h = encode_string(h, tmpline + sizeof(tmpline) - 4, |
| 1157 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 1158 | } |
| 1159 | *(h++) = '}'; |
| 1160 | } |
| 1161 | |
| 1162 | if (h < tmpline + sizeof(tmpline) - 4) { |
| 1163 | *(h++) = ' '; |
| 1164 | *(h++) = '"'; |
| 1165 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 1166 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 1167 | '#', url_encode_map, uri); |
| 1168 | *(h++) = '"'; |
| 1169 | } |
| 1170 | *h = '\0'; |
| 1171 | |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 1172 | if (!(tolog & LW_SVID)) |
| 1173 | svid = "-"; |
Willy Tarreau | 7b7a8e9 | 2011-03-27 19:53:06 +0200 | [diff] [blame] | 1174 | else switch (s->target.type) { |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 1175 | case TARG_TYPE_SERVER: |
Willy Tarreau | 7b7a8e9 | 2011-03-27 19:53:06 +0200 | [diff] [blame] | 1176 | svid = s->target.ptr.s->id; |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 1177 | break; |
| 1178 | case TARG_TYPE_APPLET: |
Willy Tarreau | 7b7a8e9 | 2011-03-27 19:53:06 +0200 | [diff] [blame] | 1179 | svid = s->target.ptr.a->name; |
Willy Tarreau | 71904a4 | 2011-02-13 14:30:26 +0100 | [diff] [blame] | 1180 | break; |
| 1181 | default: |
| 1182 | svid = "<NOSRV>"; |
| 1183 | break; |
| 1184 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1185 | |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1186 | t_request = -1; |
| 1187 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 1188 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 1189 | |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1190 | level = LOG_INFO; |
| 1191 | if (err && (fe->options2 & PR_O2_LOGERRORS)) |
| 1192 | level = LOG_ERR; |
| 1193 | |
| 1194 | send_log(prx_log, level, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1195 | "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]" |
Willy Tarreau | 1772ece | 2009-04-03 14:49:12 +0200 | [diff] [blame] | 1196 | " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld" |
| 1197 | " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %ld/%ld%s\n", |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 1198 | (s->req->prod->addr.c.from.ss_family == AF_UNIX) ? "unix" : pn, |
| 1199 | (s->req->prod->addr.c.from.ss_family == AF_UNIX) ? s->listener->luid : |
| 1200 | ntohs((s->req->prod->addr.c.from.ss_family == AF_INET) ? |
| 1201 | ((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_port : |
| 1202 | ((struct sockaddr_in6 *)&s->req->prod->addr.c.from)->sin6_port), |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1203 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
Willy Tarreau | 1772ece | 2009-04-03 14:49:12 +0200 | [diff] [blame] | 1204 | tm.tm_hour, tm.tm_min, tm.tm_sec, (int)s->logs.accept_date.tv_usec/1000, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1205 | fe->id, be->id, svid, |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1206 | t_request, |
| 1207 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1208 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 1209 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 1210 | (tolog & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 1211 | txn->status, |
Willy Tarreau | 8b3977f | 2008-01-18 11:16:32 +0100 | [diff] [blame] | 1212 | (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1213 | txn->cli_cookie ? txn->cli_cookie : "-", |
| 1214 | txn->srv_cookie ? txn->srv_cookie : "-", |
| 1215 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 1216 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 1217 | (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 1218 | (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-', |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 1219 | actconn, fe->feconn, be->beconn, target_srv(&s->target) ? target_srv(&s->target)->cur_sess : 0, |
Krzysztof Piotr Oledzki | 25b501a | 2008-01-06 16:36:16 +0100 | [diff] [blame] | 1220 | (s->flags & SN_REDISP)?"+":"", |
Willy Tarreau | ee28de0 | 2010-06-01 09:51:00 +0200 | [diff] [blame] | 1221 | (s->req->cons->conn_retries>0)?(be->conn_retries - s->req->cons->conn_retries):be->conn_retries, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1222 | s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline); |
| 1223 | |
| 1224 | s->logs.logwait = 0; |
| 1225 | } |
| 1226 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1227 | |
| 1228 | /* |
| 1229 | * Capture headers from message starting at <som> according to header list |
| 1230 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 1231 | */ |
| 1232 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1233 | char **cap, struct cap_hdr *cap_hdr) |
| 1234 | { |
| 1235 | char *eol, *sol, *col, *sov; |
| 1236 | int cur_idx; |
| 1237 | struct cap_hdr *h; |
| 1238 | int len; |
| 1239 | |
| 1240 | sol = som + hdr_idx_first_pos(idx); |
| 1241 | cur_idx = hdr_idx_first_idx(idx); |
| 1242 | |
| 1243 | while (cur_idx) { |
| 1244 | eol = sol + idx->v[cur_idx].len; |
| 1245 | |
| 1246 | col = sol; |
| 1247 | while (col < eol && *col != ':') |
| 1248 | col++; |
| 1249 | |
| 1250 | sov = col + 1; |
| 1251 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 1252 | sov++; |
| 1253 | |
| 1254 | for (h = cap_hdr; h; h = h->next) { |
| 1255 | if ((h->namelen == col - sol) && |
| 1256 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1257 | if (cap[h->index] == NULL) |
| 1258 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1259 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1260 | |
| 1261 | if (cap[h->index] == NULL) { |
| 1262 | Alert("HTTP capture : out of memory.\n"); |
| 1263 | continue; |
| 1264 | } |
| 1265 | |
| 1266 | len = eol - sov; |
| 1267 | if (len > h->len) |
| 1268 | len = h->len; |
| 1269 | |
| 1270 | memcpy(cap[h->index], sov, len); |
| 1271 | cap[h->index][len]=0; |
| 1272 | } |
| 1273 | } |
| 1274 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1275 | cur_idx = idx->v[cur_idx].next; |
| 1276 | } |
| 1277 | } |
| 1278 | |
| 1279 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1280 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1281 | */ |
| 1282 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1283 | |
| 1284 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1285 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1286 | */ |
| 1287 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1288 | ptr++; \ |
| 1289 | if (likely(ptr < end)) \ |
| 1290 | goto good; \ |
| 1291 | else { \ |
| 1292 | state = (st); \ |
| 1293 | goto http_msg_ood; \ |
| 1294 | } \ |
| 1295 | } while (0) |
| 1296 | |
| 1297 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1298 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1299 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1300 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1301 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1302 | * will give undefined results. |
| 1303 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1304 | * and that msg->sol points to the beginning of the response. |
| 1305 | * If a complete line is found (which implies that at least one CR or LF is |
| 1306 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1307 | * returned indicating an incomplete line (which does not mean that parts have |
| 1308 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1309 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1310 | * upon next call. |
| 1311 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1312 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1313 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1314 | * 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] | 1315 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1316 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1317 | const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, |
| 1318 | unsigned int state, const char *ptr, const char *end, |
| 1319 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1320 | { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1321 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1322 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1323 | http_msg_rpver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1324 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1325 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1326 | |
| 1327 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1328 | msg->sl.st.v_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1329 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1330 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1331 | state = HTTP_MSG_ERROR; |
| 1332 | break; |
| 1333 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1334 | case HTTP_MSG_RPVER_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1335 | http_msg_rpver_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1336 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1337 | msg->sl.st.c = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1338 | goto http_msg_rpcode; |
| 1339 | } |
| 1340 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1341 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1342 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1343 | state = HTTP_MSG_ERROR; |
| 1344 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1345 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1346 | case HTTP_MSG_RPCODE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1347 | http_msg_rpcode: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1348 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1349 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1350 | |
| 1351 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1352 | msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1353 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1354 | } |
| 1355 | |
| 1356 | /* so it's a CR/LF, so there is no reason phrase */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1357 | msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1358 | http_msg_rsp_reason: |
| 1359 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1360 | msg->sl.st.r = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1361 | msg->sl.st.r_l = 0; |
| 1362 | goto http_msg_rpline_eol; |
| 1363 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1364 | case HTTP_MSG_RPCODE_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1365 | http_msg_rpcode_sp: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1366 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1367 | msg->sl.st.r = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1368 | goto http_msg_rpreason; |
| 1369 | } |
| 1370 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1371 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1372 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1373 | goto http_msg_rsp_reason; |
| 1374 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1375 | case HTTP_MSG_RPREASON: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1376 | http_msg_rpreason: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1377 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1378 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1379 | msg->sl.st.r_l = (ptr - msg_buf) - msg->som - msg->sl.st.r; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1380 | http_msg_rpline_eol: |
| 1381 | /* We have seen the end of line. Note that we do not |
| 1382 | * necessarily have the \n yet, but at least we know that we |
| 1383 | * have EITHER \r OR \n, otherwise the response would not be |
| 1384 | * complete. We can then record the response length and return |
| 1385 | * to the caller which will be able to register it. |
| 1386 | */ |
| 1387 | msg->sl.st.l = ptr - msg->sol; |
| 1388 | return ptr; |
| 1389 | |
| 1390 | #ifdef DEBUG_FULL |
| 1391 | default: |
| 1392 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1393 | exit(1); |
| 1394 | #endif |
| 1395 | } |
| 1396 | |
| 1397 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1398 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1399 | if (ret_state) |
| 1400 | *ret_state = state; |
| 1401 | if (ret_ptr) |
| 1402 | *ret_ptr = (char *)ptr; |
| 1403 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1404 | } |
| 1405 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1406 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1407 | * This function parses a request line between <ptr> and <end>, starting with |
| 1408 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1409 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1410 | * will give undefined results. |
| 1411 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1412 | * and that msg->sol points to the beginning of the request. |
| 1413 | * If a complete line is found (which implies that at least one CR or LF is |
| 1414 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1415 | * returned indicating an incomplete line (which does not mean that parts have |
| 1416 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1417 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1418 | * upon next call. |
| 1419 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1420 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1421 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1422 | * 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] | 1423 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1424 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1425 | const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, |
| 1426 | unsigned int state, const char *ptr, const char *end, |
| 1427 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1428 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1429 | switch (state) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1430 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1431 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1432 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1433 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1434 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1435 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1436 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1437 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1438 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1439 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1440 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1441 | /* HTTP 0.9 request */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1442 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1443 | http_msg_req09_uri: |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1444 | msg->sl.rq.u = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1445 | http_msg_req09_uri_e: |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1446 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1447 | http_msg_req09_ver: |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1448 | msg->sl.rq.v = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1449 | msg->sl.rq.v_l = 0; |
| 1450 | goto http_msg_rqline_eol; |
| 1451 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1452 | state = HTTP_MSG_ERROR; |
| 1453 | break; |
| 1454 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1455 | case HTTP_MSG_RQMETH_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1456 | http_msg_rqmeth_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1457 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1458 | msg->sl.rq.u = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1459 | goto http_msg_rquri; |
| 1460 | } |
| 1461 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1462 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1463 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1464 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1465 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1466 | case HTTP_MSG_RQURI: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1467 | http_msg_rquri: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1468 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1469 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1470 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1471 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1472 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1473 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1474 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1475 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1476 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1477 | goto http_msg_req09_uri_e; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1478 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1479 | case HTTP_MSG_RQURI_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1480 | http_msg_rquri_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1481 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1482 | msg->sl.rq.v = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1483 | goto http_msg_rqver; |
| 1484 | } |
| 1485 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1486 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1487 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1488 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1489 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1490 | case HTTP_MSG_RQVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1491 | http_msg_rqver: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1492 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1493 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1494 | |
| 1495 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1496 | msg->sl.rq.v_l = (ptr - msg_buf) - msg->som - msg->sl.rq.v; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1497 | http_msg_rqline_eol: |
| 1498 | /* We have seen the end of line. Note that we do not |
| 1499 | * necessarily have the \n yet, but at least we know that we |
| 1500 | * have EITHER \r OR \n, otherwise the request would not be |
| 1501 | * complete. We can then record the request length and return |
| 1502 | * to the caller which will be able to register it. |
| 1503 | */ |
| 1504 | msg->sl.rq.l = ptr - msg->sol; |
| 1505 | return ptr; |
| 1506 | } |
| 1507 | |
| 1508 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1509 | state = HTTP_MSG_ERROR; |
| 1510 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1511 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1512 | #ifdef DEBUG_FULL |
| 1513 | default: |
| 1514 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1515 | exit(1); |
| 1516 | #endif |
| 1517 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1518 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1519 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1520 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1521 | if (ret_state) |
| 1522 | *ret_state = state; |
| 1523 | if (ret_ptr) |
| 1524 | *ret_ptr = (char *)ptr; |
| 1525 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1526 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1527 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1528 | /* |
| 1529 | * Returns the data from Authorization header. Function may be called more |
| 1530 | * than once so data is stored in txn->auth_data. When no header is found |
| 1531 | * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid |
| 1532 | * searching again for something we are unable to find anyway. |
| 1533 | */ |
| 1534 | |
| 1535 | char get_http_auth_buff[BUFSIZE]; |
| 1536 | |
| 1537 | int |
| 1538 | get_http_auth(struct session *s) |
| 1539 | { |
| 1540 | |
| 1541 | struct http_txn *txn = &s->txn; |
| 1542 | struct chunk auth_method; |
| 1543 | struct hdr_ctx ctx; |
| 1544 | char *h, *p; |
| 1545 | int len; |
| 1546 | |
| 1547 | #ifdef DEBUG_AUTH |
| 1548 | printf("Auth for session %p: %d\n", s, txn->auth.method); |
| 1549 | #endif |
| 1550 | |
| 1551 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1552 | return 0; |
| 1553 | |
| 1554 | if (txn->auth.method) |
| 1555 | return 1; |
| 1556 | |
| 1557 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1558 | |
| 1559 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1560 | |
| 1561 | if (txn->flags & TX_USE_PX_CONN) { |
| 1562 | h = "Proxy-Authorization"; |
| 1563 | len = strlen(h); |
| 1564 | } else { |
| 1565 | h = "Authorization"; |
| 1566 | len = strlen(h); |
| 1567 | } |
| 1568 | |
| 1569 | if (!http_find_header2(h, len, txn->req.sol, &txn->hdr_idx, &ctx)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1570 | return 0; |
| 1571 | |
| 1572 | h = ctx.line + ctx.val; |
| 1573 | |
| 1574 | p = memchr(h, ' ', ctx.vlen); |
| 1575 | if (!p || p == h) |
| 1576 | return 0; |
| 1577 | |
| 1578 | chunk_initlen(&auth_method, h, 0, p-h); |
| 1579 | chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1); |
| 1580 | |
| 1581 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
| 1582 | |
| 1583 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
| 1584 | get_http_auth_buff, BUFSIZE - 1); |
| 1585 | |
| 1586 | if (len < 0) |
| 1587 | return 0; |
| 1588 | |
| 1589 | |
| 1590 | get_http_auth_buff[len] = '\0'; |
| 1591 | |
| 1592 | p = strchr(get_http_auth_buff, ':'); |
| 1593 | |
| 1594 | if (!p) |
| 1595 | return 0; |
| 1596 | |
| 1597 | txn->auth.user = get_http_auth_buff; |
| 1598 | *p = '\0'; |
| 1599 | txn->auth.pass = p+1; |
| 1600 | |
| 1601 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1602 | return 1; |
| 1603 | } |
| 1604 | |
| 1605 | return 0; |
| 1606 | } |
| 1607 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1608 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1609 | /* |
| 1610 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1611 | * depending on the initial msg->msg_state. It can be preempted everywhere |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1612 | * when data are missing and recalled at the exact same location with no |
| 1613 | * information loss. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1614 | * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1615 | * fields. Note that msg->som and msg->sol will be initialized after completing |
| 1616 | * the first state, so that none of the msg pointers has to be initialized |
| 1617 | * prior to the first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1618 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1619 | void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx) |
| 1620 | { |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1621 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1622 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1623 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1624 | state = msg->msg_state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1625 | ptr = buf->lr; |
| 1626 | end = buf->r; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1627 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1628 | if (unlikely(ptr >= end)) |
| 1629 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1630 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1631 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1632 | /* |
| 1633 | * First, states that are specific to the response only. |
| 1634 | * We check them first so that request and headers are |
| 1635 | * closer to each other (accessed more often). |
| 1636 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1637 | case HTTP_MSG_RPBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1638 | http_msg_rpbefore: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1639 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1640 | /* we have a start of message, but we have to check |
| 1641 | * first if we need to remove some CRLF. We can only |
| 1642 | * do this when send_max=0. |
| 1643 | */ |
| 1644 | char *beg = buf->w + buf->send_max; |
| 1645 | if (beg >= buf->data + buf->size) |
| 1646 | beg -= buf->size; |
| 1647 | if (unlikely(ptr != beg)) { |
| 1648 | if (buf->send_max) |
| 1649 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1650 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1651 | buffer_ignore(buf, ptr - beg); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1652 | } |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1653 | msg->som = ptr - buf->data; |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 1654 | msg->sol = ptr; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1655 | hdr_idx_init(idx); |
| 1656 | state = HTTP_MSG_RPVER; |
| 1657 | goto http_msg_rpver; |
| 1658 | } |
| 1659 | |
| 1660 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1661 | goto http_msg_invalid; |
| 1662 | |
| 1663 | if (unlikely(*ptr == '\n')) |
| 1664 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1665 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1666 | /* stop here */ |
| 1667 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1668 | case HTTP_MSG_RPBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1669 | http_msg_rpbefore_cr: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1670 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1671 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1672 | /* stop here */ |
| 1673 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1674 | case HTTP_MSG_RPVER: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1675 | http_msg_rpver: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1676 | case HTTP_MSG_RPVER_SP: |
| 1677 | case HTTP_MSG_RPCODE: |
| 1678 | case HTTP_MSG_RPCODE_SP: |
| 1679 | case HTTP_MSG_RPREASON: |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1680 | ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1681 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1682 | if (unlikely(!ptr)) |
| 1683 | return; |
| 1684 | |
| 1685 | /* we have a full response and we know that we have either a CR |
| 1686 | * or an LF at <ptr>. |
| 1687 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1688 | //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.st.l, *ptr); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1689 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1690 | |
| 1691 | msg->sol = ptr; |
| 1692 | if (likely(*ptr == '\r')) |
| 1693 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1694 | goto http_msg_rpline_end; |
| 1695 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1696 | case HTTP_MSG_RPLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1697 | http_msg_rpline_end: |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1698 | /* msg->sol must point to the first of CR or LF. */ |
| 1699 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1700 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1701 | /* stop here */ |
| 1702 | |
| 1703 | /* |
| 1704 | * Second, states that are specific to the request only |
| 1705 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1706 | case HTTP_MSG_RQBEFORE: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1707 | http_msg_rqbefore: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1708 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1709 | /* we have a start of message, but we have to check |
| 1710 | * first if we need to remove some CRLF. We can only |
| 1711 | * do this when send_max=0. |
| 1712 | */ |
| 1713 | char *beg = buf->w + buf->send_max; |
| 1714 | if (beg >= buf->data + buf->size) |
| 1715 | beg -= buf->size; |
| 1716 | if (likely(ptr != beg)) { |
| 1717 | if (buf->send_max) |
| 1718 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1719 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1720 | buffer_ignore(buf, ptr - beg); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1721 | } |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1722 | msg->som = ptr - buf->data; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1723 | msg->sol = ptr; |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1724 | /* we will need this when keep-alive will be supported |
| 1725 | hdr_idx_init(idx); |
| 1726 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1727 | state = HTTP_MSG_RQMETH; |
| 1728 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1729 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1730 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1731 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1732 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1733 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1734 | if (unlikely(*ptr == '\n')) |
| 1735 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1736 | 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] | 1737 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1738 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1739 | case HTTP_MSG_RQBEFORE_CR: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1740 | http_msg_rqbefore_cr: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1741 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1742 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1743 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1744 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1745 | case HTTP_MSG_RQMETH: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1746 | http_msg_rqmeth: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1747 | case HTTP_MSG_RQMETH_SP: |
| 1748 | case HTTP_MSG_RQURI: |
| 1749 | case HTTP_MSG_RQURI_SP: |
| 1750 | case HTTP_MSG_RQVER: |
| 1751 | ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1752 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1753 | if (unlikely(!ptr)) |
| 1754 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1755 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1756 | /* we have a full request and we know that we have either a CR |
| 1757 | * or an LF at <ptr>. |
| 1758 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1759 | //fprintf(stderr,"som=%d rq.l=%d *ptr=0x%02x\n", msg->som, msg->sl.rq.l, *ptr); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1760 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1761 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1762 | msg->sol = ptr; |
| 1763 | if (likely(*ptr == '\r')) |
| 1764 | 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] | 1765 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1766 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1767 | case HTTP_MSG_RQLINE_END: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1768 | http_msg_rqline_end: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1769 | /* check for HTTP/0.9 request : no version information available. |
| 1770 | * msg->sol must point to the first of CR or LF. |
| 1771 | */ |
| 1772 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1773 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1774 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1775 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1776 | 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] | 1777 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1778 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1779 | /* |
| 1780 | * Common states below |
| 1781 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1782 | case HTTP_MSG_HDR_FIRST: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1783 | http_msg_hdr_first: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1784 | msg->sol = ptr; |
| 1785 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1786 | goto http_msg_hdr_name; |
| 1787 | } |
| 1788 | |
| 1789 | if (likely(*ptr == '\r')) |
| 1790 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1791 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1792 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1793 | case HTTP_MSG_HDR_NAME: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1794 | http_msg_hdr_name: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1795 | /* assumes msg->sol points to the first char */ |
| 1796 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1797 | 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] | 1798 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1799 | if (likely(*ptr == ':')) { |
| 1800 | msg->col = ptr - buf->data; |
| 1801 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| 1802 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1803 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1804 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1805 | goto http_msg_invalid; |
| 1806 | |
| 1807 | if (msg->err_pos == -1) /* capture error pointer */ |
| 1808 | msg->err_pos = ptr - buf->data; /* >= 0 now */ |
| 1809 | |
| 1810 | /* and we still accept this non-token character */ |
| 1811 | 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] | 1812 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1813 | case HTTP_MSG_HDR_L1_SP: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1814 | http_msg_hdr_l1_sp: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1815 | /* assumes msg->sol points to the first char and msg->col to the colon */ |
| 1816 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1817 | 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] | 1818 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1819 | /* header value can be basically anything except CR/LF */ |
| 1820 | msg->sov = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1821 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1822 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1823 | goto http_msg_hdr_val; |
| 1824 | } |
| 1825 | |
| 1826 | if (likely(*ptr == '\r')) |
| 1827 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1828 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1829 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1830 | case HTTP_MSG_HDR_L1_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1831 | http_msg_hdr_l1_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1832 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1833 | 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] | 1834 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1835 | case HTTP_MSG_HDR_L1_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1836 | http_msg_hdr_l1_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1837 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1838 | /* replace HT,CR,LF with spaces */ |
| 1839 | for (; buf->data+msg->sov < ptr; msg->sov++) |
| 1840 | buf->data[msg->sov] = ' '; |
| 1841 | goto http_msg_hdr_l1_sp; |
| 1842 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1843 | /* we had a header consisting only in spaces ! */ |
| 1844 | msg->eol = buf->data + msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1845 | goto http_msg_complete_header; |
| 1846 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1847 | case HTTP_MSG_HDR_VAL: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1848 | http_msg_hdr_val: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1849 | /* assumes msg->sol points to the first char, msg->col to the |
| 1850 | * colon, and msg->sov points to the first character of the |
| 1851 | * value. |
| 1852 | */ |
| 1853 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1854 | 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] | 1855 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1856 | msg->eol = ptr; |
| 1857 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1858 | * real header end in case it ends with lots of LWS, but is this |
| 1859 | * really needed ? |
| 1860 | */ |
| 1861 | if (likely(*ptr == '\r')) |
| 1862 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1863 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1864 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1865 | case HTTP_MSG_HDR_L2_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1866 | http_msg_hdr_l2_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1867 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1868 | 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] | 1869 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1870 | case HTTP_MSG_HDR_L2_LWS: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1871 | http_msg_hdr_l2_lws: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1872 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1873 | /* LWS: replace HT,CR,LF with spaces */ |
| 1874 | for (; msg->eol < ptr; msg->eol++) |
| 1875 | *msg->eol = ' '; |
| 1876 | goto http_msg_hdr_val; |
| 1877 | } |
| 1878 | http_msg_complete_header: |
| 1879 | /* |
| 1880 | * It was a new header, so the last one is finished. |
| 1881 | * Assumes msg->sol points to the first char, msg->col to the |
| 1882 | * colon, msg->sov points to the first character of the value |
| 1883 | * and msg->eol to the first CR or LF so we know how the line |
| 1884 | * ends. We insert last header into the index. |
| 1885 | */ |
| 1886 | /* |
| 1887 | fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol); |
| 1888 | write(2, msg->sol, msg->eol-msg->sol); |
| 1889 | fprintf(stderr,"\n"); |
| 1890 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1891 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1892 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r', |
| 1893 | idx, idx->tail) < 0)) |
| 1894 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1895 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1896 | msg->sol = ptr; |
| 1897 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1898 | goto http_msg_hdr_name; |
| 1899 | } |
| 1900 | |
| 1901 | if (likely(*ptr == '\r')) |
| 1902 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1903 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1904 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1905 | case HTTP_MSG_LAST_LF: |
Willy Tarreau | e3f284a | 2010-09-28 19:42:42 +0200 | [diff] [blame] | 1906 | http_msg_last_lf: |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1907 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1908 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1909 | ptr++; |
| 1910 | buf->lr = ptr; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1911 | msg->col = msg->sov = buf->lr - buf->data; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1912 | msg->eoh = msg->sol - buf->data; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1913 | msg->sol = buf->data + msg->som; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1914 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1915 | return; |
| 1916 | #ifdef DEBUG_FULL |
| 1917 | default: |
| 1918 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1919 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1920 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1921 | } |
| 1922 | http_msg_ood: |
| 1923 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1924 | msg->msg_state = state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1925 | buf->lr = ptr; |
| 1926 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1927 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1928 | http_msg_invalid: |
| 1929 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1930 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1931 | buf->lr = ptr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1932 | return; |
| 1933 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1934 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1935 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1936 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1937 | * nothing is done and 1 is returned. |
| 1938 | */ |
| 1939 | static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn) |
| 1940 | { |
| 1941 | int delta; |
| 1942 | char *cur_end; |
| 1943 | |
| 1944 | if (msg->sl.rq.v_l != 0) |
| 1945 | return 1; |
| 1946 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1947 | cur_end = msg->sol + msg->sl.rq.l; |
| 1948 | delta = 0; |
| 1949 | |
| 1950 | if (msg->sl.rq.u_l == 0) { |
| 1951 | /* if no URI was set, add "/" */ |
| 1952 | delta = buffer_replace2(req, cur_end, cur_end, " /", 2); |
| 1953 | cur_end += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1954 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1955 | } |
| 1956 | /* add HTTP version */ |
| 1957 | delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11); |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1958 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1959 | cur_end += delta; |
| 1960 | cur_end = (char *)http_parse_reqline(msg, req->data, |
| 1961 | HTTP_MSG_RQMETH, |
| 1962 | msg->sol, cur_end + 1, |
| 1963 | NULL, NULL); |
| 1964 | if (unlikely(!cur_end)) |
| 1965 | return 0; |
| 1966 | |
| 1967 | /* we have a full HTTP/1.0 request now and we know that |
| 1968 | * we have either a CR or an LF at <ptr>. |
| 1969 | */ |
| 1970 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1971 | return 1; |
| 1972 | } |
| 1973 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1974 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
| 1975 | * and "keep-alive" values. If a buffer is provided and we already know that |
| 1976 | * some headers may safely be removed, we remove them now. The <to_del> flags |
| 1977 | * are used for that : |
| 1978 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1979 | * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1). |
| 1980 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1981 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1982 | * harmless combinations may be removed. Do not call that after changes have |
| 1983 | * been processed. If unused, the buffer can be NULL, and no data will be |
| 1984 | * changed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1985 | */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1986 | void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int to_del) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1987 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1988 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1989 | const char *hdr_val = "Connection"; |
| 1990 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1991 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1992 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1993 | return; |
| 1994 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1995 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1996 | hdr_val = "Proxy-Connection"; |
| 1997 | hdr_len = 16; |
| 1998 | } |
| 1999 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2000 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2001 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2002 | while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2003 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 2004 | txn->flags |= TX_HDR_CONN_KAL; |
| 2005 | if ((to_del & 2) && buf) |
| 2006 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
| 2007 | else |
| 2008 | txn->flags |= TX_CON_KAL_SET; |
| 2009 | } |
| 2010 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 2011 | txn->flags |= TX_HDR_CONN_CLO; |
| 2012 | if ((to_del & 1) && buf) |
| 2013 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
| 2014 | else |
| 2015 | txn->flags |= TX_CON_CLO_SET; |
| 2016 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2017 | } |
| 2018 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2019 | txn->flags |= TX_HDR_CONN_PRS; |
| 2020 | return; |
| 2021 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2022 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2023 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 2024 | * added depending on the <wanted> flags, which are exclusively composed of |
| 2025 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 2026 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 2027 | */ |
| 2028 | void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int wanted) |
| 2029 | { |
| 2030 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2031 | const char *hdr_val = "Connection"; |
| 2032 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2033 | |
| 2034 | ctx.idx = 0; |
| 2035 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2036 | |
| 2037 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2038 | hdr_val = "Proxy-Connection"; |
| 2039 | hdr_len = 16; |
| 2040 | } |
| 2041 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2042 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2043 | while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2044 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 2045 | if (wanted & TX_CON_KAL_SET) |
| 2046 | txn->flags |= TX_CON_KAL_SET; |
| 2047 | else |
| 2048 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2049 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2050 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 2051 | if (wanted & TX_CON_CLO_SET) |
| 2052 | txn->flags |= TX_CON_CLO_SET; |
| 2053 | else |
| 2054 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2055 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2056 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2057 | |
| 2058 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 2059 | return; |
| 2060 | |
| 2061 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 2062 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2063 | hdr_val = "Connection: close"; |
| 2064 | hdr_len = 17; |
| 2065 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2066 | hdr_val = "Proxy-Connection: close"; |
| 2067 | hdr_len = 23; |
| 2068 | } |
| 2069 | http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2070 | } |
| 2071 | |
| 2072 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 2073 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2074 | hdr_val = "Connection: keep-alive"; |
| 2075 | hdr_len = 22; |
| 2076 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2077 | hdr_val = "Proxy-Connection: keep-alive"; |
| 2078 | hdr_len = 28; |
| 2079 | } |
| 2080 | http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2081 | } |
| 2082 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2083 | } |
| 2084 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2085 | /* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the |
| 2086 | * first byte of body, and increments msg->sov by the number of bytes parsed, |
| 2087 | * so that we know we can forward between ->som and ->sov. Note that due to |
| 2088 | * possible wrapping at the end of the buffer, it is possible that msg->sov is |
| 2089 | * lower than msg->som. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2090 | * 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] | 2091 | * 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] | 2092 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2093 | int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2094 | { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2095 | char *ptr = buf->lr; |
| 2096 | char *end = buf->data + buf->size; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2097 | unsigned int chunk = 0; |
| 2098 | |
| 2099 | /* The chunk size is in the following form, though we are only |
| 2100 | * interested in the size and CRLF : |
| 2101 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 2102 | */ |
| 2103 | while (1) { |
| 2104 | int c; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2105 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2106 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2107 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2108 | if (c < 0) /* not a hex digit anymore */ |
| 2109 | break; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2110 | if (++ptr >= end) |
| 2111 | ptr = buf->data; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2112 | if (chunk & 0xF000000) /* overflow will occur */ |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2113 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2114 | chunk = (chunk << 4) + c; |
| 2115 | } |
| 2116 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2117 | /* empty size not allowed */ |
| 2118 | if (ptr == buf->lr) |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2119 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2120 | |
| 2121 | while (http_is_spht[(unsigned char)*ptr]) { |
| 2122 | if (++ptr >= end) |
| 2123 | ptr = buf->data; |
| 2124 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2125 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2126 | } |
| 2127 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2128 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 2129 | * for the end of chunk size. |
| 2130 | */ |
| 2131 | while (1) { |
| 2132 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 2133 | /* we now have a CR or an LF at ptr */ |
| 2134 | if (likely(*ptr == '\r')) { |
| 2135 | if (++ptr >= end) |
| 2136 | ptr = buf->data; |
| 2137 | if (ptr == buf->r) |
| 2138 | return 0; |
| 2139 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2140 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2141 | if (*ptr != '\n') |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2142 | goto error; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2143 | if (++ptr >= end) |
| 2144 | ptr = buf->data; |
| 2145 | /* done */ |
| 2146 | break; |
| 2147 | } |
| 2148 | else if (*ptr == ';') { |
| 2149 | /* chunk extension, ends at next CRLF */ |
| 2150 | if (++ptr >= end) |
| 2151 | ptr = buf->data; |
| 2152 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2153 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2154 | |
| 2155 | while (!HTTP_IS_CRLF(*ptr)) { |
| 2156 | if (++ptr >= end) |
| 2157 | ptr = buf->data; |
| 2158 | if (ptr == buf->r) |
| 2159 | return 0; |
| 2160 | } |
| 2161 | /* we have a CRLF now, loop above */ |
| 2162 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2163 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2164 | else |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2165 | goto error; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2166 | } |
| 2167 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2168 | /* OK we found our CRLF and now <ptr> points to the next byte, |
| 2169 | * which may or may not be present. We save that into ->lr and |
| 2170 | * ->sov. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2171 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2172 | msg->sov += ptr - buf->lr; |
| 2173 | buf->lr = ptr; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2174 | msg->chunk_len = chunk; |
| 2175 | msg->body_len += chunk; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2176 | msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2177 | return 1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2178 | error: |
| 2179 | msg->err_pos = ptr - buf->data; |
| 2180 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2181 | } |
| 2182 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2183 | /* This function skips trailers in the buffer <buf> associated with HTTP |
| 2184 | * message <msg>. The first visited position is buf->lr. If the end of |
| 2185 | * the trailers is found, it is automatically scheduled to be forwarded, |
| 2186 | * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0. |
| 2187 | * If not enough data are available, the function does not change anything |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2188 | * except maybe buf->lr and msg->sov if it could parse some lines, and returns |
| 2189 | * zero. If a parse error is encountered, the function returns < 0 and does not |
| 2190 | * change anything except maybe buf->lr and msg->sov. Note that the message |
| 2191 | * must already be in HTTP_MSG_TRAILERS state before calling this function, |
| 2192 | * which implies that all non-trailers data have already been scheduled for |
| 2193 | * forwarding, and that the difference between msg->som and msg->sov exactly |
| 2194 | * matches the length of trailers already parsed and not forwarded. It is also |
| 2195 | * important to note that this function is designed to be able to parse wrapped |
| 2196 | * headers at end of buffer. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2197 | */ |
| 2198 | int http_forward_trailers(struct buffer *buf, struct http_msg *msg) |
| 2199 | { |
| 2200 | /* we have buf->lr which points to next line. Look for CRLF. */ |
| 2201 | while (1) { |
| 2202 | char *p1 = NULL, *p2 = NULL; |
| 2203 | char *ptr = buf->lr; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2204 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2205 | |
| 2206 | /* scan current line and stop at LF or CRLF */ |
| 2207 | while (1) { |
| 2208 | if (ptr == buf->r) |
| 2209 | return 0; |
| 2210 | |
| 2211 | if (*ptr == '\n') { |
| 2212 | if (!p1) |
| 2213 | p1 = ptr; |
| 2214 | p2 = ptr; |
| 2215 | break; |
| 2216 | } |
| 2217 | |
| 2218 | if (*ptr == '\r') { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2219 | if (p1) { |
| 2220 | msg->err_pos = ptr - buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2221 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2222 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2223 | p1 = ptr; |
| 2224 | } |
| 2225 | |
| 2226 | ptr++; |
| 2227 | if (ptr >= buf->data + buf->size) |
| 2228 | ptr = buf->data; |
| 2229 | } |
| 2230 | |
| 2231 | /* after LF; point to beginning of next line */ |
| 2232 | p2++; |
| 2233 | if (p2 >= buf->data + buf->size) |
| 2234 | p2 = buf->data; |
| 2235 | |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2236 | bytes = p2 - buf->lr; |
| 2237 | if (bytes < 0) |
| 2238 | bytes += buf->size; |
| 2239 | |
| 2240 | /* schedule this line for forwarding */ |
| 2241 | msg->sov += bytes; |
| 2242 | if (msg->sov >= buf->size) |
| 2243 | msg->sov -= buf->size; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2244 | |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2245 | if (p1 == buf->lr) { |
| 2246 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 2247 | * Everything was scheduled for forwarding, there's nothing |
| 2248 | * left from this message. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 2249 | */ |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2250 | buf->lr = p2; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2251 | msg->msg_state = HTTP_MSG_DONE; |
| 2252 | return 1; |
| 2253 | } |
| 2254 | /* OK, next line then */ |
| 2255 | buf->lr = p2; |
| 2256 | } |
| 2257 | } |
| 2258 | |
| 2259 | /* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or |
| 2260 | * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov, |
| 2261 | * ->som, buf->lr in order to include this part into the next forwarding phase. |
| 2262 | * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If |
| 2263 | * not enough data are available, the function does not change anything and |
| 2264 | * returns zero. If a parse error is encountered, the function returns < 0 and |
| 2265 | * does not change anything. Note: this function is designed to parse wrapped |
| 2266 | * CRLF at the end of the buffer. |
| 2267 | */ |
| 2268 | int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg) |
| 2269 | { |
| 2270 | char *ptr; |
| 2271 | int bytes; |
| 2272 | |
| 2273 | /* NB: we'll check data availabilty at the end. It's not a |
| 2274 | * problem because whatever we match first will be checked |
| 2275 | * against the correct length. |
| 2276 | */ |
| 2277 | bytes = 1; |
| 2278 | ptr = buf->lr; |
| 2279 | if (*ptr == '\r') { |
| 2280 | bytes++; |
| 2281 | ptr++; |
| 2282 | if (ptr >= buf->data + buf->size) |
| 2283 | ptr = buf->data; |
| 2284 | } |
| 2285 | |
Willy Tarreau | bf3f1de | 2010-03-17 15:54:24 +0100 | [diff] [blame] | 2286 | if (bytes > buf->l - buf->send_max) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2287 | return 0; |
| 2288 | |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2289 | if (*ptr != '\n') { |
| 2290 | msg->err_pos = ptr - buf->data; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2291 | return -1; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 2292 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2293 | |
| 2294 | ptr++; |
| 2295 | if (ptr >= buf->data + buf->size) |
| 2296 | ptr = buf->data; |
| 2297 | buf->lr = ptr; |
| 2298 | /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */ |
| 2299 | msg->sov = ptr - buf->data; |
| 2300 | msg->som = msg->sov - bytes; |
| 2301 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 2302 | return 1; |
| 2303 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2304 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2305 | void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg) |
| 2306 | { |
| 2307 | char *end = buf->data + buf->size; |
| 2308 | int off = buf->data + buf->size - buf->w; |
| 2309 | |
| 2310 | /* two possible cases : |
| 2311 | * - the buffer is in one contiguous block, we move it in-place |
Willy Tarreau | 8096de9 | 2010-02-26 11:12:27 +0100 | [diff] [blame] | 2312 | * - the buffer is in two blocks, we move it via the swap_buffer |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2313 | */ |
| 2314 | if (buf->l) { |
Willy Tarreau | 8096de9 | 2010-02-26 11:12:27 +0100 | [diff] [blame] | 2315 | int block1 = buf->l; |
| 2316 | int block2 = 0; |
| 2317 | if (buf->r <= buf->w) { |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2318 | /* non-contiguous block */ |
Willy Tarreau | 8096de9 | 2010-02-26 11:12:27 +0100 | [diff] [blame] | 2319 | block1 = buf->data + buf->size - buf->w; |
| 2320 | block2 = buf->r - buf->data; |
| 2321 | } |
| 2322 | if (block2) |
| 2323 | memcpy(swap_buffer, buf->data, block2); |
| 2324 | memmove(buf->data, buf->w, block1); |
| 2325 | if (block2) |
| 2326 | memcpy(buf->data + block1, swap_buffer, block2); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2327 | } |
| 2328 | |
| 2329 | /* adjust all known pointers */ |
| 2330 | buf->w = buf->data; |
| 2331 | buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size; |
| 2332 | buf->r += off; if (buf->r >= end) buf->r -= buf->size; |
| 2333 | msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size; |
| 2334 | msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size; |
| 2335 | |
| 2336 | /* adjust relative pointers */ |
| 2337 | msg->som = 0; |
| 2338 | msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size; |
| 2339 | msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size; |
| 2340 | msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size; |
| 2341 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2342 | if (msg->err_pos >= 0) { |
| 2343 | msg->err_pos += off; |
| 2344 | if (msg->err_pos >= buf->size) |
| 2345 | msg->err_pos -= buf->size; |
| 2346 | } |
| 2347 | |
| 2348 | buf->flags &= ~BF_FULL; |
| 2349 | if (buf->l >= buffer_max_len(buf)) |
| 2350 | buf->flags |= BF_FULL; |
| 2351 | } |
| 2352 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2353 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2354 | * processing can continue on next analysers, or zero if it either needs more |
| 2355 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 2356 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers |
| 2357 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2358 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2359 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2360 | int http_wait_for_request(struct session *s, struct buffer *req, int an_bit) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2361 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2362 | /* |
| 2363 | * We will parse the partial (or complete) lines. |
| 2364 | * We will check the request syntax, and also join multi-line |
| 2365 | * headers. An index of all the lines will be elaborated while |
| 2366 | * parsing. |
| 2367 | * |
| 2368 | * For the parsing, we use a 28 states FSM. |
| 2369 | * |
| 2370 | * Here is the information we currently have : |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 2371 | * req->data + msg->som = beginning of request |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2372 | * req->data + msg->eoh = end of processed headers / start of current one |
| 2373 | * msg->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2374 | * req->lr = first non-visited byte |
| 2375 | * req->r = end of data |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2376 | * |
| 2377 | * At end of parsing, we may perform a capture of the error (if any), and |
| 2378 | * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE). |
| 2379 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2380 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2381 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2382 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2383 | int cur_idx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2384 | int use_close_only; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2385 | struct http_txn *txn = &s->txn; |
| 2386 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2387 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2388 | |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2389 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 2390 | now_ms, __FUNCTION__, |
| 2391 | s, |
| 2392 | req, |
| 2393 | req->rex, req->wex, |
| 2394 | req->flags, |
| 2395 | req->l, |
| 2396 | req->analysers); |
| 2397 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2398 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2399 | s->srv_error = http_return_srv_error; |
| 2400 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2401 | /* There's a protected area at the end of the buffer for rewriting |
| 2402 | * purposes. We don't want to start to parse the request if the |
| 2403 | * protected area is affected, because we may have to move processed |
| 2404 | * data later, which is much more complicated. |
| 2405 | */ |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2406 | if (req->l && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2407 | if ((txn->flags & TX_NOT_FIRST) && |
| 2408 | unlikely((req->flags & BF_FULL) || |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2409 | req->r < req->lr || |
| 2410 | req->r > req->data + req->size - global.tune.maxrewrite)) { |
| 2411 | if (req->send_max) { |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 2412 | if (req->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) |
| 2413 | goto failed_keep_alive; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2414 | /* some data has still not left the buffer, wake us once that's done */ |
| 2415 | buffer_dont_connect(req); |
| 2416 | req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 2417 | return 0; |
| 2418 | } |
Willy Tarreau | 0499e35 | 2010-12-17 07:13:42 +0100 | [diff] [blame] | 2419 | if (req->r < req->lr || req->r > req->data + req->size - global.tune.maxrewrite) |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2420 | http_buffer_heavy_realign(req, msg); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2421 | } |
| 2422 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2423 | /* Note that we have the same problem with the response ; we |
| 2424 | * may want to send a redirect, error or anything which requires |
| 2425 | * some spare space. So we'll ensure that we have at least |
| 2426 | * maxrewrite bytes available in the response buffer before |
| 2427 | * processing that one. This will only affect pipelined |
| 2428 | * keep-alive requests. |
| 2429 | */ |
| 2430 | if ((txn->flags & TX_NOT_FIRST) && |
| 2431 | unlikely((s->rep->flags & BF_FULL) || |
| 2432 | s->rep->r < s->rep->lr || |
| 2433 | s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) { |
| 2434 | if (s->rep->send_max) { |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 2435 | if (s->rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) |
| 2436 | goto failed_keep_alive; |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2437 | /* don't let a connection request be initiated */ |
| 2438 | buffer_dont_connect(req); |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2439 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 0499e35 | 2010-12-17 07:13:42 +0100 | [diff] [blame] | 2440 | s->rep->analysers |= an_bit; /* wake us up once it changes */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2441 | return 0; |
| 2442 | } |
| 2443 | } |
| 2444 | |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2445 | if (likely(req->lr < req->r)) |
| 2446 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2447 | } |
| 2448 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2449 | /* 1: we might have to print this header in debug mode */ |
| 2450 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2451 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | c3bfeeb | 2010-04-16 09:14:45 +0200 | [diff] [blame] | 2452 | msg->sol && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2453 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2454 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2455 | |
Willy Tarreau | 663308b | 2010-06-07 14:06:08 +0200 | [diff] [blame] | 2456 | sol = req->data + msg->som; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2457 | eol = sol + msg->sl.rq.l; |
| 2458 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2459 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2460 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2461 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2462 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2463 | while (cur_idx) { |
| 2464 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2465 | debug_hdr("clihdr", s, sol, eol); |
| 2466 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2467 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2468 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2469 | } |
| 2470 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2471 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2472 | /* |
| 2473 | * Now we quickly check if we have found a full valid request. |
| 2474 | * If not so, we check the FD and buffer states before leaving. |
| 2475 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2476 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2477 | * requests are checked first. When waiting for a second request |
| 2478 | * on a keep-alive session, if we encounter and error, close, t/o, |
| 2479 | * we note the error in the session flags but don't set any state. |
| 2480 | * Since the error will be noted there, it will not be counted by |
| 2481 | * process_session() as a frontend error. |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2482 | * Last, we may increase some tracked counters' http request errors on |
| 2483 | * the cases that are deliberately the client's fault. For instance, |
| 2484 | * a timeout or connection reset is not counted as an error. However |
| 2485 | * a bad request is. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2486 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2487 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2488 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2489 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2490 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2491 | */ |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2492 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2493 | session_inc_http_req_ctr(s); |
| 2494 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2495 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2496 | goto return_bad_req; |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2497 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2498 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2499 | /* 1: Since we are in header mode, if there's no space |
| 2500 | * left for headers, we won't be able to free more |
| 2501 | * later, so the session will never terminate. We |
| 2502 | * must terminate it now. |
| 2503 | */ |
| 2504 | if (unlikely(req->flags & BF_FULL)) { |
| 2505 | /* FIXME: check if URI is set and return Status |
| 2506 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2507 | */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2508 | session_inc_http_req_ctr(s); |
| 2509 | session_inc_http_err_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2510 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2511 | goto return_bad_req; |
| 2512 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2513 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2514 | /* 2: have we encountered a read error ? */ |
| 2515 | else if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2516 | if (!(s->flags & SN_ERR_MASK)) |
| 2517 | s->flags |= SN_ERR_CLICL; |
| 2518 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2519 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2520 | goto failed_keep_alive; |
| 2521 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2522 | /* we cannot return any message on error */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2523 | if (msg->err_pos >= 0) { |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 2524 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2525 | session_inc_http_err_ctr(s); |
| 2526 | } |
| 2527 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2528 | msg->msg_state = HTTP_MSG_ERROR; |
| 2529 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2530 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2531 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2532 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2533 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2534 | if (s->listener->counters) |
| 2535 | s->listener->counters->failed_req++; |
| 2536 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2537 | if (!(s->flags & SN_FINST_MASK)) |
| 2538 | s->flags |= SN_FINST_R; |
| 2539 | return 0; |
| 2540 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2541 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2542 | /* 3: has the read timeout expired ? */ |
| 2543 | else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2544 | if (!(s->flags & SN_ERR_MASK)) |
| 2545 | s->flags |= SN_ERR_CLITO; |
| 2546 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2547 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2548 | goto failed_keep_alive; |
| 2549 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2550 | /* read timeout : give up with an error message. */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2551 | if (msg->err_pos >= 0) { |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 2552 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2553 | session_inc_http_err_ctr(s); |
| 2554 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2555 | txn->status = 408; |
| 2556 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
| 2557 | msg->msg_state = HTTP_MSG_ERROR; |
| 2558 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2559 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2560 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2561 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2562 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2563 | if (s->listener->counters) |
| 2564 | s->listener->counters->failed_req++; |
| 2565 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2566 | if (!(s->flags & SN_FINST_MASK)) |
| 2567 | s->flags |= SN_FINST_R; |
| 2568 | return 0; |
| 2569 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2570 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2571 | /* 4: have we encountered a close ? */ |
| 2572 | else if (req->flags & BF_SHUTR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2573 | if (!(s->flags & SN_ERR_MASK)) |
| 2574 | s->flags |= SN_ERR_CLICL; |
| 2575 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2576 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2577 | goto failed_keep_alive; |
| 2578 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2579 | if (msg->err_pos >= 0) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 2580 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2581 | txn->status = 400; |
| 2582 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 2583 | msg->msg_state = HTTP_MSG_ERROR; |
| 2584 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2585 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2586 | session_inc_http_err_ctr(s); |
| 2587 | session_inc_http_req_ctr(s); |
Willy Tarreau | 3e1b6d1 | 2010-03-04 23:02:38 +0100 | [diff] [blame] | 2588 | proxy_inc_fe_req_ctr(s->fe); |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2589 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2590 | if (s->listener->counters) |
| 2591 | s->listener->counters->failed_req++; |
| 2592 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2593 | if (!(s->flags & SN_FINST_MASK)) |
| 2594 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2595 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2596 | } |
| 2597 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2598 | buffer_dont_connect(req); |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2599 | req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2600 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2601 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2602 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2603 | /* If the client starts to talk, let's fall back to |
| 2604 | * request timeout processing. |
| 2605 | */ |
| 2606 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2607 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2608 | } |
| 2609 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2610 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2611 | if (!tick_isset(req->analyse_exp)) { |
| 2612 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2613 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2614 | tick_isset(s->be->timeout.httpka)) |
| 2615 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2616 | else |
| 2617 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2618 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2619 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2620 | /* we're not ready yet */ |
| 2621 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2622 | |
| 2623 | failed_keep_alive: |
| 2624 | /* Here we process low-level errors for keep-alive requests. In |
| 2625 | * short, if the request is not the first one and it experiences |
| 2626 | * a timeout, read error or shutdown, we just silently close so |
| 2627 | * that the client can try again. |
| 2628 | */ |
| 2629 | txn->status = 0; |
| 2630 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 2631 | req->analysers = 0; |
| 2632 | s->logs.logwait = 0; |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2633 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 2634 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2635 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2636 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2637 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2638 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2639 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 2640 | * later. At this point, we have the last CRLF at req->data + msg->eoh. |
| 2641 | * If the request is in HTTP/0.9 form, the rule is still true, and eoh |
| 2642 | * points to the CRLF of the request line. req->lr points to the first |
| 2643 | * byte after the last LF. msg->col and msg->sov point to the first |
| 2644 | * byte of data. msg->eol cannot be trusted because it may have been |
| 2645 | * left uninitialized (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2646 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2647 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 2648 | session_inc_http_req_ctr(s); |
Willy Tarreau | d9b587f | 2010-02-26 10:05:55 +0100 | [diff] [blame] | 2649 | proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */ |
| 2650 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2651 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2652 | /* kill the pending keep-alive timeout */ |
| 2653 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2654 | req->analyse_exp = TICK_ETERNITY; |
| 2655 | } |
| 2656 | |
| 2657 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2658 | /* Maybe we found in invalid header name while we were configured not |
| 2659 | * to block on that, so we have to capture it now. |
| 2660 | */ |
| 2661 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 2662 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2663 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2664 | /* |
| 2665 | * 1: identify the method |
| 2666 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2667 | txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2668 | |
| 2669 | /* we can make use of server redirect on GET and HEAD */ |
| 2670 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2671 | s->flags |= SN_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2672 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2673 | /* |
| 2674 | * 2: check if the URI matches the monitor_uri. |
| 2675 | * We have to do this for every request which gets in, because |
| 2676 | * the monitor-uri is defined by the frontend. |
| 2677 | */ |
| 2678 | if (unlikely((s->fe->monitor_uri_len != 0) && |
| 2679 | (s->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2680 | !memcmp(msg->sol + msg->sl.rq.u, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2681 | s->fe->monitor_uri, |
| 2682 | s->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2683 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2684 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2685 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2686 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2687 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2688 | s->flags |= SN_MONITOR; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2689 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2690 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2691 | list_for_each_entry(cond, &s->fe->mon_fail_cond, list) { |
| 2692 | int ret = acl_exec_cond(cond, s->fe, s, txn, ACL_DIR_REQ); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2693 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2694 | ret = acl_pass(ret); |
| 2695 | if (cond->pol == ACL_COND_UNLESS) |
| 2696 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2697 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2698 | if (ret) { |
| 2699 | /* we fail this request, let's return 503 service unavail */ |
| 2700 | txn->status = 503; |
| 2701 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503)); |
| 2702 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2703 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2704 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2705 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2706 | /* nothing to fail, let's reply normaly */ |
| 2707 | txn->status = 200; |
| 2708 | stream_int_retnclose(req->prod, &http_200_chunk); |
| 2709 | goto return_prx_cond; |
| 2710 | } |
| 2711 | |
| 2712 | /* |
| 2713 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2714 | * Note: we cannot log anymore if the request has been |
| 2715 | * classified as invalid. |
| 2716 | */ |
| 2717 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2718 | /* we have a complete HTTP request that we must log */ |
| 2719 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2720 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2721 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2722 | if (urilen >= REQURI_LEN) |
| 2723 | urilen = REQURI_LEN - 1; |
| 2724 | memcpy(txn->uri, &req->data[msg->som], urilen); |
| 2725 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2726 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2727 | if (!(s->logs.logwait &= ~LW_REQ)) |
| 2728 | s->do_log(s); |
| 2729 | } else { |
| 2730 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2731 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2732 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2733 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2734 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 2735 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) |
| 2736 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2737 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2738 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2739 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2740 | ((msg->sol[msg->sl.rq.v + 5] > '1') || |
| 2741 | ((msg->sol[msg->sl.rq.v + 5] == '1') && |
| 2742 | (msg->sol[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2743 | txn->flags |= TX_REQ_VER_11; |
| 2744 | |
| 2745 | /* "connection" has not been parsed yet */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2746 | txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2747 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2748 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 2749 | * we have what looks like a proxied connection instead of a connection, |
| 2750 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 2751 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 2752 | * happen to do that despite being non-standard :-( |
| 2753 | * We consider that a request not beginning with either '/' or '*' is |
| 2754 | * a proxied connection, which covers both "scheme://location" and |
| 2755 | * CONNECT ip:port. |
| 2756 | */ |
| 2757 | if ((s->fe->options2 & PR_O2_USE_PXHDR) && |
| 2758 | msg->sol[msg->sl.rq.u] != '/' && msg->sol[msg->sl.rq.u] != '*') |
| 2759 | txn->flags |= TX_USE_PX_CONN; |
| 2760 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2761 | /* transfer length unknown*/ |
| 2762 | txn->flags &= ~TX_REQ_XFER_LEN; |
| 2763 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2764 | /* 5: we may need to capture headers */ |
| 2765 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap)) |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2766 | capture_headers(msg->sol, &txn->hdr_idx, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2767 | txn->req.cap, s->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2768 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2769 | /* 6: determine the transfer-length. |
| 2770 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 2771 | * the presence of a message-body in a REQUEST and its transfer length |
| 2772 | * must be determined that way (in order of precedence) : |
| 2773 | * 1. The presence of a message-body in a request is signaled by the |
| 2774 | * inclusion of a Content-Length or Transfer-Encoding header field |
| 2775 | * in the request's header fields. When a request message contains |
| 2776 | * both a message-body of non-zero length and a method that does |
| 2777 | * not define any semantics for that request message-body, then an |
| 2778 | * origin server SHOULD either ignore the message-body or respond |
| 2779 | * with an appropriate error message (e.g., 413). A proxy or |
| 2780 | * gateway, when presented the same request, SHOULD either forward |
| 2781 | * the request inbound with the message- body or ignore the |
| 2782 | * message-body when determining a response. |
| 2783 | * |
| 2784 | * 2. If a Transfer-Encoding header field (Section 9.7) is present |
| 2785 | * and the "chunked" transfer-coding (Section 6.2) is used, the |
| 2786 | * transfer-length is defined by the use of this transfer-coding. |
| 2787 | * If a Transfer-Encoding header field is present and the "chunked" |
| 2788 | * transfer-coding is not present, the transfer-length is defined |
| 2789 | * by the sender closing the connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2790 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2791 | * 3. If a Content-Length header field is present, its decimal value in |
| 2792 | * OCTETs represents both the entity-length and the transfer-length. |
| 2793 | * If a message is received with both a Transfer-Encoding header |
| 2794 | * field and a Content-Length header field, the latter MUST be ignored. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2795 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2796 | * 4. By the server closing the connection. (Closing the connection |
| 2797 | * cannot be used to indicate the end of a request body, since that |
| 2798 | * would leave no possibility for the server to send back a response.) |
| 2799 | * |
| 2800 | * Whenever a transfer-coding is applied to a message-body, the set of |
| 2801 | * transfer-codings MUST include "chunked", unless the message indicates |
| 2802 | * it is terminated by closing the connection. When the "chunked" |
| 2803 | * transfer-coding is used, it MUST be the last transfer-coding applied |
| 2804 | * to the message-body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2805 | */ |
| 2806 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2807 | use_close_only = 0; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2808 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2809 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 2810 | while ((txn->flags & TX_REQ_VER_11) && |
| 2811 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2812 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
| 2813 | txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN); |
| 2814 | else if (txn->flags & TX_REQ_TE_CHNK) { |
| 2815 | /* bad transfer-encoding (chunked followed by something else) */ |
| 2816 | use_close_only = 1; |
| 2817 | txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN); |
| 2818 | break; |
| 2819 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2820 | } |
| 2821 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2822 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2823 | while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only && |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2824 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) { |
| 2825 | signed long long cl; |
| 2826 | |
| 2827 | if (!ctx.vlen) |
| 2828 | goto return_bad_req; |
| 2829 | |
| 2830 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) |
| 2831 | goto return_bad_req; /* parse failure */ |
| 2832 | |
| 2833 | if (cl < 0) |
| 2834 | goto return_bad_req; |
| 2835 | |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2836 | if ((txn->flags & TX_REQ_CNT_LEN) && (msg->chunk_len != cl)) |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2837 | goto return_bad_req; /* already specified, was different */ |
| 2838 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2839 | txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2840 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2841 | } |
| 2842 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2843 | /* bodyless requests have a known length */ |
| 2844 | if (!use_close_only) |
| 2845 | txn->flags |= TX_REQ_XFER_LEN; |
| 2846 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2847 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2848 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2849 | req->analyse_exp = TICK_ETERNITY; |
| 2850 | return 1; |
| 2851 | |
| 2852 | return_bad_req: |
| 2853 | /* We centralize bad requests processing here */ |
| 2854 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2855 | /* we detected a parsing error. We want to archive this request |
| 2856 | * in the dedicated proxy area for later troubleshooting. |
| 2857 | */ |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 2858 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe); |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2859 | } |
| 2860 | |
| 2861 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2862 | txn->status = 400; |
| 2863 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2864 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 2865 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2866 | if (s->listener->counters) |
| 2867 | s->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2868 | |
| 2869 | return_prx_cond: |
| 2870 | if (!(s->flags & SN_ERR_MASK)) |
| 2871 | s->flags |= SN_ERR_PRXCOND; |
| 2872 | if (!(s->flags & SN_FINST_MASK)) |
| 2873 | s->flags |= SN_FINST_R; |
| 2874 | |
| 2875 | req->analysers = 0; |
| 2876 | req->analyse_exp = TICK_ETERNITY; |
| 2877 | return 0; |
| 2878 | } |
| 2879 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2880 | /* We reached the stats page through a POST request. |
| 2881 | * Parse the posted data and enable/disable servers if necessary. |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2882 | * 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] | 2883 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2884 | int http_process_req_stat_post(struct stream_interface *si, struct http_txn *txn, struct buffer *req) |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2885 | { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2886 | struct proxy *px; |
| 2887 | struct server *sv; |
| 2888 | |
| 2889 | char *backend = NULL; |
| 2890 | int action = 0; |
| 2891 | |
| 2892 | char *first_param, *cur_param, *next_param, *end_params; |
| 2893 | |
| 2894 | first_param = req->data + txn->req.eoh + 2; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 2895 | end_params = first_param + txn->req.body_len; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2896 | |
| 2897 | cur_param = next_param = end_params; |
| 2898 | |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2899 | if (end_params >= req->data + req->size - global.tune.maxrewrite) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2900 | /* Prevent buffer overflow */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2901 | si->applet.ctx.stats.st_code = STAT_STATUS_EXCD; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2902 | return 1; |
| 2903 | } |
| 2904 | else if (end_params > req->data + req->l) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2905 | /* we need more data */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2906 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2907 | return 0; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2908 | } |
| 2909 | |
| 2910 | *end_params = '\0'; |
| 2911 | |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2912 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2913 | |
| 2914 | /* |
| 2915 | * Parse the parameters in reverse order to only store the last value. |
| 2916 | * From the html form, the backend and the action are at the end. |
| 2917 | */ |
| 2918 | while (cur_param > first_param) { |
| 2919 | char *key, *value; |
| 2920 | |
| 2921 | cur_param--; |
| 2922 | if ((*cur_param == '&') || (cur_param == first_param)) { |
| 2923 | /* Parse the key */ |
| 2924 | key = cur_param; |
| 2925 | if (cur_param != first_param) { |
| 2926 | /* delimit the string for the next loop */ |
| 2927 | *key++ = '\0'; |
| 2928 | } |
| 2929 | |
| 2930 | /* Parse the value */ |
| 2931 | value = key; |
| 2932 | while (*value != '\0' && *value != '=') { |
| 2933 | value++; |
| 2934 | } |
| 2935 | if (*value == '=') { |
| 2936 | /* Ok, a value is found, we can mark the end of the key */ |
| 2937 | *value++ = '\0'; |
| 2938 | } |
| 2939 | |
| 2940 | /* Now we can check the key to see what to do */ |
| 2941 | if (!backend && strcmp(key, "b") == 0) { |
| 2942 | backend = value; |
| 2943 | } |
| 2944 | else if (!action && strcmp(key, "action") == 0) { |
| 2945 | if (strcmp(value, "disable") == 0) { |
| 2946 | action = 1; |
| 2947 | } |
| 2948 | else if (strcmp(value, "enable") == 0) { |
| 2949 | action = 2; |
| 2950 | } else { |
| 2951 | /* unknown action, no need to continue */ |
| 2952 | break; |
| 2953 | } |
| 2954 | } |
| 2955 | else if (strcmp(key, "s") == 0) { |
| 2956 | if (backend && action && get_backend_server(backend, value, &px, &sv)) { |
| 2957 | switch (action) { |
| 2958 | case 1: |
Cyril Bonté | 1e2a170 | 2011-03-03 21:05:17 +0100 | [diff] [blame] | 2959 | if ((px->state != PR_STSTOPPED) && !(sv->state & SRV_MAINTAIN)) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2960 | /* Not already in maintenance, we can change the server state */ |
| 2961 | sv->state |= SRV_MAINTAIN; |
| 2962 | set_server_down(sv); |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2963 | si->applet.ctx.stats.st_code = STAT_STATUS_DONE; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2964 | } |
| 2965 | break; |
| 2966 | case 2: |
Cyril Bonté | 1e2a170 | 2011-03-03 21:05:17 +0100 | [diff] [blame] | 2967 | if ((px->state != PR_STSTOPPED) && (sv->state & SRV_MAINTAIN)) { |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2968 | /* Already in maintenance, we can change the server state */ |
| 2969 | set_server_up(sv); |
Willy Tarreau | 7046130 | 2010-10-22 14:39:02 +0200 | [diff] [blame] | 2970 | sv->health = sv->rise; /* up, but will fall down at first failure */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 2971 | si->applet.ctx.stats.st_code = STAT_STATUS_DONE; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2972 | } |
| 2973 | break; |
| 2974 | } |
| 2975 | } |
| 2976 | } |
| 2977 | next_param = cur_param; |
| 2978 | } |
| 2979 | } |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 2980 | return 1; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 2981 | } |
| 2982 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2983 | /* returns a pointer to the first rule which forbids access (deny or http_auth), |
| 2984 | * or NULL if everything's OK. |
| 2985 | */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2986 | static inline struct http_req_rule * |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2987 | http_check_access_rule(struct proxy *px, struct list *rules, struct session *s, struct http_txn *txn) |
| 2988 | { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2989 | struct http_req_rule *rule; |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2990 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2991 | list_for_each_entry(rule, rules, list) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2992 | int ret = 1; |
| 2993 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2994 | if (rule->action >= HTTP_REQ_ACT_MAX) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 2995 | continue; |
| 2996 | |
| 2997 | /* check condition, but only if attached */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 2998 | if (rule->cond) { |
| 2999 | ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ); |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3000 | ret = acl_pass(ret); |
| 3001 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3002 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3003 | ret = !ret; |
| 3004 | } |
| 3005 | |
| 3006 | if (ret) { |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3007 | if (rule->action == HTTP_REQ_ACT_ALLOW) |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3008 | return NULL; /* no problem */ |
| 3009 | else |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3010 | return rule; /* most likely a deny or auth rule */ |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3011 | } |
| 3012 | } |
| 3013 | return NULL; |
| 3014 | } |
| 3015 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3016 | /* This stream analyser runs all HTTP request processing which is common to |
| 3017 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 3018 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3019 | * 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] | 3020 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 3021 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3022 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3023 | int http_process_req_common(struct session *s, struct buffer *req, int an_bit, struct proxy *px) |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3024 | { |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3025 | struct http_txn *txn = &s->txn; |
| 3026 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3027 | struct acl_cond *cond; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3028 | struct http_req_rule *http_req_last_rule = NULL; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3029 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 3030 | struct cond_wordlist *wl; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3031 | int del_ka, del_cl, do_stats; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3032 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3033 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3034 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3035 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3036 | return 0; |
| 3037 | } |
| 3038 | |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3039 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 3040 | req->analyse_exp = TICK_ETERNITY; |
| 3041 | |
| 3042 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 3043 | now_ms, __FUNCTION__, |
| 3044 | s, |
| 3045 | req, |
| 3046 | req->rex, req->wex, |
| 3047 | req->flags, |
| 3048 | req->l, |
| 3049 | req->analysers); |
| 3050 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3051 | /* first check whether we have some ACLs set to block this request */ |
| 3052 | list_for_each_entry(cond, &px->block_cond, list) { |
| 3053 | int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ); |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 3054 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3055 | ret = acl_pass(ret); |
| 3056 | if (cond->pol == ACL_COND_UNLESS) |
| 3057 | ret = !ret; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 3058 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3059 | if (ret) { |
| 3060 | txn->status = 403; |
| 3061 | /* let's log the request time */ |
| 3062 | s->logs.tv_request = now; |
| 3063 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3064 | session_inc_http_err_ctr(s); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3065 | goto return_prx_cond; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3066 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3067 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3068 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3069 | /* evaluate http-request rules */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3070 | 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] | 3071 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3072 | /* evaluate stats http-request rules only if http-request is OK */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3073 | if (!http_req_last_rule) { |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3074 | do_stats = stats_check_uri(s->rep->prod, txn, px); |
| 3075 | if (do_stats) |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3076 | 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] | 3077 | } |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3078 | else |
| 3079 | do_stats = 0; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3080 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3081 | /* return a 403 if either rule has blocked */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3082 | 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] | 3083 | txn->status = 403; |
| 3084 | s->logs.tv_request = now; |
| 3085 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3086 | session_inc_http_err_ctr(s); |
Willy Tarreau | 6da0f6d | 2011-01-06 18:19:50 +0100 | [diff] [blame] | 3087 | s->fe->fe_counters.denied_req++; |
| 3088 | if (an_bit == AN_REQ_HTTP_PROCESS_BE) |
| 3089 | s->be->be_counters.denied_req++; |
| 3090 | if (s->listener->counters) |
| 3091 | s->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3092 | goto return_prx_cond; |
| 3093 | } |
| 3094 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3095 | /* try headers filters */ |
| 3096 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 3097 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3098 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3099 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3100 | /* has the request been denied ? */ |
| 3101 | if (txn->flags & TX_CLDENY) { |
| 3102 | /* no need to go further */ |
| 3103 | txn->status = 403; |
| 3104 | /* let's log the request time */ |
| 3105 | s->logs.tv_request = now; |
| 3106 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3107 | session_inc_http_err_ctr(s); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3108 | goto return_prx_cond; |
| 3109 | } |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3110 | |
| 3111 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 3112 | * which may be the same as the connect timeout if unspecified. |
| 3113 | * If unset, then set it to zero because we really want it to |
| 3114 | * eventually expire. We build the tarpit as an analyser. |
| 3115 | */ |
| 3116 | if (txn->flags & TX_CLTARPIT) { |
| 3117 | buffer_erase(s->req); |
| 3118 | /* wipe the request out so that we can drop the connection early |
| 3119 | * if the client closes first. |
| 3120 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3121 | buffer_dont_connect(req); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3122 | req->analysers = 0; /* remove switching rules etc... */ |
| 3123 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 3124 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 3125 | if (!req->analyse_exp) |
| 3126 | req->analyse_exp = tick_add(now_ms, 0); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3127 | session_inc_http_err_ctr(s); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 3128 | return 1; |
| 3129 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3130 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3131 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3132 | /* Until set to anything else, the connection mode is set as TUNNEL. It will |
| 3133 | * only change if both the request and the config reference something else. |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3134 | * Option httpclose by itself does not set a mode, it remains a tunnel mode |
| 3135 | * 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] | 3136 | * affect it (eg: server-close/keep-alive + httpclose = close). Note that we |
| 3137 | * avoid to redo the same work if FE and BE have the same settings (common). |
| 3138 | * The method consists in checking if options changed between the two calls |
| 3139 | * (implying that either one is non-null, or one of them is non-null and we |
| 3140 | * are there for the first time. |
Willy Tarreau | 4273664 | 2009-10-18 21:04:35 +0200 | [diff] [blame] | 3141 | */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3142 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3143 | del_cl = del_ka = 0; |
| 3144 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 3145 | if ((!(txn->flags & TX_HDR_CONN_PRS) && |
| 3146 | (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) || |
| 3147 | ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) != |
| 3148 | (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] | 3149 | int tmp = TX_CON_WANT_TUN; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3150 | |
Cyril Bonté | 9ea2b9a | 2010-12-29 09:36:56 +0100 | [diff] [blame] | 3151 | if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE || |
| 3152 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3153 | tmp = TX_CON_WANT_KAL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 3154 | if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO) |
| 3155 | tmp = TX_CON_WANT_SCL; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3156 | if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3157 | tmp = TX_CON_WANT_CLO; |
| 3158 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3159 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 3160 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3161 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3162 | if (!(txn->flags & TX_HDR_CONN_PRS)) { |
| 3163 | /* parse the Connection header and possibly clean it */ |
| 3164 | int to_del = 0; |
| 3165 | if ((txn->flags & TX_REQ_VER_11) || |
Willy Tarreau | 8a8e1d9 | 2010-04-05 16:15:16 +0200 | [diff] [blame] | 3166 | ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL && |
| 3167 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA))) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3168 | to_del |= 2; /* remove "keep-alive" */ |
| 3169 | if (!(txn->flags & TX_REQ_VER_11)) |
| 3170 | to_del |= 1; /* remove "close" */ |
| 3171 | http_parse_connection_header(txn, msg, req, to_del); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 3172 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 3173 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3174 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 3175 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 3176 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 3177 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
| 3178 | (txn->flags & (TX_REQ_VER_11|TX_HDR_CONN_KAL)) == 0 || /* no "connection: k-a" in 1.0 */ |
Cyril Bonté | 9ea2b9a | 2010-12-29 09:36:56 +0100 | [diff] [blame] | 3179 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose+any = forceclose */ |
Willy Tarreau | c3e8b25 | 2010-01-28 15:01:20 +0100 | [diff] [blame] | 3180 | !(txn->flags & TX_REQ_XFER_LEN) || /* no length known => close */ |
| 3181 | s->fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3182 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 3183 | } |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 3184 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3185 | /* we can be blocked here because the request needs to be authenticated, |
| 3186 | * either to pass or to access stats. |
| 3187 | */ |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3188 | if (http_req_last_rule && http_req_last_rule->action == HTTP_REQ_ACT_HTTP_AUTH) { |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3189 | struct chunk msg; |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 3190 | char *realm = http_req_last_rule->http_auth.realm; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3191 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3192 | if (!realm) |
| 3193 | realm = do_stats?STATS_DEFAULT_REALM:px->id; |
| 3194 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 3195 | sprintf(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] | 3196 | chunk_initlen(&msg, trash, sizeof(trash), strlen(trash)); |
| 3197 | txn->status = 401; |
| 3198 | stream_int_retnclose(req->prod, &msg); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3199 | /* on 401 we still count one error, because normal browsing |
| 3200 | * won't significantly increase the counter but brute force |
| 3201 | * attempts will. |
| 3202 | */ |
| 3203 | session_inc_http_err_ctr(s); |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 3204 | goto return_prx_cond; |
| 3205 | } |
| 3206 | |
Willy Tarreau | f68a15a | 2011-01-06 16:53:21 +0100 | [diff] [blame] | 3207 | /* add request headers from the rule sets in the same order */ |
| 3208 | list_for_each_entry(wl, &px->req_add, list) { |
| 3209 | if (wl->cond) { |
| 3210 | int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ); |
| 3211 | ret = acl_pass(ret); |
| 3212 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 3213 | ret = !ret; |
| 3214 | if (!ret) |
| 3215 | continue; |
| 3216 | } |
| 3217 | |
| 3218 | if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0)) |
| 3219 | goto return_bad_req; |
| 3220 | } |
| 3221 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3222 | if (do_stats) { |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 3223 | struct stats_admin_rule *stats_admin_rule; |
| 3224 | |
| 3225 | /* We need to provide stats for this request. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3226 | * FIXME!!! that one is rather dangerous, we want to |
| 3227 | * make it follow standard rules (eg: clear req->analysers). |
| 3228 | */ |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3229 | |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 3230 | /* now check whether we have some admin rules for this request */ |
| 3231 | list_for_each_entry(stats_admin_rule, &s->be->uri_auth->admin_rules, list) { |
| 3232 | int ret = 1; |
| 3233 | |
| 3234 | if (stats_admin_rule->cond) { |
| 3235 | ret = acl_exec_cond(stats_admin_rule->cond, s->be, s, &s->txn, ACL_DIR_REQ); |
| 3236 | ret = acl_pass(ret); |
| 3237 | if (stats_admin_rule->cond->pol == ACL_COND_UNLESS) |
| 3238 | ret = !ret; |
| 3239 | } |
| 3240 | |
| 3241 | if (ret) { |
| 3242 | /* no rule, or the rule matches */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 3243 | s->rep->prod->applet.ctx.stats.flags |= STAT_ADMIN; |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 3244 | break; |
| 3245 | } |
| 3246 | } |
| 3247 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 3248 | /* Was the status page requested with a POST ? */ |
| 3249 | if (txn->meth == HTTP_METH_POST) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 3250 | if (s->rep->prod->applet.ctx.stats.flags & STAT_ADMIN) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 3251 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 3252 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 3253 | * send an HTTP/1.1 100 Continue intermediate response. |
| 3254 | */ |
| 3255 | if (txn->flags & TX_REQ_VER_11) { |
| 3256 | struct hdr_ctx ctx; |
| 3257 | ctx.idx = 0; |
| 3258 | /* Expect is allowed in 1.1, look for it */ |
| 3259 | if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) && |
| 3260 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
| 3261 | buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len); |
| 3262 | } |
| 3263 | } |
| 3264 | msg->msg_state = HTTP_MSG_100_SENT; |
| 3265 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 3266 | } |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 3267 | if (!http_process_req_stat_post(s->rep->prod, txn, req)) { |
Cyril Bonté | 23b39d9 | 2011-02-10 22:54:44 +0100 | [diff] [blame] | 3268 | /* we need more data */ |
| 3269 | req->analysers |= an_bit; |
| 3270 | buffer_dont_connect(req); |
| 3271 | return 0; |
| 3272 | } |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 3273 | } else { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 3274 | s->rep->prod->applet.ctx.stats.st_code = STAT_STATUS_DENY; |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 3275 | } |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 3276 | } |
| 3277 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3278 | s->logs.tv_request = now; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3279 | s->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | b24281b | 2011-02-13 13:16:36 +0100 | [diff] [blame] | 3280 | stream_int_register_handler(s->rep->prod, &http_stats_applet); |
Willy Tarreau | 7b7a8e9 | 2011-03-27 19:53:06 +0200 | [diff] [blame] | 3281 | copy_target(&s->target, &s->rep->prod->target); // for logging only |
Willy Tarreau | bc4af05 | 2011-02-13 13:25:14 +0100 | [diff] [blame] | 3282 | s->rep->prod->applet.private = s; |
| 3283 | s->rep->prod->applet.st0 = s->rep->prod->applet.st1 = 0; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 3284 | req->analysers = 0; |
| 3285 | |
| 3286 | return 0; |
| 3287 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3288 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 3289 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3290 | /* check whether we have some ACLs set to redirect this request */ |
| 3291 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3292 | int ret = ACL_PAT_PASS; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3293 | |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3294 | if (rule->cond) { |
| 3295 | ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ); |
| 3296 | ret = acl_pass(ret); |
| 3297 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3298 | ret = !ret; |
| 3299 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3300 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3301 | if (ret) { |
Willy Tarreau | 3bb9c23 | 2010-01-03 12:24:37 +0100 | [diff] [blame] | 3302 | struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 }; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3303 | const char *msg_fmt; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3304 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3305 | /* build redirect message */ |
| 3306 | switch(rule->code) { |
| 3307 | case 303: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3308 | msg_fmt = HTTP_303; |
| 3309 | break; |
| 3310 | case 301: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3311 | msg_fmt = HTTP_301; |
| 3312 | break; |
| 3313 | case 302: |
| 3314 | default: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3315 | msg_fmt = HTTP_302; |
| 3316 | break; |
| 3317 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3318 | |
Willy Tarreau | 3bb9c23 | 2010-01-03 12:24:37 +0100 | [diff] [blame] | 3319 | if (unlikely(!chunk_strcpy(&rdr, msg_fmt))) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3320 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3321 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3322 | switch(rule->type) { |
| 3323 | case REDIRECT_TYPE_PREFIX: { |
| 3324 | const char *path; |
| 3325 | int pathlen; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3326 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3327 | path = http_get_path(txn); |
| 3328 | /* build message using path */ |
| 3329 | if (path) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 3330 | pathlen = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3331 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3332 | int qs = 0; |
| 3333 | while (qs < pathlen) { |
| 3334 | if (path[qs] == '?') { |
| 3335 | pathlen = qs; |
| 3336 | break; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3337 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3338 | qs++; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3339 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3340 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3341 | } else { |
| 3342 | path = "/"; |
| 3343 | pathlen = 1; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3344 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3345 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 3346 | if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3347 | goto return_bad_req; |
| 3348 | |
| 3349 | /* add prefix. Note that if prefix == "/", we don't want to |
| 3350 | * add anything, otherwise it makes it hard for the user to |
| 3351 | * configure a self-redirection. |
| 3352 | */ |
| 3353 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3354 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 3355 | rdr.len += rule->rdr_len; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3356 | } |
| 3357 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3358 | /* add path */ |
| 3359 | memcpy(rdr.str + rdr.len, path, pathlen); |
| 3360 | rdr.len += pathlen; |
Willy Tarreau | 81e3b4f | 2010-01-10 00:42:19 +0100 | [diff] [blame] | 3361 | |
| 3362 | /* append a slash at the end of the location is needed and missing */ |
| 3363 | if (rdr.len && rdr.str[rdr.len - 1] != '/' && |
| 3364 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3365 | if (rdr.len > rdr.size - 5) |
| 3366 | goto return_bad_req; |
| 3367 | rdr.str[rdr.len] = '/'; |
| 3368 | rdr.len++; |
| 3369 | } |
| 3370 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3371 | break; |
| 3372 | } |
| 3373 | case REDIRECT_TYPE_LOCATION: |
| 3374 | default: |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 3375 | if (rdr.len + rule->rdr_len > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3376 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3377 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3378 | /* add location */ |
| 3379 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 3380 | rdr.len += rule->rdr_len; |
| 3381 | break; |
| 3382 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3383 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3384 | if (rule->cookie_len) { |
| 3385 | memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14); |
| 3386 | rdr.len += 14; |
| 3387 | memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len); |
| 3388 | rdr.len += rule->cookie_len; |
| 3389 | memcpy(rdr.str + rdr.len, "\r\n", 2); |
| 3390 | rdr.len += 2; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3391 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3392 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3393 | /* add end of headers and the keep-alive/close status. |
| 3394 | * We may choose to set keep-alive if the Location begins |
| 3395 | * with a slash, because the client will come back to the |
| 3396 | * same server. |
| 3397 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3398 | txn->status = rule->code; |
| 3399 | /* let's log the request time */ |
| 3400 | s->logs.tv_request = now; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3401 | |
| 3402 | if (rule->rdr_len >= 1 && *rule->rdr_str == '/' && |
| 3403 | (txn->flags & TX_REQ_XFER_LEN) && |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3404 | !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.body_len && |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3405 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 3406 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 3407 | /* keep-alive possible */ |
Willy Tarreau | 7566145 | 2010-01-10 10:35:01 +0100 | [diff] [blame] | 3408 | if (!(txn->flags & TX_REQ_VER_11)) { |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3409 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3410 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30); |
| 3411 | rdr.len += 30; |
| 3412 | } else { |
| 3413 | memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24); |
| 3414 | rdr.len += 24; |
| 3415 | } |
Willy Tarreau | 7566145 | 2010-01-10 10:35:01 +0100 | [diff] [blame] | 3416 | } |
| 3417 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 3418 | rdr.len += 4; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3419 | buffer_write(req->prod->ob, rdr.str, rdr.len); |
| 3420 | /* "eat" the request */ |
| 3421 | buffer_ignore(req, msg->sov - msg->som); |
| 3422 | msg->som = msg->sov; |
| 3423 | req->analysers = AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 9300fb2 | 2010-01-05 00:58:24 +0100 | [diff] [blame] | 3424 | s->rep->analysers = AN_RES_HTTP_XFER_BODY; |
| 3425 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3426 | txn->rsp.msg_state = HTTP_MSG_DONE; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3427 | break; |
| 3428 | } else { |
| 3429 | /* keep-alive not possible */ |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3430 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3431 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 3432 | rdr.len += 29; |
| 3433 | } else { |
| 3434 | memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23); |
| 3435 | rdr.len += 23; |
| 3436 | } |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 3437 | stream_int_retnclose(req->prod, &rdr); |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3438 | goto return_prx_cond; |
| 3439 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3440 | } |
| 3441 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3442 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3443 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 3444 | * If this happens, then the data will not come immediately, so we must |
| 3445 | * send all what we have without waiting. Note that due to the small gain |
| 3446 | * in waiting for the body of the request, it's easier to simply put the |
| 3447 | * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove |
| 3448 | * itself once used. |
| 3449 | */ |
| 3450 | req->flags |= BF_SEND_DONTWAIT; |
| 3451 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3452 | /* that's OK for us now, let's move on to next analysers */ |
| 3453 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 3454 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3455 | return_bad_req: |
| 3456 | /* We centralize bad requests processing here */ |
| 3457 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3458 | /* we detected a parsing error. We want to archive this request |
| 3459 | * in the dedicated proxy area for later troubleshooting. |
| 3460 | */ |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 3461 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe); |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3462 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3463 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3464 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3465 | txn->status = 400; |
| 3466 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3467 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3468 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3469 | if (s->listener->counters) |
| 3470 | s->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3471 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3472 | return_prx_cond: |
| 3473 | if (!(s->flags & SN_ERR_MASK)) |
| 3474 | s->flags |= SN_ERR_PRXCOND; |
| 3475 | if (!(s->flags & SN_FINST_MASK)) |
| 3476 | s->flags |= SN_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 3477 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3478 | req->analysers = 0; |
| 3479 | req->analyse_exp = TICK_ETERNITY; |
| 3480 | return 0; |
| 3481 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3482 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3483 | /* This function performs all the processing enabled for the current request. |
| 3484 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 3485 | * needs more data, encounters an error, or wants to immediately abort the |
| 3486 | * request. It relies on buffers flags, and updates s->req->analysers. |
| 3487 | */ |
| 3488 | int http_process_request(struct session *s, struct buffer *req, int an_bit) |
| 3489 | { |
| 3490 | struct http_txn *txn = &s->txn; |
| 3491 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3492 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3493 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3494 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3495 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3496 | return 0; |
| 3497 | } |
| 3498 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3499 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 3500 | now_ms, __FUNCTION__, |
| 3501 | s, |
| 3502 | req, |
| 3503 | req->rex, req->wex, |
| 3504 | req->flags, |
| 3505 | req->l, |
| 3506 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3507 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3508 | /* |
| 3509 | * Right now, we know that we have processed the entire headers |
| 3510 | * and that unwanted requests have been filtered out. We can do |
| 3511 | * whatever we want with the remaining request. Also, now we |
| 3512 | * may have separate values for ->fe, ->be. |
| 3513 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3514 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3515 | /* |
| 3516 | * If HTTP PROXY is set we simply get remote server address |
| 3517 | * parsing incoming request. |
| 3518 | */ |
| 3519 | if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) { |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 3520 | url2sa(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, &s->req->cons->addr.s.to); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3521 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3522 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3523 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3524 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3525 | * Note that doing so might move headers in the request, but |
| 3526 | * the fields will stay coherent and the URI will not move. |
| 3527 | * This should only be performed in the backend. |
| 3528 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 3529 | 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] | 3530 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 3531 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 3532 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3533 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3534 | * 8: the appsession cookie was looked up very early in 1.2, |
| 3535 | * so let's do the same now. |
| 3536 | */ |
| 3537 | |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 3538 | /* It needs to look into the URI unless persistence must be ignored */ |
| 3539 | if ((txn->sessid == NULL) && s->be->appsession_name && !(s->flags & SN_IGNORE_PRST)) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 3540 | get_srv_from_appsession(s, msg->sol + msg->sl.rq.u, msg->sl.rq.u_l); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3541 | } |
| 3542 | |
| 3543 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3544 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 3545 | * asks for it. |
| 3546 | */ |
| 3547 | if ((s->fe->options | s->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 3548 | if (s->req->prod->addr.c.from.ss_family == AF_INET) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3549 | /* Add an X-Forwarded-For header unless the source IP is |
| 3550 | * in the 'except' network range. |
| 3551 | */ |
| 3552 | if ((!s->fe->except_mask.s_addr || |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 3553 | (((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_addr.s_addr & s->fe->except_mask.s_addr) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3554 | != s->fe->except_net.s_addr) && |
| 3555 | (!s->be->except_mask.s_addr || |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 3556 | (((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_addr.s_addr & s->be->except_mask.s_addr) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3557 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3558 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3559 | unsigned char *pn; |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 3560 | pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.c.from)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3561 | |
| 3562 | /* Note: we rely on the backend to get the header name to be used for |
| 3563 | * x-forwarded-for, because the header is really meant for the backends. |
| 3564 | * However, if the backend did not specify any option, we have to rely |
| 3565 | * on the frontend's header name. |
| 3566 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3567 | if (s->be->fwdfor_hdr_len) { |
| 3568 | len = s->be->fwdfor_hdr_len; |
| 3569 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3570 | } else { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3571 | len = s->fe->fwdfor_hdr_len; |
| 3572 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3573 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3574 | len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
Willy Tarreau | edcf668 | 2008-11-30 23:15:34 +0100 | [diff] [blame] | 3575 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3576 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3577 | &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3578 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3579 | } |
| 3580 | } |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 3581 | else if (s->req->prod->addr.c.from.ss_family == AF_INET6) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3582 | /* FIXME: for the sake of completeness, we should also support |
| 3583 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3584 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3585 | int len; |
| 3586 | char pn[INET6_ADDRSTRLEN]; |
| 3587 | inet_ntop(AF_INET6, |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 3588 | (const void *)&((struct sockaddr_in6 *)(&s->req->prod->addr.c.from))->sin6_addr, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3589 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3590 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3591 | /* Note: we rely on the backend to get the header name to be used for |
| 3592 | * x-forwarded-for, because the header is really meant for the backends. |
| 3593 | * However, if the backend did not specify any option, we have to rely |
| 3594 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3595 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3596 | if (s->be->fwdfor_hdr_len) { |
| 3597 | len = s->be->fwdfor_hdr_len; |
| 3598 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
| 3599 | } else { |
| 3600 | len = s->fe->fwdfor_hdr_len; |
| 3601 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3602 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3603 | len += sprintf(trash + len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3604 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3605 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3606 | &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3607 | goto return_bad_req; |
| 3608 | } |
| 3609 | } |
| 3610 | |
| 3611 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3612 | * 10: add X-Original-To if either the frontend or the backend |
| 3613 | * asks for it. |
| 3614 | */ |
| 3615 | if ((s->fe->options | s->be->options) & PR_O_ORGTO) { |
| 3616 | |
| 3617 | /* FIXME: don't know if IPv6 can handle that case too. */ |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 3618 | if (s->req->prod->addr.c.from.ss_family == AF_INET) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3619 | /* Add an X-Original-To header unless the destination IP is |
| 3620 | * in the 'except' network range. |
| 3621 | */ |
| 3622 | if (!(s->flags & SN_FRT_ADDR_SET)) |
| 3623 | get_frt_addr(s); |
| 3624 | |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 3625 | if (s->req->prod->addr.c.to.ss_family == AF_INET && |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 3626 | ((!s->fe->except_mask_to.s_addr || |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 3627 | (((struct sockaddr_in *)&s->req->prod->addr.c.to)->sin_addr.s_addr & s->fe->except_mask_to.s_addr) |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 3628 | != s->fe->except_to.s_addr) && |
| 3629 | (!s->be->except_mask_to.s_addr || |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 3630 | (((struct sockaddr_in *)&s->req->prod->addr.c.to)->sin_addr.s_addr & s->be->except_mask_to.s_addr) |
Emeric Brun | 5bd86a8 | 2010-10-22 17:23:04 +0200 | [diff] [blame] | 3631 | != s->be->except_to.s_addr))) { |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3632 | int len; |
| 3633 | unsigned char *pn; |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 3634 | pn = (unsigned char *)&((struct sockaddr_in *)&s->req->prod->addr.c.to)->sin_addr; |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3635 | |
| 3636 | /* Note: we rely on the backend to get the header name to be used for |
| 3637 | * x-original-to, because the header is really meant for the backends. |
| 3638 | * However, if the backend did not specify any option, we have to rely |
| 3639 | * on the frontend's header name. |
| 3640 | */ |
| 3641 | if (s->be->orgto_hdr_len) { |
| 3642 | len = s->be->orgto_hdr_len; |
| 3643 | memcpy(trash, s->be->orgto_hdr_name, len); |
| 3644 | } else { |
| 3645 | len = s->fe->orgto_hdr_len; |
| 3646 | memcpy(trash, s->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3647 | } |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3648 | len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
| 3649 | |
| 3650 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3651 | &txn->hdr_idx, trash, len) < 0)) |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3652 | goto return_bad_req; |
| 3653 | } |
| 3654 | } |
| 3655 | } |
| 3656 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3657 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */ |
| 3658 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
Cyril Bonté | 9ea2b9a | 2010-12-29 09:36:56 +0100 | [diff] [blame] | 3659 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3660 | unsigned int want_flags = 0; |
| 3661 | |
| 3662 | if (txn->flags & TX_REQ_VER_11) { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3663 | if (((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
| 3664 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) && |
| 3665 | !((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3666 | want_flags |= TX_CON_CLO_SET; |
| 3667 | } else { |
Willy Tarreau | 22a9534 | 2010-09-29 14:31:41 +0200 | [diff] [blame] | 3668 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL && |
| 3669 | !((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) || |
| 3670 | ((s->fe->options2|s->be->options2) & PR_O2_FAKE_KA)) |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3671 | want_flags |= TX_CON_KAL_SET; |
| 3672 | } |
| 3673 | |
| 3674 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 3675 | http_change_connection_header(txn, msg, req, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3676 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3677 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3678 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3679 | /* If we have no server assigned yet and we're balancing on url_param |
| 3680 | * with a POST request, we may be interested in checking the body for |
| 3681 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3682 | */ |
| 3683 | if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 3684 | s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3685 | s->be->url_param_post_limit != 0 && |
Willy Tarreau | 61a21a3 | 2011-03-01 20:35:49 +0100 | [diff] [blame] | 3686 | (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK))) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3687 | buffer_dont_connect(req); |
| 3688 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3689 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3690 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3691 | if (txn->flags & TX_REQ_XFER_LEN) |
| 3692 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 3693 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3694 | /************************************************************* |
| 3695 | * OK, that's finished for the headers. We have done what we * |
| 3696 | * could. Let's switch to the DATA state. * |
| 3697 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3698 | req->analyse_exp = TICK_ETERNITY; |
| 3699 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3700 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3701 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3702 | /* OK let's go on with the BODY now */ |
| 3703 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3704 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3705 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3706 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3707 | /* we detected a parsing error. We want to archive this request |
| 3708 | * in the dedicated proxy area for later troubleshooting. |
| 3709 | */ |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 3710 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, msg->msg_state, s->fe); |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3711 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3712 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3713 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3714 | txn->status = 400; |
| 3715 | req->analysers = 0; |
| 3716 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3717 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3718 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3719 | if (s->listener->counters) |
| 3720 | s->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3721 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3722 | if (!(s->flags & SN_ERR_MASK)) |
| 3723 | s->flags |= SN_ERR_PRXCOND; |
| 3724 | if (!(s->flags & SN_FINST_MASK)) |
| 3725 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3726 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3727 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3728 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3729 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 3730 | * returns zero, at the beginning because it prevents any other processing |
| 3731 | * from occurring, and at the end because it terminates the request. |
| 3732 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3733 | int http_process_tarpit(struct session *s, struct buffer *req, int an_bit) |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3734 | { |
| 3735 | struct http_txn *txn = &s->txn; |
| 3736 | |
| 3737 | /* This connection is being tarpitted. The CLIENT side has |
| 3738 | * already set the connect expiration date to the right |
| 3739 | * timeout. We just have to check that the client is still |
| 3740 | * there and that the timeout has not expired. |
| 3741 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3742 | buffer_dont_connect(req); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3743 | if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 && |
| 3744 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 3745 | return 0; |
| 3746 | |
| 3747 | /* We will set the queue timer to the time spent, just for |
| 3748 | * logging purposes. We fake a 500 server error, so that the |
| 3749 | * attacker will not suspect his connection has been tarpitted. |
| 3750 | * It will not cause trouble to the logs because we can exclude |
| 3751 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 3752 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3753 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3754 | |
| 3755 | txn->status = 500; |
| 3756 | if (req->flags != BF_READ_ERROR) |
| 3757 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500)); |
| 3758 | |
| 3759 | req->analysers = 0; |
| 3760 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3761 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3762 | s->fe->fe_counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3763 | if (s->listener->counters) |
| 3764 | s->listener->counters->failed_req++; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3765 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3766 | if (!(s->flags & SN_ERR_MASK)) |
| 3767 | s->flags |= SN_ERR_PRXCOND; |
| 3768 | if (!(s->flags & SN_FINST_MASK)) |
| 3769 | s->flags |= SN_FINST_T; |
| 3770 | return 0; |
| 3771 | } |
| 3772 | |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3773 | /* This function is an analyser which processes the HTTP request body. It looks |
| 3774 | * for parameters to be used for the load balancing algorithm (url_param). It |
| 3775 | * must only be called after the standard HTTP request processing has occurred, |
| 3776 | * because it expects the request to be parsed. It returns zero if it needs to |
| 3777 | * read more data, or 1 once it has completed its analysis. |
| 3778 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3779 | int http_process_request_body(struct session *s, struct buffer *req, int an_bit) |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3780 | { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3781 | struct http_txn *txn = &s->txn; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3782 | struct http_msg *msg = &s->txn.req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3783 | long long limit = s->be->url_param_post_limit; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3784 | |
| 3785 | /* We have to parse the HTTP request body to find any required data. |
| 3786 | * "balance url_param check_post" should have been the only way to get |
| 3787 | * into this. We were brought here after HTTP header analysis, so all |
| 3788 | * related structures are ready. |
| 3789 | */ |
| 3790 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3791 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 3792 | goto missing_data; |
| 3793 | |
| 3794 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 3795 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 3796 | * send an HTTP/1.1 100 Continue intermediate response. |
| 3797 | */ |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 3798 | if (txn->flags & TX_REQ_VER_11) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3799 | struct hdr_ctx ctx; |
| 3800 | ctx.idx = 0; |
| 3801 | /* Expect is allowed in 1.1, look for it */ |
| 3802 | if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) && |
| 3803 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
| 3804 | buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len); |
| 3805 | } |
| 3806 | } |
| 3807 | msg->msg_state = HTTP_MSG_100_SENT; |
| 3808 | } |
| 3809 | |
| 3810 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3811 | /* we have msg->col and msg->sov which both point to the first |
| 3812 | * byte of message body. msg->som still points to the beginning |
| 3813 | * of the message. We must save the body in req->lr because it |
| 3814 | * survives buffer re-alignments. |
| 3815 | */ |
| 3816 | req->lr = req->data + msg->sov; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3817 | if (txn->flags & TX_REQ_TE_CHNK) |
| 3818 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 3819 | else |
| 3820 | msg->msg_state = HTTP_MSG_DATA; |
| 3821 | } |
| 3822 | |
| 3823 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3824 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3825 | * set ->sov and ->lr to point to the body and switch to DATA or |
| 3826 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3827 | */ |
| 3828 | int ret = http_parse_chunk_size(req, msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3829 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3830 | if (!ret) |
| 3831 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3832 | else if (ret < 0) { |
| 3833 | session_inc_http_err_ctr(s); |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3834 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3835 | } |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3836 | } |
| 3837 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3838 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3839 | * We have the first non-header byte in msg->col, which is either the |
| 3840 | * beginning of the chunk size or of the data. The first data byte is in |
| 3841 | * msg->sov, which is equal to msg->col when not using transfer-encoding. |
| 3842 | * We're waiting for at least <url_param_post_limit> bytes after msg->sov. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3843 | */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3844 | |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 3845 | if (msg->body_len < limit) |
| 3846 | limit = msg->body_len; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3847 | |
Willy Tarreau | 7c96f67 | 2009-12-27 22:47:25 +0100 | [diff] [blame] | 3848 | if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3849 | goto http_end; |
| 3850 | |
| 3851 | missing_data: |
| 3852 | /* we get here if we need to wait for more data */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3853 | if (req->flags & BF_FULL) { |
| 3854 | session_inc_http_err_ctr(s); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3855 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 3856 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3857 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3858 | if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) { |
| 3859 | txn->status = 408; |
| 3860 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 3861 | |
| 3862 | if (!(s->flags & SN_ERR_MASK)) |
| 3863 | s->flags |= SN_ERR_CLITO; |
| 3864 | if (!(s->flags & SN_FINST_MASK)) |
| 3865 | s->flags |= SN_FINST_D; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3866 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3867 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3868 | |
| 3869 | /* we get here if we need to wait for more data */ |
| 3870 | if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3871 | /* Not enough data. We'll re-use the http-request |
| 3872 | * timeout here. Ideally, we should set the timeout |
| 3873 | * relative to the accept() date. We just set the |
| 3874 | * request timeout once at the beginning of the |
| 3875 | * request. |
| 3876 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3877 | buffer_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3878 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 3879 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3880 | return 0; |
| 3881 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3882 | |
| 3883 | http_end: |
| 3884 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 3885 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 3886 | req->analysers &= ~an_bit; |
| 3887 | req->analyse_exp = TICK_ETERNITY; |
| 3888 | return 1; |
| 3889 | |
| 3890 | return_bad_req: /* let's centralize all bad requests */ |
| 3891 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3892 | txn->status = 400; |
| 3893 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 3894 | |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 3895 | if (!(s->flags & SN_ERR_MASK)) |
| 3896 | s->flags |= SN_ERR_PRXCOND; |
| 3897 | if (!(s->flags & SN_FINST_MASK)) |
| 3898 | s->flags |= SN_FINST_R; |
| 3899 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3900 | return_err_msg: |
| 3901 | req->analysers = 0; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3902 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3903 | if (s->listener->counters) |
| 3904 | s->listener->counters->failed_req++; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3905 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3906 | } |
| 3907 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3908 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 3909 | * right now but it works. |
| 3910 | */ |
| 3911 | void http_end_txn_clean_session(struct session *s) |
| 3912 | { |
| 3913 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 3914 | * server's connections. We need a safer way to reinitialize buffer |
| 3915 | * flags. We also need a more accurate method for computing per-request |
| 3916 | * data. |
| 3917 | */ |
| 3918 | http_silent_debug(__LINE__, s); |
| 3919 | |
| 3920 | s->req->cons->flags |= SI_FL_NOLINGER; |
| 3921 | s->req->cons->shutr(s->req->cons); |
| 3922 | s->req->cons->shutw(s->req->cons); |
| 3923 | |
| 3924 | http_silent_debug(__LINE__, s); |
| 3925 | |
| 3926 | if (s->flags & SN_BE_ASSIGNED) |
| 3927 | s->be->beconn--; |
| 3928 | |
| 3929 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3930 | session_process_counters(s); |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 3931 | session_stop_backend_counters(s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3932 | |
| 3933 | if (s->txn.status) { |
| 3934 | int n; |
| 3935 | |
| 3936 | n = s->txn.status / 100; |
| 3937 | if (n < 1 || n > 5) |
| 3938 | n = 0; |
| 3939 | |
| 3940 | if (s->fe->mode == PR_MODE_HTTP) |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3941 | s->fe->fe_counters.p.http.rsp[n]++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3942 | |
Willy Tarreau | 2465779 | 2010-02-26 10:30:28 +0100 | [diff] [blame] | 3943 | if ((s->flags & SN_BE_ASSIGNED) && |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3944 | (s->be->mode == PR_MODE_HTTP)) |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 3945 | s->be->be_counters.p.http.rsp[n]++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3946 | } |
| 3947 | |
| 3948 | /* don't count other requests' data */ |
| 3949 | s->logs.bytes_in -= s->req->l - s->req->send_max; |
| 3950 | s->logs.bytes_out -= s->rep->l - s->rep->send_max; |
| 3951 | |
| 3952 | /* let's do a final log if we need it */ |
| 3953 | if (s->logs.logwait && |
| 3954 | !(s->flags & SN_MONITOR) && |
| 3955 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 3956 | s->do_log(s); |
| 3957 | } |
| 3958 | |
| 3959 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 3960 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 3961 | tv_zero(&s->logs.tv_request); |
| 3962 | s->logs.t_queue = -1; |
| 3963 | s->logs.t_connect = -1; |
| 3964 | s->logs.t_data = -1; |
| 3965 | s->logs.t_close = 0; |
| 3966 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 3967 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 3968 | |
| 3969 | s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max; |
| 3970 | s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max; |
| 3971 | |
| 3972 | if (s->pend_pos) |
| 3973 | pendconn_free(s->pend_pos); |
| 3974 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 3975 | if (target_srv(&s->target)) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3976 | if (s->flags & SN_CURR_SESS) { |
| 3977 | s->flags &= ~SN_CURR_SESS; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 3978 | target_srv(&s->target)->cur_sess--; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3979 | } |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 3980 | if (may_dequeue_tasks(target_srv(&s->target), s->be)) |
| 3981 | process_srv_queue(target_srv(&s->target)); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3982 | } |
| 3983 | |
| 3984 | if (unlikely(s->srv_conn)) |
| 3985 | sess_change_server(s, NULL); |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 3986 | clear_target(&s->target); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3987 | |
| 3988 | s->req->cons->state = s->req->cons->prev_state = SI_ST_INI; |
| 3989 | s->req->cons->fd = -1; /* just to help with debugging */ |
| 3990 | s->req->cons->err_type = SI_ET_NONE; |
Willy Tarreau | 0b3a411 | 2011-03-27 19:16:56 +0200 | [diff] [blame] | 3991 | s->req->cons->conn_retries = 0; /* used for logging too */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3992 | s->req->cons->err_loc = NULL; |
| 3993 | s->req->cons->exp = TICK_ETERNITY; |
| 3994 | s->req->cons->flags = SI_FL_NONE; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3995 | s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST); |
| 3996 | s->rep->flags &= ~(BF_SHUTR|BF_SHUTR_NOW|BF_READ_ATTACHED|BF_READ_ERROR|BF_READ_NOEXP|BF_STREAMER|BF_STREAMER_FAST|BF_WRITE_PARTIAL); |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 3997 | 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] | 3998 | s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE); |
| 3999 | s->txn.meth = 0; |
| 4000 | http_reset_txn(s); |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 4001 | s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | ee55dc0 | 2010-06-01 10:56:34 +0200 | [diff] [blame] | 4002 | if (s->fe->options2 & PR_O2_INDEPSTR) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4003 | s->req->cons->flags |= SI_FL_INDEP_STR; |
| 4004 | |
| 4005 | /* if the request buffer is not empty, it means we're |
| 4006 | * about to process another request, so send pending |
| 4007 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 4008 | * Just don't do this if the buffer is close to be full, |
| 4009 | * because the request will wait for it to flush a little |
| 4010 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4011 | */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 4012 | if (s->req->l > s->req->send_max) { |
| 4013 | if (s->rep->send_max && |
| 4014 | !(s->rep->flags & BF_FULL) && |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 4015 | s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite) |
| 4016 | s->rep->flags |= BF_EXPECT_MORE; |
| 4017 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4018 | |
| 4019 | /* we're removing the analysers, we MUST re-enable events detection */ |
| 4020 | buffer_auto_read(s->req); |
| 4021 | buffer_auto_close(s->req); |
| 4022 | buffer_auto_read(s->rep); |
| 4023 | buffer_auto_close(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4024 | |
| 4025 | /* make ->lr point to the first non-forwarded byte */ |
| 4026 | s->req->lr = s->req->w + s->req->send_max; |
| 4027 | if (s->req->lr >= s->req->data + s->req->size) |
| 4028 | s->req->lr -= s->req->size; |
| 4029 | s->rep->lr = s->rep->w + s->rep->send_max; |
| 4030 | if (s->rep->lr >= s->rep->data + s->rep->size) |
| 4031 | s->rep->lr -= s->req->size; |
| 4032 | |
Willy Tarreau | 342b11c | 2010-11-24 16:22:09 +0100 | [diff] [blame] | 4033 | s->req->analysers = s->listener->analysers; |
| 4034 | s->req->analysers &= ~AN_REQ_DECODE_PROXY; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4035 | s->rep->analysers = 0; |
| 4036 | |
| 4037 | http_silent_debug(__LINE__, s); |
| 4038 | } |
| 4039 | |
| 4040 | |
| 4041 | /* This function updates the request state machine according to the response |
| 4042 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4043 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4044 | * it is only used to find when a request/response couple is complete. Both |
| 4045 | * this function and its equivalent should loop until both return zero. It |
| 4046 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4047 | */ |
| 4048 | int http_sync_req_state(struct session *s) |
| 4049 | { |
| 4050 | struct buffer *buf = s->req; |
| 4051 | struct http_txn *txn = &s->txn; |
| 4052 | unsigned int old_flags = buf->flags; |
| 4053 | unsigned int old_state = txn->req.msg_state; |
| 4054 | |
| 4055 | http_silent_debug(__LINE__, s); |
| 4056 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 4057 | return 0; |
| 4058 | |
| 4059 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4060 | /* No need to read anymore, the request was completely parsed. |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4061 | * We can shut the read side unless we want to abort_on_close, |
| 4062 | * or we have a POST request. The issue with POST requests is |
| 4063 | * that some browsers still send a CRLF after the request, and |
| 4064 | * this CRLF must be read so that it does not remain in the kernel |
| 4065 | * buffers, otherwise a close could cause an RST on some systems |
| 4066 | * (eg: Linux). |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4067 | */ |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4068 | if (!(s->be->options & PR_O_ABRT_CLOSE) && txn->meth != HTTP_METH_POST) |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4069 | buffer_dont_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4070 | |
| 4071 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 4072 | goto wait_other_side; |
| 4073 | |
| 4074 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 4075 | /* The server has not finished to respond, so we |
| 4076 | * don't want to move in order not to upset it. |
| 4077 | */ |
| 4078 | goto wait_other_side; |
| 4079 | } |
| 4080 | |
| 4081 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 4082 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4083 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4084 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 4085 | goto wait_other_side; |
| 4086 | } |
| 4087 | |
| 4088 | /* When we get here, it means that both the request and the |
| 4089 | * response have finished receiving. Depending on the connection |
| 4090 | * mode, we'll have to wait for the last bytes to leave in either |
| 4091 | * direction, and sometimes for a close to be effective. |
| 4092 | */ |
| 4093 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4094 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4095 | /* Server-close mode : queue a connection close to the server */ |
| 4096 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4097 | buffer_shutw_now(buf); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4098 | } |
| 4099 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4100 | /* Option forceclose is set, or either side wants to close, |
| 4101 | * let's enforce it now that we're not expecting any new |
| 4102 | * data to come. The caller knows the session is complete |
| 4103 | * once both states are CLOSED. |
| 4104 | */ |
| 4105 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4106 | buffer_shutr_now(buf); |
| 4107 | buffer_shutw_now(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4108 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4109 | } |
| 4110 | else { |
| 4111 | /* The last possible modes are keep-alive and tunnel. Since tunnel |
| 4112 | * mode does not set the body analyser, we can't reach this place |
| 4113 | * in tunnel mode, so we're left with keep-alive only. |
| 4114 | * This mode is currently not implemented, we switch to tunnel mode. |
| 4115 | */ |
| 4116 | buffer_auto_read(buf); |
| 4117 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4118 | } |
| 4119 | |
| 4120 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) { |
| 4121 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4122 | buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
| 4123 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4124 | if (!(buf->flags & BF_OUT_EMPTY)) { |
| 4125 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 4126 | goto http_msg_closing; |
| 4127 | } |
| 4128 | else { |
| 4129 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4130 | goto http_msg_closed; |
| 4131 | } |
| 4132 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4133 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4134 | } |
| 4135 | |
| 4136 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 4137 | http_msg_closing: |
| 4138 | /* nothing else to forward, just waiting for the output buffer |
| 4139 | * to be empty and for the shutw_now to take effect. |
| 4140 | */ |
| 4141 | if (buf->flags & BF_OUT_EMPTY) { |
| 4142 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4143 | goto http_msg_closed; |
| 4144 | } |
| 4145 | else if (buf->flags & BF_SHUTW) { |
| 4146 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4147 | goto wait_other_side; |
| 4148 | } |
| 4149 | } |
| 4150 | |
| 4151 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 4152 | http_msg_closed: |
| 4153 | goto wait_other_side; |
| 4154 | } |
| 4155 | |
| 4156 | wait_other_side: |
| 4157 | http_silent_debug(__LINE__, s); |
| 4158 | return txn->req.msg_state != old_state || buf->flags != old_flags; |
| 4159 | } |
| 4160 | |
| 4161 | |
| 4162 | /* This function updates the response state machine according to the request |
| 4163 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 4164 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 4165 | * it is only used to find when a request/response couple is complete. Both |
| 4166 | * this function and its equivalent should loop until both return zero. It |
| 4167 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 4168 | */ |
| 4169 | int http_sync_res_state(struct session *s) |
| 4170 | { |
| 4171 | struct buffer *buf = s->rep; |
| 4172 | struct http_txn *txn = &s->txn; |
| 4173 | unsigned int old_flags = buf->flags; |
| 4174 | unsigned int old_state = txn->rsp.msg_state; |
| 4175 | |
| 4176 | http_silent_debug(__LINE__, s); |
| 4177 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 4178 | return 0; |
| 4179 | |
| 4180 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 4181 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4182 | * still monitor the server connection for a possible close |
| 4183 | * while the request is being uploaded, so we don't disable |
| 4184 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4185 | */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4186 | /* buffer_dont_read(buf); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4187 | |
| 4188 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 4189 | goto wait_other_side; |
| 4190 | |
| 4191 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 4192 | /* The client seems to still be sending data, probably |
| 4193 | * because we got an error response during an upload. |
| 4194 | * We have the choice of either breaking the connection |
| 4195 | * or letting it pass through. Let's do the later. |
| 4196 | */ |
| 4197 | goto wait_other_side; |
| 4198 | } |
| 4199 | |
| 4200 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 4201 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4202 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4203 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
| 4204 | goto wait_other_side; |
| 4205 | } |
| 4206 | |
| 4207 | /* When we get here, it means that both the request and the |
| 4208 | * response have finished receiving. Depending on the connection |
| 4209 | * mode, we'll have to wait for the last bytes to leave in either |
| 4210 | * direction, and sometimes for a close to be effective. |
| 4211 | */ |
| 4212 | |
| 4213 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4214 | /* Server-close mode : shut read and wait for the request |
| 4215 | * side to close its output buffer. The caller will detect |
| 4216 | * when we're in DONE and the other is in CLOSED and will |
| 4217 | * catch that for the final cleanup. |
| 4218 | */ |
| 4219 | if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW))) |
| 4220 | buffer_shutr_now(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4221 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4222 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4223 | /* Option forceclose is set, or either side wants to close, |
| 4224 | * let's enforce it now that we're not expecting any new |
| 4225 | * data to come. The caller knows the session is complete |
| 4226 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4227 | */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4228 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) { |
| 4229 | buffer_shutr_now(buf); |
| 4230 | buffer_shutw_now(buf); |
| 4231 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4232 | } |
| 4233 | else { |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 4234 | /* The last possible modes are keep-alive and tunnel. Since tunnel |
| 4235 | * mode does not set the body analyser, we can't reach this place |
| 4236 | * in tunnel mode, so we're left with keep-alive only. |
| 4237 | * This mode is currently not implemented, we switch to tunnel mode. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4238 | */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4239 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4240 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4241 | } |
| 4242 | |
| 4243 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) { |
| 4244 | /* if we've just closed an output, let's switch */ |
| 4245 | if (!(buf->flags & BF_OUT_EMPTY)) { |
| 4246 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 4247 | goto http_msg_closing; |
| 4248 | } |
| 4249 | else { |
| 4250 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4251 | goto http_msg_closed; |
| 4252 | } |
| 4253 | } |
| 4254 | goto wait_other_side; |
| 4255 | } |
| 4256 | |
| 4257 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 4258 | http_msg_closing: |
| 4259 | /* nothing else to forward, just waiting for the output buffer |
| 4260 | * to be empty and for the shutw_now to take effect. |
| 4261 | */ |
| 4262 | if (buf->flags & BF_OUT_EMPTY) { |
| 4263 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 4264 | goto http_msg_closed; |
| 4265 | } |
| 4266 | else if (buf->flags & BF_SHUTW) { |
| 4267 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4268 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4269 | if (target_srv(&s->target)) |
| 4270 | target_srv(&s->target)->counters.cli_aborts++; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4271 | goto wait_other_side; |
| 4272 | } |
| 4273 | } |
| 4274 | |
| 4275 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 4276 | http_msg_closed: |
| 4277 | /* drop any pending data */ |
| 4278 | buffer_ignore(buf, buf->l - buf->send_max); |
| 4279 | buffer_auto_close(buf); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4280 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4281 | goto wait_other_side; |
| 4282 | } |
| 4283 | |
| 4284 | wait_other_side: |
| 4285 | http_silent_debug(__LINE__, s); |
| 4286 | return txn->rsp.msg_state != old_state || buf->flags != old_flags; |
| 4287 | } |
| 4288 | |
| 4289 | |
| 4290 | /* Resync the request and response state machines. Return 1 if either state |
| 4291 | * changes. |
| 4292 | */ |
| 4293 | int http_resync_states(struct session *s) |
| 4294 | { |
| 4295 | struct http_txn *txn = &s->txn; |
| 4296 | int old_req_state = txn->req.msg_state; |
| 4297 | int old_res_state = txn->rsp.msg_state; |
| 4298 | |
| 4299 | http_silent_debug(__LINE__, s); |
| 4300 | http_sync_req_state(s); |
| 4301 | while (1) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4302 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4303 | if (!http_sync_res_state(s)) |
| 4304 | break; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4305 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4306 | if (!http_sync_req_state(s)) |
| 4307 | break; |
| 4308 | } |
| 4309 | http_silent_debug(__LINE__, s); |
| 4310 | /* OK, both state machines agree on a compatible state. |
| 4311 | * There are a few cases we're interested in : |
| 4312 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 4313 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 4314 | * directions, so let's simply disable both analysers. |
| 4315 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 4316 | * request. |
| 4317 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 4318 | * with server-close mode means we've completed one request and we |
| 4319 | * must re-initialize the server connection. |
| 4320 | */ |
| 4321 | |
| 4322 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 4323 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 4324 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4325 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
| 4326 | s->req->analysers = 0; |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4327 | buffer_auto_close(s->req); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4328 | buffer_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4329 | s->rep->analysers = 0; |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4330 | buffer_auto_close(s->rep); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4331 | buffer_auto_read(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4332 | } |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4333 | else if (txn->rsp.msg_state == HTTP_MSG_CLOSED || |
| 4334 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4335 | txn->req.msg_state == HTTP_MSG_ERROR || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4336 | (s->rep->flags & BF_SHUTW)) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4337 | s->rep->analysers = 0; |
| 4338 | buffer_auto_close(s->rep); |
| 4339 | buffer_auto_read(s->rep); |
| 4340 | s->req->analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4341 | buffer_abort(s->req); |
| 4342 | buffer_auto_close(s->req); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4343 | buffer_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4344 | buffer_ignore(s->req, s->req->l - s->req->send_max); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4345 | } |
| 4346 | else if (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4347 | txn->rsp.msg_state == HTTP_MSG_DONE && |
| 4348 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) { |
| 4349 | /* server-close: terminate this server connection and |
| 4350 | * reinitialize a fresh-new transaction. |
| 4351 | */ |
| 4352 | http_end_txn_clean_session(s); |
| 4353 | } |
| 4354 | |
| 4355 | http_silent_debug(__LINE__, s); |
| 4356 | return txn->req.msg_state != old_req_state || |
| 4357 | txn->rsp.msg_state != old_res_state; |
| 4358 | } |
| 4359 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4360 | /* This function is an analyser which forwards request body (including chunk |
| 4361 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 4362 | * zero byte. The only situation where it must not be called is when we're in |
| 4363 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 4364 | * remaining data and to resync after end of body. It expects the msg_state to |
| 4365 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 4366 | * 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] | 4367 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4368 | * bytes of pending data + the headers if not already done (between som and sov). |
| 4369 | * It eventually adjusts som to match sov after the data in between have been sent. |
| 4370 | */ |
| 4371 | int http_request_forward_body(struct session *s, struct buffer *req, int an_bit) |
| 4372 | { |
| 4373 | struct http_txn *txn = &s->txn; |
| 4374 | struct http_msg *msg = &s->txn.req; |
| 4375 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4376 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4377 | return 0; |
| 4378 | |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 4379 | if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) || |
| 4380 | ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4381 | /* Output closed while we were sending data. We must abort and |
| 4382 | * wake the other side up. |
| 4383 | */ |
| 4384 | msg->msg_state = HTTP_MSG_ERROR; |
| 4385 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 4386 | return 1; |
| 4387 | } |
| 4388 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4389 | /* in most states, we should abort in case of early close */ |
| 4390 | buffer_auto_close(req); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4391 | |
| 4392 | /* Note that we don't have to send 100-continue back because we don't |
| 4393 | * need the data to complete our job, and it's up to the server to |
| 4394 | * decide whether to return 100, 417 or anything else in return of |
| 4395 | * an "Expect: 100-continue" header. |
| 4396 | */ |
| 4397 | |
| 4398 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4399 | /* we have msg->col and msg->sov which both point to the first |
| 4400 | * byte of message body. msg->som still points to the beginning |
| 4401 | * of the message. We must save the body in req->lr because it |
| 4402 | * survives buffer re-alignments. |
| 4403 | */ |
| 4404 | req->lr = req->data + msg->sov; |
| 4405 | if (txn->flags & TX_REQ_TE_CHNK) |
| 4406 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4407 | else { |
| 4408 | msg->msg_state = HTTP_MSG_DATA; |
| 4409 | } |
| 4410 | } |
| 4411 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4412 | while (1) { |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4413 | int bytes; |
| 4414 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4415 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4416 | /* we may have some data pending */ |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4417 | bytes = msg->sov - msg->som; |
| 4418 | if (msg->chunk_len || bytes) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4419 | msg->som = msg->sov; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 4420 | if (likely(bytes < 0)) /* sov may have wrapped at the end */ |
| 4421 | bytes += req->size; |
| 4422 | msg->chunk_len += (unsigned int)bytes; |
| 4423 | msg->chunk_len -= buffer_forward(req, msg->chunk_len); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4424 | } |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 4425 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4426 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 4427 | /* must still forward */ |
| 4428 | if (req->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4429 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4430 | |
| 4431 | /* nothing left to forward */ |
| 4432 | if (txn->flags & TX_REQ_TE_CHNK) |
| 4433 | msg->msg_state = HTTP_MSG_DATA_CRLF; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4434 | else |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4435 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4436 | } |
| 4437 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4438 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4439 | * set ->sov and ->lr to point to the body and switch to DATA or |
| 4440 | * TRAILERS state. |
| 4441 | */ |
| 4442 | int ret = http_parse_chunk_size(req, msg); |
| 4443 | |
| 4444 | if (!ret) |
| 4445 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4446 | else if (ret < 0) { |
| 4447 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4448 | if (msg->err_pos >= 0) |
| 4449 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_CHUNK_SIZE, s->be); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4450 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4451 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4452 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | 57f5c12 | 2010-12-02 00:37:14 +0100 | [diff] [blame] | 4453 | /* Don't set a PUSH at the end of that chunk if it's not the last one */ |
| 4454 | if (msg->msg_state == HTTP_MSG_DATA) |
| 4455 | req->flags |= BF_EXPECT_MORE; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4456 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4457 | else if (msg->msg_state == HTTP_MSG_DATA_CRLF) { |
| 4458 | /* we want the CRLF after the data */ |
| 4459 | int ret; |
| 4460 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 4461 | req->lr = req->w + req->send_max; |
| 4462 | if (req->lr >= req->data + req->size) |
| 4463 | req->lr -= req->size; |
| 4464 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4465 | ret = http_skip_chunk_crlf(req, msg); |
| 4466 | |
| 4467 | if (ret == 0) |
| 4468 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4469 | else if (ret < 0) { |
| 4470 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4471 | if (msg->err_pos >= 0) |
| 4472 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_DATA_CRLF, s->be); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4473 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4474 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4475 | /* we're in MSG_CHUNK_SIZE now */ |
| 4476 | } |
| 4477 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
| 4478 | int ret = http_forward_trailers(req, msg); |
| 4479 | |
| 4480 | if (ret == 0) |
| 4481 | goto missing_data; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4482 | else if (ret < 0) { |
| 4483 | session_inc_http_err_ctr(s); |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4484 | if (msg->err_pos >= 0) |
| 4485 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, HTTP_MSG_TRAILERS, s->be); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4486 | goto return_bad_req; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4487 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4488 | /* we're in HTTP_MSG_DONE now */ |
| 4489 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4490 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4491 | int old_state = msg->msg_state; |
| 4492 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4493 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 4494 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4495 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 4496 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 4497 | buffer_dont_close(req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4498 | if (http_resync_states(s)) { |
| 4499 | /* some state changes occurred, maybe the analyser |
| 4500 | * was disabled too. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 4501 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4502 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4503 | if (req->flags & BF_SHUTW) { |
| 4504 | /* request errors are most likely due to |
| 4505 | * the server aborting the transfer. |
| 4506 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4507 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4508 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 4509 | if (msg->err_pos >= 0) |
| 4510 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, old_state, s->be); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4511 | goto return_bad_req; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 4512 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4513 | return 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4514 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4515 | |
| 4516 | /* If "option abortonclose" is set on the backend, we |
| 4517 | * want to monitor the client's connection and forward |
| 4518 | * any shutdown notification to the server, which will |
| 4519 | * decide whether to close or to go on processing the |
| 4520 | * request. |
| 4521 | */ |
| 4522 | if (s->be->options & PR_O_ABRT_CLOSE) { |
| 4523 | buffer_auto_read(req); |
| 4524 | buffer_auto_close(req); |
| 4525 | } |
Willy Tarreau | 58bd8fd | 2010-09-28 14:16:41 +0200 | [diff] [blame] | 4526 | else if (s->txn.meth == HTTP_METH_POST) { |
| 4527 | /* POST requests may require to read extra CRLF |
| 4528 | * sent by broken browsers and which could cause |
| 4529 | * an RST to be sent upon close on some systems |
| 4530 | * (eg: Linux). |
| 4531 | */ |
| 4532 | buffer_auto_read(req); |
| 4533 | } |
Willy Tarreau | 5c54c71 | 2010-07-17 08:02:58 +0200 | [diff] [blame] | 4534 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4535 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4536 | } |
| 4537 | } |
| 4538 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4539 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4540 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4541 | if (req->flags & BF_SHUTR) { |
| 4542 | if (!(s->flags & SN_ERR_MASK)) |
| 4543 | s->flags |= SN_ERR_CLICL; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4544 | if (!(s->flags & SN_FINST_MASK)) { |
| 4545 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4546 | s->flags |= SN_FINST_H; |
| 4547 | else |
| 4548 | s->flags |= SN_FINST_D; |
| 4549 | } |
| 4550 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4551 | s->fe->fe_counters.cli_aborts++; |
| 4552 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4553 | if (target_srv(&s->target)) |
| 4554 | target_srv(&s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4555 | |
| 4556 | goto return_bad_req_stats_ok; |
Willy Tarreau | 79ebac6 | 2010-06-07 13:47:49 +0200 | [diff] [blame] | 4557 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4558 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4559 | /* waiting for the last bits to leave the buffer */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4560 | if (req->flags & BF_SHUTW) |
| 4561 | goto aborted_xfer; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4562 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 4563 | /* When TE: chunked is used, we need to get there again to parse remaining |
| 4564 | * chunks even if the client has closed, so we don't want to set BF_DONTCLOSE. |
| 4565 | */ |
| 4566 | if (txn->flags & TX_REQ_TE_CHNK) |
| 4567 | buffer_dont_close(req); |
| 4568 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4569 | http_silent_debug(__LINE__, s); |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4570 | return 0; |
| 4571 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4572 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4573 | s->fe->fe_counters.failed_req++; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4574 | if (s->listener->counters) |
| 4575 | s->listener->counters->failed_req++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4576 | return_bad_req_stats_ok: |
| 4577 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4578 | if (txn->status) { |
| 4579 | /* Note: we don't send any error if some data were already sent */ |
| 4580 | stream_int_retnclose(req->prod, NULL); |
| 4581 | } else { |
| 4582 | txn->status = 400; |
| 4583 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 4584 | } |
| 4585 | req->analysers = 0; |
| 4586 | 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] | 4587 | |
| 4588 | if (!(s->flags & SN_ERR_MASK)) |
| 4589 | s->flags |= SN_ERR_PRXCOND; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4590 | if (!(s->flags & SN_FINST_MASK)) { |
| 4591 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4592 | s->flags |= SN_FINST_H; |
| 4593 | else |
| 4594 | s->flags |= SN_FINST_D; |
| 4595 | } |
| 4596 | return 0; |
| 4597 | |
| 4598 | aborted_xfer: |
| 4599 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4600 | if (txn->status) { |
| 4601 | /* Note: we don't send any error if some data were already sent */ |
| 4602 | stream_int_retnclose(req->prod, NULL); |
| 4603 | } else { |
| 4604 | txn->status = 502; |
| 4605 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_502)); |
| 4606 | } |
| 4607 | req->analysers = 0; |
| 4608 | s->rep->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 4609 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4610 | s->fe->fe_counters.srv_aborts++; |
| 4611 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4612 | if (target_srv(&s->target)) |
| 4613 | target_srv(&s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 4614 | |
| 4615 | if (!(s->flags & SN_ERR_MASK)) |
| 4616 | s->flags |= SN_ERR_SRVCL; |
| 4617 | if (!(s->flags & SN_FINST_MASK)) { |
| 4618 | if (txn->rsp.msg_state < HTTP_MSG_ERROR) |
| 4619 | s->flags |= SN_FINST_H; |
| 4620 | else |
| 4621 | s->flags |= SN_FINST_D; |
| 4622 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4623 | return 0; |
| 4624 | } |
| 4625 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4626 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 4627 | * processing can continue on next analysers, or zero if it either needs more |
| 4628 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 4629 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers |
| 4630 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 4631 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4632 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4633 | int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit) |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4634 | { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4635 | struct http_txn *txn = &s->txn; |
| 4636 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4637 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4638 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4639 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4640 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4641 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4642 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 4643 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4644 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4645 | rep, |
| 4646 | rep->rex, rep->wex, |
| 4647 | rep->flags, |
| 4648 | rep->l, |
| 4649 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 4650 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4651 | /* |
| 4652 | * Now parse the partial (or complete) lines. |
| 4653 | * We will check the response syntax, and also join multi-line |
| 4654 | * headers. An index of all the lines will be elaborated while |
| 4655 | * parsing. |
| 4656 | * |
| 4657 | * For the parsing, we use a 28 states FSM. |
| 4658 | * |
| 4659 | * Here is the information we currently have : |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4660 | * rep->data + msg->som = beginning of response |
| 4661 | * rep->data + msg->eoh = end of processed headers / start of current one |
| 4662 | * msg->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4663 | * rep->lr = first non-visited byte |
| 4664 | * rep->r = end of data |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4665 | * Once we reach MSG_BODY, rep->sol = rep->data + msg->som |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4666 | */ |
| 4667 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4668 | /* There's a protected area at the end of the buffer for rewriting |
| 4669 | * purposes. We don't want to start to parse the request if the |
| 4670 | * protected area is affected, because we may have to move processed |
| 4671 | * data later, which is much more complicated. |
| 4672 | */ |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4673 | if (rep->l && msg->msg_state < HTTP_MSG_ERROR) { |
| 4674 | if (unlikely((rep->flags & BF_FULL) || |
| 4675 | rep->r < rep->lr || |
| 4676 | rep->r > rep->data + rep->size - global.tune.maxrewrite)) { |
| 4677 | if (rep->send_max) { |
| 4678 | /* some data has still not left the buffer, wake us once that's done */ |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 4679 | if (rep->flags & (BF_SHUTW|BF_SHUTW_NOW|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) |
| 4680 | goto abort_response; |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4681 | buffer_dont_close(rep); |
| 4682 | rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 4683 | return 0; |
| 4684 | } |
| 4685 | if (rep->l <= rep->size - global.tune.maxrewrite) |
| 4686 | http_buffer_heavy_realign(rep, msg); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4687 | } |
| 4688 | |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4689 | if (likely(rep->lr < rep->r)) |
| 4690 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4691 | } |
| 4692 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4693 | /* 1: we might have to print this header in debug mode */ |
| 4694 | if (unlikely((global.mode & MODE_DEBUG) && |
| 4695 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | c3bfeeb | 2010-04-16 09:14:45 +0200 | [diff] [blame] | 4696 | msg->sol && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4697 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4698 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4699 | |
Willy Tarreau | 663308b | 2010-06-07 14:06:08 +0200 | [diff] [blame] | 4700 | sol = rep->data + msg->som; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 4701 | eol = sol + msg->sl.st.l; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4702 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4703 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4704 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 4705 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4706 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4707 | while (cur_idx) { |
| 4708 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 4709 | debug_hdr("srvhdr", s, sol, eol); |
| 4710 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 4711 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 4712 | } |
| 4713 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4714 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4715 | /* |
| 4716 | * Now we quickly check if we have found a full valid response. |
| 4717 | * If not so, we check the FD and buffer states before leaving. |
| 4718 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4719 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4720 | * responses are checked first. |
| 4721 | * |
| 4722 | * Depending on whether the client is still there or not, we |
| 4723 | * may send an error response back or not. Note that normally |
| 4724 | * we should only check for HTTP status there, and check I/O |
| 4725 | * errors somewhere else. |
| 4726 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4727 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4728 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4729 | /* Invalid response */ |
| 4730 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4731 | /* we detected a parsing error. We want to archive this response |
| 4732 | * in the dedicated proxy area for later troubleshooting. |
| 4733 | */ |
| 4734 | hdr_response_bad: |
| 4735 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 4736 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4737 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4738 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4739 | if (target_srv(&s->target)) { |
| 4740 | target_srv(&s->target)->counters.failed_resp++; |
| 4741 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4742 | } |
Willy Tarreau | 6464841 | 2010-03-05 10:41:54 +0100 | [diff] [blame] | 4743 | abort_response: |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4744 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4745 | rep->analysers = 0; |
| 4746 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4747 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4748 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4749 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
| 4750 | |
| 4751 | if (!(s->flags & SN_ERR_MASK)) |
| 4752 | s->flags |= SN_ERR_PRXCOND; |
| 4753 | if (!(s->flags & SN_FINST_MASK)) |
| 4754 | s->flags |= SN_FINST_H; |
| 4755 | |
| 4756 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4757 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4758 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4759 | /* too large response does not fit in buffer. */ |
| 4760 | else if (rep->flags & BF_FULL) { |
| 4761 | goto hdr_response_bad; |
| 4762 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4763 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4764 | /* read error */ |
| 4765 | else if (rep->flags & BF_READ_ERROR) { |
| 4766 | if (msg->err_pos >= 0) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 4767 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4768 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4769 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4770 | if (target_srv(&s->target)) { |
| 4771 | target_srv(&s->target)->counters.failed_resp++; |
| 4772 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_ERROR); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4773 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 4774 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4775 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4776 | rep->analysers = 0; |
| 4777 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4778 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4779 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4780 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 4781 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4782 | if (!(s->flags & SN_ERR_MASK)) |
| 4783 | s->flags |= SN_ERR_SRVCL; |
| 4784 | if (!(s->flags & SN_FINST_MASK)) |
| 4785 | s->flags |= SN_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 4786 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4787 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4788 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4789 | /* read timeout : return a 504 to the client. */ |
| 4790 | else if (rep->flags & BF_READ_TIMEOUT) { |
| 4791 | if (msg->err_pos >= 0) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 4792 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4793 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4794 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4795 | if (target_srv(&s->target)) { |
| 4796 | target_srv(&s->target)->counters.failed_resp++; |
| 4797 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_READ_TIMEOUT); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4798 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4799 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4800 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4801 | rep->analysers = 0; |
| 4802 | txn->status = 504; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4803 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4804 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4805 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4806 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4807 | if (!(s->flags & SN_ERR_MASK)) |
| 4808 | s->flags |= SN_ERR_SRVTO; |
| 4809 | if (!(s->flags & SN_FINST_MASK)) |
| 4810 | s->flags |= SN_FINST_H; |
| 4811 | return 0; |
| 4812 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 4813 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4814 | /* close from server */ |
| 4815 | else if (rep->flags & BF_SHUTR) { |
| 4816 | if (msg->err_pos >= 0) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 4817 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4818 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4819 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4820 | if (target_srv(&s->target)) { |
| 4821 | target_srv(&s->target)->counters.failed_resp++; |
| 4822 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_BROKEN_PIPE); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4823 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4824 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4825 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4826 | rep->analysers = 0; |
| 4827 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4828 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4829 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4830 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4831 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4832 | if (!(s->flags & SN_ERR_MASK)) |
| 4833 | s->flags |= SN_ERR_SRVCL; |
| 4834 | if (!(s->flags & SN_FINST_MASK)) |
| 4835 | s->flags |= SN_FINST_H; |
| 4836 | return 0; |
| 4837 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4838 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4839 | /* write error to client (we don't send any message then) */ |
| 4840 | else if (rep->flags & BF_WRITE_ERROR) { |
| 4841 | if (msg->err_pos >= 0) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 4842 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe); |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4843 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 4844 | s->be->be_counters.failed_resp++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4845 | rep->analysers = 0; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4846 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4847 | |
| 4848 | if (!(s->flags & SN_ERR_MASK)) |
| 4849 | s->flags |= SN_ERR_CLICL; |
| 4850 | if (!(s->flags & SN_FINST_MASK)) |
| 4851 | s->flags |= SN_FINST_H; |
| 4852 | |
| 4853 | /* process_session() will take care of the error */ |
| 4854 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4855 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4856 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4857 | buffer_dont_close(rep); |
| 4858 | return 0; |
| 4859 | } |
| 4860 | |
| 4861 | /* More interesting part now : we know that we have a complete |
| 4862 | * response which at least looks like HTTP. We have an indicator |
| 4863 | * of each header's length, so we can parse them quickly. |
| 4864 | */ |
| 4865 | |
| 4866 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 4867 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, msg->msg_state, s->fe); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4868 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4869 | /* |
| 4870 | * 1: get the status code |
| 4871 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4872 | n = msg->sol[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4873 | if (n < 1 || n > 5) |
| 4874 | n = 0; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 4875 | /* when the client triggers a 4xx from the server, it's most often due |
| 4876 | * to a missing object or permission. These events should be tracked |
| 4877 | * because if they happen often, it may indicate a brute force or a |
| 4878 | * vulnerability scan. |
| 4879 | */ |
| 4880 | if (n == 4) |
| 4881 | session_inc_http_err_ctr(s); |
| 4882 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4883 | if (target_srv(&s->target)) |
| 4884 | target_srv(&s->target)->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4885 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4886 | /* check if the response is HTTP/1.1 or above */ |
| 4887 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4888 | ((msg->sol[5] > '1') || |
| 4889 | ((msg->sol[5] == '1') && |
| 4890 | (msg->sol[7] >= '1')))) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4891 | txn->flags |= TX_RES_VER_11; |
| 4892 | |
| 4893 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4894 | txn->flags &= ~(TX_HDR_CONN_PRS|TX_HDR_CONN_CLO|TX_HDR_CONN_KAL|TX_CON_CLO_SET|TX_CON_KAL_SET); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4895 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4896 | /* transfer length unknown*/ |
| 4897 | txn->flags &= ~TX_RES_XFER_LEN; |
| 4898 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4899 | txn->status = strl2ui(msg->sol + msg->sl.st.c, msg->sl.st.c_l); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4900 | |
Willy Tarreau | 3965040 | 2010-03-15 19:44:39 +0100 | [diff] [blame] | 4901 | /* Adjust server's health based on status code. Note: status codes 501 |
| 4902 | * and 505 are triggered on demand by client request, so we must not |
| 4903 | * count them as server failures. |
| 4904 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4905 | if (target_srv(&s->target)) { |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4906 | if (txn->status >= 100 && (txn->status < 500 || txn->status == 501 || txn->status == 505)) |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4907 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_OK); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4908 | else |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 4909 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_STS); |
Willy Tarreau | d45b3d5 | 2010-05-20 11:49:03 +0200 | [diff] [blame] | 4910 | } |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4911 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4912 | /* |
| 4913 | * 2: check for cacheability. |
| 4914 | */ |
| 4915 | |
| 4916 | switch (txn->status) { |
| 4917 | case 200: |
| 4918 | case 203: |
| 4919 | case 206: |
| 4920 | case 300: |
| 4921 | case 301: |
| 4922 | case 410: |
| 4923 | /* RFC2616 @13.4: |
| 4924 | * "A response received with a status code of |
| 4925 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 4926 | * by a cache (...) unless a cache-control |
| 4927 | * directive prohibits caching." |
| 4928 | * |
| 4929 | * RFC2616 @9.5: POST method : |
| 4930 | * "Responses to this method are not cacheable, |
| 4931 | * unless the response includes appropriate |
| 4932 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4933 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4934 | if (likely(txn->meth != HTTP_METH_POST) && |
| 4935 | (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
| 4936 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 4937 | break; |
| 4938 | default: |
| 4939 | break; |
| 4940 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4941 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4942 | /* |
| 4943 | * 3: we may need to capture headers |
| 4944 | */ |
| 4945 | s->logs.logwait &= ~LW_RESP; |
| 4946 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap)) |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4947 | capture_headers(msg->sol, &txn->hdr_idx, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4948 | txn->rsp.cap, s->fe->rsp_cap); |
| 4949 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4950 | /* 4: determine the transfer-length. |
| 4951 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 4952 | * the presence of a message-body in a RESPONSE and its transfer length |
| 4953 | * must be determined that way : |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4954 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4955 | * All responses to the HEAD request method MUST NOT include a |
| 4956 | * message-body, even though the presence of entity-header fields |
| 4957 | * might lead one to believe they do. All 1xx (informational), 204 |
| 4958 | * (No Content), and 304 (Not Modified) responses MUST NOT include a |
| 4959 | * message-body. All other responses do include a message-body, |
| 4960 | * although it MAY be of zero length. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4961 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4962 | * 1. Any response which "MUST NOT" include a message-body (such as the |
| 4963 | * 1xx, 204 and 304 responses and any response to a HEAD request) is |
| 4964 | * always terminated by the first empty line after the header fields, |
| 4965 | * regardless of the entity-header fields present in the message. |
| 4966 | * |
| 4967 | * 2. If a Transfer-Encoding header field (Section 9.7) is present and |
| 4968 | * the "chunked" transfer-coding (Section 6.2) is used, the |
| 4969 | * transfer-length is defined by the use of this transfer-coding. |
| 4970 | * If a Transfer-Encoding header field is present and the "chunked" |
| 4971 | * transfer-coding is not present, the transfer-length is defined by |
| 4972 | * the sender closing the connection. |
| 4973 | * |
| 4974 | * 3. If a Content-Length header field is present, its decimal value in |
| 4975 | * OCTETs represents both the entity-length and the transfer-length. |
| 4976 | * If a message is received with both a Transfer-Encoding header |
| 4977 | * field and a Content-Length header field, the latter MUST be ignored. |
| 4978 | * |
| 4979 | * 4. If the message uses the media type "multipart/byteranges", and |
| 4980 | * the transfer-length is not otherwise specified, then this self- |
| 4981 | * delimiting media type defines the transfer-length. This media |
| 4982 | * type MUST NOT be used unless the sender knows that the recipient |
| 4983 | * can parse it; the presence in a request of a Range header with |
| 4984 | * multiple byte-range specifiers from a 1.1 client implies that the |
| 4985 | * client can parse multipart/byteranges responses. |
| 4986 | * |
| 4987 | * 5. By the server closing the connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4988 | */ |
| 4989 | |
| 4990 | /* Skip parsing if no content length is possible. The response flags |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 4991 | * 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] | 4992 | * 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] | 4993 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 4994 | */ |
| 4995 | if (txn->meth == HTTP_METH_HEAD || |
| 4996 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4997 | txn->status == 204 || txn->status == 304) { |
| 4998 | txn->flags |= TX_RES_XFER_LEN; |
| 4999 | goto skip_content_length; |
| 5000 | } |
| 5001 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5002 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5003 | ctx.idx = 0; |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 5004 | while ((txn->flags & TX_RES_VER_11) && |
| 5005 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5006 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
| 5007 | txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN); |
| 5008 | else if (txn->flags & TX_RES_TE_CHNK) { |
| 5009 | /* bad transfer-encoding (chunked followed by something else) */ |
| 5010 | use_close_only = 1; |
| 5011 | txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN); |
| 5012 | break; |
| 5013 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5014 | } |
| 5015 | |
| 5016 | /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */ |
| 5017 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5018 | while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only && |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5019 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) { |
| 5020 | signed long long cl; |
| 5021 | |
| 5022 | if (!ctx.vlen) |
| 5023 | goto hdr_response_bad; |
| 5024 | |
| 5025 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) |
| 5026 | goto hdr_response_bad; /* parse failure */ |
| 5027 | |
| 5028 | if (cl < 0) |
| 5029 | goto hdr_response_bad; |
| 5030 | |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5031 | if ((txn->flags & TX_RES_CNT_LEN) && (msg->chunk_len != cl)) |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5032 | goto hdr_response_bad; /* already specified, was different */ |
| 5033 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5034 | txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN; |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5035 | msg->body_len = msg->chunk_len = cl; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5036 | } |
| 5037 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 5038 | /* FIXME: we should also implement the multipart/byterange method. |
| 5039 | * For now on, we resort to close mode in this case (unknown length). |
| 5040 | */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5041 | skip_content_length: |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 5042 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5043 | /* end of job, return OK */ |
| 5044 | rep->analysers &= ~an_bit; |
| 5045 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 5046 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5047 | return 1; |
| 5048 | } |
| 5049 | |
| 5050 | /* This function performs all the processing enabled for the current response. |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5051 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 5052 | * and updates t->rep->analysers. It might make sense to explode it into several |
| 5053 | * other functions. It works like process_request (see indications above). |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5054 | */ |
| 5055 | int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px) |
| 5056 | { |
| 5057 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5058 | struct http_msg *msg = &txn->rsp; |
| 5059 | struct proxy *cur_proxy; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 5060 | struct cond_wordlist *wl; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5061 | |
| 5062 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 5063 | now_ms, __FUNCTION__, |
| 5064 | t, |
| 5065 | rep, |
| 5066 | rep->rex, rep->wex, |
| 5067 | rep->flags, |
| 5068 | rep->l, |
| 5069 | rep->analysers); |
| 5070 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 5071 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5072 | return 0; |
| 5073 | |
| 5074 | rep->analysers &= ~an_bit; |
| 5075 | rep->analyse_exp = TICK_ETERNITY; |
| 5076 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5077 | /* Now we have to check if we need to modify the Connection header. |
| 5078 | * This is more difficult on the response than it is on the request, |
| 5079 | * because we can have two different HTTP versions and we don't know |
| 5080 | * how the client will interprete a response. For instance, let's say |
| 5081 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 5082 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 5083 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 5084 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 5085 | * the lack of header as a keep-alive. Thus we will use two flags |
| 5086 | * indicating how a request MAY be understood by the client. In case |
| 5087 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 5088 | * ambiguous cases such as both close and keepalive are seen, then we |
| 5089 | * will fall back to explicit close. Note that we won't take risks with |
| 5090 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5091 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5092 | */ |
| 5093 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5094 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 5095 | txn->status == 101)) { |
| 5096 | /* Either we've established an explicit tunnel, or we're |
| 5097 | * switching the protocol. In both cases, we're very unlikely |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5098 | * to understand the next protocols. We have to switch to tunnel |
| 5099 | * mode, so that we transfer the request and responses then let |
| 5100 | * this protocol pass unmodified. When we later implement specific |
| 5101 | * parsers for such protocols, we'll want to check the Upgrade |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5102 | * header which contains information about that protocol for |
| 5103 | * responses with status 101 (eg: see RFC2817 about TLS). |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5104 | */ |
| 5105 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 5106 | } |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5107 | else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 5108 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
| 5109 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5110 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5111 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5112 | /* on unknown transfer length, we must close */ |
| 5113 | if (!(txn->flags & TX_RES_XFER_LEN) && |
| 5114 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 5115 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5116 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5117 | /* now adjust header transformations depending on current state */ |
| 5118 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 5119 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 5120 | to_del |= 2; /* remove "keep-alive" on any response */ |
| 5121 | if (!(txn->flags & TX_RES_VER_11)) |
| 5122 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5123 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5124 | else { /* SCL / KAL */ |
| 5125 | to_del |= 1; /* remove "close" on any response */ |
| 5126 | if ((txn->flags & (TX_RES_VER_11|TX_REQ_VER_11)) == (TX_RES_VER_11|TX_REQ_VER_11)) |
| 5127 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5128 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5129 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5130 | /* Parse and remove some headers from the connection header */ |
| 5131 | http_parse_connection_header(txn, msg, rep, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5132 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5133 | /* Some keep-alive responses are converted to Server-close if |
| 5134 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5135 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5136 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 5137 | if ((txn->flags & TX_HDR_CONN_CLO) || |
| 5138 | (txn->flags & (TX_HDR_CONN_KAL|TX_RES_VER_11)) == 0) |
| 5139 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5140 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 5141 | } |
| 5142 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 5143 | if (1) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5144 | /* |
| 5145 | * 3: we will have to evaluate the filters. |
| 5146 | * As opposed to version 1.2, now they will be evaluated in the |
| 5147 | * filters order and not in the header order. This means that |
| 5148 | * each filter has to be validated among all headers. |
| 5149 | * |
| 5150 | * Filters are tried with ->be first, then with ->fe if it is |
| 5151 | * different from ->be. |
| 5152 | */ |
| 5153 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5154 | cur_proxy = t->be; |
| 5155 | while (1) { |
| 5156 | struct proxy *rule_set = cur_proxy; |
| 5157 | |
| 5158 | /* try headers filters */ |
| 5159 | if (rule_set->rsp_exp != NULL) { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 5160 | if (apply_filters_to_response(t, rep, rule_set) < 0) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5161 | return_bad_resp: |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5162 | if (target_srv(&t->target)) { |
| 5163 | target_srv(&t->target)->counters.failed_resp++; |
| 5164 | health_adjust(target_srv(&t->target), HANA_STATUS_HTTP_RSP); |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 5165 | } |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5166 | t->be->be_counters.failed_resp++; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5167 | return_srv_prx_502: |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 5168 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5169 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 5170 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 5171 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | 8e89b84 | 2009-10-18 23:56:35 +0200 | [diff] [blame] | 5172 | stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5173 | if (!(t->flags & SN_ERR_MASK)) |
| 5174 | t->flags |= SN_ERR_PRXCOND; |
| 5175 | if (!(t->flags & SN_FINST_MASK)) |
| 5176 | t->flags |= SN_FINST_H; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 5177 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5178 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5179 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5180 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5181 | /* has the response been denied ? */ |
| 5182 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5183 | if (target_srv(&t->target)) |
| 5184 | target_srv(&t->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5185 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5186 | t->be->be_counters.denied_resp++; |
| 5187 | t->fe->fe_counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5188 | if (t->listener->counters) |
| 5189 | t->listener->counters->denied_resp++; |
| 5190 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5191 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 5192 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5193 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5194 | /* add response headers from the rule sets in the same order */ |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 5195 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 5196 | if (txn->status < 200) |
| 5197 | break; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 5198 | if (wl->cond) { |
| 5199 | int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR); |
| 5200 | ret = acl_pass(ret); |
| 5201 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 5202 | ret = !ret; |
| 5203 | if (!ret) |
| 5204 | continue; |
| 5205 | } |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 5206 | if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5207 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 5208 | } |
| 5209 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5210 | /* check whether we're already working on the frontend */ |
| 5211 | if (cur_proxy == t->fe) |
| 5212 | break; |
| 5213 | cur_proxy = t->fe; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5214 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5215 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5216 | /* |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5217 | * We may be facing a 100-continue response, in which case this |
| 5218 | * is not the right response, and we're waiting for the next one. |
| 5219 | * 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] | 5220 | * next one. |
| 5221 | */ |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5222 | if (unlikely(txn->status == 100)) { |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5223 | hdr_idx_init(&txn->hdr_idx); |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5224 | buffer_forward(rep, rep->lr - msg->sol); |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5225 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 5226 | txn->status = 0; |
| 5227 | rep->analysers |= AN_RES_WAIT_HTTP | an_bit; |
| 5228 | return 1; |
| 5229 | } |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5230 | else if (unlikely(txn->status < 200)) |
| 5231 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5232 | |
| 5233 | /* we don't have any 1xx status code now */ |
| 5234 | |
| 5235 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5236 | * 4: check for server cookie. |
| 5237 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5238 | if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name || |
| 5239 | (t->be->options & PR_O_CHK_CACHE)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5240 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5241 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5242 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5243 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5244 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5245 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5246 | if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5247 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5248 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5249 | /* |
| 5250 | * 6: add server cookie in the response if needed |
| 5251 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5252 | if (target_srv(&t->target) && (t->be->options & PR_O_COOK_INS) && |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 5253 | !((txn->flags & TX_SCK_FOUND) && (t->be->options2 & PR_O2_COOK_PSV)) && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5254 | (!(t->flags & SN_DIRECT) || |
| 5255 | ((t->be->cookie_maxidle || txn->cookie_last_date) && |
| 5256 | (!txn->cookie_last_date || (txn->cookie_last_date - date.tv_sec) < 0)) || |
| 5257 | (t->be->cookie_maxlife && !txn->cookie_first_date) || // set the first_date |
| 5258 | (!t->be->cookie_maxlife && txn->cookie_first_date)) && // remove the first_date |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 5259 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) && |
| 5260 | !(t->flags & SN_IGNORE_PRST)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5261 | int len; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5262 | /* the server is known, it's not the one the client requested, or the |
| 5263 | * cookie's last seen date needs to be refreshed. We have to |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5264 | * insert a set-cookie here, except if we want to insert only on POST |
| 5265 | * requests and this one isn't. Note that servers which don't have cookies |
| 5266 | * (eg: some backup servers) will return a full cookie removal request. |
| 5267 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5268 | if (!target_srv(&t->target)->cookie) { |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5269 | len = sprintf(trash, |
| 5270 | "Set-Cookie: %s=; Expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/", |
| 5271 | t->be->cookie_name); |
| 5272 | } |
| 5273 | else { |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5274 | len = sprintf(trash, "Set-Cookie: %s=%s", t->be->cookie_name, target_srv(&t->target)->cookie); |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5275 | |
| 5276 | if (t->be->cookie_maxidle || t->be->cookie_maxlife) { |
| 5277 | /* emit last_date, which is mandatory */ |
| 5278 | trash[len++] = COOKIE_DELIM_DATE; |
| 5279 | s30tob64((date.tv_sec+3) >> 2, trash + len); len += 5; |
| 5280 | if (t->be->cookie_maxlife) { |
| 5281 | /* emit first_date, which is either the original one or |
| 5282 | * the current date. |
| 5283 | */ |
| 5284 | trash[len++] = COOKIE_DELIM_DATE; |
| 5285 | s30tob64(txn->cookie_first_date ? |
| 5286 | txn->cookie_first_date >> 2 : |
| 5287 | (date.tv_sec+3) >> 2, trash + len); |
| 5288 | len += 5; |
| 5289 | } |
| 5290 | } |
| 5291 | len += sprintf(trash + len, "; path=/"); |
| 5292 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5293 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5294 | if (t->be->cookie_domain) |
| 5295 | len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5296 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5297 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 5298 | trash, len) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5299 | goto return_bad_resp; |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5300 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 5301 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5302 | if (target_srv(&t->target)->cookie && (t->flags & SN_DIRECT)) |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 5303 | /* the server did not change, only the date was updated */ |
| 5304 | txn->flags |= TX_SCK_UPDATED; |
| 5305 | else |
| 5306 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5307 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5308 | /* Here, we will tell an eventual cache on the client side that we don't |
| 5309 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 5310 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 5311 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 5312 | */ |
| 5313 | if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5314 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5315 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 5316 | |
| 5317 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 5318 | "Cache-control: private", 22) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5319 | goto return_bad_resp; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5320 | } |
| 5321 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5322 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5323 | /* |
| 5324 | * 7: check if result will be cacheable with a cookie. |
| 5325 | * We'll block the response if security checks have caught |
| 5326 | * nasty things such as a cacheable cookie. |
| 5327 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 5328 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) == |
| 5329 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_PRESENT)) && |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 5330 | (t->be->options & PR_O_CHK_CACHE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5331 | |
| 5332 | /* we're in presence of a cacheable response containing |
| 5333 | * a set-cookie header. We'll block it as requested by |
| 5334 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5335 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5336 | if (target_srv(&t->target)) |
| 5337 | target_srv(&t->target)->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5338 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5339 | t->be->be_counters.denied_resp++; |
| 5340 | t->fe->fe_counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5341 | if (t->listener->counters) |
| 5342 | t->listener->counters->denied_resp++; |
| 5343 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5344 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5345 | t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>"); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5346 | send_log(t->be, LOG_ALERT, |
| 5347 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5348 | t->be->id, target_srv(&t->target) ? target_srv(&t->target)->id : "<dispatch>"); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5349 | goto return_srv_prx_502; |
| 5350 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5351 | |
| 5352 | /* |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5353 | * 8: adjust "Connection: close" or "Connection: keep-alive" if needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5354 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 5355 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
| 5356 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) { |
| 5357 | unsigned int want_flags = 0; |
| 5358 | |
| 5359 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5360 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 5361 | /* we want a keep-alive response here. Keep-alive header |
| 5362 | * required if either side is not 1.1. |
| 5363 | */ |
| 5364 | if ((txn->flags & (TX_REQ_VER_11|TX_RES_VER_11)) != (TX_REQ_VER_11|TX_RES_VER_11)) |
| 5365 | want_flags |= TX_CON_KAL_SET; |
| 5366 | } |
| 5367 | else { |
| 5368 | /* we want a close response here. Close header required if |
| 5369 | * the server is 1.1, regardless of the client. |
| 5370 | */ |
| 5371 | if (txn->flags & TX_RES_VER_11) |
| 5372 | want_flags |= TX_CON_CLO_SET; |
| 5373 | } |
| 5374 | |
| 5375 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 5376 | http_change_connection_header(txn, msg, rep, want_flags); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5377 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5378 | |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 5379 | skip_header_mangling: |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 5380 | if ((txn->flags & TX_RES_XFER_LEN) || |
| 5381 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5382 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 5383 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5384 | /************************************************************* |
| 5385 | * OK, that's finished for the headers. We have done what we * |
| 5386 | * could. Let's switch to the DATA state. * |
| 5387 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5388 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5389 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5390 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5391 | /* if the user wants to log as soon as possible, without counting |
| 5392 | * bytes from the server, then this is the right moment. We have |
| 5393 | * to temporarily assign bytes_out to log what we currently have. |
| 5394 | */ |
| 5395 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 5396 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 5397 | t->logs.bytes_out = txn->rsp.eoh; |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 5398 | t->do_log(t); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5399 | t->logs.bytes_out = 0; |
| 5400 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5401 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5402 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 5403 | * otherwise we would not let the client side wake up. |
| 5404 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5405 | |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5406 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5407 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 5408 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 5409 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5410 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5411 | /* This function is an analyser which forwards response body (including chunk |
| 5412 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 5413 | * zero byte. The only situation where it must not be called is when we're in |
| 5414 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 5415 | * remaining data and to resync after end of body. It expects the msg_state to |
| 5416 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 5417 | * 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] | 5418 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward chunk_len |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5419 | * bytes of pending data + the headers if not already done (between som and sov). |
| 5420 | * It eventually adjusts som to match sov after the data in between have been sent. |
| 5421 | */ |
| 5422 | int http_response_forward_body(struct session *s, struct buffer *res, int an_bit) |
| 5423 | { |
| 5424 | struct http_txn *txn = &s->txn; |
| 5425 | struct http_msg *msg = &s->txn.rsp; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5426 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5427 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5428 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 5429 | return 0; |
| 5430 | |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 5431 | if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 5432 | ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) || |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 5433 | !s->req->analysers) { |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5434 | /* Output closed while we were sending data. We must abort and |
| 5435 | * wake the other side up. |
| 5436 | */ |
| 5437 | msg->msg_state = HTTP_MSG_ERROR; |
| 5438 | http_resync_states(s); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 5439 | return 1; |
| 5440 | } |
| 5441 | |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5442 | /* in most states, we should abort in case of early close */ |
| 5443 | buffer_auto_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 5444 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5445 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 5446 | /* we have msg->col and msg->sov which both point to the first |
| 5447 | * byte of message body. msg->som still points to the beginning |
| 5448 | * of the message. We must save the body in req->lr because it |
| 5449 | * survives buffer re-alignments. |
| 5450 | */ |
| 5451 | res->lr = res->data + msg->sov; |
| 5452 | if (txn->flags & TX_RES_TE_CHNK) |
| 5453 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 5454 | else { |
| 5455 | msg->msg_state = HTTP_MSG_DATA; |
| 5456 | } |
| 5457 | } |
| 5458 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5459 | while (1) { |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5460 | int bytes; |
| 5461 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5462 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 5463 | /* we may have some data pending */ |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5464 | bytes = msg->sov - msg->som; |
| 5465 | if (msg->chunk_len || bytes) { |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 5466 | msg->som = msg->sov; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5467 | if (likely(bytes < 0)) /* sov may have wrapped at the end */ |
| 5468 | bytes += res->size; |
| 5469 | msg->chunk_len += (unsigned int)bytes; |
| 5470 | msg->chunk_len -= buffer_forward(res, msg->chunk_len); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 5471 | } |
| 5472 | |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5473 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5474 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 5475 | /* must still forward */ |
| 5476 | if (res->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5477 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5478 | |
| 5479 | /* nothing left to forward */ |
| 5480 | if (txn->flags & TX_RES_TE_CHNK) |
| 5481 | msg->msg_state = HTTP_MSG_DATA_CRLF; |
| 5482 | else |
| 5483 | msg->msg_state = HTTP_MSG_DONE; |
| 5484 | } |
| 5485 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 5486 | /* read the chunk size and assign it to ->chunk_len, then |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5487 | * set ->sov to point to the body and switch to DATA or TRAILERS state. |
| 5488 | */ |
| 5489 | int ret = http_parse_chunk_size(res, msg); |
| 5490 | |
| 5491 | if (!ret) |
| 5492 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5493 | else if (ret < 0) { |
| 5494 | if (msg->err_pos >= 0) |
| 5495 | http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_CHUNK_SIZE, s->fe); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5496 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5497 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5498 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | 57f5c12 | 2010-12-02 00:37:14 +0100 | [diff] [blame] | 5499 | /* Don't set a PUSH at the end of that chunk if it's not the last one */ |
| 5500 | if (msg->msg_state == HTTP_MSG_DATA) |
| 5501 | res->flags |= BF_EXPECT_MORE; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5502 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5503 | else if (msg->msg_state == HTTP_MSG_DATA_CRLF) { |
| 5504 | /* we want the CRLF after the data */ |
| 5505 | int ret; |
| 5506 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 5507 | res->lr = res->w + res->send_max; |
| 5508 | if (res->lr >= res->data + res->size) |
| 5509 | res->lr -= res->size; |
| 5510 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5511 | ret = http_skip_chunk_crlf(res, msg); |
| 5512 | |
| 5513 | if (!ret) |
| 5514 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5515 | else if (ret < 0) { |
| 5516 | if (msg->err_pos >= 0) |
| 5517 | http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_DATA_CRLF, s->fe); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5518 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5519 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5520 | /* we're in MSG_CHUNK_SIZE now */ |
| 5521 | } |
| 5522 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
| 5523 | int ret = http_forward_trailers(res, msg); |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5524 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5525 | if (ret == 0) |
| 5526 | goto missing_data; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5527 | else if (ret < 0) { |
| 5528 | if (msg->err_pos >= 0) |
| 5529 | http_capture_bad_message(&s->be->invalid_rep, s, res, msg, HTTP_MSG_TRAILERS, s->fe); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5530 | goto return_bad_res; |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5531 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5532 | /* we're in HTTP_MSG_DONE now */ |
| 5533 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5534 | else { |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5535 | int old_state = msg->msg_state; |
| 5536 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5537 | /* other states, DONE...TUNNEL */ |
Willy Tarreau | 4fe4190 | 2010-06-07 22:27:41 +0200 | [diff] [blame] | 5538 | /* for keep-alive we don't want to forward closes on DONE */ |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5539 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5540 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 5541 | buffer_dont_close(res); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5542 | if (http_resync_states(s)) { |
| 5543 | http_silent_debug(__LINE__, s); |
| 5544 | /* some state changes occurred, maybe the analyser |
| 5545 | * was disabled too. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5546 | */ |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5547 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 5548 | if (res->flags & BF_SHUTW) { |
| 5549 | /* response errors are most likely due to |
| 5550 | * the client aborting the transfer. |
| 5551 | */ |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5552 | goto aborted_xfer; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5553 | } |
Willy Tarreau | e1582eb | 2010-12-12 13:10:11 +0100 | [diff] [blame] | 5554 | if (msg->err_pos >= 0) |
| 5555 | http_capture_bad_message(&s->be->invalid_rep, s, res, msg, old_state, s->fe); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5556 | goto return_bad_res; |
Willy Tarreau | 3fe693b | 2010-12-12 12:50:05 +0100 | [diff] [blame] | 5557 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5558 | return 1; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5559 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5560 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5561 | } |
| 5562 | } |
| 5563 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5564 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5565 | /* stop waiting for data if the input is closed before the end */ |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5566 | if (res->flags & BF_SHUTR) { |
| 5567 | if (!(s->flags & SN_ERR_MASK)) |
| 5568 | s->flags |= SN_ERR_SRVCL; |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5569 | s->be->be_counters.srv_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5570 | if (target_srv(&s->target)) |
| 5571 | target_srv(&s->target)->counters.srv_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5572 | goto return_bad_res_stats_ok; |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5573 | } |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5574 | |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5575 | if (res->flags & BF_SHUTW) |
| 5576 | goto aborted_xfer; |
| 5577 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5578 | /* 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] | 5579 | if (!s->req->analysers) |
| 5580 | goto return_bad_res; |
| 5581 | |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5582 | /* forward any pending data */ |
| 5583 | bytes = msg->sov - msg->som; |
| 5584 | if (msg->chunk_len || bytes) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5585 | msg->som = msg->sov; |
Willy Tarreau | d8ee85a | 2011-03-28 16:06:28 +0200 | [diff] [blame] | 5586 | if (likely(bytes < 0)) /* sov may have wrapped at the end */ |
| 5587 | bytes += res->size; |
| 5588 | msg->chunk_len += (unsigned int)bytes; |
| 5589 | msg->chunk_len -= buffer_forward(res, msg->chunk_len); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5590 | } |
| 5591 | |
Willy Tarreau | 92aa1fa | 2010-08-28 18:57:20 +0200 | [diff] [blame] | 5592 | /* When TE: chunked is used, we need to get there again to parse remaining |
| 5593 | * chunks even if the server has closed, so we don't want to set BF_DONTCLOSE. |
| 5594 | * Similarly, with keep-alive on the client side, we don't want to forward a |
| 5595 | * close. |
| 5596 | */ |
| 5597 | if ((txn->flags & TX_RES_TE_CHNK) || |
| 5598 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 5599 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) |
| 5600 | buffer_dont_close(res); |
| 5601 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5602 | /* the session handler will take care of timeouts and errors */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5603 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5604 | return 0; |
| 5605 | |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5606 | return_bad_res: /* let's centralize all bad responses */ |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5607 | s->be->be_counters.failed_resp++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5608 | if (target_srv(&s->target)) |
| 5609 | target_srv(&s->target)->counters.failed_resp++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5610 | |
| 5611 | return_bad_res_stats_ok: |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5612 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 5613 | /* don't send any error message as we're in the body */ |
| 5614 | stream_int_retnclose(res->cons, NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5615 | res->analysers = 0; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5616 | s->req->analysers = 0; /* we're in data phase, we want to abort both directions */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5617 | if (target_srv(&s->target)) |
| 5618 | health_adjust(target_srv(&s->target), HANA_STATUS_HTTP_HDRRSP); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5619 | |
| 5620 | if (!(s->flags & SN_ERR_MASK)) |
| 5621 | s->flags |= SN_ERR_PRXCOND; |
| 5622 | if (!(s->flags & SN_FINST_MASK)) |
Willy Tarreau | 40dba09 | 2010-03-04 18:14:51 +0100 | [diff] [blame] | 5623 | s->flags |= SN_FINST_D; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5624 | return 0; |
| 5625 | |
| 5626 | aborted_xfer: |
| 5627 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 5628 | /* don't send any error message as we're in the body */ |
| 5629 | stream_int_retnclose(res->cons, NULL); |
| 5630 | res->analysers = 0; |
| 5631 | s->req->analysers = 0; /* we're in data phase, we want to abort both directions */ |
| 5632 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5633 | s->fe->fe_counters.cli_aborts++; |
| 5634 | s->be->be_counters.cli_aborts++; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 5635 | if (target_srv(&s->target)) |
| 5636 | target_srv(&s->target)->counters.cli_aborts++; |
Willy Tarreau | ed2fd2d | 2010-12-29 11:23:27 +0100 | [diff] [blame] | 5637 | |
| 5638 | if (!(s->flags & SN_ERR_MASK)) |
| 5639 | s->flags |= SN_ERR_CLICL; |
| 5640 | if (!(s->flags & SN_FINST_MASK)) |
| 5641 | s->flags |= SN_FINST_D; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5642 | return 0; |
| 5643 | } |
| 5644 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5645 | /* Iterate the same filter through all request headers. |
| 5646 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5647 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5648 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5649 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5650 | int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5651 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5652 | char term; |
| 5653 | char *cur_ptr, *cur_end, *cur_next; |
| 5654 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5655 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5656 | struct hdr_idx_elem *cur_hdr; |
| 5657 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 5658 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5659 | last_hdr = 0; |
| 5660 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5661 | cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5662 | old_idx = 0; |
| 5663 | |
| 5664 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5665 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5666 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5667 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5668 | (exp->action == ACT_ALLOW || |
| 5669 | exp->action == ACT_DENY || |
| 5670 | exp->action == ACT_TARPIT)) |
| 5671 | return 0; |
| 5672 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5673 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5674 | if (!cur_idx) |
| 5675 | break; |
| 5676 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5677 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5678 | cur_ptr = cur_next; |
| 5679 | cur_end = cur_ptr + cur_hdr->len; |
| 5680 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5681 | |
| 5682 | /* Now we have one header between cur_ptr and cur_end, |
| 5683 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5684 | */ |
| 5685 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5686 | /* The annoying part is that pattern matching needs |
| 5687 | * that we modify the contents to null-terminate all |
| 5688 | * strings before testing them. |
| 5689 | */ |
| 5690 | |
| 5691 | term = *cur_end; |
| 5692 | *cur_end = '\0'; |
| 5693 | |
| 5694 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5695 | switch (exp->action) { |
| 5696 | case ACT_SETBE: |
| 5697 | /* It is not possible to jump a second time. |
| 5698 | * FIXME: should we return an HTTP/500 here so that |
| 5699 | * the admin knows there's a problem ? |
| 5700 | */ |
| 5701 | if (t->be != t->fe) |
| 5702 | break; |
| 5703 | |
| 5704 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 5705 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5706 | last_hdr = 1; |
| 5707 | break; |
| 5708 | |
| 5709 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5710 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5711 | last_hdr = 1; |
| 5712 | break; |
| 5713 | |
| 5714 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5715 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5716 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5717 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5718 | t->fe->fe_counters.denied_req++; |
| 5719 | if (t->fe != t->be) |
| 5720 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5721 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5722 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5723 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5724 | break; |
| 5725 | |
| 5726 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5727 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5728 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5729 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5730 | t->fe->fe_counters.denied_req++; |
| 5731 | if (t->fe != t->be) |
| 5732 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5733 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5734 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5735 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5736 | break; |
| 5737 | |
| 5738 | case ACT_REPLACE: |
| 5739 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5740 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 5741 | /* FIXME: if the user adds a newline in the replacement, the |
| 5742 | * index will not be recalculated for now, and the new line |
| 5743 | * will not be counted as a new header. |
| 5744 | */ |
| 5745 | |
| 5746 | cur_end += delta; |
| 5747 | cur_next += delta; |
| 5748 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5749 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5750 | break; |
| 5751 | |
| 5752 | case ACT_REMOVE: |
| 5753 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 5754 | cur_next += delta; |
| 5755 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5756 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5757 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5758 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5759 | cur_hdr->len = 0; |
| 5760 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 5761 | cur_idx = old_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5762 | break; |
| 5763 | |
| 5764 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5765 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5766 | if (cur_end) |
| 5767 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5768 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5769 | /* keep the link from this header to next one in case of later |
| 5770 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5771 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5772 | old_idx = cur_idx; |
| 5773 | } |
| 5774 | return 0; |
| 5775 | } |
| 5776 | |
| 5777 | |
| 5778 | /* Apply the filter to the request line. |
| 5779 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 5780 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5781 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5782 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5783 | */ |
| 5784 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 5785 | { |
| 5786 | char term; |
| 5787 | char *cur_ptr, *cur_end; |
| 5788 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5789 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5790 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5791 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5792 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5793 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5794 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5795 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5796 | (exp->action == ACT_ALLOW || |
| 5797 | exp->action == ACT_DENY || |
| 5798 | exp->action == ACT_TARPIT)) |
| 5799 | return 0; |
| 5800 | else if (exp->action == ACT_REMOVE) |
| 5801 | return 0; |
| 5802 | |
| 5803 | done = 0; |
| 5804 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5805 | cur_ptr = txn->req.sol; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5806 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5807 | |
| 5808 | /* Now we have the request line between cur_ptr and cur_end */ |
| 5809 | |
| 5810 | /* The annoying part is that pattern matching needs |
| 5811 | * that we modify the contents to null-terminate all |
| 5812 | * strings before testing them. |
| 5813 | */ |
| 5814 | |
| 5815 | term = *cur_end; |
| 5816 | *cur_end = '\0'; |
| 5817 | |
| 5818 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5819 | switch (exp->action) { |
| 5820 | case ACT_SETBE: |
| 5821 | /* It is not possible to jump a second time. |
| 5822 | * FIXME: should we return an HTTP/500 here so that |
| 5823 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5824 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5825 | if (t->be != t->fe) |
| 5826 | break; |
| 5827 | |
| 5828 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 5829 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5830 | done = 1; |
| 5831 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5832 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5833 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5834 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5835 | done = 1; |
| 5836 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5837 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5838 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5839 | txn->flags |= TX_CLDENY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5840 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5841 | t->fe->fe_counters.denied_req++; |
| 5842 | if (t->fe != t->be) |
| 5843 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5844 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5845 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5846 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5847 | done = 1; |
| 5848 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5849 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5850 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5851 | txn->flags |= TX_CLTARPIT; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5852 | |
Willy Tarreau | 7d0aaf3 | 2011-03-10 23:25:56 +0100 | [diff] [blame] | 5853 | t->fe->fe_counters.denied_req++; |
| 5854 | if (t->fe != t->be) |
| 5855 | t->be->be_counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5856 | if (t->listener->counters) |
Willy Tarreau | bb69539 | 2010-06-23 08:43:37 +0200 | [diff] [blame] | 5857 | t->listener->counters->denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5858 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5859 | done = 1; |
| 5860 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5861 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5862 | case ACT_REPLACE: |
| 5863 | *cur_end = term; /* restore the string terminator */ |
| 5864 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5865 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 5866 | /* FIXME: if the user adds a newline in the replacement, the |
| 5867 | * index will not be recalculated for now, and the new line |
| 5868 | * will not be counted as a new header. |
| 5869 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5870 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5871 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5872 | cur_end += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5873 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5874 | HTTP_MSG_RQMETH, |
| 5875 | cur_ptr, cur_end + 1, |
| 5876 | NULL, NULL); |
| 5877 | if (unlikely(!cur_end)) |
| 5878 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5879 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5880 | /* we have a full request and we know that we have either a CR |
| 5881 | * or an LF at <ptr>. |
| 5882 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5883 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 5884 | 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] | 5885 | /* there is no point trying this regex on headers */ |
| 5886 | return 1; |
| 5887 | } |
| 5888 | } |
| 5889 | *cur_end = term; /* restore the string terminator */ |
| 5890 | return done; |
| 5891 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 5892 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5893 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5894 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5895 | /* |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5896 | * 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] | 5897 | * 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] | 5898 | * unparsable request. Since it can manage the switch to another backend, it |
| 5899 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5900 | */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5901 | int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5902 | { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5903 | struct http_txn *txn = &s->txn; |
| 5904 | struct hdr_exp *exp; |
| 5905 | |
| 5906 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5907 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5908 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5909 | /* |
| 5910 | * The interleaving of transformations and verdicts |
| 5911 | * makes it difficult to decide to continue or stop |
| 5912 | * the evaluation. |
| 5913 | */ |
| 5914 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5915 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 5916 | break; |
| 5917 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5918 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5919 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5920 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5921 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5922 | |
| 5923 | /* if this filter had a condition, evaluate it now and skip to |
| 5924 | * next filter if the condition does not match. |
| 5925 | */ |
| 5926 | if (exp->cond) { |
| 5927 | ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ); |
| 5928 | ret = acl_pass(ret); |
| 5929 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 5930 | ret = !ret; |
| 5931 | |
| 5932 | if (!ret) |
| 5933 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5934 | } |
| 5935 | |
| 5936 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5937 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5938 | if (unlikely(ret < 0)) |
| 5939 | return -1; |
| 5940 | |
| 5941 | if (likely(ret == 0)) { |
| 5942 | /* The filter did not match the request, it can be |
| 5943 | * iterated through all headers. |
| 5944 | */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5945 | apply_filter_to_req_headers(s, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5946 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5947 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5948 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5949 | } |
| 5950 | |
| 5951 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5952 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5953 | /* |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5954 | * Try to retrieve the server associated to the appsession. |
| 5955 | * If the server is found, it's assigned to the session. |
| 5956 | */ |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5957 | 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] | 5958 | struct http_txn *txn = &t->txn; |
| 5959 | appsess *asession = NULL; |
| 5960 | char *sessid_temp = NULL; |
| 5961 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5962 | if (len > t->be->appsession_len) { |
| 5963 | len = t->be->appsession_len; |
| 5964 | } |
| 5965 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5966 | if (t->be->options2 & PR_O2_AS_REQL) { |
| 5967 | /* 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] | 5968 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5969 | /* 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] | 5970 | pool_free2(apools.sessid, txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5971 | } |
| 5972 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5973 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5974 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5975 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5976 | return; |
| 5977 | } |
| 5978 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5979 | memcpy(txn->sessid, buf, len); |
| 5980 | txn->sessid[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5981 | } |
| 5982 | |
| 5983 | if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) { |
| 5984 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5985 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5986 | return; |
| 5987 | } |
| 5988 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5989 | memcpy(sessid_temp, buf, len); |
| 5990 | sessid_temp[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5991 | |
| 5992 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp); |
| 5993 | /* free previously allocated memory */ |
| 5994 | pool_free2(apools.sessid, sessid_temp); |
| 5995 | |
| 5996 | if (asession != NULL) { |
| 5997 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 5998 | if (!(t->be->options2 & PR_O2_AS_REQL)) |
| 5999 | asession->request_count++; |
| 6000 | |
| 6001 | if (asession->serverid != NULL) { |
| 6002 | struct server *srv = t->be->srv; |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 6003 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6004 | while (srv) { |
| 6005 | if (strcmp(srv->id, asession->serverid) == 0) { |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 6006 | if ((srv->state & SRV_RUNNING) || |
| 6007 | (t->be->options & PR_O_PERSIST) || |
| 6008 | (t->flags & SN_FORCE_PRST)) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6009 | /* we found the server and it's usable */ |
| 6010 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 2a6d88d | 2010-01-24 13:10:43 +0100 | [diff] [blame] | 6011 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6012 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 6013 | set_target_server(&t->target, srv); |
Willy Tarreau | 664beb8 | 2011-03-10 11:38:29 +0100 | [diff] [blame] | 6014 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6015 | break; |
| 6016 | } else { |
| 6017 | txn->flags &= ~TX_CK_MASK; |
| 6018 | txn->flags |= TX_CK_DOWN; |
| 6019 | } |
| 6020 | } |
| 6021 | srv = srv->next; |
| 6022 | } |
| 6023 | } |
| 6024 | } |
| 6025 | } |
| 6026 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6027 | /* Find the end of a cookie value contained between <s> and <e>. It works the |
| 6028 | * same way as with headers above except that the semi-colon also ends a token. |
| 6029 | * See RFC2965 for more information. Note that it requires a valid header to |
| 6030 | * return a valid result. |
| 6031 | */ |
| 6032 | char *find_cookie_value_end(char *s, const char *e) |
| 6033 | { |
| 6034 | int quoted, qdpair; |
| 6035 | |
| 6036 | quoted = qdpair = 0; |
| 6037 | for (; s < e; s++) { |
| 6038 | if (qdpair) qdpair = 0; |
| 6039 | else if (quoted) { |
| 6040 | if (*s == '\\') qdpair = 1; |
| 6041 | else if (*s == '"') quoted = 0; |
| 6042 | } |
| 6043 | else if (*s == '"') quoted = 1; |
| 6044 | else if (*s == ',' || *s == ';') return s; |
| 6045 | } |
| 6046 | return s; |
| 6047 | } |
| 6048 | |
| 6049 | /* Delete a value in a header between delimiters <from> and <next> in buffer |
| 6050 | * <buf>. The number of characters displaced is returned, and the pointer to |
| 6051 | * the first delimiter is updated if required. The function tries as much as |
| 6052 | * possible to respect the following principles : |
| 6053 | * - replace <from> delimiter by the <next> one unless <from> points to a |
| 6054 | * colon, in which case <next> is simply removed |
| 6055 | * - set exactly one space character after the new first delimiter, unless |
| 6056 | * there are not enough characters in the block being moved to do so. |
| 6057 | * - remove unneeded spaces before the previous delimiter and after the new |
| 6058 | * one. |
| 6059 | * |
| 6060 | * It is the caller's responsibility to ensure that : |
| 6061 | * - <from> points to a valid delimiter or the colon ; |
| 6062 | * - <next> points to a valid delimiter or the final CR/LF ; |
| 6063 | * - there are non-space chars before <from> ; |
| 6064 | * - there is a CR/LF at or after <next>. |
| 6065 | */ |
| 6066 | int del_hdr_value(struct buffer *buf, char **from, char *next) |
| 6067 | { |
| 6068 | char *prev = *from; |
| 6069 | |
| 6070 | if (*prev == ':') { |
| 6071 | /* We're removing the first value, preserve the colon and add a |
| 6072 | * space if possible. |
| 6073 | */ |
| 6074 | if (!http_is_crlf[(unsigned char)*next]) |
| 6075 | next++; |
| 6076 | prev++; |
| 6077 | if (prev < next) |
| 6078 | *prev++ = ' '; |
| 6079 | |
| 6080 | while (http_is_spht[(unsigned char)*next]) |
| 6081 | next++; |
| 6082 | } else { |
| 6083 | /* Remove useless spaces before the old delimiter. */ |
| 6084 | while (http_is_spht[(unsigned char)*(prev-1)]) |
| 6085 | prev--; |
| 6086 | *from = prev; |
| 6087 | |
| 6088 | /* copy the delimiter and if possible a space if we're |
| 6089 | * not at the end of the line. |
| 6090 | */ |
| 6091 | if (!http_is_crlf[(unsigned char)*next]) { |
| 6092 | *prev++ = *next++; |
| 6093 | if (prev + 1 < next) |
| 6094 | *prev++ = ' '; |
| 6095 | while (http_is_spht[(unsigned char)*next]) |
| 6096 | next++; |
| 6097 | } |
| 6098 | } |
| 6099 | return buffer_replace2(buf, prev, next, NULL, 0); |
| 6100 | } |
| 6101 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6102 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 6103 | * 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] | 6104 | * desirable to call it only when needed. This code is quite complex because |
| 6105 | * of the multiple very crappy and ambiguous syntaxes we have to support. it |
| 6106 | * highly recommended not to touch this part without a good reason ! |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6107 | */ |
| 6108 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 6109 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6110 | struct http_txn *txn = &t->txn; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6111 | int preserve_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6112 | int cur_idx, old_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6113 | char *hdr_beg, *hdr_end, *hdr_next, *del_from; |
| 6114 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6115 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6116 | /* Iterate through the headers, we start with the start line. */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 6117 | old_idx = 0; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6118 | hdr_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6119 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6120 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6121 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6122 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6123 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6124 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6125 | hdr_beg = hdr_next; |
| 6126 | hdr_end = hdr_beg + cur_hdr->len; |
| 6127 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6128 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6129 | /* We have one full header between hdr_beg and hdr_end, and the |
| 6130 | * next header starts at hdr_next. We're only interested in |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6131 | * "Cookie:" headers. |
| 6132 | */ |
| 6133 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6134 | val = http_header_match2(hdr_beg, hdr_end, "Cookie", 6); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6135 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6136 | old_idx = cur_idx; |
| 6137 | continue; |
| 6138 | } |
| 6139 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6140 | del_from = NULL; /* nothing to be deleted */ |
| 6141 | preserve_hdr = 0; /* assume we may kill the whole header */ |
| 6142 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6143 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 6144 | * attributes whose name begin with a '$', and associate them with |
| 6145 | * the right cookie, if we want to delete this cookie. |
| 6146 | * So there are 3 cases for each cookie read : |
| 6147 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 6148 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 6149 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 6150 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 6151 | * "special" cookie. |
| 6152 | * At the end of loop, if a "special" cookie remains, we may have to |
| 6153 | * remove it. If no application cookie persists in the header, we |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6154 | * *MUST* delete it. |
| 6155 | * |
| 6156 | * Note: RFC2965 is unclear about the processing of spaces around |
| 6157 | * the equal sign in the ATTR=VALUE form. A careful inspection of |
| 6158 | * the RFC explicitly allows spaces before it, and not within the |
| 6159 | * tokens (attrs or values). An inspection of RFC2109 allows that |
| 6160 | * too but section 10.1.3 lets one think that spaces may be allowed |
| 6161 | * after the equal sign too, resulting in some (rare) buggy |
| 6162 | * implementations trying to do that. So let's do what servers do. |
| 6163 | * Latest ietf draft forbids spaces all around. Also, earlier RFCs |
| 6164 | * allowed quoted strings in values, with any possible character |
| 6165 | * after a backslash, including control chars and delimitors, which |
| 6166 | * causes parsing to become ambiguous. Browsers also allow spaces |
| 6167 | * within values even without quotes. |
| 6168 | * |
| 6169 | * We have to keep multiple pointers in order to support cookie |
| 6170 | * removal at the beginning, middle or end of header without |
| 6171 | * corrupting the header. All of these headers are valid : |
| 6172 | * |
| 6173 | * Cookie:NAME1=VALUE1;NAME2=VALUE2;NAME3=VALUE3\r\n |
| 6174 | * Cookie:NAME1=VALUE1;NAME2_ONLY ;NAME3=VALUE3\r\n |
| 6175 | * Cookie: NAME1 = VALUE 1 ; NAME2 = VALUE2 ; NAME3 = VALUE3\r\n |
| 6176 | * | | | | | | | | | |
| 6177 | * | | | | | | | | hdr_end <--+ |
| 6178 | * | | | | | | | +--> next |
| 6179 | * | | | | | | +----> val_end |
| 6180 | * | | | | | +-----------> val_beg |
| 6181 | * | | | | +--------------> equal |
| 6182 | * | | | +----------------> att_end |
| 6183 | * | | +---------------------> att_beg |
| 6184 | * | +--------------------------> prev |
| 6185 | * +--------------------------------> hdr_beg |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6186 | */ |
| 6187 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6188 | for (prev = hdr_beg + 6; prev < hdr_end; prev = next) { |
| 6189 | /* Iterate through all cookies on this line */ |
| 6190 | |
| 6191 | /* find att_beg */ |
| 6192 | att_beg = prev + 1; |
| 6193 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 6194 | att_beg++; |
| 6195 | |
| 6196 | /* find att_end : this is the first character after the last non |
| 6197 | * space before the equal. It may be equal to hdr_end. |
| 6198 | */ |
| 6199 | equal = att_end = att_beg; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6200 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6201 | while (equal < hdr_end) { |
| 6202 | if (*equal == '=' || *equal == ',' || *equal == ';') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6203 | break; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6204 | if (http_is_spht[(unsigned char)*equal++]) |
| 6205 | continue; |
| 6206 | att_end = equal; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6207 | } |
| 6208 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6209 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6210 | * is between <att_beg> and <equal>, both may be identical. |
| 6211 | */ |
| 6212 | |
| 6213 | /* look for end of cookie if there is an equal sign */ |
| 6214 | if (equal < hdr_end && *equal == '=') { |
| 6215 | /* look for the beginning of the value */ |
| 6216 | val_beg = equal + 1; |
| 6217 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 6218 | val_beg++; |
| 6219 | |
| 6220 | /* find the end of the value, respecting quotes */ |
| 6221 | next = find_cookie_value_end(val_beg, hdr_end); |
| 6222 | |
| 6223 | /* make val_end point to the first white space or delimitor after the value */ |
| 6224 | val_end = next; |
| 6225 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 6226 | val_end--; |
| 6227 | } else { |
| 6228 | val_beg = val_end = next = equal; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6229 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6230 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6231 | /* We have nothing to do with attributes beginning with '$'. However, |
| 6232 | * they will automatically be removed if a header before them is removed, |
| 6233 | * since they're supposed to be linked together. |
| 6234 | */ |
| 6235 | if (*att_beg == '$') |
| 6236 | continue; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6237 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6238 | /* Ignore cookies with no equal sign */ |
| 6239 | if (equal == next) { |
| 6240 | /* This is not our cookie, so we must preserve it. But if we already |
| 6241 | * scheduled another cookie for removal, we cannot remove the |
| 6242 | * complete header, but we can remove the previous block itself. |
| 6243 | */ |
| 6244 | preserve_hdr = 1; |
| 6245 | if (del_from != NULL) { |
| 6246 | int delta = del_hdr_value(req, &del_from, prev); |
| 6247 | val_end += delta; |
| 6248 | next += delta; |
| 6249 | hdr_end += delta; |
| 6250 | hdr_next += delta; |
| 6251 | cur_hdr->len += delta; |
| 6252 | http_msg_move_end(&txn->req, delta); |
| 6253 | prev = del_from; |
| 6254 | del_from = NULL; |
| 6255 | } |
| 6256 | continue; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 6257 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6258 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6259 | /* if there are spaces around the equal sign, we need to |
| 6260 | * strip them otherwise we'll get trouble for cookie captures, |
| 6261 | * or even for rewrites. Since this happens extremely rarely, |
| 6262 | * it does not hurt performance. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6263 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6264 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 6265 | int stripped_before = 0; |
| 6266 | int stripped_after = 0; |
| 6267 | |
| 6268 | if (att_end != equal) { |
| 6269 | stripped_before = buffer_replace2(req, att_end, equal, NULL, 0); |
| 6270 | equal += stripped_before; |
| 6271 | val_beg += stripped_before; |
| 6272 | } |
| 6273 | |
| 6274 | if (val_beg > equal + 1) { |
| 6275 | stripped_after = buffer_replace2(req, equal + 1, val_beg, NULL, 0); |
| 6276 | val_beg += stripped_after; |
| 6277 | stripped_before += stripped_after; |
| 6278 | } |
| 6279 | |
| 6280 | val_end += stripped_before; |
| 6281 | next += stripped_before; |
| 6282 | hdr_end += stripped_before; |
| 6283 | hdr_next += stripped_before; |
| 6284 | cur_hdr->len += stripped_before; |
| 6285 | http_msg_move_end(&txn->req, stripped_before); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6286 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6287 | /* now everything is as on the diagram above */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6288 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6289 | /* First, let's see if we want to capture this cookie. We check |
| 6290 | * that we don't already have a client side cookie, because we |
| 6291 | * can only capture one. Also as an optimisation, we ignore |
| 6292 | * cookies shorter than the declared name. |
| 6293 | */ |
| 6294 | if (t->fe->capture_name != NULL && txn->cli_cookie == NULL && |
| 6295 | (val_end - att_beg >= t->fe->capture_namelen) && |
| 6296 | memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
| 6297 | int log_len = val_end - att_beg; |
| 6298 | |
| 6299 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
| 6300 | Alert("HTTP logging : out of memory.\n"); |
| 6301 | } else { |
| 6302 | if (log_len > t->fe->capture_len) |
| 6303 | log_len = t->fe->capture_len; |
| 6304 | memcpy(txn->cli_cookie, att_beg, log_len); |
| 6305 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6306 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6307 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6308 | |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6309 | /* Persistence cookies in passive, rewrite or insert mode have the |
| 6310 | * following form : |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6311 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6312 | * Cookie: NAME=SRV[|<lastseen>[|<firstseen>]] |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6313 | * |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6314 | * For cookies in prefix mode, the form is : |
| 6315 | * |
| 6316 | * Cookie: NAME=SRV~VALUE |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6317 | */ |
| 6318 | if ((att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 6319 | (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) { |
| 6320 | struct server *srv = t->be->srv; |
| 6321 | char *delim; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6322 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6323 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 6324 | * have the server ID between val_beg and delim, and the original cookie between |
| 6325 | * delim+1 and val_end. Otherwise, delim==val_end : |
| 6326 | * |
| 6327 | * Cookie: NAME=SRV; # in all but prefix modes |
| 6328 | * Cookie: NAME=SRV~OPAQUE ; # in prefix mode |
| 6329 | * | || || | |+-> next |
| 6330 | * | || || | +--> val_end |
| 6331 | * | || || +---------> delim |
| 6332 | * | || |+------------> val_beg |
| 6333 | * | || +-------------> att_end = equal |
| 6334 | * | |+-----------------> att_beg |
| 6335 | * | +------------------> prev |
| 6336 | * +-------------------------> hdr_beg |
| 6337 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6338 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6339 | if (t->be->options & PR_O_COOK_PFX) { |
| 6340 | for (delim = val_beg; delim < val_end; delim++) |
| 6341 | if (*delim == COOKIE_DELIM) |
| 6342 | break; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6343 | } else { |
| 6344 | char *vbar1; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6345 | delim = val_end; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6346 | /* Now check if the cookie contains a date field, which would |
| 6347 | * appear after a vertical bar ('|') just after the server name |
| 6348 | * and before the delimiter. |
| 6349 | */ |
| 6350 | vbar1 = memchr(val_beg, COOKIE_DELIM_DATE, val_end - val_beg); |
| 6351 | if (vbar1) { |
| 6352 | /* OK, so left of the bar is the server's cookie and |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6353 | * right is the last seen date. It is a base64 encoded |
| 6354 | * 30-bit value representing the UNIX date since the |
| 6355 | * epoch in 4-second quantities. |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6356 | */ |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6357 | int val; |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6358 | delim = vbar1++; |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6359 | if (val_end - vbar1 >= 5) { |
| 6360 | val = b64tos30(vbar1); |
| 6361 | if (val > 0) |
| 6362 | txn->cookie_last_date = val << 2; |
| 6363 | } |
| 6364 | /* look for a second vertical bar */ |
| 6365 | vbar1 = memchr(vbar1, COOKIE_DELIM_DATE, val_end - vbar1); |
| 6366 | if (vbar1 && (val_end - vbar1 > 5)) { |
| 6367 | val = b64tos30(vbar1 + 1); |
| 6368 | if (val > 0) |
| 6369 | txn->cookie_first_date = val << 2; |
| 6370 | } |
Willy Tarreau | bca9969 | 2010-10-06 19:25:55 +0200 | [diff] [blame] | 6371 | } |
| 6372 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6373 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6374 | /* if the cookie has an expiration date and the proxy wants to check |
| 6375 | * it, then we do that now. We first check if the cookie is too old, |
| 6376 | * then only if it has expired. We detect strict overflow because the |
| 6377 | * time resolution here is not great (4 seconds). Cookies with dates |
| 6378 | * in the future are ignored if their offset is beyond one day. This |
| 6379 | * allows an admin to fix timezone issues without expiring everyone |
| 6380 | * and at the same time avoids keeping unwanted side effects for too |
| 6381 | * long. |
| 6382 | */ |
| 6383 | if (txn->cookie_first_date && t->be->cookie_maxlife && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6384 | (((signed)(date.tv_sec - txn->cookie_first_date) > (signed)t->be->cookie_maxlife) || |
| 6385 | ((signed)(txn->cookie_first_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6386 | txn->flags &= ~TX_CK_MASK; |
| 6387 | txn->flags |= TX_CK_OLD; |
| 6388 | delim = val_beg; // let's pretend we have not found the cookie |
| 6389 | txn->cookie_first_date = 0; |
| 6390 | txn->cookie_last_date = 0; |
| 6391 | } |
| 6392 | else if (txn->cookie_last_date && t->be->cookie_maxidle && |
Willy Tarreau | ef4f391 | 2010-10-07 21:00:29 +0200 | [diff] [blame] | 6393 | (((signed)(date.tv_sec - txn->cookie_last_date) > (signed)t->be->cookie_maxidle) || |
| 6394 | ((signed)(txn->cookie_last_date - date.tv_sec) > 86400))) { |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6395 | txn->flags &= ~TX_CK_MASK; |
| 6396 | txn->flags |= TX_CK_EXPIRED; |
| 6397 | delim = val_beg; // let's pretend we have not found the cookie |
| 6398 | txn->cookie_first_date = 0; |
| 6399 | txn->cookie_last_date = 0; |
| 6400 | } |
| 6401 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6402 | /* Here, we'll look for the first running server which supports the cookie. |
| 6403 | * This allows to share a same cookie between several servers, for example |
| 6404 | * to dedicate backup servers to specific servers only. |
| 6405 | * However, to prevent clients from sticking to cookie-less backup server |
| 6406 | * when they have incidentely learned an empty cookie, we simply ignore |
| 6407 | * empty cookies and mark them as invalid. |
| 6408 | * The same behaviour is applied when persistence must be ignored. |
| 6409 | */ |
| 6410 | if ((delim == val_beg) || (t->flags & SN_IGNORE_PRST)) |
| 6411 | srv = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6412 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6413 | while (srv) { |
| 6414 | if (srv->cookie && (srv->cklen == delim - val_beg) && |
| 6415 | !memcmp(val_beg, srv->cookie, delim - val_beg)) { |
| 6416 | if ((srv->state & SRV_RUNNING) || |
| 6417 | (t->be->options & PR_O_PERSIST) || |
| 6418 | (t->flags & SN_FORCE_PRST)) { |
| 6419 | /* we found the server and we can use it */ |
| 6420 | txn->flags &= ~TX_CK_MASK; |
| 6421 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
| 6422 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 6423 | set_target_server(&t->target, srv); |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6424 | break; |
| 6425 | } else { |
| 6426 | /* we found a server, but it's down, |
| 6427 | * mark it as such and go on in case |
| 6428 | * another one is available. |
| 6429 | */ |
| 6430 | txn->flags &= ~TX_CK_MASK; |
| 6431 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6432 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6433 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6434 | srv = srv->next; |
| 6435 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6436 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 6437 | if (!srv && !(txn->flags & (TX_CK_DOWN|TX_CK_EXPIRED|TX_CK_OLD))) { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6438 | /* no server matched this cookie */ |
| 6439 | txn->flags &= ~TX_CK_MASK; |
| 6440 | txn->flags |= TX_CK_INVALID; |
| 6441 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6442 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6443 | /* depending on the cookie mode, we may have to either : |
| 6444 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 6445 | * the server never sees it ; |
| 6446 | * - remove the server id from the cookie value, and tag the cookie as an |
| 6447 | * application cookie so that it does not get accidentely removed later, |
| 6448 | * if we're in cookie prefix mode |
| 6449 | */ |
| 6450 | if ((t->be->options & PR_O_COOK_PFX) && (delim != val_end)) { |
| 6451 | int delta; /* negative */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6452 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6453 | delta = buffer_replace2(req, val_beg, delim + 1, NULL, 0); |
| 6454 | val_end += delta; |
| 6455 | next += delta; |
| 6456 | hdr_end += delta; |
| 6457 | hdr_next += delta; |
| 6458 | cur_hdr->len += delta; |
| 6459 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6460 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6461 | del_from = NULL; |
| 6462 | preserve_hdr = 1; /* we want to keep this cookie */ |
| 6463 | } |
| 6464 | else if (del_from == NULL && |
| 6465 | (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) { |
| 6466 | del_from = prev; |
| 6467 | } |
| 6468 | } else { |
| 6469 | /* This is not our cookie, so we must preserve it. But if we already |
| 6470 | * scheduled another cookie for removal, we cannot remove the |
| 6471 | * complete header, but we can remove the previous block itself. |
| 6472 | */ |
| 6473 | preserve_hdr = 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6474 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6475 | if (del_from != NULL) { |
| 6476 | int delta = del_hdr_value(req, &del_from, prev); |
Willy Tarreau | b810554 | 2010-11-24 18:31:28 +0100 | [diff] [blame] | 6477 | if (att_beg >= del_from) |
| 6478 | att_beg += delta; |
| 6479 | if (att_end >= del_from) |
| 6480 | att_end += delta; |
| 6481 | val_beg += delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6482 | val_end += delta; |
| 6483 | next += delta; |
| 6484 | hdr_end += delta; |
| 6485 | hdr_next += delta; |
| 6486 | cur_hdr->len += delta; |
| 6487 | http_msg_move_end(&txn->req, delta); |
| 6488 | prev = del_from; |
| 6489 | del_from = NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6490 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6491 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6492 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6493 | /* Look for the appsession cookie unless persistence must be ignored */ |
| 6494 | if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) { |
| 6495 | int cmp_len, value_len; |
| 6496 | char *value_begin; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 6497 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6498 | if (t->be->options2 & PR_O2_AS_PFX) { |
| 6499 | cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len); |
| 6500 | value_begin = att_beg + t->be->appsession_name_len; |
| 6501 | value_len = val_end - att_beg - t->be->appsession_name_len; |
| 6502 | } else { |
| 6503 | cmp_len = att_end - att_beg; |
| 6504 | value_begin = val_beg; |
| 6505 | value_len = val_end - val_beg; |
| 6506 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6507 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6508 | /* let's see if the cookie is our appcookie */ |
| 6509 | if (cmp_len == t->be->appsession_name_len && |
| 6510 | memcmp(att_beg, t->be->appsession_name, cmp_len) == 0) { |
| 6511 | manage_client_side_appsession(t, value_begin, value_len); |
| 6512 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6513 | } |
| 6514 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6515 | /* continue with next cookie on this header line */ |
| 6516 | att_beg = next; |
| 6517 | } /* for each cookie */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6518 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6519 | /* There are no more cookies on this line. |
| 6520 | * We may still have one (or several) marked for deletion at the |
| 6521 | * end of the line. We must do this now in two ways : |
| 6522 | * - if some cookies must be preserved, we only delete from the |
| 6523 | * mark to the end of line ; |
| 6524 | * - if nothing needs to be preserved, simply delete the whole header |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6525 | */ |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6526 | if (del_from) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6527 | int delta; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6528 | if (preserve_hdr) { |
| 6529 | delta = del_hdr_value(req, &del_from, hdr_end); |
| 6530 | hdr_end = del_from; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6531 | cur_hdr->len += delta; |
| 6532 | } else { |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6533 | delta = buffer_replace2(req, hdr_beg, hdr_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6534 | |
| 6535 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6536 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6537 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6538 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6539 | cur_idx = old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6540 | } |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6541 | hdr_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6542 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6543 | } |
| 6544 | |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6545 | /* check next header */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6546 | old_idx = cur_idx; |
Willy Tarreau | eb7b0a2 | 2010-08-31 16:45:02 +0200 | [diff] [blame] | 6547 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6548 | } |
| 6549 | |
| 6550 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6551 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 6552 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 6553 | */ |
| 6554 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 6555 | { |
| 6556 | char term; |
| 6557 | char *cur_ptr, *cur_end, *cur_next; |
| 6558 | int cur_idx, old_idx, last_hdr; |
| 6559 | struct http_txn *txn = &t->txn; |
| 6560 | struct hdr_idx_elem *cur_hdr; |
| 6561 | int len, delta; |
| 6562 | |
| 6563 | last_hdr = 0; |
| 6564 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6565 | cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6566 | old_idx = 0; |
| 6567 | |
| 6568 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6569 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6570 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6571 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6572 | (exp->action == ACT_ALLOW || |
| 6573 | exp->action == ACT_DENY)) |
| 6574 | return 0; |
| 6575 | |
| 6576 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 6577 | if (!cur_idx) |
| 6578 | break; |
| 6579 | |
| 6580 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 6581 | cur_ptr = cur_next; |
| 6582 | cur_end = cur_ptr + cur_hdr->len; |
| 6583 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6584 | |
| 6585 | /* Now we have one header between cur_ptr and cur_end, |
| 6586 | * and the next header starts at cur_next. |
| 6587 | */ |
| 6588 | |
| 6589 | /* The annoying part is that pattern matching needs |
| 6590 | * that we modify the contents to null-terminate all |
| 6591 | * strings before testing them. |
| 6592 | */ |
| 6593 | |
| 6594 | term = *cur_end; |
| 6595 | *cur_end = '\0'; |
| 6596 | |
| 6597 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6598 | switch (exp->action) { |
| 6599 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6600 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6601 | last_hdr = 1; |
| 6602 | break; |
| 6603 | |
| 6604 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6605 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6606 | last_hdr = 1; |
| 6607 | break; |
| 6608 | |
| 6609 | case ACT_REPLACE: |
| 6610 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 6611 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 6612 | /* FIXME: if the user adds a newline in the replacement, the |
| 6613 | * index will not be recalculated for now, and the new line |
| 6614 | * will not be counted as a new header. |
| 6615 | */ |
| 6616 | |
| 6617 | cur_end += delta; |
| 6618 | cur_next += delta; |
| 6619 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6620 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6621 | break; |
| 6622 | |
| 6623 | case ACT_REMOVE: |
| 6624 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 6625 | cur_next += delta; |
| 6626 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6627 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6628 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6629 | txn->hdr_idx.used--; |
| 6630 | cur_hdr->len = 0; |
| 6631 | cur_end = NULL; /* null-term has been rewritten */ |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 6632 | cur_idx = old_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6633 | break; |
| 6634 | |
| 6635 | } |
| 6636 | } |
| 6637 | if (cur_end) |
| 6638 | *cur_end = term; /* restore the string terminator */ |
| 6639 | |
| 6640 | /* keep the link from this header to next one in case of later |
| 6641 | * removal of next header. |
| 6642 | */ |
| 6643 | old_idx = cur_idx; |
| 6644 | } |
| 6645 | return 0; |
| 6646 | } |
| 6647 | |
| 6648 | |
| 6649 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 6650 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 6651 | * or -1 if a replacement resulted in an invalid status line. |
| 6652 | */ |
| 6653 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 6654 | { |
| 6655 | char term; |
| 6656 | char *cur_ptr, *cur_end; |
| 6657 | int done; |
| 6658 | struct http_txn *txn = &t->txn; |
| 6659 | int len, delta; |
| 6660 | |
| 6661 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6662 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6663 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6664 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6665 | (exp->action == ACT_ALLOW || |
| 6666 | exp->action == ACT_DENY)) |
| 6667 | return 0; |
| 6668 | else if (exp->action == ACT_REMOVE) |
| 6669 | return 0; |
| 6670 | |
| 6671 | done = 0; |
| 6672 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6673 | cur_ptr = txn->rsp.sol; |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 6674 | cur_end = cur_ptr + txn->rsp.sl.st.l; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6675 | |
| 6676 | /* Now we have the status line between cur_ptr and cur_end */ |
| 6677 | |
| 6678 | /* The annoying part is that pattern matching needs |
| 6679 | * that we modify the contents to null-terminate all |
| 6680 | * strings before testing them. |
| 6681 | */ |
| 6682 | |
| 6683 | term = *cur_end; |
| 6684 | *cur_end = '\0'; |
| 6685 | |
| 6686 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 6687 | switch (exp->action) { |
| 6688 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6689 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6690 | done = 1; |
| 6691 | break; |
| 6692 | |
| 6693 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6694 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6695 | done = 1; |
| 6696 | break; |
| 6697 | |
| 6698 | case ACT_REPLACE: |
| 6699 | *cur_end = term; /* restore the string terminator */ |
| 6700 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 6701 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 6702 | /* FIXME: if the user adds a newline in the replacement, the |
| 6703 | * index will not be recalculated for now, and the new line |
| 6704 | * will not be counted as a new header. |
| 6705 | */ |
| 6706 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6707 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6708 | cur_end += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6709 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 6710 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6711 | cur_ptr, cur_end + 1, |
| 6712 | NULL, NULL); |
| 6713 | if (unlikely(!cur_end)) |
| 6714 | return -1; |
| 6715 | |
| 6716 | /* we have a full respnse and we know that we have either a CR |
| 6717 | * or an LF at <ptr>. |
| 6718 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6719 | txn->status = strl2ui(txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l); |
Willy Tarreau | 1ba0e5f | 2010-06-07 13:57:32 +0200 | [diff] [blame] | 6720 | 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] | 6721 | /* there is no point trying this regex on headers */ |
| 6722 | return 1; |
| 6723 | } |
| 6724 | } |
| 6725 | *cur_end = term; /* restore the string terminator */ |
| 6726 | return done; |
| 6727 | } |
| 6728 | |
| 6729 | |
| 6730 | |
| 6731 | /* |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6732 | * 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] | 6733 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 6734 | * unparsable response. |
| 6735 | */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6736 | int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6737 | { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6738 | struct http_txn *txn = &s->txn; |
| 6739 | struct hdr_exp *exp; |
| 6740 | |
| 6741 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6742 | int ret; |
| 6743 | |
| 6744 | /* |
| 6745 | * The interleaving of transformations and verdicts |
| 6746 | * makes it difficult to decide to continue or stop |
| 6747 | * the evaluation. |
| 6748 | */ |
| 6749 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6750 | if (txn->flags & TX_SVDENY) |
| 6751 | break; |
| 6752 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6753 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6754 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 6755 | exp->action == ACT_PASS)) { |
| 6756 | exp = exp->next; |
| 6757 | continue; |
| 6758 | } |
| 6759 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6760 | /* if this filter had a condition, evaluate it now and skip to |
| 6761 | * next filter if the condition does not match. |
| 6762 | */ |
| 6763 | if (exp->cond) { |
| 6764 | ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR); |
| 6765 | ret = acl_pass(ret); |
| 6766 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 6767 | ret = !ret; |
| 6768 | if (!ret) |
| 6769 | continue; |
| 6770 | } |
| 6771 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6772 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6773 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6774 | if (unlikely(ret < 0)) |
| 6775 | return -1; |
| 6776 | |
| 6777 | if (likely(ret == 0)) { |
| 6778 | /* The filter did not match the response, it can be |
| 6779 | * iterated through all headers. |
| 6780 | */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6781 | apply_filter_to_resp_headers(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6782 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6783 | } |
| 6784 | return 0; |
| 6785 | } |
| 6786 | |
| 6787 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6788 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 6789 | * 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] | 6790 | * desirable to call it only when needed. This function is also used when we |
| 6791 | * 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] | 6792 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6793 | void manage_server_side_cookies(struct session *t, struct buffer *res) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6794 | { |
| 6795 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6796 | struct server *srv; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6797 | int is_cookie2; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6798 | int cur_idx, old_idx, delta; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6799 | char *hdr_beg, *hdr_end, *hdr_next; |
| 6800 | char *prev, *att_beg, *att_end, *equal, *val_beg, *val_end, *next; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6801 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6802 | /* Iterate through the headers. |
| 6803 | * we start with the start line. |
| 6804 | */ |
| 6805 | old_idx = 0; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6806 | hdr_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6807 | |
| 6808 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 6809 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6810 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6811 | |
| 6812 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6813 | hdr_beg = hdr_next; |
| 6814 | hdr_end = hdr_beg + cur_hdr->len; |
| 6815 | hdr_next = hdr_end + cur_hdr->cr + 1; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6816 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6817 | /* We have one full header between hdr_beg and hdr_end, and the |
| 6818 | * next header starts at hdr_next. We're only interested in |
| 6819 | * "Set-Cookie" and "Set-Cookie2" headers. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6820 | */ |
| 6821 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6822 | is_cookie2 = 0; |
| 6823 | prev = hdr_beg + 10; |
| 6824 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie", 10); |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6825 | if (!val) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6826 | val = http_header_match2(hdr_beg, hdr_end, "Set-Cookie2", 11); |
| 6827 | if (!val) { |
| 6828 | old_idx = cur_idx; |
| 6829 | continue; |
| 6830 | } |
| 6831 | is_cookie2 = 1; |
| 6832 | prev = hdr_beg + 11; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6833 | } |
| 6834 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6835 | /* OK, right now we know we have a Set-Cookie* at hdr_beg, and |
| 6836 | * <prev> points to the colon. |
| 6837 | */ |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 6838 | txn->flags |= TX_SCK_PRESENT; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6839 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6840 | /* Maybe we only wanted to see if there was a Set-Cookie (eg: |
| 6841 | * check-cache is enabled) and we are not interested in checking |
| 6842 | * them. Warning, the cookie capture is declared in the frontend. |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6843 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6844 | if (t->be->cookie_name == NULL && |
| 6845 | t->be->appsession_name == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6846 | t->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6847 | return; |
| 6848 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6849 | /* OK so now we know we have to process this response cookie. |
| 6850 | * The format of the Set-Cookie header is slightly different |
| 6851 | * from the format of the Cookie header in that it does not |
| 6852 | * support the comma as a cookie delimiter (thus the header |
| 6853 | * cannot be folded) because the Expires attribute described in |
| 6854 | * the original Netscape's spec may contain an unquoted date |
| 6855 | * with a comma inside. We have to live with this because |
| 6856 | * many browsers don't support Max-Age and some browsers don't |
| 6857 | * support quoted strings. However the Set-Cookie2 header is |
| 6858 | * clean. |
| 6859 | * |
| 6860 | * We have to keep multiple pointers in order to support cookie |
| 6861 | * removal at the beginning, middle or end of header without |
| 6862 | * corrupting the header (in case of set-cookie2). A special |
| 6863 | * pointer, <scav> points to the beginning of the set-cookie-av |
| 6864 | * fields after the first semi-colon. The <next> pointer points |
| 6865 | * either to the end of line (set-cookie) or next unquoted comma |
| 6866 | * (set-cookie2). All of these headers are valid : |
| 6867 | * |
| 6868 | * Set-Cookie: NAME1 = VALUE 1 ; Secure; Path="/"\r\n |
| 6869 | * Set-Cookie:NAME=VALUE; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 6870 | * Set-Cookie: NAME = VALUE ; Secure; Expires=Thu, 01-Jan-1970 00:00:01 GMT\r\n |
| 6871 | * Set-Cookie2: NAME1 = VALUE 1 ; Max-Age=0, NAME2=VALUE2; Discard\r\n |
| 6872 | * | | | | | | | | | | |
| 6873 | * | | | | | | | | +-> next hdr_end <--+ |
| 6874 | * | | | | | | | +------------> scav |
| 6875 | * | | | | | | +--------------> val_end |
| 6876 | * | | | | | +--------------------> val_beg |
| 6877 | * | | | | +----------------------> equal |
| 6878 | * | | | +------------------------> att_end |
| 6879 | * | | +----------------------------> att_beg |
| 6880 | * | +------------------------------> prev |
| 6881 | * +-----------------------------------------> hdr_beg |
| 6882 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6883 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6884 | for (; prev < hdr_end; prev = next) { |
| 6885 | /* Iterate through all cookies on this line */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6886 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6887 | /* find att_beg */ |
| 6888 | att_beg = prev + 1; |
| 6889 | while (att_beg < hdr_end && http_is_spht[(unsigned char)*att_beg]) |
| 6890 | att_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6891 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6892 | /* find att_end : this is the first character after the last non |
| 6893 | * space before the equal. It may be equal to hdr_end. |
| 6894 | */ |
| 6895 | equal = att_end = att_beg; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6896 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6897 | while (equal < hdr_end) { |
| 6898 | if (*equal == '=' || *equal == ';' || (is_cookie2 && *equal == ',')) |
| 6899 | break; |
| 6900 | if (http_is_spht[(unsigned char)*equal++]) |
| 6901 | continue; |
| 6902 | att_end = equal; |
| 6903 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6904 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6905 | /* here, <equal> points to '=', a delimitor or the end. <att_end> |
| 6906 | * is between <att_beg> and <equal>, both may be identical. |
| 6907 | */ |
| 6908 | |
| 6909 | /* look for end of cookie if there is an equal sign */ |
| 6910 | if (equal < hdr_end && *equal == '=') { |
| 6911 | /* look for the beginning of the value */ |
| 6912 | val_beg = equal + 1; |
| 6913 | while (val_beg < hdr_end && http_is_spht[(unsigned char)*val_beg]) |
| 6914 | val_beg++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6915 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6916 | /* find the end of the value, respecting quotes */ |
| 6917 | next = find_cookie_value_end(val_beg, hdr_end); |
| 6918 | |
| 6919 | /* make val_end point to the first white space or delimitor after the value */ |
| 6920 | val_end = next; |
| 6921 | while (val_end > val_beg && http_is_spht[(unsigned char)*(val_end - 1)]) |
| 6922 | val_end--; |
| 6923 | } else { |
| 6924 | /* <equal> points to next comma, semi-colon or EOL */ |
| 6925 | val_beg = val_end = next = equal; |
| 6926 | } |
| 6927 | |
| 6928 | if (next < hdr_end) { |
| 6929 | /* Set-Cookie2 supports multiple cookies, and <next> points to |
| 6930 | * a colon or semi-colon before the end. So skip all attr-value |
| 6931 | * pairs and look for the next comma. For Set-Cookie, since |
| 6932 | * commas are permitted in values, skip to the end. |
| 6933 | */ |
| 6934 | if (is_cookie2) |
| 6935 | next = find_hdr_value_end(next, hdr_end); |
| 6936 | else |
| 6937 | next = hdr_end; |
| 6938 | } |
| 6939 | |
| 6940 | /* Now everything is as on the diagram above */ |
| 6941 | |
| 6942 | /* Ignore cookies with no equal sign */ |
| 6943 | if (equal == val_end) |
| 6944 | continue; |
| 6945 | |
| 6946 | /* If there are spaces around the equal sign, we need to |
| 6947 | * strip them otherwise we'll get trouble for cookie captures, |
| 6948 | * or even for rewrites. Since this happens extremely rarely, |
| 6949 | * it does not hurt performance. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6950 | */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6951 | if (unlikely(att_end != equal || val_beg > equal + 1)) { |
| 6952 | int stripped_before = 0; |
| 6953 | int stripped_after = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6954 | |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6955 | if (att_end != equal) { |
| 6956 | stripped_before = buffer_replace2(res, att_end, equal, NULL, 0); |
| 6957 | equal += stripped_before; |
| 6958 | val_beg += stripped_before; |
| 6959 | } |
| 6960 | |
| 6961 | if (val_beg > equal + 1) { |
| 6962 | stripped_after = buffer_replace2(res, equal + 1, val_beg, NULL, 0); |
| 6963 | val_beg += stripped_after; |
| 6964 | stripped_before += stripped_after; |
| 6965 | } |
| 6966 | |
| 6967 | val_end += stripped_before; |
| 6968 | next += stripped_before; |
| 6969 | hdr_end += stripped_before; |
| 6970 | hdr_next += stripped_before; |
| 6971 | cur_hdr->len += stripped_before; |
Willy Tarreau | 1fc1f45 | 2011-04-07 22:35:37 +0200 | [diff] [blame] | 6972 | http_msg_move_end(&txn->rsp, stripped_before); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6973 | } |
| 6974 | |
| 6975 | /* First, let's see if we want to capture this cookie. We check |
| 6976 | * that we don't already have a server side cookie, because we |
| 6977 | * can only capture one. Also as an optimisation, we ignore |
| 6978 | * cookies shorter than the declared name. |
| 6979 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6980 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 6981 | txn->srv_cookie == NULL && |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6982 | (val_end - att_beg >= t->fe->capture_namelen) && |
| 6983 | memcmp(att_beg, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
| 6984 | int log_len = val_end - att_beg; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 6985 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6986 | Alert("HTTP logging : out of memory.\n"); |
| 6987 | } |
Willy Tarreau | f70fc75 | 2010-11-19 11:27:18 +0100 | [diff] [blame] | 6988 | else { |
| 6989 | if (log_len > t->fe->capture_len) |
| 6990 | log_len = t->fe->capture_len; |
| 6991 | memcpy(txn->srv_cookie, att_beg, log_len); |
| 6992 | txn->srv_cookie[log_len] = 0; |
| 6993 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6994 | } |
| 6995 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 6996 | srv = target_srv(&t->target); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6997 | /* now check if we need to process it for persistence */ |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 6998 | if (!(t->flags & SN_IGNORE_PRST) && |
| 6999 | (att_end - att_beg == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 7000 | (memcmp(att_beg, t->be->cookie_name, att_end - att_beg) == 0)) { |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7001 | /* assume passive cookie by default */ |
| 7002 | txn->flags &= ~TX_SCK_MASK; |
| 7003 | txn->flags |= TX_SCK_FOUND; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7004 | |
| 7005 | /* If the cookie is in insert mode on a known server, we'll delete |
| 7006 | * this occurrence because we'll insert another one later. |
| 7007 | * 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] | 7008 | * a direct access. |
| 7009 | */ |
Willy Tarreau | ba4c5be | 2010-10-23 12:46:42 +0200 | [diff] [blame] | 7010 | if (t->be->options2 & PR_O2_COOK_PSV) { |
| 7011 | /* The "preserve" flag was set, we don't want to touch the |
| 7012 | * server's cookie. |
| 7013 | */ |
| 7014 | } |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7015 | else if ((srv && (t->be->options & PR_O_COOK_INS)) || |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 7016 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7017 | /* this cookie must be deleted */ |
| 7018 | if (*prev == ':' && next == hdr_end) { |
| 7019 | /* whole header */ |
| 7020 | delta = buffer_replace2(res, hdr_beg, hdr_next, NULL, 0); |
| 7021 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 7022 | txn->hdr_idx.used--; |
| 7023 | cur_hdr->len = 0; |
Willy Tarreau | 26db59e | 2010-11-28 06:57:24 +0100 | [diff] [blame] | 7024 | cur_idx = old_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7025 | hdr_next += delta; |
| 7026 | http_msg_move_end(&txn->rsp, delta); |
| 7027 | /* note: while both invalid now, <next> and <hdr_end> |
| 7028 | * are still equal, so the for() will stop as expected. |
| 7029 | */ |
| 7030 | } else { |
| 7031 | /* just remove the value */ |
| 7032 | int delta = del_hdr_value(res, &prev, next); |
| 7033 | next = prev; |
| 7034 | hdr_end += delta; |
| 7035 | hdr_next += delta; |
| 7036 | cur_hdr->len += delta; |
| 7037 | http_msg_move_end(&txn->rsp, delta); |
| 7038 | } |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7039 | txn->flags &= ~TX_SCK_MASK; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7040 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7041 | /* and go on with next cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7042 | } |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7043 | else if (srv && srv->cookie && (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7044 | /* replace bytes val_beg->val_end with the cookie name associated |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7045 | * with this server since we know it. |
| 7046 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7047 | delta = buffer_replace2(res, val_beg, val_end, srv->cookie, srv->cklen); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7048 | next += delta; |
| 7049 | hdr_end += delta; |
| 7050 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7051 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7052 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7053 | |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7054 | txn->flags &= ~TX_SCK_MASK; |
| 7055 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7056 | } |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7057 | else if (srv && srv && (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7058 | /* insert the cookie name associated with this server |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7059 | * before existing cookie, and insert a delimiter between them.. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7060 | */ |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7061 | delta = buffer_replace2(res, val_beg, val_beg, srv->cookie, srv->cklen + 1); |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7062 | next += delta; |
| 7063 | hdr_end += delta; |
| 7064 | hdr_next += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7065 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 7066 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7067 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7068 | val_beg[srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | f134831 | 2010-10-07 15:54:11 +0200 | [diff] [blame] | 7069 | txn->flags &= ~TX_SCK_MASK; |
| 7070 | txn->flags |= TX_SCK_REPLACED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7071 | } |
| 7072 | } |
Cyril Bonté | 47fdd8e | 2010-04-25 00:00:51 +0200 | [diff] [blame] | 7073 | /* next, let's see if the cookie is our appcookie, unless persistence must be ignored */ |
| 7074 | else if (!(t->flags & SN_IGNORE_PRST) && (t->be->appsession_name != NULL)) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7075 | int cmp_len, value_len; |
| 7076 | char *value_begin; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7077 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7078 | if (t->be->options2 & PR_O2_AS_PFX) { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7079 | cmp_len = MIN(val_end - att_beg, t->be->appsession_name_len); |
| 7080 | value_begin = att_beg + t->be->appsession_name_len; |
| 7081 | 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] | 7082 | } else { |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7083 | cmp_len = att_end - att_beg; |
| 7084 | value_begin = val_beg; |
| 7085 | value_len = MIN(t->be->appsession_len, val_end - val_beg); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7086 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7087 | |
Cyril Bonté | 17530c3 | 2010-04-06 21:11:10 +0200 | [diff] [blame] | 7088 | if ((cmp_len == t->be->appsession_name_len) && |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7089 | (memcmp(att_beg, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 7090 | /* free a possibly previously allocated memory */ |
| 7091 | pool_free2(apools.sessid, txn->sessid); |
| 7092 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7093 | /* Store the sessid in the session for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7094 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7095 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 7096 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 7097 | return; |
| 7098 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7099 | memcpy(txn->sessid, value_begin, value_len); |
| 7100 | txn->sessid[value_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7101 | } |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7102 | } |
| 7103 | /* that's done for this cookie, check the next one on the same |
| 7104 | * line when next != hdr_end (only if is_cookie2). |
| 7105 | */ |
| 7106 | } |
| 7107 | /* check next header */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7108 | old_idx = cur_idx; |
Willy Tarreau | 24581ba | 2010-08-31 22:39:35 +0200 | [diff] [blame] | 7109 | } |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7110 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7111 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7112 | appsess *asession = NULL; |
| 7113 | /* only do insert, if lookup fails */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7114 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7115 | if (asession == NULL) { |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 7116 | size_t server_id_len; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7117 | if ((asession = pool_alloc2(pool2_appsess)) == NULL) { |
| 7118 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 7119 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 7120 | return; |
| 7121 | } |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 7122 | asession->serverid = NULL; /* to avoid a double free in case of allocation error */ |
| 7123 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7124 | if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 7125 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 7126 | 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] | 7127 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7128 | return; |
| 7129 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7130 | memcpy(asession->sessid, txn->sessid, t->be->appsession_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7131 | asession->sessid[t->be->appsession_len] = 0; |
| 7132 | |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7133 | server_id_len = strlen(target_srv(&t->target)->id) + 1; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7134 | if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | 77eb9b8 | 2010-11-19 11:29:06 +0100 | [diff] [blame] | 7135 | Alert("Not enough Memory process_srv():asession->serverid:malloc().\n"); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7136 | 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] | 7137 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7138 | return; |
| 7139 | } |
| 7140 | asession->serverid[0] = '\0'; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7141 | memcpy(asession->serverid, target_srv(&t->target)->id, server_id_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 7142 | |
| 7143 | asession->request_count = 0; |
| 7144 | appsession_hash_insert(&(t->be->htbl_proxy), asession); |
| 7145 | } |
| 7146 | |
| 7147 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 7148 | asession->request_count++; |
| 7149 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7150 | } |
| 7151 | |
| 7152 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7153 | /* |
| 7154 | * Check if response is cacheable or not. Updates t->flags. |
| 7155 | */ |
| 7156 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 7157 | { |
| 7158 | struct http_txn *txn = &t->txn; |
| 7159 | char *p1, *p2; |
| 7160 | |
| 7161 | char *cur_ptr, *cur_end, *cur_next; |
| 7162 | int cur_idx; |
| 7163 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 7164 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7165 | return; |
| 7166 | |
| 7167 | /* Iterate through the headers. |
| 7168 | * we start with the start line. |
| 7169 | */ |
| 7170 | cur_idx = 0; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7171 | cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7172 | |
| 7173 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 7174 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7175 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7176 | |
| 7177 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 7178 | cur_ptr = cur_next; |
| 7179 | cur_end = cur_ptr + cur_hdr->len; |
| 7180 | cur_next = cur_end + cur_hdr->cr + 1; |
| 7181 | |
| 7182 | /* We have one full header between cur_ptr and cur_end, and the |
| 7183 | * next header starts at cur_next. We're only interested in |
| 7184 | * "Cookie:" headers. |
| 7185 | */ |
| 7186 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7187 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 7188 | if (val) { |
| 7189 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 7190 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 7191 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 7192 | return; |
| 7193 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7194 | } |
| 7195 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7196 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 7197 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7198 | continue; |
| 7199 | |
| 7200 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 7201 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 7202 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7203 | |
| 7204 | if (p1 >= cur_end) /* no more info */ |
| 7205 | continue; |
| 7206 | |
| 7207 | /* p1 is at the beginning of the value */ |
| 7208 | p2 = p1; |
| 7209 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 7210 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7211 | p2++; |
| 7212 | |
| 7213 | /* we have a complete value between p1 and p2 */ |
| 7214 | if (p2 < cur_end && *p2 == '=') { |
| 7215 | /* we have something of the form no-cache="set-cookie" */ |
| 7216 | if ((cur_end - p1 >= 21) && |
| 7217 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 7218 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7219 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7220 | continue; |
| 7221 | } |
| 7222 | |
| 7223 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 7224 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 7225 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 7226 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 7227 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7228 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7229 | return; |
| 7230 | } |
| 7231 | |
| 7232 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 7233 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 7234 | continue; |
| 7235 | } |
| 7236 | } |
| 7237 | } |
| 7238 | |
| 7239 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7240 | /* |
| 7241 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 7242 | * If the server is found, it's assigned to the session. |
| 7243 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7244 | 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] | 7245 | { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7246 | char *end_params, *first_param, *cur_param, *next_param; |
| 7247 | char separator; |
| 7248 | int value_len; |
| 7249 | |
| 7250 | int mode = t->be->options2 & PR_O2_AS_M_ANY; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7251 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 7252 | if (t->be->appsession_name == NULL || |
Cyril Bonté | 17530c3 | 2010-04-06 21:11:10 +0200 | [diff] [blame] | 7253 | (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] | 7254 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7255 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7256 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7257 | first_param = NULL; |
| 7258 | switch (mode) { |
| 7259 | case PR_O2_AS_M_PP: |
| 7260 | first_param = memchr(begin, ';', len); |
| 7261 | break; |
| 7262 | case PR_O2_AS_M_QS: |
| 7263 | first_param = memchr(begin, '?', len); |
| 7264 | break; |
| 7265 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7266 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7267 | if (first_param == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7268 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7269 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7270 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7271 | switch (mode) { |
| 7272 | case PR_O2_AS_M_PP: |
| 7273 | if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) { |
| 7274 | end_params = (char *) begin + len; |
| 7275 | } |
| 7276 | separator = ';'; |
| 7277 | break; |
| 7278 | case PR_O2_AS_M_QS: |
| 7279 | end_params = (char *) begin + len; |
| 7280 | separator = '&'; |
| 7281 | break; |
| 7282 | default: |
| 7283 | /* unknown mode, shouldn't happen */ |
| 7284 | return; |
| 7285 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7286 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 7287 | cur_param = next_param = end_params; |
| 7288 | while (cur_param > first_param) { |
| 7289 | cur_param--; |
| 7290 | if ((cur_param[0] == separator) || (cur_param == first_param)) { |
| 7291 | /* let's see if this is the appsession parameter */ |
| 7292 | if ((cur_param + t->be->appsession_name_len + 1 < next_param) && |
| 7293 | ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') && |
| 7294 | (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 7295 | /* Cool... it's the right one */ |
| 7296 | cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2); |
| 7297 | value_len = MIN(t->be->appsession_len, next_param - cur_param); |
| 7298 | if (value_len > 0) { |
| 7299 | manage_client_side_appsession(t, cur_param, value_len); |
| 7300 | } |
| 7301 | break; |
| 7302 | } |
| 7303 | next_param = cur_param; |
| 7304 | } |
| 7305 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7306 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 7307 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 7308 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7309 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7310 | } |
| 7311 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7312 | /* |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7313 | * 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] | 7314 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7315 | * |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7316 | * 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] | 7317 | * uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7318 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7319 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7320 | */ |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7321 | 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] | 7322 | { |
| 7323 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7324 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7325 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 7326 | if (!uri_auth) |
| 7327 | return 0; |
| 7328 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7329 | 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] | 7330 | return 0; |
| 7331 | |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7332 | memset(&si->applet.ctx.stats, 0, sizeof(si->applet.ctx.stats)); |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 7333 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7334 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 7335 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7336 | return 0; |
| 7337 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7338 | h = txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 7339 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 7340 | /* the URI is in h */ |
| 7341 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7342 | return 0; |
| 7343 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7344 | h += uri_auth->uri_len; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7345 | while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) { |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7346 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7347 | si->applet.ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7348 | break; |
| 7349 | } |
| 7350 | h++; |
| 7351 | } |
| 7352 | |
| 7353 | if (uri_auth->refresh) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7354 | h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len; |
| 7355 | while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) { |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7356 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7357 | si->applet.ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 7358 | break; |
| 7359 | } |
| 7360 | h++; |
| 7361 | } |
| 7362 | } |
| 7363 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7364 | h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len; |
| 7365 | while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) { |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 7366 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7367 | si->applet.ctx.stats.flags |= STAT_FMT_CSV; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 7368 | break; |
| 7369 | } |
| 7370 | h++; |
| 7371 | } |
| 7372 | |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7373 | h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len; |
| 7374 | while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 8) { |
| 7375 | if (memcmp(h, ";st=", 4) == 0) { |
| 7376 | h += 4; |
| 7377 | |
| 7378 | if (memcmp(h, STAT_STATUS_DONE, 4) == 0) |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7379 | si->applet.ctx.stats.st_code = STAT_STATUS_DONE; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7380 | else if (memcmp(h, STAT_STATUS_NONE, 4) == 0) |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7381 | si->applet.ctx.stats.st_code = STAT_STATUS_NONE; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7382 | else if (memcmp(h, STAT_STATUS_EXCD, 4) == 0) |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7383 | si->applet.ctx.stats.st_code = STAT_STATUS_EXCD; |
Cyril Bonté | 474be41 | 2010-10-12 00:14:36 +0200 | [diff] [blame] | 7384 | else if (memcmp(h, STAT_STATUS_DENY, 4) == 0) |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7385 | si->applet.ctx.stats.st_code = STAT_STATUS_DENY; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7386 | else |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7387 | si->applet.ctx.stats.st_code = STAT_STATUS_UNKN; |
Cyril Bonté | 70be45d | 2010-10-12 00:14:35 +0200 | [diff] [blame] | 7388 | break; |
| 7389 | } |
| 7390 | h++; |
| 7391 | } |
| 7392 | |
Willy Tarreau | 295a837 | 2011-03-10 11:25:07 +0100 | [diff] [blame] | 7393 | si->applet.ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO; |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 7394 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7395 | return 1; |
| 7396 | } |
| 7397 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7398 | /* |
| 7399 | * Capture a bad request or response and archive it in the proxy's structure. |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7400 | * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not |
| 7401 | * assume that msg->sol = buf->data + msg->som. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7402 | */ |
| 7403 | void http_capture_bad_message(struct error_snapshot *es, struct session *s, |
| 7404 | struct buffer *buf, struct http_msg *msg, |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 7405 | int state, struct proxy *other_end) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7406 | { |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7407 | if (buf->r <= (buf->data + msg->som)) { /* message wraps */ |
| 7408 | int len1 = buf->size - msg->som; |
| 7409 | es->len = buf->r - (buf->data + msg->som) + buf->size; |
| 7410 | memcpy(es->buf, buf->data + msg->som, MIN(len1, sizeof(es->buf))); |
| 7411 | if (es->len > len1 && len1 < sizeof(es->buf)) |
| 7412 | memcpy(es->buf, buf->data, MIN(es->len, sizeof(es->buf)) - len1); |
| 7413 | } |
| 7414 | else { |
| 7415 | es->len = buf->r - (buf->data + msg->som); |
| 7416 | memcpy(es->buf, buf->data + msg->som, MIN(es->len, sizeof(es->buf))); |
| 7417 | } |
| 7418 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7419 | if (msg->err_pos >= 0) |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 7420 | es->pos = msg->err_pos - msg->som; |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7421 | else if (buf->lr >= (buf->data + msg->som)) |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 7422 | es->pos = buf->lr - (buf->data + msg->som); |
Willy Tarreau | 81f2fb9 | 2010-12-12 13:09:08 +0100 | [diff] [blame] | 7423 | else |
| 7424 | es->pos = buf->lr - (buf->data + msg->som) + buf->size; |
| 7425 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7426 | es->when = date; // user-visible date |
| 7427 | es->sid = s->uniq_id; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7428 | es->srv = target_srv(&s->target); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7429 | es->oe = other_end; |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 7430 | es->src = s->req->prod->addr.c.from; |
Willy Tarreau | 078272e | 2010-12-12 12:46:33 +0100 | [diff] [blame] | 7431 | es->state = state; |
| 7432 | es->flags = buf->flags; |
Willy Tarreau | 10479e4 | 2010-12-12 14:00:34 +0100 | [diff] [blame] | 7433 | es->ev_id = error_snapshot_id++; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 7434 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 7435 | |
Willy Tarreau | bce7088 | 2009-09-07 11:51:47 +0200 | [diff] [blame] | 7436 | /* return the IP address pointed to by occurrence <occ> of header <hname> in |
| 7437 | * HTTP message <msg> indexed in <idx>. If <occ> is strictly positive, the |
| 7438 | * occurrence number corresponding to this value is returned. If <occ> is |
| 7439 | * strictly negative, the occurrence number before the end corresponding to |
| 7440 | * this value is returned. If <occ> is null, any value is returned, so it is |
| 7441 | * not recommended to use it that way. Negative occurrences are limited to |
| 7442 | * a small value because it is required to keep them in memory while scanning. |
| 7443 | * IP address 0.0.0.0 is returned if no match is found. |
| 7444 | */ |
| 7445 | unsigned int get_ip_from_hdr2(struct http_msg *msg, const char *hname, int hlen, struct hdr_idx *idx, int occ) |
| 7446 | { |
| 7447 | struct hdr_ctx ctx; |
| 7448 | unsigned int hdr_hist[MAX_HDR_HISTORY]; |
| 7449 | unsigned int hist_ptr; |
| 7450 | int found = 0; |
| 7451 | |
| 7452 | ctx.idx = 0; |
| 7453 | if (occ >= 0) { |
| 7454 | while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) { |
| 7455 | occ--; |
| 7456 | if (occ <= 0) { |
| 7457 | found = 1; |
| 7458 | break; |
| 7459 | } |
| 7460 | } |
| 7461 | if (!found) |
| 7462 | return 0; |
| 7463 | return inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen); |
| 7464 | } |
| 7465 | |
| 7466 | /* negative occurrence, we scan all the list then walk back */ |
| 7467 | if (-occ > MAX_HDR_HISTORY) |
| 7468 | return 0; |
| 7469 | |
| 7470 | hist_ptr = 0; |
| 7471 | hdr_hist[hist_ptr] = 0; |
| 7472 | while (http_find_header2(hname, hlen, msg->sol, idx, &ctx)) { |
| 7473 | hdr_hist[hist_ptr++] = inetaddr_host_lim(ctx.line+ctx.val, ctx.line+ctx.val+ctx.vlen); |
| 7474 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 7475 | hist_ptr = 0; |
| 7476 | found++; |
| 7477 | } |
| 7478 | if (-occ > found) |
| 7479 | return 0; |
| 7480 | /* OK now we have the last occurrence in [hist_ptr-1], and we need to |
| 7481 | * find occurrence -occ, so we have to check [hist_ptr+occ]. |
| 7482 | */ |
| 7483 | hist_ptr += occ; |
| 7484 | if (hist_ptr >= MAX_HDR_HISTORY) |
| 7485 | hist_ptr -= MAX_HDR_HISTORY; |
| 7486 | return hdr_hist[hist_ptr]; |
| 7487 | } |
| 7488 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 7489 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7490 | * Print a debug line with a header |
| 7491 | */ |
| 7492 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 7493 | { |
| 7494 | int len, max; |
| 7495 | len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 7496 | dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7497 | max = end - start; |
| 7498 | UBOUND(max, sizeof(trash) - len - 1); |
| 7499 | len += strlcpy2(trash + len, start, max + 1); |
| 7500 | trash[len++] = '\n'; |
| 7501 | write(1, trash, len); |
| 7502 | } |
| 7503 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7504 | /* |
| 7505 | * Initialize a new HTTP transaction for session <s>. It is assumed that all |
| 7506 | * the required fields are properly allocated and that we only need to (re)init |
| 7507 | * them. This should be used before processing any new request. |
| 7508 | */ |
| 7509 | void http_init_txn(struct session *s) |
| 7510 | { |
| 7511 | struct http_txn *txn = &s->txn; |
| 7512 | struct proxy *fe = s->fe; |
| 7513 | |
| 7514 | txn->flags = 0; |
| 7515 | txn->status = -1; |
| 7516 | |
Willy Tarreau | f64d141 | 2010-10-07 20:06:11 +0200 | [diff] [blame] | 7517 | txn->cookie_first_date = 0; |
| 7518 | txn->cookie_last_date = 0; |
| 7519 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7520 | txn->req.sol = txn->req.eol = NULL; |
| 7521 | txn->req.som = txn->req.eoh = 0; /* relative to the buffer */ |
| 7522 | txn->rsp.sol = txn->rsp.eol = NULL; |
| 7523 | txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */ |
Willy Tarreau | 124d991 | 2011-03-01 20:30:48 +0100 | [diff] [blame] | 7524 | txn->req.chunk_len = 0LL; |
| 7525 | txn->req.body_len = 0LL; |
| 7526 | txn->rsp.chunk_len = 0LL; |
| 7527 | txn->rsp.body_len = 0LL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7528 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 7529 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7530 | |
| 7531 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7532 | |
| 7533 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 7534 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 7535 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 7536 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7537 | if (txn->req.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7538 | memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *)); |
| 7539 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7540 | if (txn->rsp.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7541 | memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
| 7542 | |
| 7543 | if (txn->hdr_idx.v) |
| 7544 | hdr_idx_init(&txn->hdr_idx); |
| 7545 | } |
| 7546 | |
| 7547 | /* to be used at the end of a transaction */ |
| 7548 | void http_end_txn(struct session *s) |
| 7549 | { |
| 7550 | struct http_txn *txn = &s->txn; |
| 7551 | |
| 7552 | /* these ones will have been dynamically allocated */ |
| 7553 | pool_free2(pool2_requri, txn->uri); |
| 7554 | pool_free2(pool2_capture, txn->cli_cookie); |
| 7555 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7556 | pool_free2(apools.sessid, txn->sessid); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7557 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 7558 | txn->sessid = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7559 | txn->uri = NULL; |
| 7560 | txn->srv_cookie = NULL; |
| 7561 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 7562 | |
| 7563 | if (txn->req.cap) { |
| 7564 | struct cap_hdr *h; |
| 7565 | for (h = s->fe->req_cap; h; h = h->next) |
| 7566 | pool_free2(h->pool, txn->req.cap[h->index]); |
| 7567 | memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *)); |
| 7568 | } |
| 7569 | |
| 7570 | if (txn->rsp.cap) { |
| 7571 | struct cap_hdr *h; |
| 7572 | for (h = s->fe->rsp_cap; h; h = h->next) |
| 7573 | pool_free2(h->pool, txn->rsp.cap[h->index]); |
| 7574 | memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *)); |
| 7575 | } |
| 7576 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7577 | } |
| 7578 | |
| 7579 | /* to be used at the end of a transaction to prepare a new one */ |
| 7580 | void http_reset_txn(struct session *s) |
| 7581 | { |
| 7582 | http_end_txn(s); |
| 7583 | http_init_txn(s); |
| 7584 | |
| 7585 | s->be = s->fe; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7586 | s->logs.logwait = s->fe->to_log; |
Willy Tarreau | 827aee9 | 2011-03-10 16:55:02 +0100 | [diff] [blame] | 7587 | s->srv_conn = NULL; |
Willy Tarreau | 9e000c6 | 2011-03-10 14:03:36 +0100 | [diff] [blame] | 7588 | clear_target(&s->target); |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 7589 | /* re-init store persistence */ |
| 7590 | s->store_count = 0; |
| 7591 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7592 | s->pend_pos = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7593 | |
| 7594 | s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */ |
| 7595 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 7596 | /* We must trim any excess data from the response buffer, because we |
| 7597 | * may have blocked an invalid response from a server that we don't |
| 7598 | * want to accidentely forward once we disable the analysers, nor do |
| 7599 | * we want those data to come along with next response. A typical |
| 7600 | * example of such data would be from a buggy server responding to |
| 7601 | * a HEAD with some data, or sending more than the advertised |
| 7602 | * content-length. |
| 7603 | */ |
| 7604 | if (unlikely(s->rep->l > s->rep->send_max)) { |
| 7605 | s->rep->l = s->rep->send_max; |
| 7606 | s->rep->r = s->rep->w + s->rep->l; |
| 7607 | if (s->rep->r >= s->rep->data + s->rep->size) |
| 7608 | s->rep->r -= s->rep->size; |
| 7609 | } |
| 7610 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7611 | s->req->rto = s->fe->timeout.client; |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 7612 | s->req->wto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7613 | |
Willy Tarreau | d04e858 | 2010-05-31 12:31:35 +0200 | [diff] [blame] | 7614 | s->rep->rto = TICK_ETERNITY; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7615 | s->rep->wto = s->fe->timeout.client; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 7616 | |
| 7617 | s->req->rex = TICK_ETERNITY; |
| 7618 | s->req->wex = TICK_ETERNITY; |
| 7619 | s->req->analyse_exp = TICK_ETERNITY; |
| 7620 | s->rep->rex = TICK_ETERNITY; |
| 7621 | s->rep->wex = TICK_ETERNITY; |
| 7622 | s->rep->analyse_exp = TICK_ETERNITY; |
| 7623 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7624 | |
Willy Tarreau | ff011f2 | 2011-01-06 17:51:27 +0100 | [diff] [blame] | 7625 | void free_http_req_rules(struct list *r) { |
| 7626 | struct http_req_rule *tr, *pr; |
| 7627 | |
| 7628 | list_for_each_entry_safe(pr, tr, r, list) { |
| 7629 | LIST_DEL(&pr->list); |
| 7630 | if (pr->action == HTTP_REQ_ACT_HTTP_AUTH) |
| 7631 | free(pr->http_auth.realm); |
| 7632 | |
| 7633 | free(pr); |
| 7634 | } |
| 7635 | } |
| 7636 | |
| 7637 | struct http_req_rule *parse_http_req_cond(const char **args, const char *file, int linenum, struct proxy *proxy) |
| 7638 | { |
| 7639 | struct http_req_rule *rule; |
| 7640 | int cur_arg; |
| 7641 | |
| 7642 | rule = (struct http_req_rule*)calloc(1, sizeof(struct http_req_rule)); |
| 7643 | if (!rule) { |
| 7644 | Alert("parsing [%s:%d]: out of memory.\n", file, linenum); |
| 7645 | return NULL; |
| 7646 | } |
| 7647 | |
| 7648 | if (!*args[0]) { |
| 7649 | goto req_error_parsing; |
| 7650 | } else if (!strcmp(args[0], "allow")) { |
| 7651 | rule->action = HTTP_REQ_ACT_ALLOW; |
| 7652 | cur_arg = 1; |
| 7653 | } else if (!strcmp(args[0], "deny")) { |
| 7654 | rule->action = HTTP_REQ_ACT_DENY; |
| 7655 | cur_arg = 1; |
| 7656 | } else if (!strcmp(args[0], "auth")) { |
| 7657 | rule->action = HTTP_REQ_ACT_HTTP_AUTH; |
| 7658 | cur_arg = 1; |
| 7659 | |
| 7660 | while(*args[cur_arg]) { |
| 7661 | if (!strcmp(args[cur_arg], "realm")) { |
| 7662 | rule->http_auth.realm = strdup(args[cur_arg + 1]); |
| 7663 | cur_arg+=2; |
| 7664 | continue; |
| 7665 | } else |
| 7666 | break; |
| 7667 | } |
| 7668 | } else { |
| 7669 | req_error_parsing: |
| 7670 | Alert("parsing [%s:%d]: %s '%s', expects 'allow', 'deny', 'auth'.\n", |
| 7671 | file, linenum, *args[1]?"unknown parameter":"missing keyword in", args[*args[1]?1:0]); |
| 7672 | return NULL; |
| 7673 | } |
| 7674 | |
| 7675 | if (strcmp(args[cur_arg], "if") == 0 || strcmp(args[cur_arg], "unless") == 0) { |
| 7676 | struct acl_cond *cond; |
| 7677 | |
| 7678 | if ((cond = build_acl_cond(file, linenum, proxy, args+cur_arg)) == NULL) { |
| 7679 | Alert("parsing [%s:%d] : error detected while parsing an 'http-request %s' condition.\n", |
| 7680 | file, linenum, args[0]); |
| 7681 | return NULL; |
| 7682 | } |
| 7683 | rule->cond = cond; |
| 7684 | } |
| 7685 | else if (*args[cur_arg]) { |
| 7686 | Alert("parsing [%s:%d]: 'http-request %s' expects 'realm' for 'auth' or" |
| 7687 | " either 'if' or 'unless' followed by a condition but found '%s'.\n", |
| 7688 | file, linenum, args[0], args[cur_arg]); |
| 7689 | return NULL; |
| 7690 | } |
| 7691 | |
| 7692 | return rule; |
| 7693 | } |
| 7694 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7695 | /************************************************************************/ |
| 7696 | /* The code below is dedicated to ACL parsing and matching */ |
| 7697 | /************************************************************************/ |
| 7698 | |
| 7699 | |
| 7700 | |
| 7701 | |
| 7702 | /* 1. Check on METHOD |
| 7703 | * We use the pre-parsed method if it is known, and store its number as an |
| 7704 | * integer. If it is unknown, we use the pointer and the length. |
| 7705 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7706 | static int acl_parse_meth(const char **text, struct acl_pattern *pattern, int *opaque) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7707 | { |
| 7708 | int len, meth; |
| 7709 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7710 | len = strlen(*text); |
| 7711 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7712 | |
| 7713 | pattern->val.i = meth; |
| 7714 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7715 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7716 | if (!pattern->ptr.str) |
| 7717 | return 0; |
| 7718 | pattern->len = len; |
| 7719 | } |
| 7720 | return 1; |
| 7721 | } |
| 7722 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7723 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7724 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7725 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7726 | { |
| 7727 | int meth; |
| 7728 | struct http_txn *txn = l7; |
| 7729 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7730 | if (!txn) |
| 7731 | return 0; |
| 7732 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7733 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7734 | return 0; |
| 7735 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7736 | meth = txn->meth; |
| 7737 | test->i = meth; |
| 7738 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7739 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7740 | /* ensure the indexes are not affected */ |
| 7741 | return 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7742 | test->len = txn->req.sl.rq.m_l; |
| 7743 | test->ptr = txn->req.sol; |
| 7744 | } |
| 7745 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 7746 | return 1; |
| 7747 | } |
| 7748 | |
| 7749 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 7750 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 7751 | int icase; |
| 7752 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7753 | if (test->i != pattern->val.i) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 7754 | return ACL_PAT_FAIL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7755 | |
| 7756 | if (test->i != HTTP_METH_OTHER) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 7757 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7758 | |
| 7759 | /* Other method, we must compare the strings */ |
| 7760 | if (pattern->len != test->len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 7761 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 7762 | |
| 7763 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 7764 | if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) || |
| 7765 | (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 7766 | return ACL_PAT_FAIL; |
| 7767 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7768 | } |
| 7769 | |
| 7770 | /* 2. Check on Request/Status Version |
| 7771 | * We simply compare strings here. |
| 7772 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7773 | static int acl_parse_ver(const char **text, struct acl_pattern *pattern, int *opaque) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7774 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7775 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7776 | if (!pattern->ptr.str) |
| 7777 | return 0; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 7778 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7779 | return 1; |
| 7780 | } |
| 7781 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7782 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7783 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7784 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7785 | { |
| 7786 | struct http_txn *txn = l7; |
| 7787 | char *ptr; |
| 7788 | int len; |
| 7789 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7790 | if (!txn) |
| 7791 | return 0; |
| 7792 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7793 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7794 | return 0; |
| 7795 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7796 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7797 | ptr = txn->req.sol + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7798 | |
| 7799 | while ((len-- > 0) && (*ptr++ != '/')); |
| 7800 | if (len <= 0) |
| 7801 | return 0; |
| 7802 | |
| 7803 | test->ptr = ptr; |
| 7804 | test->len = len; |
| 7805 | |
| 7806 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 7807 | return 1; |
| 7808 | } |
| 7809 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7810 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7811 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7812 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7813 | { |
| 7814 | struct http_txn *txn = l7; |
| 7815 | char *ptr; |
| 7816 | int len; |
| 7817 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7818 | if (!txn) |
| 7819 | return 0; |
| 7820 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7821 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7822 | return 0; |
| 7823 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7824 | len = txn->rsp.sl.st.v_l; |
| 7825 | ptr = txn->rsp.sol; |
| 7826 | |
| 7827 | while ((len-- > 0) && (*ptr++ != '/')); |
| 7828 | if (len <= 0) |
| 7829 | return 0; |
| 7830 | |
| 7831 | test->ptr = ptr; |
| 7832 | test->len = len; |
| 7833 | |
| 7834 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 7835 | return 1; |
| 7836 | } |
| 7837 | |
| 7838 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7839 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7840 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7841 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7842 | { |
| 7843 | struct http_txn *txn = l7; |
| 7844 | char *ptr; |
| 7845 | int len; |
| 7846 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7847 | if (!txn) |
| 7848 | return 0; |
| 7849 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7850 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7851 | return 0; |
| 7852 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7853 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7854 | ptr = txn->rsp.sol + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7855 | |
| 7856 | test->i = __strl2ui(ptr, len); |
| 7857 | test->flags = ACL_TEST_F_VOL_1ST; |
| 7858 | return 1; |
| 7859 | } |
| 7860 | |
| 7861 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 7862 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 7863 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7864 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7865 | { |
| 7866 | struct http_txn *txn = l7; |
| 7867 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7868 | if (!txn) |
| 7869 | return 0; |
| 7870 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7871 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7872 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7873 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7874 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7875 | /* ensure the indexes are not affected */ |
| 7876 | return 0; |
| 7877 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7878 | test->len = txn->req.sl.rq.u_l; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7879 | test->ptr = txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7880 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 7881 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 7882 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7883 | return 1; |
| 7884 | } |
| 7885 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7886 | static int |
| 7887 | acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7888 | struct acl_expr *expr, struct acl_test *test) |
| 7889 | { |
| 7890 | struct http_txn *txn = l7; |
| 7891 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7892 | if (!txn) |
| 7893 | return 0; |
| 7894 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7895 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7896 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7897 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7898 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7899 | /* ensure the indexes are not affected */ |
| 7900 | return 0; |
| 7901 | |
| 7902 | /* Parse HTTP request */ |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 7903 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.s.to); |
| 7904 | test->ptr = (void *)&((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_addr; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7905 | test->i = AF_INET; |
| 7906 | |
| 7907 | /* |
| 7908 | * If we are parsing url in frontend space, we prepare backend stage |
| 7909 | * to not parse again the same url ! optimization lazyness... |
| 7910 | */ |
| 7911 | if (px->options & PR_O_HTTP_PROXY) |
| 7912 | l4->flags |= SN_ADDR_SET; |
| 7913 | |
| 7914 | test->flags = ACL_TEST_F_READ_ONLY; |
| 7915 | return 1; |
| 7916 | } |
| 7917 | |
| 7918 | static int |
| 7919 | acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7920 | struct acl_expr *expr, struct acl_test *test) |
| 7921 | { |
| 7922 | struct http_txn *txn = l7; |
| 7923 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7924 | if (!txn) |
| 7925 | return 0; |
| 7926 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7927 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7928 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7929 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7930 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7931 | /* ensure the indexes are not affected */ |
| 7932 | return 0; |
| 7933 | |
| 7934 | /* Same optimization as url_ip */ |
Willy Tarreau | 957c0a5 | 2011-03-03 17:42:23 +0100 | [diff] [blame] | 7935 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->req->cons->addr.s.to); |
| 7936 | test->i = ntohs(((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_port); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 7937 | |
| 7938 | if (px->options & PR_O_HTTP_PROXY) |
| 7939 | l4->flags |= SN_ADDR_SET; |
| 7940 | |
| 7941 | test->flags = ACL_TEST_F_READ_ONLY; |
| 7942 | return 1; |
| 7943 | } |
| 7944 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7945 | /* 5. Check on HTTP header. A pointer to the beginning of the value is returned. |
| 7946 | * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr(). |
| 7947 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7948 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7949 | acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7950 | struct acl_expr *expr, struct acl_test *test) |
| 7951 | { |
| 7952 | struct http_txn *txn = l7; |
| 7953 | struct hdr_idx *idx = &txn->hdr_idx; |
| 7954 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7955 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7956 | if (!txn) |
| 7957 | return 0; |
| 7958 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7959 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 7960 | /* search for header from the beginning */ |
| 7961 | ctx->idx = 0; |
| 7962 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7963 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 7964 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 7965 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 7966 | test->len = ctx->vlen; |
| 7967 | test->ptr = (char *)ctx->line + ctx->val; |
| 7968 | return 1; |
| 7969 | } |
| 7970 | |
| 7971 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 7972 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 7973 | return 0; |
| 7974 | } |
| 7975 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7976 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7977 | acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7978 | struct acl_expr *expr, struct acl_test *test) |
| 7979 | { |
| 7980 | struct http_txn *txn = l7; |
| 7981 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7982 | if (!txn) |
| 7983 | return 0; |
| 7984 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7985 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7986 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7987 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7988 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7989 | /* ensure the indexes are not affected */ |
| 7990 | return 0; |
| 7991 | |
| 7992 | return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test); |
| 7993 | } |
| 7994 | |
| 7995 | static int |
| 7996 | acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7997 | struct acl_expr *expr, struct acl_test *test) |
| 7998 | { |
| 7999 | struct http_txn *txn = l7; |
| 8000 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8001 | if (!txn) |
| 8002 | return 0; |
| 8003 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8004 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8005 | return 0; |
| 8006 | |
| 8007 | return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test); |
| 8008 | } |
| 8009 | |
| 8010 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 8011 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 8012 | */ |
| 8013 | static int |
| 8014 | acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8015 | struct acl_expr *expr, struct acl_test *test) |
| 8016 | { |
| 8017 | struct http_txn *txn = l7; |
| 8018 | struct hdr_idx *idx = &txn->hdr_idx; |
| 8019 | struct hdr_ctx ctx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8020 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8021 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8022 | if (!txn) |
| 8023 | return 0; |
| 8024 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8025 | ctx.idx = 0; |
| 8026 | cnt = 0; |
| 8027 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx)) |
| 8028 | cnt++; |
| 8029 | |
| 8030 | test->i = cnt; |
| 8031 | test->flags = ACL_TEST_F_VOL_HDR; |
| 8032 | return 1; |
| 8033 | } |
| 8034 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8035 | static int |
| 8036 | acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8037 | struct acl_expr *expr, struct acl_test *test) |
| 8038 | { |
| 8039 | struct http_txn *txn = l7; |
| 8040 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8041 | if (!txn) |
| 8042 | return 0; |
| 8043 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8044 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8045 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8046 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8047 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 8048 | /* ensure the indexes are not affected */ |
| 8049 | return 0; |
| 8050 | |
| 8051 | return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test); |
| 8052 | } |
| 8053 | |
| 8054 | static int |
| 8055 | acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8056 | struct acl_expr *expr, struct acl_test *test) |
| 8057 | { |
| 8058 | struct http_txn *txn = l7; |
| 8059 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8060 | if (!txn) |
| 8061 | return 0; |
| 8062 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8063 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8064 | return 0; |
| 8065 | |
| 8066 | return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test); |
| 8067 | } |
| 8068 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8069 | /* 7. Check on HTTP header's integer value. The integer value is returned. |
| 8070 | * FIXME: the type is 'int', it may not be appropriate for everything. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8071 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8072 | */ |
| 8073 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8074 | acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8075 | struct acl_expr *expr, struct acl_test *test) |
| 8076 | { |
| 8077 | struct http_txn *txn = l7; |
| 8078 | struct hdr_idx *idx = &txn->hdr_idx; |
| 8079 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8080 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8081 | if (!txn) |
| 8082 | return 0; |
| 8083 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8084 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 8085 | /* search for header from the beginning */ |
| 8086 | ctx->idx = 0; |
| 8087 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8088 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 8089 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 8090 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 8091 | test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen); |
| 8092 | return 1; |
| 8093 | } |
| 8094 | |
| 8095 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 8096 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 8097 | return 0; |
| 8098 | } |
| 8099 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8100 | static int |
| 8101 | acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8102 | struct acl_expr *expr, struct acl_test *test) |
| 8103 | { |
| 8104 | struct http_txn *txn = l7; |
| 8105 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8106 | if (!txn) |
| 8107 | return 0; |
| 8108 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8109 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8110 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8111 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8112 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 8113 | /* ensure the indexes are not affected */ |
| 8114 | return 0; |
| 8115 | |
| 8116 | return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test); |
| 8117 | } |
| 8118 | |
| 8119 | static int |
| 8120 | acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8121 | struct acl_expr *expr, struct acl_test *test) |
| 8122 | { |
| 8123 | struct http_txn *txn = l7; |
| 8124 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8125 | if (!txn) |
| 8126 | return 0; |
| 8127 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8128 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8129 | return 0; |
| 8130 | |
| 8131 | return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test); |
| 8132 | } |
| 8133 | |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8134 | /* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned. |
| 8135 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 8136 | */ |
| 8137 | static int |
| 8138 | acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol, |
| 8139 | struct acl_expr *expr, struct acl_test *test) |
| 8140 | { |
| 8141 | struct http_txn *txn = l7; |
| 8142 | struct hdr_idx *idx = &txn->hdr_idx; |
| 8143 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
| 8144 | |
| 8145 | if (!txn) |
| 8146 | return 0; |
| 8147 | |
| 8148 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 8149 | /* search for header from the beginning */ |
| 8150 | ctx->idx = 0; |
| 8151 | |
| 8152 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 8153 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 8154 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 8155 | /* Same optimization as url_ip */ |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 8156 | memset(&((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_addr, 0, sizeof(((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_addr)); |
| 8157 | url2ipv4((char *)ctx->line + ctx->val, &((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_addr); |
| 8158 | test->ptr = (void *)&((struct sockaddr_in *)&l4->req->cons->addr.s.to)->sin_addr; |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8159 | test->i = AF_INET; |
| 8160 | return 1; |
| 8161 | } |
| 8162 | |
| 8163 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 8164 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 8165 | return 0; |
| 8166 | } |
| 8167 | |
| 8168 | static int |
| 8169 | acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8170 | struct acl_expr *expr, struct acl_test *test) |
| 8171 | { |
| 8172 | struct http_txn *txn = l7; |
| 8173 | |
| 8174 | if (!txn) |
| 8175 | return 0; |
| 8176 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8177 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8178 | return 0; |
| 8179 | |
| 8180 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 8181 | /* ensure the indexes are not affected */ |
| 8182 | return 0; |
| 8183 | |
| 8184 | return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test); |
| 8185 | } |
| 8186 | |
| 8187 | static int |
| 8188 | acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8189 | struct acl_expr *expr, struct acl_test *test) |
| 8190 | { |
| 8191 | struct http_txn *txn = l7; |
| 8192 | |
| 8193 | if (!txn) |
| 8194 | return 0; |
| 8195 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8196 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 8197 | return 0; |
| 8198 | |
| 8199 | return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test); |
| 8200 | } |
| 8201 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8202 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 8203 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 8204 | */ |
| 8205 | static int |
| 8206 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8207 | struct acl_expr *expr, struct acl_test *test) |
| 8208 | { |
| 8209 | struct http_txn *txn = l7; |
| 8210 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 8211 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8212 | if (!txn) |
| 8213 | return 0; |
| 8214 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8215 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8216 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 8217 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8218 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 8219 | /* ensure the indexes are not affected */ |
| 8220 | return 0; |
| 8221 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 8222 | end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 8223 | ptr = http_get_path(txn); |
| 8224 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8225 | return 0; |
| 8226 | |
| 8227 | /* OK, we got the '/' ! */ |
| 8228 | test->ptr = ptr; |
| 8229 | |
| 8230 | while (ptr < end && *ptr != '?') |
| 8231 | ptr++; |
| 8232 | |
| 8233 | test->len = ptr - test->ptr; |
| 8234 | |
| 8235 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 8236 | test->flags = ACL_TEST_F_VOL_1ST; |
| 8237 | return 1; |
| 8238 | } |
| 8239 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8240 | static int |
| 8241 | acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir, |
| 8242 | struct acl_expr *expr, struct acl_test *test) |
| 8243 | { |
| 8244 | struct buffer *req = s->req; |
| 8245 | struct http_txn *txn = &s->txn; |
| 8246 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8247 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8248 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 8249 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 8250 | */ |
| 8251 | |
| 8252 | if (!s || !req) |
| 8253 | return 0; |
| 8254 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8255 | if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8256 | /* Already decoded as OK */ |
| 8257 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 8258 | return 1; |
| 8259 | } |
| 8260 | |
| 8261 | /* Try to decode HTTP request */ |
| 8262 | if (likely(req->lr < req->r)) |
| 8263 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
| 8264 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 8265 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8266 | if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) { |
| 8267 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 8268 | return 1; |
| 8269 | } |
| 8270 | /* wait for final state */ |
| 8271 | test->flags |= ACL_TEST_F_MAY_CHANGE; |
| 8272 | return 0; |
| 8273 | } |
| 8274 | |
| 8275 | /* OK we got a valid HTTP request. We have some minor preparation to |
| 8276 | * perform so that further checks can rely on HTTP tests. |
| 8277 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 8278 | txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8279 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 8280 | s->flags |= SN_REDIRECTABLE; |
| 8281 | |
| 8282 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) { |
| 8283 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 8284 | return 1; |
| 8285 | } |
| 8286 | |
| 8287 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 8288 | return 1; |
| 8289 | } |
| 8290 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 8291 | /* return a valid test if the current request is the first one on the connection */ |
| 8292 | static int |
| 8293 | acl_fetch_http_first_req(struct proxy *px, struct session *s, void *l7, int dir, |
| 8294 | struct acl_expr *expr, struct acl_test *test) |
| 8295 | { |
| 8296 | if (!s) |
| 8297 | return 0; |
| 8298 | |
| 8299 | if (s->txn.flags & TX_NOT_FIRST) |
| 8300 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 8301 | else |
| 8302 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 8303 | |
| 8304 | return 1; |
| 8305 | } |
| 8306 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8307 | static int |
| 8308 | acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir, |
| 8309 | struct acl_expr *expr, struct acl_test *test) |
| 8310 | { |
| 8311 | |
| 8312 | if (!s) |
| 8313 | return 0; |
| 8314 | |
| 8315 | if (!get_http_auth(s)) |
| 8316 | return 0; |
| 8317 | |
| 8318 | test->ctx.a[0] = expr->arg.ul; |
| 8319 | test->ctx.a[1] = s->txn.auth.user; |
| 8320 | test->ctx.a[2] = s->txn.auth.pass; |
| 8321 | |
| 8322 | test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH; |
| 8323 | |
| 8324 | return 1; |
| 8325 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8326 | |
| 8327 | /************************************************************************/ |
| 8328 | /* All supported keywords must be declared here. */ |
| 8329 | /************************************************************************/ |
| 8330 | |
| 8331 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 8332 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 8333 | { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT }, |
| 8334 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8335 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT }, |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 8336 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP }, |
| 8337 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP }, |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8338 | { "status", acl_parse_int, acl_fetch_stcode, acl_match_int, ACL_USE_L7RTR_PERMANENT }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8339 | |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 8340 | { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP }, |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8341 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 8342 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 8343 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 8344 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 8345 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 8346 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | b337b53 | 2010-05-13 20:03:41 +0200 | [diff] [blame] | 8347 | { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP }, |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8348 | { "url_port", acl_parse_int, acl_fetch_url_port, acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8349 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8350 | /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */ |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 8351 | { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP }, |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8352 | { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 8353 | { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 8354 | { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 8355 | { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 8356 | { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 8357 | { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 8358 | { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
| 8359 | { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | b337b53 | 2010-05-13 20:03:41 +0200 | [diff] [blame] | 8360 | { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP }, |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 8361 | |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 8362 | { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP }, |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8363 | { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE }, |
| 8364 | { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE }, |
| 8365 | { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE }, |
| 8366 | { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE }, |
| 8367 | { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE }, |
| 8368 | { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE }, |
| 8369 | { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
| 8370 | { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
Willy Tarreau | b337b53 | 2010-05-13 20:03:41 +0200 | [diff] [blame] | 8371 | { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE|ACL_MAY_LOOKUP }, |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8372 | |
Willy Tarreau | c426296 | 2010-05-10 23:42:40 +0200 | [diff] [blame] | 8373 | { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE|ACL_MAY_LOOKUP }, |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 8374 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 8375 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 8376 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 8377 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 8378 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 8379 | { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 8380 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 8381 | #if 0 |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8382 | { "line", acl_parse_str, acl_fetch_line, acl_match_str }, |
| 8383 | { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg }, |
| 8384 | { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg }, |
| 8385 | { "line_end", acl_parse_str, acl_fetch_line, acl_match_end }, |
| 8386 | { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub }, |
| 8387 | { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir }, |
| 8388 | { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom }, |
| 8389 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8390 | { "cook", acl_parse_str, acl_fetch_cook, acl_match_str }, |
| 8391 | { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg }, |
| 8392 | { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg }, |
| 8393 | { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end }, |
| 8394 | { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub }, |
| 8395 | { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir }, |
| 8396 | { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom }, |
| 8397 | { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst }, |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 8398 | #endif |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8399 | |
Willy Tarreau | 7f18e52 | 2010-10-22 20:04:13 +0200 | [diff] [blame] | 8400 | { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT }, |
| 8401 | { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth, ACL_USE_L7REQ_PERMANENT }, |
| 8402 | { "http_first_req", acl_parse_nothing, acl_fetch_http_first_req, acl_match_nothing, ACL_USE_L7REQ_PERMANENT }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8403 | { NULL, NULL, NULL, NULL }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8404 | }}; |
| 8405 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8406 | /************************************************************************/ |
| 8407 | /* The code below is dedicated to pattern fetching and matching */ |
| 8408 | /************************************************************************/ |
| 8409 | |
| 8410 | /* extract the IP address from the last occurrence of specified header. Note |
| 8411 | * that we should normally first extract the string then convert it to IP, |
| 8412 | * but right now we have all the functions to do this seemlessly, and we will |
| 8413 | * be able to change that later without touching the configuration. |
| 8414 | */ |
| 8415 | static int |
| 8416 | pattern_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 8417 | const struct pattern_arg *arg_p, int arg_i, union pattern_data *data) |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8418 | { |
| 8419 | struct http_txn *txn = l7; |
| 8420 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 8421 | data->ip.s_addr = htonl(get_ip_from_hdr2(&txn->req, arg_p->data.str.str, arg_p->data.str.len, &txn->hdr_idx, -1)); |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8422 | return data->ip.s_addr != 0; |
| 8423 | } |
| 8424 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8425 | /* |
| 8426 | * Given a path string and its length, find the position of beginning of the |
| 8427 | * query string. Returns NULL if no query string is found in the path. |
| 8428 | * |
| 8429 | * Example: if path = "/foo/bar/fubar?yo=mama;ye=daddy", and n = 22: |
| 8430 | * |
| 8431 | * find_query_string(path, n) points to "yo=mama;ye=daddy" string. |
| 8432 | */ |
| 8433 | static inline char *find_query_string(char *path, size_t path_l) |
| 8434 | { |
| 8435 | char *p; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 8436 | |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8437 | p = memchr(path, '?', path_l); |
| 8438 | return p ? p + 1 : NULL; |
| 8439 | } |
| 8440 | |
| 8441 | static inline int is_param_delimiter(char c) |
| 8442 | { |
| 8443 | return c == '&' || c == ';'; |
| 8444 | } |
| 8445 | |
| 8446 | /* |
| 8447 | * Given a url parameter, find the starting position of the first occurence, |
| 8448 | * or NULL if the parameter is not found. |
| 8449 | * |
| 8450 | * Example: if query_string is "yo=mama;ye=daddy" and url_param_name is "ye", |
| 8451 | * the function will return query_string+8. |
| 8452 | */ |
| 8453 | static char* |
| 8454 | find_url_param_pos(char* query_string, size_t query_string_l, |
| 8455 | char* url_param_name, size_t url_param_name_l) |
| 8456 | { |
| 8457 | char *pos, *last; |
| 8458 | |
| 8459 | pos = query_string; |
| 8460 | last = query_string + query_string_l - url_param_name_l - 1; |
| 8461 | |
| 8462 | while (pos <= last) { |
| 8463 | if (pos[url_param_name_l] == '=') { |
| 8464 | if (memcmp(pos, url_param_name, url_param_name_l) == 0) |
| 8465 | return pos; |
| 8466 | pos += url_param_name_l + 1; |
| 8467 | } |
| 8468 | while (pos <= last && !is_param_delimiter(*pos)) |
| 8469 | pos++; |
| 8470 | pos++; |
| 8471 | } |
| 8472 | return NULL; |
| 8473 | } |
| 8474 | |
| 8475 | /* |
| 8476 | * Given a url parameter name, returns its value and size into *value and |
| 8477 | * *value_l respectively, and returns non-zero. If the parameter is not found, |
| 8478 | * zero is returned and value/value_l are not touched. |
| 8479 | */ |
| 8480 | static int |
| 8481 | find_url_param_value(char* path, size_t path_l, |
| 8482 | char* url_param_name, size_t url_param_name_l, |
| 8483 | char** value, size_t* value_l) |
| 8484 | { |
| 8485 | char *query_string, *qs_end; |
| 8486 | char *arg_start; |
| 8487 | char *value_start, *value_end; |
| 8488 | |
| 8489 | query_string = find_query_string(path, path_l); |
| 8490 | if (!query_string) |
| 8491 | return 0; |
| 8492 | |
| 8493 | qs_end = path + path_l; |
| 8494 | arg_start = find_url_param_pos(query_string, qs_end - query_string, |
| 8495 | url_param_name, url_param_name_l); |
| 8496 | if (!arg_start) |
| 8497 | return 0; |
| 8498 | |
| 8499 | value_start = arg_start + url_param_name_l + 1; |
| 8500 | value_end = value_start; |
| 8501 | |
| 8502 | while ((value_end < qs_end) && !is_param_delimiter(*value_end)) |
| 8503 | value_end++; |
| 8504 | |
| 8505 | *value = value_start; |
| 8506 | *value_l = value_end - value_start; |
Willy Tarreau | 0013433 | 2011-01-04 14:57:34 +0100 | [diff] [blame] | 8507 | return value_end != value_start; |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8508 | } |
| 8509 | |
| 8510 | static int |
| 8511 | pattern_fetch_url_param(struct proxy *px, struct session *l4, void *l7, int dir, |
| 8512 | const struct pattern_arg *arg_p, int arg_i, union pattern_data *data) |
| 8513 | { |
| 8514 | struct http_txn *txn = l7; |
| 8515 | struct http_msg *msg = &txn->req; |
| 8516 | char *url_param_value; |
| 8517 | size_t url_param_value_l; |
| 8518 | |
| 8519 | if (!find_url_param_value(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, |
| 8520 | arg_p->data.str.str, arg_p->data.str.len, |
| 8521 | &url_param_value, &url_param_value_l)) |
| 8522 | return 0; |
| 8523 | |
| 8524 | data->str.str = url_param_value; |
| 8525 | data->str.len = url_param_value_l; |
| 8526 | return 1; |
| 8527 | } |
| 8528 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 8529 | |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8530 | /************************************************************************/ |
| 8531 | /* All supported keywords must be declared here. */ |
| 8532 | /************************************************************************/ |
| 8533 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 8534 | static struct pattern_fetch_kw_list pattern_fetch_keywords = {{ },{ |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 8535 | { "hdr", pattern_fetch_hdr_ip, pattern_arg_str, PATTERN_TYPE_IP, PATTERN_FETCH_REQ }, |
David Cournapeau | 16023ee | 2010-12-23 20:55:41 +0900 | [diff] [blame] | 8536 | { "url_param", pattern_fetch_url_param, pattern_arg_str, PATTERN_TYPE_STRING, PATTERN_FETCH_REQ }, |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 8537 | { NULL, NULL, NULL, 0, 0 }, |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8538 | }}; |
| 8539 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8540 | |
| 8541 | __attribute__((constructor)) |
| 8542 | static void __http_protocol_init(void) |
| 8543 | { |
| 8544 | acl_register_keywords(&acl_kws); |
Willy Tarreau | 4a56897 | 2010-05-12 08:08:50 +0200 | [diff] [blame] | 8545 | pattern_register_fetches(&pattern_fetch_keywords); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 8546 | } |
| 8547 | |
| 8548 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 8549 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 8550 | * Local variables: |
| 8551 | * c-indent-level: 8 |
| 8552 | * c-basic-offset: 8 |
| 8553 | * End: |
| 8554 | */ |