Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP protocol analyzer |
| 3 | * |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 4 | * Copyright 2000-2008 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> |
| 27 | #include <common/compat.h> |
| 28 | #include <common/config.h> |
Willy Tarreau | a4cd1f5 | 2006-12-16 19:57:26 +0100 | [diff] [blame] | 29 | #include <common/debug.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 30 | #include <common/memory.h> |
| 31 | #include <common/mini-clist.h> |
| 32 | #include <common/standard.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 33 | #include <common/ticks.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 34 | #include <common/time.h> |
| 35 | #include <common/uri_auth.h> |
| 36 | #include <common/version.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 37 | |
| 38 | #include <types/capture.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 39 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 40 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 41 | #include <proto/acl.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 42 | #include <proto/backend.h> |
| 43 | #include <proto/buffers.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 44 | #include <proto/dumpstats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 45 | #include <proto/fd.h> |
| 46 | #include <proto/log.h> |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 47 | #include <proto/hdr_idx.h> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 48 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 49 | #include <proto/proto_http.h> |
| 50 | #include <proto/queue.h> |
| 51 | #include <proto/session.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 52 | #include <proto/stream_interface.h> |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 53 | #include <proto/stream_sock.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 54 | #include <proto/task.h> |
| 55 | |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 56 | #ifdef CONFIG_HAP_TCPSPLICE |
| 57 | #include <libtcpsplice.h> |
| 58 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 59 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 60 | #define DEBUG_PARSE_NO_SPEEDUP |
| 61 | #undef DEBUG_PARSE_NO_SPEEDUP |
| 62 | |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 63 | /* This is used to perform a quick jump as an alternative to a break/continue |
| 64 | * instruction. The first argument is the label for normal operation, and the |
| 65 | * second one is the break/continue instruction in the no_speedup mode. |
| 66 | */ |
| 67 | |
| 68 | #ifdef DEBUG_PARSE_NO_SPEEDUP |
| 69 | #define QUICK_JUMP(x,y) y |
| 70 | #else |
| 71 | #define QUICK_JUMP(x,y) goto x |
| 72 | #endif |
| 73 | |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 74 | /* This is used by remote monitoring */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 75 | const char HTTP_200[] = |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 76 | "HTTP/1.0 200 OK\r\n" |
| 77 | "Cache-Control: no-cache\r\n" |
| 78 | "Connection: close\r\n" |
| 79 | "Content-Type: text/html\r\n" |
| 80 | "\r\n" |
| 81 | "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n"; |
| 82 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 83 | const struct chunk http_200_chunk = { |
| 84 | .str = (char *)&HTTP_200, |
| 85 | .len = sizeof(HTTP_200)-1 |
| 86 | }; |
| 87 | |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 88 | const char *HTTP_301 = |
| 89 | "HTTP/1.0 301 Moved Permantenly\r\n" |
| 90 | "Cache-Control: no-cache\r\n" |
| 91 | "Connection: close\r\n" |
| 92 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 93 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 94 | const char *HTTP_302 = |
| 95 | "HTTP/1.0 302 Found\r\n" |
| 96 | "Cache-Control: no-cache\r\n" |
| 97 | "Connection: close\r\n" |
| 98 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 99 | |
| 100 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 101 | const char *HTTP_303 = |
| 102 | "HTTP/1.0 303 See Other\r\n" |
| 103 | "Cache-Control: no-cache\r\n" |
| 104 | "Connection: close\r\n" |
| 105 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 106 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 107 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 108 | const char *HTTP_401_fmt = |
| 109 | "HTTP/1.0 401 Unauthorized\r\n" |
| 110 | "Cache-Control: no-cache\r\n" |
| 111 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 112 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 113 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 114 | "\r\n" |
| 115 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 116 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 117 | |
| 118 | const int http_err_codes[HTTP_ERR_SIZE] = { |
| 119 | [HTTP_ERR_400] = 400, |
| 120 | [HTTP_ERR_403] = 403, |
| 121 | [HTTP_ERR_408] = 408, |
| 122 | [HTTP_ERR_500] = 500, |
| 123 | [HTTP_ERR_502] = 502, |
| 124 | [HTTP_ERR_503] = 503, |
| 125 | [HTTP_ERR_504] = 504, |
| 126 | }; |
| 127 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 128 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 129 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 130 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 131 | "Cache-Control: no-cache\r\n" |
| 132 | "Connection: close\r\n" |
| 133 | "Content-Type: text/html\r\n" |
| 134 | "\r\n" |
| 135 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 136 | |
| 137 | [HTTP_ERR_403] = |
| 138 | "HTTP/1.0 403 Forbidden\r\n" |
| 139 | "Cache-Control: no-cache\r\n" |
| 140 | "Connection: close\r\n" |
| 141 | "Content-Type: text/html\r\n" |
| 142 | "\r\n" |
| 143 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 144 | |
| 145 | [HTTP_ERR_408] = |
| 146 | "HTTP/1.0 408 Request Time-out\r\n" |
| 147 | "Cache-Control: no-cache\r\n" |
| 148 | "Connection: close\r\n" |
| 149 | "Content-Type: text/html\r\n" |
| 150 | "\r\n" |
| 151 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 152 | |
| 153 | [HTTP_ERR_500] = |
| 154 | "HTTP/1.0 500 Server Error\r\n" |
| 155 | "Cache-Control: no-cache\r\n" |
| 156 | "Connection: close\r\n" |
| 157 | "Content-Type: text/html\r\n" |
| 158 | "\r\n" |
| 159 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 160 | |
| 161 | [HTTP_ERR_502] = |
| 162 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 163 | "Cache-Control: no-cache\r\n" |
| 164 | "Connection: close\r\n" |
| 165 | "Content-Type: text/html\r\n" |
| 166 | "\r\n" |
| 167 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 168 | |
| 169 | [HTTP_ERR_503] = |
| 170 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 171 | "Cache-Control: no-cache\r\n" |
| 172 | "Connection: close\r\n" |
| 173 | "Content-Type: text/html\r\n" |
| 174 | "\r\n" |
| 175 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 176 | |
| 177 | [HTTP_ERR_504] = |
| 178 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 179 | "Cache-Control: no-cache\r\n" |
| 180 | "Connection: close\r\n" |
| 181 | "Content-Type: text/html\r\n" |
| 182 | "\r\n" |
| 183 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 184 | |
| 185 | }; |
| 186 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 187 | /* We must put the messages here since GCC cannot initialize consts depending |
| 188 | * on strlen(). |
| 189 | */ |
| 190 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 191 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 192 | #define FD_SETS_ARE_BITFIELDS |
| 193 | #ifdef FD_SETS_ARE_BITFIELDS |
| 194 | /* |
| 195 | * This map is used with all the FD_* macros to check whether a particular bit |
| 196 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 197 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 198 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 199 | * exclusively to the macros. |
| 200 | */ |
| 201 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 202 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 203 | |
| 204 | #else |
| 205 | #error "Check if your OS uses bitfields for fd_sets" |
| 206 | #endif |
| 207 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 208 | void init_proto_http() |
| 209 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 210 | int i; |
| 211 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 212 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 213 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 214 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 215 | if (!http_err_msgs[msg]) { |
| 216 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 217 | abort(); |
| 218 | } |
| 219 | |
| 220 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 221 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 222 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 223 | |
| 224 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 225 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 226 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 227 | * printable characters above. |
| 228 | */ |
| 229 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 230 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 231 | for (i = 0; i < 32; i++) { |
| 232 | FD_SET(i, hdr_encode_map); |
| 233 | FD_SET(i, url_encode_map); |
| 234 | } |
| 235 | for (i = 127; i < 256; i++) { |
| 236 | FD_SET(i, hdr_encode_map); |
| 237 | FD_SET(i, url_encode_map); |
| 238 | } |
| 239 | |
| 240 | tmp = "\"#{|}"; |
| 241 | while (*tmp) { |
| 242 | FD_SET(*tmp, hdr_encode_map); |
| 243 | tmp++; |
| 244 | } |
| 245 | |
| 246 | tmp = "\"#"; |
| 247 | while (*tmp) { |
| 248 | FD_SET(*tmp, url_encode_map); |
| 249 | tmp++; |
| 250 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 251 | |
| 252 | /* memory allocations */ |
| 253 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 254 | pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 255 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 256 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 257 | /* |
| 258 | * We have 26 list of methods (1 per first letter), each of which can have |
| 259 | * up to 3 entries (2 valid, 1 null). |
| 260 | */ |
| 261 | struct http_method_desc { |
| 262 | http_meth_t meth; |
| 263 | int len; |
| 264 | const char text[8]; |
| 265 | }; |
| 266 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 267 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 268 | ['C' - 'A'] = { |
| 269 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 270 | }, |
| 271 | ['D' - 'A'] = { |
| 272 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 273 | }, |
| 274 | ['G' - 'A'] = { |
| 275 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 276 | }, |
| 277 | ['H' - 'A'] = { |
| 278 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 279 | }, |
| 280 | ['P' - 'A'] = { |
| 281 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 282 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 283 | }, |
| 284 | ['T' - 'A'] = { |
| 285 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 286 | }, |
| 287 | /* rest is empty like this : |
| 288 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 289 | */ |
| 290 | }; |
| 291 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 292 | /* 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] | 293 | * 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] | 294 | * RFC2616 for those chars. |
| 295 | */ |
| 296 | |
| 297 | const char http_is_spht[256] = { |
| 298 | [' '] = 1, ['\t'] = 1, |
| 299 | }; |
| 300 | |
| 301 | const char http_is_crlf[256] = { |
| 302 | ['\r'] = 1, ['\n'] = 1, |
| 303 | }; |
| 304 | |
| 305 | const char http_is_lws[256] = { |
| 306 | [' '] = 1, ['\t'] = 1, |
| 307 | ['\r'] = 1, ['\n'] = 1, |
| 308 | }; |
| 309 | |
| 310 | const char http_is_sep[256] = { |
| 311 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 312 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 313 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 314 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 315 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 316 | }; |
| 317 | |
| 318 | const char http_is_ctl[256] = { |
| 319 | [0 ... 31] = 1, |
| 320 | [127] = 1, |
| 321 | }; |
| 322 | |
| 323 | /* |
| 324 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 325 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 326 | * them correctly. Instead, define every non-CTL char's status. |
| 327 | */ |
| 328 | const char http_is_token[256] = { |
| 329 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 330 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 331 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 332 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 333 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 334 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 335 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 336 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 337 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 338 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 339 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 340 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 341 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 342 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 343 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 344 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 345 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 346 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 347 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 348 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 349 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 350 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 351 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 352 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 353 | }; |
| 354 | |
| 355 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 356 | /* |
| 357 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 358 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 359 | */ |
| 360 | const char http_is_ver_token[256] = { |
| 361 | ['.'] = 1, ['/'] = 1, |
| 362 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 363 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| 364 | ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| 365 | }; |
| 366 | |
| 367 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 368 | #ifdef DEBUG_FULL |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 369 | static char *cli_stnames[4] = { "DAT", "SHR", "SHW", "CLS" }; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 370 | #endif |
| 371 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 372 | /* |
| 373 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 374 | * CRLF. Text length is measured first, so it cannot be NULL. |
| 375 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 376 | * of headers is automatically adjusted. The number of bytes added is returned |
| 377 | * on success, otherwise <0 is returned indicating an error. |
| 378 | */ |
| 379 | int http_header_add_tail(struct buffer *b, struct http_msg *msg, |
| 380 | struct hdr_idx *hdr_idx, const char *text) |
| 381 | { |
| 382 | int bytes, len; |
| 383 | |
| 384 | len = strlen(text); |
| 385 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 386 | if (!bytes) |
| 387 | return -1; |
| 388 | msg->eoh += bytes; |
| 389 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 390 | } |
| 391 | |
| 392 | /* |
| 393 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 394 | * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then |
| 395 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 396 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 397 | * of headers is automatically adjusted. The number of bytes added is returned |
| 398 | * on success, otherwise <0 is returned indicating an error. |
| 399 | */ |
| 400 | int http_header_add_tail2(struct buffer *b, struct http_msg *msg, |
| 401 | struct hdr_idx *hdr_idx, const char *text, int len) |
| 402 | { |
| 403 | int bytes; |
| 404 | |
| 405 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 406 | if (!bytes) |
| 407 | return -1; |
| 408 | msg->eoh += bytes; |
| 409 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 410 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 411 | |
| 412 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 413 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 414 | * If so, returns the position of the first non-space character relative to |
| 415 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 416 | * to return a pointer to the place after the first space. Returns 0 if the |
| 417 | * header name does not match. Checks are case-insensitive. |
| 418 | */ |
| 419 | int http_header_match2(const char *hdr, const char *end, |
| 420 | const char *name, int len) |
| 421 | { |
| 422 | const char *val; |
| 423 | |
| 424 | if (hdr + len >= end) |
| 425 | return 0; |
| 426 | if (hdr[len] != ':') |
| 427 | return 0; |
| 428 | if (strncasecmp(hdr, name, len) != 0) |
| 429 | return 0; |
| 430 | val = hdr + len + 1; |
| 431 | while (val < end && HTTP_IS_SPHT(*val)) |
| 432 | val++; |
| 433 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 434 | return len + 2; /* we may replace starting from second space */ |
| 435 | return val - hdr; |
| 436 | } |
| 437 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 438 | /* Find the end of the header value contained between <s> and <e>. |
| 439 | * See RFC2616, par 2.2 for more information. Note that it requires |
| 440 | * a valid header to return a valid result. |
| 441 | */ |
| 442 | const char *find_hdr_value_end(const char *s, const char *e) |
| 443 | { |
| 444 | int quoted, qdpair; |
| 445 | |
| 446 | quoted = qdpair = 0; |
| 447 | for (; s < e; s++) { |
| 448 | if (qdpair) qdpair = 0; |
| 449 | else if (quoted && *s == '\\') qdpair = 1; |
| 450 | else if (quoted && *s == '"') quoted = 0; |
| 451 | else if (*s == '"') quoted = 1; |
| 452 | else if (*s == ',') return s; |
| 453 | } |
| 454 | return s; |
| 455 | } |
| 456 | |
| 457 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 458 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 459 | * structure holds everything necessary to use the header and find next |
| 460 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 461 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 462 | * 1 when it finds a value, and 0 when there is no more. |
| 463 | */ |
| 464 | int http_find_header2(const char *name, int len, |
| 465 | const char *sol, struct hdr_idx *idx, |
| 466 | struct hdr_ctx *ctx) |
| 467 | { |
| 468 | __label__ return_hdr, next_hdr; |
| 469 | const char *eol, *sov; |
| 470 | int cur_idx; |
| 471 | |
| 472 | if (ctx->idx) { |
| 473 | /* We have previously returned a value, let's search |
| 474 | * another one on the same line. |
| 475 | */ |
| 476 | cur_idx = ctx->idx; |
| 477 | sol = ctx->line; |
| 478 | sov = sol + ctx->val + ctx->vlen; |
| 479 | eol = sol + idx->v[cur_idx].len; |
| 480 | |
| 481 | if (sov >= eol) |
| 482 | /* no more values in this header */ |
| 483 | goto next_hdr; |
| 484 | |
| 485 | /* values remaining for this header, skip the comma */ |
| 486 | sov++; |
| 487 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 488 | sov++; |
| 489 | |
| 490 | goto return_hdr; |
| 491 | } |
| 492 | |
| 493 | /* first request for this header */ |
| 494 | sol += hdr_idx_first_pos(idx); |
| 495 | cur_idx = hdr_idx_first_idx(idx); |
| 496 | |
| 497 | while (cur_idx) { |
| 498 | eol = sol + idx->v[cur_idx].len; |
| 499 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 500 | if (len == 0) { |
| 501 | /* No argument was passed, we want any header. |
| 502 | * To achieve this, we simply build a fake request. */ |
| 503 | while (sol + len < eol && sol[len] != ':') |
| 504 | len++; |
| 505 | name = sol; |
| 506 | } |
| 507 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 508 | if ((len < eol - sol) && |
| 509 | (sol[len] == ':') && |
| 510 | (strncasecmp(sol, name, len) == 0)) { |
| 511 | |
| 512 | sov = sol + len + 1; |
| 513 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 514 | sov++; |
| 515 | return_hdr: |
| 516 | ctx->line = sol; |
| 517 | ctx->idx = cur_idx; |
| 518 | ctx->val = sov - sol; |
| 519 | |
| 520 | eol = find_hdr_value_end(sov, eol); |
| 521 | ctx->vlen = eol - sov; |
| 522 | return 1; |
| 523 | } |
| 524 | next_hdr: |
| 525 | sol = eol + idx->v[cur_idx].cr + 1; |
| 526 | cur_idx = idx->v[cur_idx].next; |
| 527 | } |
| 528 | return 0; |
| 529 | } |
| 530 | |
| 531 | int http_find_header(const char *name, |
| 532 | const char *sol, struct hdr_idx *idx, |
| 533 | struct hdr_ctx *ctx) |
| 534 | { |
| 535 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 536 | } |
| 537 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 538 | /* This function handles a server error at the stream interface level. The |
| 539 | * stream interface is assumed to be already in a closed state. An optional |
| 540 | * message is copied into the input buffer, and an HTTP status code stored. |
| 541 | * The error flags are set to the values in arguments. Any pending request |
| 542 | * is flushed. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 543 | */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 544 | static void http_server_error(struct session *t, struct stream_interface *si, |
| 545 | int err, int finst, int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 546 | { |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 547 | buffer_flush(si->ob); |
| 548 | buffer_flush(si->ib); |
| 549 | buffer_write_ena(si->ib); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 550 | if (status > 0 && msg) { |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 551 | t->txn.status = status; |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 552 | buffer_write(si->ib, msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 553 | } |
| 554 | if (!(t->flags & SN_ERR_MASK)) |
| 555 | t->flags |= err; |
| 556 | if (!(t->flags & SN_FINST_MASK)) |
| 557 | t->flags |= finst; |
| 558 | } |
| 559 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 560 | /* This function returns the appropriate error location for the given session |
| 561 | * and message. |
| 562 | */ |
| 563 | |
| 564 | struct chunk *error_message(struct session *s, int msgnum) |
| 565 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 566 | if (s->be->errmsg[msgnum].str) |
| 567 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 568 | else if (s->fe->errmsg[msgnum].str) |
| 569 | return &s->fe->errmsg[msgnum]; |
| 570 | else |
| 571 | return &http_err_chunks[msgnum]; |
| 572 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 573 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 574 | /* |
| 575 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 576 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 577 | */ |
| 578 | static http_meth_t find_http_meth(const char *str, const int len) |
| 579 | { |
| 580 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 581 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 582 | |
| 583 | m = ((unsigned)*str - 'A'); |
| 584 | |
| 585 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 586 | for (h = http_methods[m]; h->len > 0; h++) { |
| 587 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 588 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 589 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 590 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 591 | }; |
| 592 | return HTTP_METH_OTHER; |
| 593 | } |
| 594 | return HTTP_METH_NONE; |
| 595 | |
| 596 | } |
| 597 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 598 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 599 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 600 | * It is returned otherwise. |
| 601 | */ |
| 602 | static char * |
| 603 | http_get_path(struct http_txn *txn) |
| 604 | { |
| 605 | char *ptr, *end; |
| 606 | |
| 607 | ptr = txn->req.sol + txn->req.sl.rq.u; |
| 608 | end = ptr + txn->req.sl.rq.u_l; |
| 609 | |
| 610 | if (ptr >= end) |
| 611 | return NULL; |
| 612 | |
| 613 | /* RFC2616, par. 5.1.2 : |
| 614 | * Request-URI = "*" | absuri | abspath | authority |
| 615 | */ |
| 616 | |
| 617 | if (*ptr == '*') |
| 618 | return NULL; |
| 619 | |
| 620 | if (isalpha((unsigned char)*ptr)) { |
| 621 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 622 | ptr++; |
| 623 | while (ptr < end && |
| 624 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 625 | ptr++; |
| 626 | /* skip '://' */ |
| 627 | if (ptr == end || *ptr++ != ':') |
| 628 | return NULL; |
| 629 | if (ptr == end || *ptr++ != '/') |
| 630 | return NULL; |
| 631 | if (ptr == end || *ptr++ != '/') |
| 632 | return NULL; |
| 633 | } |
| 634 | /* skip [user[:passwd]@]host[:[port]] */ |
| 635 | |
| 636 | while (ptr < end && *ptr != '/') |
| 637 | ptr++; |
| 638 | |
| 639 | if (ptr == end) |
| 640 | return NULL; |
| 641 | |
| 642 | /* OK, we got the '/' ! */ |
| 643 | return ptr; |
| 644 | } |
| 645 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 646 | /* Returns a 302 for a redirectable request. This may only be called just after |
| 647 | * the stream interface has moved to SI_ST_ASS. Unprocessable requests are |
| 648 | * left unchanged and will follow normal proxy processing. |
| 649 | */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 650 | void perform_http_redirect(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 651 | { |
| 652 | struct http_txn *txn; |
| 653 | struct chunk rdr; |
| 654 | char *path; |
| 655 | int len; |
| 656 | |
| 657 | /* 1: create the response header */ |
| 658 | rdr.len = strlen(HTTP_302); |
| 659 | rdr.str = trash; |
| 660 | memcpy(rdr.str, HTTP_302, rdr.len); |
| 661 | |
| 662 | /* 2: add the server's prefix */ |
| 663 | if (rdr.len + s->srv->rdr_len > sizeof(trash)) |
| 664 | return; |
| 665 | |
| 666 | memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len); |
| 667 | rdr.len += s->srv->rdr_len; |
| 668 | |
| 669 | /* 3: add the request URI */ |
| 670 | txn = &s->txn; |
| 671 | path = http_get_path(txn); |
| 672 | if (!path) |
| 673 | return; |
| 674 | |
| 675 | len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path; |
| 676 | if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */ |
| 677 | return; |
| 678 | |
| 679 | memcpy(rdr.str + rdr.len, path, len); |
| 680 | rdr.len += len; |
| 681 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 682 | rdr.len += 4; |
| 683 | |
| 684 | /* prepare to return without error. */ |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame] | 685 | si->shutr(si); |
| 686 | si->shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 687 | si->err_type = SI_ET_NONE; |
| 688 | si->err_loc = NULL; |
| 689 | si->state = SI_ST_CLO; |
| 690 | |
| 691 | /* send the message */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 692 | 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] | 693 | |
| 694 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
| 695 | if (s->srv) |
| 696 | s->srv->cum_sess++; |
| 697 | } |
| 698 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 699 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 700 | * that the server side is closed. Note that err_type is actually a |
| 701 | * bitmask, where almost only aborts may be cumulated with other |
| 702 | * values. We consider that aborted operations are more important |
| 703 | * than timeouts or errors due to the fact that nobody else in the |
| 704 | * logs might explain incomplete retries. All others should avoid |
| 705 | * being cumulated. It should normally not be possible to have multiple |
| 706 | * aborts at once, but just in case, the first one in sequence is reported. |
| 707 | */ |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 708 | void http_return_srv_error(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 709 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 710 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 711 | |
| 712 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 713 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q, |
| 714 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 715 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 716 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C, |
| 717 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 718 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 719 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q, |
| 720 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 721 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 722 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q, |
| 723 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 724 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 725 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C, |
| 726 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 727 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 728 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C, |
| 729 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 730 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 731 | http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C, |
| 732 | 500, error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 733 | } |
| 734 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 735 | extern const char sess_term_cond[8]; |
| 736 | extern const char sess_fin_state[8]; |
| 737 | extern const char *monthname[12]; |
| 738 | const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */ |
| 739 | const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown, |
| 740 | Set-cookie seen and left unchanged (passive), Set-cookie Deleted, |
| 741 | unknown, Set-cookie Rewritten */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 742 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 743 | struct pool_head *pool2_capture; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 744 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 745 | /* |
| 746 | * send a log for the session when we have enough info about it. |
| 747 | * Will not log if the frontend has no log defined. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 748 | */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 749 | void http_sess_log(struct session *s) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 750 | { |
| 751 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 752 | struct proxy *fe = s->fe; |
| 753 | struct proxy *be = s->be; |
| 754 | struct proxy *prx_log; |
| 755 | struct http_txn *txn = &s->txn; |
| 756 | int tolog; |
| 757 | char *uri, *h; |
| 758 | char *svid; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 759 | struct tm tm; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 760 | static char tmpline[MAX_SYSLOG_LEN]; |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 761 | int t_request; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 762 | int hdr; |
| 763 | |
| 764 | if (fe->logfac1 < 0 && fe->logfac2 < 0) |
| 765 | return; |
| 766 | prx_log = fe; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 767 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 768 | if (s->cli_addr.ss_family == AF_INET) |
| 769 | inet_ntop(AF_INET, |
| 770 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 771 | pn, sizeof(pn)); |
| 772 | else |
| 773 | inet_ntop(AF_INET6, |
| 774 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 775 | pn, sizeof(pn)); |
| 776 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 777 | get_localtime(s->logs.accept_date.tv_sec, &tm); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 778 | |
| 779 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 780 | tolog = fe->to_log; |
| 781 | |
| 782 | h = tmpline; |
| 783 | if (fe->to_log & LW_REQHDR && |
| 784 | txn->req.cap && |
| 785 | (h < tmpline + sizeof(tmpline) - 10)) { |
| 786 | *(h++) = ' '; |
| 787 | *(h++) = '{'; |
| 788 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 789 | if (hdr) |
| 790 | *(h++) = '|'; |
| 791 | if (txn->req.cap[hdr] != NULL) |
| 792 | h = encode_string(h, tmpline + sizeof(tmpline) - 7, |
| 793 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 794 | } |
| 795 | *(h++) = '}'; |
| 796 | } |
| 797 | |
| 798 | if (fe->to_log & LW_RSPHDR && |
| 799 | txn->rsp.cap && |
| 800 | (h < tmpline + sizeof(tmpline) - 7)) { |
| 801 | *(h++) = ' '; |
| 802 | *(h++) = '{'; |
| 803 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 804 | if (hdr) |
| 805 | *(h++) = '|'; |
| 806 | if (txn->rsp.cap[hdr] != NULL) |
| 807 | h = encode_string(h, tmpline + sizeof(tmpline) - 4, |
| 808 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 809 | } |
| 810 | *(h++) = '}'; |
| 811 | } |
| 812 | |
| 813 | if (h < tmpline + sizeof(tmpline) - 4) { |
| 814 | *(h++) = ' '; |
| 815 | *(h++) = '"'; |
| 816 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 817 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 818 | '#', url_encode_map, uri); |
| 819 | *(h++) = '"'; |
| 820 | } |
| 821 | *h = '\0'; |
| 822 | |
| 823 | svid = (tolog & LW_SVID) ? |
| 824 | (s->data_source != DATA_SRC_STATS) ? |
| 825 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 826 | |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 827 | t_request = -1; |
| 828 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 829 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 830 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 831 | send_log(prx_log, LOG_INFO, |
| 832 | "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]" |
| 833 | " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld" |
Krzysztof Piotr Oledzki | 25b501a | 2008-01-06 16:36:16 +0100 | [diff] [blame] | 834 | " %s %s %c%c%c%c %d/%d/%d/%d/%s%u %d/%d%s\n", |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 835 | pn, |
| 836 | (s->cli_addr.ss_family == AF_INET) ? |
| 837 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 838 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 839 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 840 | tm.tm_hour, tm.tm_min, tm.tm_sec, s->logs.accept_date.tv_usec/1000, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 841 | fe->id, be->id, svid, |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 842 | t_request, |
| 843 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 844 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 845 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 846 | (tolog & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 847 | txn->status, |
Willy Tarreau | 8b3977f | 2008-01-18 11:16:32 +0100 | [diff] [blame] | 848 | (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 849 | txn->cli_cookie ? txn->cli_cookie : "-", |
| 850 | txn->srv_cookie ? txn->srv_cookie : "-", |
| 851 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 852 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 853 | (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 854 | (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-', |
| 855 | actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0, |
Krzysztof Piotr Oledzki | 25b501a | 2008-01-06 16:36:16 +0100 | [diff] [blame] | 856 | (s->flags & SN_REDISP)?"+":"", |
| 857 | (s->conn_retries>0)?(be->conn_retries - s->conn_retries):be->conn_retries, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 858 | s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline); |
| 859 | |
| 860 | s->logs.logwait = 0; |
| 861 | } |
| 862 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 863 | |
| 864 | /* |
| 865 | * Capture headers from message starting at <som> according to header list |
| 866 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 867 | */ |
| 868 | void capture_headers(char *som, struct hdr_idx *idx, |
| 869 | char **cap, struct cap_hdr *cap_hdr) |
| 870 | { |
| 871 | char *eol, *sol, *col, *sov; |
| 872 | int cur_idx; |
| 873 | struct cap_hdr *h; |
| 874 | int len; |
| 875 | |
| 876 | sol = som + hdr_idx_first_pos(idx); |
| 877 | cur_idx = hdr_idx_first_idx(idx); |
| 878 | |
| 879 | while (cur_idx) { |
| 880 | eol = sol + idx->v[cur_idx].len; |
| 881 | |
| 882 | col = sol; |
| 883 | while (col < eol && *col != ':') |
| 884 | col++; |
| 885 | |
| 886 | sov = col + 1; |
| 887 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 888 | sov++; |
| 889 | |
| 890 | for (h = cap_hdr; h; h = h->next) { |
| 891 | if ((h->namelen == col - sol) && |
| 892 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 893 | if (cap[h->index] == NULL) |
| 894 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 895 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 896 | |
| 897 | if (cap[h->index] == NULL) { |
| 898 | Alert("HTTP capture : out of memory.\n"); |
| 899 | continue; |
| 900 | } |
| 901 | |
| 902 | len = eol - sov; |
| 903 | if (len > h->len) |
| 904 | len = h->len; |
| 905 | |
| 906 | memcpy(cap[h->index], sov, len); |
| 907 | cap[h->index][len]=0; |
| 908 | } |
| 909 | } |
| 910 | sol = eol + idx->v[cur_idx].cr + 1; |
| 911 | cur_idx = idx->v[cur_idx].next; |
| 912 | } |
| 913 | } |
| 914 | |
| 915 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 916 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 917 | */ |
| 918 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 919 | |
| 920 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 921 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 922 | */ |
| 923 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 924 | ptr++; \ |
| 925 | if (likely(ptr < end)) \ |
| 926 | goto good; \ |
| 927 | else { \ |
| 928 | state = (st); \ |
| 929 | goto http_msg_ood; \ |
| 930 | } \ |
| 931 | } while (0) |
| 932 | |
| 933 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 934 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 935 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 936 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 937 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 938 | * will give undefined results. |
| 939 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 940 | * and that msg->sol points to the beginning of the response. |
| 941 | * If a complete line is found (which implies that at least one CR or LF is |
| 942 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 943 | * returned indicating an incomplete line (which does not mean that parts have |
| 944 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 945 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 946 | * upon next call. |
| 947 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 948 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 949 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 950 | * 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] | 951 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 952 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 953 | const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, |
| 954 | unsigned int state, const char *ptr, const char *end, |
| 955 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 956 | { |
| 957 | __label__ |
| 958 | http_msg_rpver, |
| 959 | http_msg_rpver_sp, |
| 960 | http_msg_rpcode, |
| 961 | http_msg_rpcode_sp, |
| 962 | http_msg_rpreason, |
| 963 | http_msg_rpline_eol, |
| 964 | http_msg_ood, /* out of data */ |
| 965 | http_msg_invalid; |
| 966 | |
| 967 | switch (state) { |
| 968 | http_msg_rpver: |
| 969 | case HTTP_MSG_RPVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 970 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 971 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 972 | |
| 973 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 974 | msg->sl.st.v_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 975 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 976 | } |
| 977 | goto http_msg_invalid; |
| 978 | |
| 979 | http_msg_rpver_sp: |
| 980 | case HTTP_MSG_RPVER_SP: |
| 981 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 982 | msg->sl.st.c = ptr - msg_buf; |
| 983 | goto http_msg_rpcode; |
| 984 | } |
| 985 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 986 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 987 | /* so it's a CR/LF, this is invalid */ |
| 988 | goto http_msg_invalid; |
| 989 | |
| 990 | http_msg_rpcode: |
| 991 | case HTTP_MSG_RPCODE: |
| 992 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 993 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 994 | |
| 995 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 996 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 997 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 998 | } |
| 999 | |
| 1000 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1001 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 1002 | http_msg_rsp_reason: |
| 1003 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
| 1004 | msg->sl.st.r = ptr - msg_buf; |
| 1005 | msg->sl.st.r_l = 0; |
| 1006 | goto http_msg_rpline_eol; |
| 1007 | |
| 1008 | http_msg_rpcode_sp: |
| 1009 | case HTTP_MSG_RPCODE_SP: |
| 1010 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1011 | msg->sl.st.r = ptr - msg_buf; |
| 1012 | goto http_msg_rpreason; |
| 1013 | } |
| 1014 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1015 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1016 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1017 | goto http_msg_rsp_reason; |
| 1018 | |
| 1019 | http_msg_rpreason: |
| 1020 | case HTTP_MSG_RPREASON: |
| 1021 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1022 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
| 1023 | msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r; |
| 1024 | http_msg_rpline_eol: |
| 1025 | /* We have seen the end of line. Note that we do not |
| 1026 | * necessarily have the \n yet, but at least we know that we |
| 1027 | * have EITHER \r OR \n, otherwise the response would not be |
| 1028 | * complete. We can then record the response length and return |
| 1029 | * to the caller which will be able to register it. |
| 1030 | */ |
| 1031 | msg->sl.st.l = ptr - msg->sol; |
| 1032 | return ptr; |
| 1033 | |
| 1034 | #ifdef DEBUG_FULL |
| 1035 | default: |
| 1036 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1037 | exit(1); |
| 1038 | #endif |
| 1039 | } |
| 1040 | |
| 1041 | http_msg_ood: |
| 1042 | /* out of data */ |
| 1043 | if (ret_state) |
| 1044 | *ret_state = state; |
| 1045 | if (ret_ptr) |
| 1046 | *ret_ptr = (char *)ptr; |
| 1047 | return NULL; |
| 1048 | |
| 1049 | http_msg_invalid: |
| 1050 | /* invalid message */ |
| 1051 | if (ret_state) |
| 1052 | *ret_state = HTTP_MSG_ERROR; |
| 1053 | return NULL; |
| 1054 | } |
| 1055 | |
| 1056 | |
| 1057 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1058 | * This function parses a request line between <ptr> and <end>, starting with |
| 1059 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1060 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1061 | * will give undefined results. |
| 1062 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1063 | * and that msg->sol points to the beginning of the request. |
| 1064 | * If a complete line is found (which implies that at least one CR or LF is |
| 1065 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1066 | * returned indicating an incomplete line (which does not mean that parts have |
| 1067 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1068 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1069 | * upon next call. |
| 1070 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1071 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1072 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1073 | * 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] | 1074 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1075 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1076 | const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, |
| 1077 | unsigned int state, const char *ptr, const char *end, |
| 1078 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1079 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1080 | __label__ |
| 1081 | http_msg_rqmeth, |
| 1082 | http_msg_rqmeth_sp, |
| 1083 | http_msg_rquri, |
| 1084 | http_msg_rquri_sp, |
| 1085 | http_msg_rqver, |
| 1086 | http_msg_rqline_eol, |
| 1087 | http_msg_ood, /* out of data */ |
| 1088 | http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1089 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1090 | switch (state) { |
| 1091 | http_msg_rqmeth: |
| 1092 | case HTTP_MSG_RQMETH: |
| 1093 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1094 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1095 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1096 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1097 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1098 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1099 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1100 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1101 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1102 | /* HTTP 0.9 request */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1103 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1104 | http_msg_req09_uri: |
| 1105 | msg->sl.rq.u = ptr - msg_buf; |
| 1106 | http_msg_req09_uri_e: |
| 1107 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1108 | http_msg_req09_ver: |
| 1109 | msg->sl.rq.v = ptr - msg_buf; |
| 1110 | msg->sl.rq.v_l = 0; |
| 1111 | goto http_msg_rqline_eol; |
| 1112 | } |
| 1113 | goto http_msg_invalid; |
| 1114 | |
| 1115 | http_msg_rqmeth_sp: |
| 1116 | case HTTP_MSG_RQMETH_SP: |
| 1117 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1118 | msg->sl.rq.u = ptr - msg_buf; |
| 1119 | goto http_msg_rquri; |
| 1120 | } |
| 1121 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1122 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1123 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1124 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1125 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1126 | http_msg_rquri: |
| 1127 | case HTTP_MSG_RQURI: |
| 1128 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1129 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1130 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1131 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1132 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1133 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1134 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1135 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1136 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1137 | goto http_msg_req09_uri_e; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1138 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1139 | http_msg_rquri_sp: |
| 1140 | case HTTP_MSG_RQURI_SP: |
| 1141 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1142 | msg->sl.rq.v = ptr - msg_buf; |
| 1143 | goto http_msg_rqver; |
| 1144 | } |
| 1145 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1146 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1147 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1148 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1149 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1150 | http_msg_rqver: |
| 1151 | case HTTP_MSG_RQVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1152 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1153 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1154 | |
| 1155 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1156 | msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v; |
| 1157 | http_msg_rqline_eol: |
| 1158 | /* We have seen the end of line. Note that we do not |
| 1159 | * necessarily have the \n yet, but at least we know that we |
| 1160 | * have EITHER \r OR \n, otherwise the request would not be |
| 1161 | * complete. We can then record the request length and return |
| 1162 | * to the caller which will be able to register it. |
| 1163 | */ |
| 1164 | msg->sl.rq.l = ptr - msg->sol; |
| 1165 | return ptr; |
| 1166 | } |
| 1167 | |
| 1168 | /* neither an HTTP_VER token nor a CRLF */ |
| 1169 | goto http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1170 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1171 | #ifdef DEBUG_FULL |
| 1172 | default: |
| 1173 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1174 | exit(1); |
| 1175 | #endif |
| 1176 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1177 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1178 | http_msg_ood: |
| 1179 | /* out of data */ |
| 1180 | if (ret_state) |
| 1181 | *ret_state = state; |
| 1182 | if (ret_ptr) |
| 1183 | *ret_ptr = (char *)ptr; |
| 1184 | return NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1185 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1186 | http_msg_invalid: |
| 1187 | /* invalid message */ |
| 1188 | if (ret_state) |
| 1189 | *ret_state = HTTP_MSG_ERROR; |
| 1190 | return NULL; |
| 1191 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1192 | |
| 1193 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1194 | /* |
| 1195 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1196 | * depending on the initial msg->msg_state. It can be preempted everywhere |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1197 | * when data are missing and recalled at the exact same location with no |
| 1198 | * information loss. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1199 | * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other |
| 1200 | * fields. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1201 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1202 | void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx) |
| 1203 | { |
| 1204 | __label__ |
| 1205 | http_msg_rqbefore, |
| 1206 | http_msg_rqbefore_cr, |
| 1207 | http_msg_rqmeth, |
| 1208 | http_msg_rqline_end, |
| 1209 | http_msg_hdr_first, |
| 1210 | http_msg_hdr_name, |
| 1211 | http_msg_hdr_l1_sp, |
| 1212 | http_msg_hdr_l1_lf, |
| 1213 | http_msg_hdr_l1_lws, |
| 1214 | http_msg_hdr_val, |
| 1215 | http_msg_hdr_l2_lf, |
| 1216 | http_msg_hdr_l2_lws, |
| 1217 | http_msg_complete_header, |
| 1218 | http_msg_last_lf, |
| 1219 | http_msg_ood, /* out of data */ |
| 1220 | http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1221 | |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1222 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1223 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1224 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1225 | state = msg->msg_state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1226 | ptr = buf->lr; |
| 1227 | end = buf->r; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1228 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1229 | if (unlikely(ptr >= end)) |
| 1230 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1231 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1232 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1233 | /* |
| 1234 | * First, states that are specific to the response only. |
| 1235 | * We check them first so that request and headers are |
| 1236 | * closer to each other (accessed more often). |
| 1237 | */ |
| 1238 | http_msg_rpbefore: |
| 1239 | case HTTP_MSG_RPBEFORE: |
| 1240 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1241 | if (likely(ptr == buf->data)) { |
| 1242 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1243 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1244 | } else { |
| 1245 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1246 | /* only skip empty leading lines, don't remove them */ |
| 1247 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1248 | msg->som = ptr - buf->data; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1249 | #else |
| 1250 | /* Remove empty leading lines, as recommended by |
| 1251 | * RFC2616. This takes a lot of time because we |
| 1252 | * must move all the buffer backwards, but this |
| 1253 | * is rarely needed. The method above will be |
| 1254 | * cleaner when we'll be able to start sending |
| 1255 | * the request from any place in the buffer. |
| 1256 | */ |
| 1257 | buf->lr = ptr; |
| 1258 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1259 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1260 | msg->sol = buf->data; |
| 1261 | ptr = buf->data; |
| 1262 | end = buf->r; |
| 1263 | #endif |
| 1264 | } |
| 1265 | hdr_idx_init(idx); |
| 1266 | state = HTTP_MSG_RPVER; |
| 1267 | goto http_msg_rpver; |
| 1268 | } |
| 1269 | |
| 1270 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1271 | goto http_msg_invalid; |
| 1272 | |
| 1273 | if (unlikely(*ptr == '\n')) |
| 1274 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1275 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1276 | /* stop here */ |
| 1277 | |
| 1278 | http_msg_rpbefore_cr: |
| 1279 | case HTTP_MSG_RPBEFORE_CR: |
| 1280 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1281 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1282 | /* stop here */ |
| 1283 | |
| 1284 | http_msg_rpver: |
| 1285 | case HTTP_MSG_RPVER: |
| 1286 | case HTTP_MSG_RPVER_SP: |
| 1287 | case HTTP_MSG_RPCODE: |
| 1288 | case HTTP_MSG_RPCODE_SP: |
| 1289 | case HTTP_MSG_RPREASON: |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1290 | ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1291 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1292 | if (unlikely(!ptr)) |
| 1293 | return; |
| 1294 | |
| 1295 | /* we have a full response and we know that we have either a CR |
| 1296 | * or an LF at <ptr>. |
| 1297 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1298 | //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] | 1299 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1300 | |
| 1301 | msg->sol = ptr; |
| 1302 | if (likely(*ptr == '\r')) |
| 1303 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1304 | goto http_msg_rpline_end; |
| 1305 | |
| 1306 | http_msg_rpline_end: |
| 1307 | case HTTP_MSG_RPLINE_END: |
| 1308 | /* msg->sol must point to the first of CR or LF. */ |
| 1309 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1310 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1311 | /* stop here */ |
| 1312 | |
| 1313 | /* |
| 1314 | * Second, states that are specific to the request only |
| 1315 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1316 | http_msg_rqbefore: |
| 1317 | case HTTP_MSG_RQBEFORE: |
| 1318 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1319 | if (likely(ptr == buf->data)) { |
| 1320 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1321 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1322 | } else { |
| 1323 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1324 | /* only skip empty leading lines, don't remove them */ |
| 1325 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1326 | msg->som = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1327 | #else |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1328 | /* Remove empty leading lines, as recommended by |
| 1329 | * RFC2616. This takes a lot of time because we |
| 1330 | * must move all the buffer backwards, but this |
| 1331 | * is rarely needed. The method above will be |
| 1332 | * cleaner when we'll be able to start sending |
| 1333 | * the request from any place in the buffer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1334 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1335 | buf->lr = ptr; |
| 1336 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1337 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1338 | msg->sol = buf->data; |
| 1339 | ptr = buf->data; |
| 1340 | end = buf->r; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1341 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1342 | } |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1343 | /* we will need this when keep-alive will be supported |
| 1344 | hdr_idx_init(idx); |
| 1345 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1346 | state = HTTP_MSG_RQMETH; |
| 1347 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1348 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1349 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1350 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1351 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1352 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1353 | if (unlikely(*ptr == '\n')) |
| 1354 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1355 | 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] | 1356 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1357 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1358 | http_msg_rqbefore_cr: |
| 1359 | case HTTP_MSG_RQBEFORE_CR: |
| 1360 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1361 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1362 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1363 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1364 | http_msg_rqmeth: |
| 1365 | case HTTP_MSG_RQMETH: |
| 1366 | case HTTP_MSG_RQMETH_SP: |
| 1367 | case HTTP_MSG_RQURI: |
| 1368 | case HTTP_MSG_RQURI_SP: |
| 1369 | case HTTP_MSG_RQVER: |
| 1370 | ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1371 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1372 | if (unlikely(!ptr)) |
| 1373 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1374 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1375 | /* we have a full request and we know that we have either a CR |
| 1376 | * or an LF at <ptr>. |
| 1377 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1378 | //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] | 1379 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1380 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1381 | msg->sol = ptr; |
| 1382 | if (likely(*ptr == '\r')) |
| 1383 | 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] | 1384 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1385 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1386 | http_msg_rqline_end: |
| 1387 | case HTTP_MSG_RQLINE_END: |
| 1388 | /* check for HTTP/0.9 request : no version information available. |
| 1389 | * msg->sol must point to the first of CR or LF. |
| 1390 | */ |
| 1391 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1392 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1393 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1394 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1395 | 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] | 1396 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1397 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1398 | /* |
| 1399 | * Common states below |
| 1400 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1401 | http_msg_hdr_first: |
| 1402 | case HTTP_MSG_HDR_FIRST: |
| 1403 | msg->sol = ptr; |
| 1404 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1405 | goto http_msg_hdr_name; |
| 1406 | } |
| 1407 | |
| 1408 | if (likely(*ptr == '\r')) |
| 1409 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1410 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1411 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1412 | http_msg_hdr_name: |
| 1413 | case HTTP_MSG_HDR_NAME: |
| 1414 | /* assumes msg->sol points to the first char */ |
| 1415 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1416 | 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] | 1417 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1418 | if (likely(*ptr == ':')) { |
| 1419 | msg->col = ptr - buf->data; |
| 1420 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| 1421 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1422 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1423 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1424 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1425 | http_msg_hdr_l1_sp: |
| 1426 | case HTTP_MSG_HDR_L1_SP: |
| 1427 | /* assumes msg->sol points to the first char and msg->col to the colon */ |
| 1428 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1429 | 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] | 1430 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1431 | /* header value can be basically anything except CR/LF */ |
| 1432 | msg->sov = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1433 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1434 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1435 | goto http_msg_hdr_val; |
| 1436 | } |
| 1437 | |
| 1438 | if (likely(*ptr == '\r')) |
| 1439 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1440 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1441 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1442 | http_msg_hdr_l1_lf: |
| 1443 | case HTTP_MSG_HDR_L1_LF: |
| 1444 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1445 | 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] | 1446 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1447 | http_msg_hdr_l1_lws: |
| 1448 | case HTTP_MSG_HDR_L1_LWS: |
| 1449 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1450 | /* replace HT,CR,LF with spaces */ |
| 1451 | for (; buf->data+msg->sov < ptr; msg->sov++) |
| 1452 | buf->data[msg->sov] = ' '; |
| 1453 | goto http_msg_hdr_l1_sp; |
| 1454 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1455 | /* we had a header consisting only in spaces ! */ |
| 1456 | msg->eol = buf->data + msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1457 | goto http_msg_complete_header; |
| 1458 | |
| 1459 | http_msg_hdr_val: |
| 1460 | case HTTP_MSG_HDR_VAL: |
| 1461 | /* assumes msg->sol points to the first char, msg->col to the |
| 1462 | * colon, and msg->sov points to the first character of the |
| 1463 | * value. |
| 1464 | */ |
| 1465 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1466 | 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] | 1467 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1468 | msg->eol = ptr; |
| 1469 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1470 | * real header end in case it ends with lots of LWS, but is this |
| 1471 | * really needed ? |
| 1472 | */ |
| 1473 | if (likely(*ptr == '\r')) |
| 1474 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1475 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1476 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1477 | http_msg_hdr_l2_lf: |
| 1478 | case HTTP_MSG_HDR_L2_LF: |
| 1479 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1480 | 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] | 1481 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1482 | http_msg_hdr_l2_lws: |
| 1483 | case HTTP_MSG_HDR_L2_LWS: |
| 1484 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1485 | /* LWS: replace HT,CR,LF with spaces */ |
| 1486 | for (; msg->eol < ptr; msg->eol++) |
| 1487 | *msg->eol = ' '; |
| 1488 | goto http_msg_hdr_val; |
| 1489 | } |
| 1490 | http_msg_complete_header: |
| 1491 | /* |
| 1492 | * It was a new header, so the last one is finished. |
| 1493 | * Assumes msg->sol points to the first char, msg->col to the |
| 1494 | * colon, msg->sov points to the first character of the value |
| 1495 | * and msg->eol to the first CR or LF so we know how the line |
| 1496 | * ends. We insert last header into the index. |
| 1497 | */ |
| 1498 | /* |
| 1499 | fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol); |
| 1500 | write(2, msg->sol, msg->eol-msg->sol); |
| 1501 | fprintf(stderr,"\n"); |
| 1502 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1503 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1504 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r', |
| 1505 | idx, idx->tail) < 0)) |
| 1506 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1507 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1508 | msg->sol = ptr; |
| 1509 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1510 | goto http_msg_hdr_name; |
| 1511 | } |
| 1512 | |
| 1513 | if (likely(*ptr == '\r')) |
| 1514 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1515 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1516 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1517 | http_msg_last_lf: |
| 1518 | case HTTP_MSG_LAST_LF: |
| 1519 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1520 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1521 | ptr++; |
| 1522 | buf->lr = ptr; |
| 1523 | msg->eoh = msg->sol - buf->data; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1524 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1525 | return; |
| 1526 | #ifdef DEBUG_FULL |
| 1527 | default: |
| 1528 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1529 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1530 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1531 | } |
| 1532 | http_msg_ood: |
| 1533 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1534 | msg->msg_state = state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1535 | buf->lr = ptr; |
| 1536 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1537 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1538 | http_msg_invalid: |
| 1539 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1540 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1541 | return; |
| 1542 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1543 | |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1544 | /* This function performs all the processing enabled for the current request. |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1545 | * It normally returns zero, but may return 1 if it absolutely needs to be |
| 1546 | * called again after other functions. It relies on buffers flags, and updates |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1547 | * t->req->analysers. It might make sense to explode it into several other |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 1548 | * functions. Its behaviour is rather simple : |
| 1549 | * - all enabled analysers are called in turn from the lower to the higher |
| 1550 | * bit. |
| 1551 | * - if an analyser does not have enough data, it must return without calling |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 1552 | * other ones. It should also probably reset the BF_WRITE_ENA bit to ensure |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 1553 | * that unprocessed data will not be forwarded. But that probably depends on |
| 1554 | * the protocol. Generally it is not reset in case of errors. |
| 1555 | * - if an analyser has enough data, it just has to pass on to the next |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 1556 | * analyser without touching BF_WRITE_ENA (it is enabled prior to |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 1557 | * analysis). |
| 1558 | * - if an analyser thinks it has no added value anymore staying here, it must |
| 1559 | * reset its bit from the analysers flags in order not to be called anymore. |
| 1560 | * |
| 1561 | * In the future, analysers should be able to indicate that they want to be |
| 1562 | * called after XXX bytes have been received (or transfered), and the min of |
| 1563 | * all's wishes will be used to ring back (unless a special condition occurs). |
| 1564 | * |
| 1565 | * |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1566 | */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1567 | int process_request(struct session *t) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1568 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1569 | struct buffer *req = t->req; |
| 1570 | struct buffer *rep = t->rep; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1571 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 1572 | 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] | 1573 | now_ms, __FUNCTION__, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 1574 | t, |
| 1575 | req, |
| 1576 | req->rex, req->wex, |
| 1577 | req->flags, |
| 1578 | req->l, |
| 1579 | req->analysers); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1580 | |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 1581 | /* The tcp-inspect analyser is always called alone */ |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1582 | if (req->analysers & AN_REQ_INSPECT) { |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1583 | struct tcp_rule *rule; |
| 1584 | int partial; |
| 1585 | |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 1586 | /* We will abort if we encounter a read error. In theory, we |
| 1587 | * should not abort if we get a close, it might be valid, |
| 1588 | * although very unlikely. FIXME: we'll abort for now, this |
| 1589 | * will be easier to change later. |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1590 | */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1591 | if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1592 | req->analysers = 0; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 1593 | //t->fe->failed_req++; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1594 | if (!(t->flags & SN_ERR_MASK)) |
| 1595 | t->flags |= SN_ERR_CLICL; |
| 1596 | if (!(t->flags & SN_FINST_MASK)) |
| 1597 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1598 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1599 | } |
| 1600 | |
| 1601 | /* Abort if client read timeout has expired */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1602 | else if (req->flags & BF_READ_TIMEOUT) { |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1603 | req->analysers = 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1604 | t->fe->failed_req++; |
| 1605 | if (!(t->flags & SN_ERR_MASK)) |
| 1606 | t->flags |= SN_ERR_CLITO; |
| 1607 | if (!(t->flags & SN_FINST_MASK)) |
| 1608 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1609 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1610 | } |
| 1611 | |
| 1612 | /* We don't know whether we have enough data, so must proceed |
| 1613 | * this way : |
| 1614 | * - iterate through all rules in their declaration order |
| 1615 | * - if one rule returns MISS, it means the inspect delay is |
| 1616 | * not over yet, then return immediately, otherwise consider |
| 1617 | * it as a non-match. |
| 1618 | * - if one rule returns OK, then return OK |
| 1619 | * - if one rule returns KO, then return KO |
| 1620 | */ |
| 1621 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 1622 | if (req->flags & BF_SHUTR || tick_is_expired(req->analyse_exp, now_ms)) |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1623 | partial = 0; |
| 1624 | else |
| 1625 | partial = ACL_PARTIAL; |
| 1626 | |
| 1627 | list_for_each_entry(rule, &t->fe->tcp_req.inspect_rules, list) { |
| 1628 | int ret = ACL_PAT_PASS; |
| 1629 | |
| 1630 | if (rule->cond) { |
| 1631 | ret = acl_exec_cond(rule->cond, t->fe, t, NULL, ACL_DIR_REQ | partial); |
| 1632 | if (ret == ACL_PAT_MISS) { |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 1633 | buffer_write_dis(req); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1634 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 1635 | if (!tick_isset(req->analyse_exp)) |
| 1636 | req->analyse_exp = tick_add_ifset(now_ms, t->fe->tcp_req.inspect_delay); |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1637 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1638 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1639 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1640 | ret = acl_pass(ret); |
| 1641 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 1642 | ret = !ret; |
| 1643 | } |
| 1644 | |
| 1645 | if (ret) { |
| 1646 | /* we have a matching rule. */ |
| 1647 | if (rule->action == TCP_ACT_REJECT) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1648 | buffer_abort(req); |
| 1649 | buffer_abort(rep); |
| 1650 | //FIXME: this delete this |
| 1651 | //fd_delete(t->cli_fd); |
| 1652 | //t->cli_state = CL_STCLOSE; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1653 | req->analysers = 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1654 | t->fe->failed_req++; |
| 1655 | if (!(t->flags & SN_ERR_MASK)) |
| 1656 | t->flags |= SN_ERR_PRXCOND; |
| 1657 | if (!(t->flags & SN_FINST_MASK)) |
| 1658 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1659 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1660 | } |
| 1661 | /* otherwise accept */ |
| 1662 | break; |
| 1663 | } |
| 1664 | } |
| 1665 | |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1666 | /* if we get there, it means we have no rule which matches, or |
| 1667 | * we have an explicit accept, so we apply the default accept. |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1668 | */ |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1669 | req->analysers &= ~AN_REQ_INSPECT; |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 1670 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1671 | } |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1672 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1673 | if (req->analysers & AN_REQ_HTTP_HDR) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1674 | /* |
| 1675 | * Now parse the partial (or complete) lines. |
| 1676 | * We will check the request syntax, and also join multi-line |
| 1677 | * headers. An index of all the lines will be elaborated while |
| 1678 | * parsing. |
| 1679 | * |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1680 | * For the parsing, we use a 28 states FSM. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1681 | * |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1682 | * Here is the information we currently have : |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1683 | * req->data + req->som = beginning of request |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1684 | * req->data + req->eoh = end of processed headers / start of current one |
| 1685 | * req->data + req->eol = end of current header or line (LF or CRLF) |
| 1686 | * req->lr = first non-visited byte |
| 1687 | * req->r = end of data |
| 1688 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1689 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1690 | int cur_idx; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1691 | struct http_txn *txn = &t->txn; |
| 1692 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1693 | struct proxy *cur_proxy; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1694 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1695 | if (likely(req->lr < req->r)) |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1696 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1697 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1698 | /* 1: we might have to print this header in debug mode */ |
| 1699 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1700 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1701 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1702 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1703 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1704 | sol = req->data + msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1705 | eol = sol + msg->sl.rq.l; |
| 1706 | debug_hdr("clireq", t, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 1707 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1708 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 1709 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1710 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1711 | while (cur_idx) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1712 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1713 | debug_hdr("clihdr", t, sol, eol); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1714 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 1715 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1716 | } |
| 1717 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1718 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1719 | |
| 1720 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1721 | * Now we quickly check if we have found a full valid request. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1722 | * If not so, we check the FD and buffer states before leaving. |
| 1723 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1724 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 1725 | * requests are checked first. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1726 | * |
| 1727 | */ |
| 1728 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1729 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1730 | /* |
| 1731 | * First, let's catch bad requests. |
| 1732 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1733 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1734 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1735 | |
| 1736 | /* 1: Since we are in header mode, if there's no space |
| 1737 | * left for headers, we won't be able to free more |
| 1738 | * later, so the session will never terminate. We |
| 1739 | * must terminate it now. |
| 1740 | */ |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 1741 | if (unlikely(req->flags & BF_FULL)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1742 | /* FIXME: check if URI is set and return Status |
| 1743 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1744 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1745 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1746 | } |
| 1747 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 1748 | /* 2: have we encountered a read error ? */ |
| 1749 | else if (req->flags & BF_READ_ERROR) { |
| 1750 | /* we cannot return any message on error */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1751 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1752 | req->analysers = 0; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 1753 | //t->fe->failed_req++; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1754 | if (!(t->flags & SN_ERR_MASK)) |
| 1755 | t->flags |= SN_ERR_CLICL; |
| 1756 | if (!(t->flags & SN_FINST_MASK)) |
| 1757 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1758 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1759 | } |
| 1760 | |
| 1761 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 1762 | else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1763 | /* read timeout : give up with an error message. */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1764 | txn->status = 408; |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 1765 | stream_int_retnclose(req->prod, error_message(t, HTTP_ERR_408)); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1766 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1767 | req->analysers = 0; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 1768 | t->fe->failed_req++; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1769 | if (!(t->flags & SN_ERR_MASK)) |
| 1770 | t->flags |= SN_ERR_CLITO; |
| 1771 | if (!(t->flags & SN_FINST_MASK)) |
| 1772 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1773 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1774 | } |
| 1775 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 1776 | /* 4: have we encountered a close ? */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 1777 | else if (req->flags & BF_SHUTR) { |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 1778 | txn->status = 400; |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 1779 | stream_int_retnclose(req->prod, error_message(t, HTTP_ERR_400)); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1780 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1781 | req->analysers = 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1782 | t->fe->failed_req++; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 1783 | |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1784 | if (!(t->flags & SN_ERR_MASK)) |
| 1785 | t->flags |= SN_ERR_CLICL; |
| 1786 | if (!(t->flags & SN_FINST_MASK)) |
| 1787 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1788 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1789 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1790 | |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 1791 | buffer_write_dis(req); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1792 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 1793 | if (!tick_isset(req->analyse_exp)) |
| 1794 | req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1795 | |
| 1796 | /* we're not ready yet */ |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1797 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1798 | } |
| 1799 | |
| 1800 | |
| 1801 | /**************************************************************** |
| 1802 | * More interesting part now : we know that we have a complete * |
| 1803 | * request which at least looks like HTTP. We have an indicator * |
| 1804 | * of each header's length, so we can parse them quickly. * |
| 1805 | ****************************************************************/ |
| 1806 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1807 | req->analysers &= ~AN_REQ_HTTP_HDR; |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 1808 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1809 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1810 | /* ensure we keep this pointer to the beginning of the message */ |
| 1811 | msg->sol = req->data + msg->som; |
| 1812 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1813 | /* |
| 1814 | * 1: identify the method |
| 1815 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1816 | txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1817 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1818 | /* we can make use of server redirect on GET and HEAD */ |
| 1819 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 1820 | t->flags |= SN_REDIRECTABLE; |
| 1821 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1822 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1823 | * 2: check if the URI matches the monitor_uri. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1824 | * We have to do this for every request which gets in, because |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1825 | * the monitor-uri is defined by the frontend. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1826 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1827 | if (unlikely((t->fe->monitor_uri_len != 0) && |
| 1828 | (t->fe->monitor_uri_len == msg->sl.rq.u_l) && |
| 1829 | !memcmp(&req->data[msg->sl.rq.u], |
| 1830 | t->fe->monitor_uri, |
| 1831 | t->fe->monitor_uri_len))) { |
| 1832 | /* |
| 1833 | * We have found the monitor URI |
| 1834 | */ |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1835 | struct acl_cond *cond; |
| 1836 | cur_proxy = t->fe; |
| 1837 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1838 | t->flags |= SN_MONITOR; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1839 | |
| 1840 | /* Check if we want to fail this monitor request or not */ |
| 1841 | list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) { |
| 1842 | int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1843 | |
| 1844 | ret = acl_pass(ret); |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1845 | if (cond->pol == ACL_COND_UNLESS) |
| 1846 | ret = !ret; |
| 1847 | |
| 1848 | if (ret) { |
| 1849 | /* we fail this request, let's return 503 service unavail */ |
| 1850 | txn->status = 503; |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 1851 | stream_int_retnclose(req->prod, error_message(t, HTTP_ERR_503)); |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1852 | goto return_prx_cond; |
| 1853 | } |
| 1854 | } |
| 1855 | |
| 1856 | /* nothing to fail, let's reply normaly */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1857 | txn->status = 200; |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 1858 | stream_int_retnclose(req->prod, &http_200_chunk); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1859 | goto return_prx_cond; |
| 1860 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 1861 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1862 | /* |
| 1863 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 1864 | * Note: we cannot log anymore if the request has been |
| 1865 | * classified as invalid. |
| 1866 | */ |
| 1867 | if (unlikely(t->logs.logwait & LW_REQ)) { |
| 1868 | /* we have a complete HTTP request that we must log */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 1869 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1870 | int urilen = msg->sl.rq.l; |
| 1871 | |
| 1872 | if (urilen >= REQURI_LEN) |
| 1873 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1874 | memcpy(txn->uri, &req->data[msg->som], urilen); |
| 1875 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1876 | |
| 1877 | if (!(t->logs.logwait &= ~LW_REQ)) |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 1878 | t->do_log(t); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1879 | } else { |
| 1880 | Alert("HTTP logging : out of memory.\n"); |
| 1881 | } |
| 1882 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1883 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1884 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1885 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 1886 | if (unlikely(msg->sl.rq.v_l == 0)) { |
| 1887 | int delta; |
| 1888 | char *cur_end; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1889 | msg->sol = req->data + msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1890 | cur_end = msg->sol + msg->sl.rq.l; |
| 1891 | delta = 0; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1892 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1893 | if (msg->sl.rq.u_l == 0) { |
| 1894 | /* if no URI was set, add "/" */ |
| 1895 | delta = buffer_replace2(req, cur_end, cur_end, " /", 2); |
| 1896 | cur_end += delta; |
| 1897 | msg->eoh += delta; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1898 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1899 | /* add HTTP version */ |
| 1900 | delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11); |
| 1901 | msg->eoh += delta; |
| 1902 | cur_end += delta; |
| 1903 | cur_end = (char *)http_parse_reqline(msg, req->data, |
| 1904 | HTTP_MSG_RQMETH, |
| 1905 | msg->sol, cur_end + 1, |
| 1906 | NULL, NULL); |
| 1907 | if (unlikely(!cur_end)) |
| 1908 | goto return_bad_req; |
| 1909 | |
| 1910 | /* we have a full HTTP/1.0 request now and we know that |
| 1911 | * we have either a CR or an LF at <ptr>. |
| 1912 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1913 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1914 | } |
| 1915 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1916 | |
| 1917 | /* 5: we may need to capture headers */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1918 | if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap)) |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1919 | capture_headers(req->data + msg->som, &txn->hdr_idx, |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1920 | txn->req.cap, t->fe->req_cap); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1921 | |
| 1922 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1923 | * 6: we will have to evaluate the filters. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1924 | * As opposed to version 1.2, now they will be evaluated in the |
| 1925 | * filters order and not in the header order. This means that |
| 1926 | * each filter has to be validated among all headers. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1927 | * |
| 1928 | * We can now check whether we want to switch to another |
| 1929 | * backend, in which case we will re-check the backend's |
| 1930 | * filters and various options. In order to support 3-level |
| 1931 | * switching, here's how we should proceed : |
| 1932 | * |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1933 | * a) run be. |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1934 | * if (switch) then switch ->be to the new backend. |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1935 | * b) run be if (be != fe). |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1936 | * There cannot be any switch from there, so ->be cannot be |
| 1937 | * changed anymore. |
| 1938 | * |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1939 | * => filters always apply to ->be, then ->be may change. |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1940 | * |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1941 | * The response path will be able to apply either ->be, or |
| 1942 | * ->be then ->fe filters in order to match the reverse of |
| 1943 | * the forward sequence. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1944 | */ |
| 1945 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1946 | do { |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 1947 | struct acl_cond *cond; |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 1948 | struct redirect_rule *rule; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1949 | struct proxy *rule_set = t->be; |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1950 | cur_proxy = t->be; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1951 | |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 1952 | /* first check whether we have some ACLs set to redirect this request */ |
| 1953 | list_for_each_entry(rule, &cur_proxy->redirect_rules, list) { |
| 1954 | int ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 1955 | |
| 1956 | ret = acl_pass(ret); |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 1957 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 1958 | ret = !ret; |
| 1959 | |
| 1960 | if (ret) { |
| 1961 | struct chunk rdr = { trash, 0 }; |
| 1962 | const char *msg_fmt; |
| 1963 | |
| 1964 | /* build redirect message */ |
| 1965 | switch(rule->code) { |
| 1966 | case 303: |
| 1967 | rdr.len = strlen(HTTP_303); |
| 1968 | msg_fmt = HTTP_303; |
| 1969 | break; |
| 1970 | case 301: |
| 1971 | rdr.len = strlen(HTTP_301); |
| 1972 | msg_fmt = HTTP_301; |
| 1973 | break; |
| 1974 | case 302: |
| 1975 | default: |
| 1976 | rdr.len = strlen(HTTP_302); |
| 1977 | msg_fmt = HTTP_302; |
| 1978 | break; |
| 1979 | } |
| 1980 | |
| 1981 | if (unlikely(rdr.len > sizeof(trash))) |
| 1982 | goto return_bad_req; |
| 1983 | memcpy(rdr.str, msg_fmt, rdr.len); |
| 1984 | |
| 1985 | switch(rule->type) { |
| 1986 | case REDIRECT_TYPE_PREFIX: { |
| 1987 | const char *path; |
| 1988 | int pathlen; |
| 1989 | |
| 1990 | path = http_get_path(txn); |
| 1991 | /* build message using path */ |
| 1992 | if (path) { |
| 1993 | pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path; |
| 1994 | } else { |
| 1995 | path = "/"; |
| 1996 | pathlen = 1; |
| 1997 | } |
| 1998 | |
| 1999 | if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4) |
| 2000 | goto return_bad_req; |
| 2001 | |
| 2002 | /* add prefix */ |
| 2003 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 2004 | rdr.len += rule->rdr_len; |
| 2005 | |
| 2006 | /* add path */ |
| 2007 | memcpy(rdr.str + rdr.len, path, pathlen); |
| 2008 | rdr.len += pathlen; |
| 2009 | break; |
| 2010 | } |
| 2011 | case REDIRECT_TYPE_LOCATION: |
| 2012 | default: |
| 2013 | if (rdr.len + rule->rdr_len > sizeof(trash) - 4) |
| 2014 | goto return_bad_req; |
| 2015 | |
| 2016 | /* add location */ |
| 2017 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 2018 | rdr.len += rule->rdr_len; |
| 2019 | break; |
| 2020 | } |
| 2021 | |
| 2022 | /* add end of headers */ |
| 2023 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 2024 | rdr.len += 4; |
| 2025 | |
| 2026 | txn->status = rule->code; |
| 2027 | /* let's log the request time */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2028 | t->logs.tv_request = now; |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 2029 | stream_int_retnclose(req->prod, &rdr); |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2030 | goto return_prx_cond; |
| 2031 | } |
| 2032 | } |
| 2033 | |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2034 | /* first check whether we have some ACLs set to block this request */ |
| 2035 | list_for_each_entry(cond, &cur_proxy->block_cond, list) { |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 2036 | int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2037 | |
| 2038 | ret = acl_pass(ret); |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2039 | if (cond->pol == ACL_COND_UNLESS) |
| 2040 | ret = !ret; |
| 2041 | |
| 2042 | if (ret) { |
| 2043 | txn->status = 403; |
| 2044 | /* let's log the request time */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2045 | t->logs.tv_request = now; |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 2046 | stream_int_retnclose(req->prod, error_message(t, HTTP_ERR_403)); |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2047 | goto return_prx_cond; |
| 2048 | } |
| 2049 | } |
| 2050 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2051 | /* try headers filters */ |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2052 | if (rule_set->req_exp != NULL) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2053 | if (apply_filters_to_request(t, req, rule_set->req_exp) < 0) |
| 2054 | goto return_bad_req; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2055 | } |
| 2056 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2057 | if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) { |
| 2058 | /* to ensure correct connection accounting on |
| 2059 | * the backend, we count the connection for the |
| 2060 | * one managing the queue. |
| 2061 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2062 | t->be->beconn++; |
| 2063 | if (t->be->beconn > t->be->beconn_max) |
| 2064 | t->be->beconn_max = t->be->beconn; |
| 2065 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2066 | t->flags |= SN_BE_ASSIGNED; |
| 2067 | } |
| 2068 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2069 | /* has the request been denied ? */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2070 | if (txn->flags & TX_CLDENY) { |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2071 | /* no need to go further */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2072 | txn->status = 403; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2073 | /* let's log the request time */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2074 | t->logs.tv_request = now; |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 2075 | stream_int_retnclose(req->prod, error_message(t, HTTP_ERR_403)); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2076 | goto return_prx_cond; |
| 2077 | } |
| 2078 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2079 | /* We might have to check for "Connection:" */ |
Krzysztof Oledzki | 336d475 | 2007-12-25 02:40:22 +0100 | [diff] [blame] | 2080 | if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2081 | !(t->flags & SN_CONN_CLOSED)) { |
| 2082 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2083 | int cur_idx, old_idx, delta, val; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2084 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2085 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2086 | cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2087 | old_idx = 0; |
| 2088 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2089 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 2090 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2091 | cur_ptr = cur_next; |
| 2092 | cur_end = cur_ptr + cur_hdr->len; |
| 2093 | cur_next = cur_end + cur_hdr->cr + 1; |
| 2094 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2095 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 2096 | if (val) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2097 | /* 3 possibilities : |
| 2098 | * - we have already set Connection: close, |
| 2099 | * so we remove this line. |
| 2100 | * - we have not yet set Connection: close, |
| 2101 | * but this line indicates close. We leave |
| 2102 | * it untouched and set the flag. |
| 2103 | * - we have not yet set Connection: close, |
| 2104 | * and this line indicates non-close. We |
| 2105 | * replace it. |
| 2106 | */ |
| 2107 | if (t->flags & SN_CONN_CLOSED) { |
| 2108 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2109 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2110 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2111 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 2112 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2113 | cur_hdr->len = 0; |
| 2114 | } else { |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2115 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 2116 | delta = buffer_replace2(req, cur_ptr + val, cur_end, |
| 2117 | "close", 5); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2118 | cur_next += delta; |
| 2119 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2120 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2121 | } |
| 2122 | t->flags |= SN_CONN_CLOSED; |
| 2123 | } |
| 2124 | } |
| 2125 | old_idx = cur_idx; |
| 2126 | } |
Willy Tarreau | f2f0ee8 | 2007-03-30 12:02:43 +0200 | [diff] [blame] | 2127 | } |
| 2128 | /* add request headers from the rule sets in the same order */ |
| 2129 | for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) { |
| 2130 | if (unlikely(http_header_add_tail(req, |
| 2131 | &txn->req, |
| 2132 | &txn->hdr_idx, |
| 2133 | rule_set->req_add[cur_idx])) < 0) |
| 2134 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2135 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2136 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2137 | /* check if stats URI was requested, and if an auth is needed */ |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 2138 | if (rule_set->uri_auth != NULL && |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2139 | (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) { |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2140 | /* we have to check the URI and auth for this request. |
| 2141 | * FIXME!!! that one is rather dangerous, we want to |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2142 | * make it follow standard rules (eg: clear req->analysers). |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2143 | */ |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2144 | if (stats_check_uri_auth(t, rule_set)) |
| 2145 | return 1; |
| 2146 | } |
| 2147 | |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2148 | /* now check whether we have some switching rules for this request */ |
| 2149 | if (!(t->flags & SN_BE_ASSIGNED)) { |
| 2150 | struct switching_rule *rule; |
| 2151 | |
| 2152 | list_for_each_entry(rule, &cur_proxy->switching_rules, list) { |
| 2153 | int ret; |
| 2154 | |
| 2155 | ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2156 | |
| 2157 | ret = acl_pass(ret); |
Willy Tarreau | a8cfa34 | 2008-07-09 11:23:31 +0200 | [diff] [blame] | 2158 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2159 | ret = !ret; |
| 2160 | |
| 2161 | if (ret) { |
| 2162 | t->be = rule->be.backend; |
| 2163 | t->be->beconn++; |
| 2164 | if (t->be->beconn > t->be->beconn_max) |
| 2165 | t->be->beconn_max = t->be->beconn; |
| 2166 | t->be->cum_beconn++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2167 | |
| 2168 | /* assign new parameters to the session from the new backend */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2169 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 2170 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2171 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2172 | t->flags |= SN_BE_ASSIGNED; |
| 2173 | break; |
| 2174 | } |
| 2175 | } |
| 2176 | } |
| 2177 | |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 2178 | if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) { |
| 2179 | /* No backend was set, but there was a default |
| 2180 | * backend set in the frontend, so we use it and |
| 2181 | * loop again. |
| 2182 | */ |
| 2183 | t->be = cur_proxy->defbe.be; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2184 | t->be->beconn++; |
| 2185 | if (t->be->beconn > t->be->beconn_max) |
| 2186 | t->be->beconn_max = t->be->beconn; |
| 2187 | t->be->cum_beconn++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2188 | |
| 2189 | /* assign new parameters to the session from the new backend */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2190 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 2191 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2192 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 2193 | t->flags |= SN_BE_ASSIGNED; |
| 2194 | } |
| 2195 | } while (t->be != cur_proxy); /* we loop only if t->be has changed */ |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2196 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2197 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2198 | if (!(t->flags & SN_BE_ASSIGNED)) { |
| 2199 | /* To ensure correct connection accounting on |
| 2200 | * the backend, we count the connection for the |
| 2201 | * one managing the queue. |
| 2202 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2203 | t->be->beconn++; |
| 2204 | if (t->be->beconn > t->be->beconn_max) |
| 2205 | t->be->beconn_max = t->be->beconn; |
| 2206 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2207 | t->flags |= SN_BE_ASSIGNED; |
| 2208 | } |
| 2209 | |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2210 | /* |
| 2211 | * Right now, we know that we have processed the entire headers |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2212 | * and that unwanted requests have been filtered out. We can do |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2213 | * whatever we want with the remaining request. Also, now we |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2214 | * may have separate values for ->fe, ->be. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2215 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2216 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 2217 | /* |
| 2218 | * If HTTP PROXY is set we simply get remote server address |
| 2219 | * parsing incoming request. |
| 2220 | */ |
| 2221 | if ((t->be->options & PR_O_HTTP_PROXY) && !(t->flags & SN_ADDR_SET)) { |
| 2222 | url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &t->srv_addr); |
| 2223 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2224 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2225 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2226 | * 7: the appsession cookie was looked up very early in 1.2, |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2227 | * so let's do the same now. |
| 2228 | */ |
| 2229 | |
| 2230 | /* It needs to look into the URI */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2231 | if (t->be->appsession_name) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2232 | get_srv_from_appsession(t, &req->data[msg->som], msg->sl.rq.l); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2233 | } |
| 2234 | |
| 2235 | |
| 2236 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2237 | * 8: Now we can work with the cookies. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2238 | * Note that doing so might move headers in the request, but |
| 2239 | * the fields will stay coherent and the URI will not move. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2240 | * This should only be performed in the backend. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2241 | */ |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 2242 | if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name) |
| 2243 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2244 | manage_client_side_cookies(t, req); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2245 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2246 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2247 | /* |
Willy Tarreau | bb046ac | 2007-03-03 19:17:03 +0100 | [diff] [blame] | 2248 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 2249 | * asks for it. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2250 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2251 | if ((t->fe->options | t->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2252 | if (t->cli_addr.ss_family == AF_INET) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2253 | /* Add an X-Forwarded-For header unless the source IP is |
| 2254 | * in the 'except' network range. |
| 2255 | */ |
| 2256 | if ((!t->fe->except_mask.s_addr || |
| 2257 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr) |
| 2258 | != t->fe->except_net.s_addr) && |
| 2259 | (!t->be->except_mask.s_addr || |
| 2260 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr) |
| 2261 | != t->be->except_net.s_addr)) { |
| 2262 | int len; |
| 2263 | unsigned char *pn; |
| 2264 | pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr; |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2265 | |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2266 | /* Note: we rely on the backend to get the header name to be used for |
| 2267 | * x-forwarded-for, because the header is really meant for the backends. |
| 2268 | * However, if the backend did not specify any option, we have to rely |
| 2269 | * on the frontend's header name. |
| 2270 | */ |
| 2271 | if (t->be->fwdfor_hdr_len) { |
| 2272 | len = t->be->fwdfor_hdr_len; |
| 2273 | memcpy(trash, t->be->fwdfor_hdr_name, len); |
| 2274 | } else { |
| 2275 | len = t->fe->fwdfor_hdr_len; |
| 2276 | memcpy(trash, t->fe->fwdfor_hdr_name, len); |
| 2277 | } |
| 2278 | len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2279 | |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2280 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2281 | &txn->hdr_idx, trash, len)) < 0) |
| 2282 | goto return_bad_req; |
| 2283 | } |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2284 | } |
| 2285 | else if (t->cli_addr.ss_family == AF_INET6) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2286 | /* FIXME: for the sake of completeness, we should also support |
| 2287 | * 'except' here, although it is mostly useless in this case. |
| 2288 | */ |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2289 | int len; |
| 2290 | char pn[INET6_ADDRSTRLEN]; |
| 2291 | inet_ntop(AF_INET6, |
| 2292 | (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr, |
| 2293 | pn, sizeof(pn)); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2294 | |
| 2295 | /* Note: we rely on the backend to get the header name to be used for |
| 2296 | * x-forwarded-for, because the header is really meant for the backends. |
| 2297 | * However, if the backend did not specify any option, we have to rely |
| 2298 | * on the frontend's header name. |
| 2299 | */ |
| 2300 | if (t->be->fwdfor_hdr_len) { |
| 2301 | len = t->be->fwdfor_hdr_len; |
| 2302 | memcpy(trash, t->be->fwdfor_hdr_name, len); |
| 2303 | } else { |
| 2304 | len = t->fe->fwdfor_hdr_len; |
| 2305 | memcpy(trash, t->fe->fwdfor_hdr_name, len); |
| 2306 | } |
| 2307 | len += sprintf(trash + len, ": %s", pn); |
| 2308 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2309 | if (unlikely(http_header_add_tail2(req, &txn->req, |
| 2310 | &txn->hdr_idx, trash, len)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2311 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2312 | } |
| 2313 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2314 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2315 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2316 | * 10: add "Connection: close" if needed and not yet set. |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2317 | * Note that we do not need to add it in case of HTTP/1.0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2318 | */ |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2319 | if (!(t->flags & SN_CONN_CLOSED) && |
Krzysztof Oledzki | 336d475 | 2007-12-25 02:40:22 +0100 | [diff] [blame] | 2320 | ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) { |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2321 | if ((unlikely(msg->sl.rq.v_l != 8) || |
| 2322 | unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) && |
| 2323 | unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx, |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2324 | "Connection: close", 17)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2325 | goto return_bad_req; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2326 | t->flags |= SN_CONN_CLOSED; |
Willy Tarreau | e15d913 | 2006-12-14 22:26:42 +0100 | [diff] [blame] | 2327 | } |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2328 | /* Before we switch to data, was assignment set in manage_client_side_cookie? |
| 2329 | * If not assigned, perhaps we are balancing on url_param, but this is a |
| 2330 | * POST; and the parameters are in the body, maybe scan there to find our server. |
| 2331 | * (unless headers overflowed the buffer?) |
| 2332 | */ |
| 2333 | if (!(t->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 2334 | t->txn.meth == HTTP_METH_POST && t->be->url_param_name != NULL && |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2335 | t->be->url_param_post_limit != 0 && !(req->flags & BF_FULL) && |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2336 | memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) { |
| 2337 | /* are there enough bytes here? total == l || r || rlim ? |
| 2338 | * len is unsigned, but eoh is int, |
| 2339 | * how many bytes of body have we received? |
| 2340 | * eoh is the first empty line of the header |
| 2341 | */ |
| 2342 | /* already established CRLF or LF at eoh, move to start of message, find message length in buffer */ |
Willy Tarreau | fb0528b | 2008-08-11 00:21:56 +0200 | [diff] [blame] | 2343 | unsigned long len = req->l - (msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2344 | |
| 2345 | /* If we have HTTP/1.1 and Expect: 100-continue, then abort. |
| 2346 | * We can't assume responsibility for the server's decision, |
| 2347 | * on this URI and header set. See rfc2616: 14.20, 8.2.3, |
| 2348 | * We also can't change our mind later, about which server to choose, so round robin. |
| 2349 | */ |
| 2350 | if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) { |
| 2351 | struct hdr_ctx ctx; |
| 2352 | ctx.idx = 0; |
| 2353 | /* Expect is allowed in 1.1, look for it */ |
| 2354 | http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx); |
| 2355 | if (ctx.idx != 0 && |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2356 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2357 | /* We can't reliablly stall and wait for data, because of |
| 2358 | * .NET clients that don't conform to rfc2616; so, no need for |
| 2359 | * the next block to check length expectations. |
| 2360 | * We could send 100 status back to the client, but then we need to |
| 2361 | * re-write headers, and send the message. And this isn't the right |
| 2362 | * place for that action. |
| 2363 | * TODO: support Expect elsewhere and delete this block. |
| 2364 | */ |
| 2365 | goto end_check_maybe_wait_for_body; |
| 2366 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2367 | |
| 2368 | if (likely(len > t->be->url_param_post_limit)) { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2369 | /* nothing to do, we got enough */ |
| 2370 | } else { |
| 2371 | /* limit implies we are supposed to need this many bytes |
| 2372 | * to find the parameter. Let's see how many bytes we can wait for. |
| 2373 | */ |
| 2374 | long long hint = len; |
| 2375 | struct hdr_ctx ctx; |
| 2376 | ctx.idx = 0; |
| 2377 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx); |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2378 | if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) { |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 2379 | buffer_write_dis(req); |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2380 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2381 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2382 | else { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2383 | ctx.idx = 0; |
| 2384 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx); |
| 2385 | /* now if we have a length, we'll take the hint */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2386 | if (ctx.idx) { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2387 | /* We have Content-Length */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2388 | if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint)) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2389 | hint = 0; /* parse failure, untrusted client */ |
| 2390 | else { |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2391 | if (hint > 0) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2392 | msg->hdr_content_len = hint; |
| 2393 | else |
| 2394 | hint = 0; /* bad client, sent negative length */ |
| 2395 | } |
| 2396 | } |
| 2397 | /* but limited to what we care about, maybe we don't expect any entity data (hint == 0) */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2398 | if (t->be->url_param_post_limit < hint) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2399 | hint = t->be->url_param_post_limit; |
| 2400 | /* now do we really need to buffer more data? */ |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2401 | if (len < hint) { |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 2402 | buffer_write_dis(req); |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2403 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2404 | } |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2405 | /* else... There are no body bytes to wait for */ |
| 2406 | } |
| 2407 | } |
| 2408 | } |
| 2409 | end_check_maybe_wait_for_body: |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2410 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2411 | /************************************************************* |
| 2412 | * OK, that's finished for the headers. We have done what we * |
| 2413 | * could. Let's switch to the DATA state. * |
| 2414 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2415 | |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2416 | buffer_set_rlim(req, BUFSIZE); /* no more rewrite needed */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2417 | t->logs.tv_request = now; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2418 | |
Willy Tarreau | 1fa3126 | 2007-12-03 00:36:16 +0100 | [diff] [blame] | 2419 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 2420 | * which may be the same as the connect timeout if unspecified. |
| 2421 | * If unset, then set it to zero because we really want it to |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2422 | * eventually expire. We build the tarpit as an analyser. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2423 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2424 | if (txn->flags & TX_CLTARPIT) { |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2425 | buffer_flush(t->req); |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2426 | /* flush the request so that we can drop the connection early |
| 2427 | * if the client closes first. |
| 2428 | */ |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 2429 | buffer_write_dis(req); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2430 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 2431 | req->analyse_exp = tick_add_ifset(now_ms, t->be->timeout.tarpit); |
| 2432 | if (!req->analyse_exp) |
| 2433 | req->analyse_exp = now_ms; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2434 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2435 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2436 | /* OK let's go on with the BODY now */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2437 | goto end_of_headers; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2438 | |
| 2439 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2440 | txn->req.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2441 | txn->status = 400; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2442 | req->analysers = 0; |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 2443 | stream_int_retnclose(req->prod, error_message(t, HTTP_ERR_400)); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 2444 | t->fe->failed_req++; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2445 | return_prx_cond: |
| 2446 | if (!(t->flags & SN_ERR_MASK)) |
| 2447 | t->flags |= SN_ERR_PRXCOND; |
| 2448 | if (!(t->flags & SN_FINST_MASK)) |
| 2449 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2450 | return 0; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2451 | end_of_headers: |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2452 | ; // to keep gcc happy |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2453 | } |
| 2454 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2455 | if (req->analysers & AN_REQ_HTTP_TARPIT) { |
| 2456 | struct http_txn *txn = &t->txn; |
| 2457 | |
| 2458 | /* This connection is being tarpitted. The CLIENT side has |
| 2459 | * already set the connect expiration date to the right |
| 2460 | * timeout. We just have to check that the client is still |
| 2461 | * there and that the timeout has not expired. |
| 2462 | */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 2463 | if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 && |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2464 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 2465 | return 0; |
| 2466 | |
| 2467 | /* We will set the queue timer to the time spent, just for |
| 2468 | * logging purposes. We fake a 500 server error, so that the |
| 2469 | * attacker will not suspect his connection has been tarpitted. |
| 2470 | * It will not cause trouble to the logs because we can exclude |
| 2471 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 2472 | */ |
| 2473 | trace_term(t, TT_HTTP_SRV_2); |
| 2474 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 2475 | |
| 2476 | txn->status = 500; |
| 2477 | if (req->flags != BF_READ_ERROR) |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 2478 | stream_int_retnclose(req->prod, error_message(t, HTTP_ERR_500)); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2479 | |
| 2480 | req->analysers = 0; |
| 2481 | req->analyse_exp = TICK_ETERNITY; |
| 2482 | |
| 2483 | t->fe->failed_req++; |
| 2484 | if (!(t->flags & SN_ERR_MASK)) |
| 2485 | t->flags |= SN_ERR_PRXCOND; |
| 2486 | if (!(t->flags & SN_FINST_MASK)) |
| 2487 | t->flags |= SN_FINST_T; |
| 2488 | return 0; |
| 2489 | } |
| 2490 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2491 | if (req->analysers & AN_REQ_HTTP_BODY) { |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2492 | /* We have to parse the HTTP request body to find any required data. |
| 2493 | * "balance url_param check_post" should have been the only way to get |
| 2494 | * into this. We were brought here after HTTP header analysis, so all |
| 2495 | * related structures are ready. |
| 2496 | */ |
| 2497 | struct http_msg *msg = &t->txn.req; |
| 2498 | unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1; |
| 2499 | long long limit = t->be->url_param_post_limit; |
| 2500 | struct hdr_ctx ctx; |
| 2501 | |
| 2502 | ctx.idx = 0; |
| 2503 | |
| 2504 | /* now if we have a length, we'll take the hint */ |
| 2505 | http_find_header2("Transfer-Encoding", 17, msg->sol, &t->txn.hdr_idx, &ctx); |
| 2506 | if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) { |
| 2507 | unsigned int chunk = 0; |
| 2508 | while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) { |
| 2509 | char c = msg->sol[body]; |
| 2510 | if (ishex(c)) { |
| 2511 | unsigned int hex = toupper(c) - '0'; |
| 2512 | if (hex > 9) |
| 2513 | hex -= 'A' - '9' - 1; |
| 2514 | chunk = (chunk << 4) | hex; |
| 2515 | } else |
| 2516 | break; |
| 2517 | body++; |
| 2518 | } |
| 2519 | if (body + 2 >= req->l) /* we want CRLF too */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2520 | goto http_body_end; /* end of buffer? data missing! */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2521 | |
| 2522 | if (memcmp(msg->sol+body, "\r\n", 2) != 0) |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2523 | goto http_body_end; /* chunked encoding len ends with CRLF, and we don't have it yet */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2524 | |
| 2525 | body += 2; // skip CRLF |
| 2526 | |
| 2527 | /* if we support more then one chunk here, we have to do it again when assigning server |
| 2528 | * 1. how much entity data do we have? new var |
| 2529 | * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here |
| 2530 | * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found) |
| 2531 | */ |
| 2532 | |
| 2533 | if (chunk < limit) |
| 2534 | limit = chunk; /* only reading one chunk */ |
| 2535 | } else { |
| 2536 | if (msg->hdr_content_len < limit) |
| 2537 | limit = msg->hdr_content_len; |
| 2538 | } |
| 2539 | |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2540 | http_body_end: |
| 2541 | /* we leave once we know we have nothing left to do. This means that we have |
| 2542 | * enough bytes, or that we know we'll not get any more (buffer full, read |
| 2543 | * buffer closed). |
| 2544 | */ |
| 2545 | if (req->l - body >= limit || /* enough bytes! */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 2546 | req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR | BF_READ_TIMEOUT) || |
Willy Tarreau | c52164a | 2008-08-17 19:17:57 +0200 | [diff] [blame] | 2547 | tick_is_expired(req->analyse_exp, now_ms)) { |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2548 | /* The situation will not evolve, so let's give up on the analysis. */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2549 | t->logs.tv_request = now; /* update the request timer to reflect full request */ |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2550 | req->analysers &= ~AN_REQ_HTTP_BODY; |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 2551 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2552 | } |
Willy Tarreau | c52164a | 2008-08-17 19:17:57 +0200 | [diff] [blame] | 2553 | else { |
| 2554 | /* Not enough data. We'll re-use the http-request |
| 2555 | * timeout here. Ideally, we should set the timeout |
| 2556 | * relative to the accept() date. We just set the |
| 2557 | * request timeout once at the beginning of the |
| 2558 | * request. |
| 2559 | */ |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 2560 | buffer_write_dis(req); |
Willy Tarreau | c52164a | 2008-08-17 19:17:57 +0200 | [diff] [blame] | 2561 | if (!tick_isset(req->analyse_exp)) |
| 2562 | req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq); |
| 2563 | return 0; |
| 2564 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2565 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2566 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2567 | /* Note: eventhough nobody should set an unknown flag, clearing them right now will |
| 2568 | * probably reduce one day's debugging session. |
| 2569 | */ |
| 2570 | #ifdef DEBUG_DEV |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2571 | if (req->analysers & ~(AN_REQ_INSPECT | AN_REQ_HTTP_HDR | AN_REQ_HTTP_TARPIT | AN_REQ_HTTP_BODY)) { |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2572 | fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n", |
| 2573 | __FILE__, __LINE__, req->analysers); |
| 2574 | ABORT_NOW(); |
| 2575 | } |
| 2576 | #endif |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2577 | req->analysers &= AN_REQ_INSPECT | AN_REQ_HTTP_HDR | AN_REQ_HTTP_TARPIT | AN_REQ_HTTP_BODY; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2578 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2579 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2580 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2581 | /* This function performs all the processing enabled for the current response. |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2582 | * It normally returns zero, but may return 1 if it absolutely needs to be |
| 2583 | * called again after other functions. It relies on buffers flags, and updates |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2584 | * t->rep->analysers. It might make sense to explode it into several other |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2585 | * functions. It works like process_request (see indications above). |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2586 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2587 | int process_response(struct session *t) |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2588 | { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2589 | struct http_txn *txn = &t->txn; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2590 | struct buffer *req = t->req; |
| 2591 | struct buffer *rep = t->rep; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2592 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 2593 | 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] | 2594 | now_ms, __FUNCTION__, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 2595 | t, |
| 2596 | rep, |
| 2597 | rep->rex, rep->wex, |
| 2598 | rep->flags, |
| 2599 | rep->l, |
| 2600 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 2601 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2602 | if (rep->analysers & AN_RTR_HTTP_HDR) { /* receiving server headers */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2603 | /* |
| 2604 | * Now parse the partial (or complete) lines. |
| 2605 | * We will check the response syntax, and also join multi-line |
| 2606 | * headers. An index of all the lines will be elaborated while |
| 2607 | * parsing. |
| 2608 | * |
| 2609 | * For the parsing, we use a 28 states FSM. |
| 2610 | * |
| 2611 | * Here is the information we currently have : |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2612 | * rep->data + rep->som = beginning of response |
| 2613 | * rep->data + rep->eoh = end of processed headers / start of current one |
| 2614 | * rep->data + rep->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2615 | * rep->lr = first non-visited byte |
| 2616 | * rep->r = end of data |
| 2617 | */ |
Willy Tarreau | 7f875f6 | 2008-08-11 17:35:01 +0200 | [diff] [blame] | 2618 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2619 | int cur_idx; |
| 2620 | struct http_msg *msg = &txn->rsp; |
| 2621 | struct proxy *cur_proxy; |
| 2622 | |
| 2623 | if (likely(rep->lr < rep->r)) |
| 2624 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
| 2625 | |
| 2626 | /* 1: we might have to print this header in debug mode */ |
| 2627 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2628 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
| 2629 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
| 2630 | char *eol, *sol; |
| 2631 | |
| 2632 | sol = rep->data + msg->som; |
| 2633 | eol = sol + msg->sl.rq.l; |
| 2634 | debug_hdr("srvrep", t, sol, eol); |
| 2635 | |
| 2636 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2637 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
| 2638 | |
| 2639 | while (cur_idx) { |
| 2640 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2641 | debug_hdr("srvhdr", t, sol, eol); |
| 2642 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2643 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2644 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2645 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2646 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2647 | /* |
| 2648 | * Now we quickly check if we have found a full valid response. |
| 2649 | * If not so, we check the FD and buffer states before leaving. |
| 2650 | * A full response is indicated by the fact that we have seen |
| 2651 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 2652 | * responses are checked first. |
| 2653 | * |
| 2654 | * Depending on whether the client is still there or not, we |
| 2655 | * may send an error response back or not. Note that normally |
| 2656 | * we should only check for HTTP status there, and check I/O |
| 2657 | * errors somewhere else. |
| 2658 | */ |
| 2659 | |
| 2660 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2661 | /* Invalid response */ |
| 2662 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 2663 | hdr_response_bad: |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2664 | //buffer_shutr(rep); |
| 2665 | //buffer_shutw(req); |
| 2666 | //fd_delete(req->cons->fd); |
| 2667 | //req->cons->state = SI_ST_CLO; |
| 2668 | buffer_shutr_now(rep); |
| 2669 | buffer_shutw_now(req); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2670 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2671 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2672 | t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2673 | //sess_change_server(t, NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2674 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2675 | t->be->failed_resp++; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2676 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2677 | txn->status = 502; |
Willy Tarreau | 81acfab | 2008-11-30 19:22:53 +0100 | [diff] [blame] | 2678 | stream_int_return(rep->cons, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2679 | if (!(t->flags & SN_ERR_MASK)) |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2680 | t->flags |= SN_ERR_PRXCOND; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2681 | if (!(t->flags & SN_FINST_MASK)) |
| 2682 | t->flags |= SN_FINST_H; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 2683 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2684 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 2685 | // process_srv_queue(t->srv); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2686 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2687 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2688 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2689 | /* too large response does not fit in buffer. */ |
| 2690 | else if (rep->flags & BF_FULL) { |
| 2691 | goto hdr_response_bad; |
| 2692 | } |
| 2693 | /* read error */ |
| 2694 | else if (rep->flags & BF_READ_ERROR) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2695 | buffer_shutr_now(rep); |
| 2696 | buffer_shutw_now(req); |
| 2697 | //fd_delete(req->cons->fd); |
| 2698 | //req->cons->state = SI_ST_CLO; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2699 | //if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2700 | //t->srv->cur_sess--; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2701 | //t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2702 | //sess_change_server(t, NULL); |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2703 | //} |
| 2704 | //t->be->failed_resp++; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2705 | rep->analysers = 0; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2706 | txn->status = 502; |
Willy Tarreau | 81acfab | 2008-11-30 19:22:53 +0100 | [diff] [blame] | 2707 | stream_int_return(rep->cons, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2708 | if (!(t->flags & SN_ERR_MASK)) |
| 2709 | t->flags |= SN_ERR_SRVCL; |
| 2710 | if (!(t->flags & SN_FINST_MASK)) |
| 2711 | t->flags |= SN_FINST_H; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 2712 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2713 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 2714 | // process_srv_queue(t->srv); |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2715 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2716 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2717 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2718 | /* read timeout : return a 504 to the client. */ |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2719 | else if (rep->flags & BF_READ_TIMEOUT) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2720 | buffer_shutr_now(rep); |
| 2721 | buffer_shutw_now(req); |
| 2722 | //fd_delete(req->cons->fd); |
| 2723 | //req->cons->state = SI_ST_CLO; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2724 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2725 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2726 | t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2727 | //sess_change_server(t, NULL); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2728 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2729 | t->be->failed_resp++; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2730 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2731 | txn->status = 504; |
Willy Tarreau | 81acfab | 2008-11-30 19:22:53 +0100 | [diff] [blame] | 2732 | stream_int_return(rep->cons, error_message(t, HTTP_ERR_504)); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2733 | if (!(t->flags & SN_ERR_MASK)) |
| 2734 | t->flags |= SN_ERR_SRVTO; |
| 2735 | if (!(t->flags & SN_FINST_MASK)) |
| 2736 | t->flags |= SN_FINST_H; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 2737 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2738 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 2739 | // process_srv_queue(t->srv); |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2740 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2741 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2742 | /* write error to client, or close from server */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 2743 | else if (rep->flags & (BF_WRITE_ERROR|BF_SHUTR)) { |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2744 | buffer_shutr_now(rep); |
| 2745 | buffer_shutw_now(req); |
| 2746 | //fd_delete(req->cons->fd); |
| 2747 | //req->cons->state = SI_ST_CLO; |
| 2748 | if (t->srv) { |
| 2749 | //t->srv->cur_sess--; |
| 2750 | t->srv->failed_resp++; |
| 2751 | //sess_change_server(t, NULL); |
| 2752 | } |
| 2753 | t->be->failed_resp++; |
| 2754 | rep->analysers = 0; |
| 2755 | txn->status = 502; |
Willy Tarreau | 81acfab | 2008-11-30 19:22:53 +0100 | [diff] [blame] | 2756 | stream_int_return(rep->cons, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2757 | if (!(t->flags & SN_ERR_MASK)) |
| 2758 | t->flags |= SN_ERR_SRVCL; |
| 2759 | if (!(t->flags & SN_FINST_MASK)) |
| 2760 | t->flags |= SN_FINST_H; |
| 2761 | |
| 2762 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 2763 | // process_srv_queue(t->srv); |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2764 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2765 | return 0; |
| 2766 | } |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 2767 | buffer_write_dis(rep); |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2768 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2769 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2770 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2771 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2772 | /***************************************************************** |
| 2773 | * More interesting part now : we know that we have a complete * |
| 2774 | * response which at least looks like HTTP. We have an indicator * |
| 2775 | * of each header's length, so we can parse them quickly. * |
| 2776 | ****************************************************************/ |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2777 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2778 | rep->analysers &= ~AN_RTR_HTTP_HDR; |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 2779 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2780 | /* ensure we keep this pointer to the beginning of the message */ |
| 2781 | msg->sol = rep->data + msg->som; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2782 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2783 | /* |
| 2784 | * 1: get the status code and check for cacheability. |
| 2785 | */ |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2786 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2787 | t->logs.logwait &= ~LW_RESP; |
| 2788 | txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2789 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2790 | switch (txn->status) { |
| 2791 | case 200: |
| 2792 | case 203: |
| 2793 | case 206: |
| 2794 | case 300: |
| 2795 | case 301: |
| 2796 | case 410: |
| 2797 | /* RFC2616 @13.4: |
| 2798 | * "A response received with a status code of |
| 2799 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 2800 | * by a cache (...) unless a cache-control |
| 2801 | * directive prohibits caching." |
| 2802 | * |
| 2803 | * RFC2616 @9.5: POST method : |
| 2804 | * "Responses to this method are not cacheable, |
| 2805 | * unless the response includes appropriate |
| 2806 | * Cache-Control or Expires header fields." |
| 2807 | */ |
| 2808 | if (likely(txn->meth != HTTP_METH_POST) && |
| 2809 | (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
| 2810 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 2811 | break; |
| 2812 | default: |
| 2813 | break; |
| 2814 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2815 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2816 | /* |
| 2817 | * 2: we may need to capture headers |
| 2818 | */ |
| 2819 | if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap)) |
| 2820 | capture_headers(rep->data + msg->som, &txn->hdr_idx, |
| 2821 | txn->rsp.cap, t->fe->rsp_cap); |
| 2822 | |
| 2823 | /* |
| 2824 | * 3: we will have to evaluate the filters. |
| 2825 | * As opposed to version 1.2, now they will be evaluated in the |
| 2826 | * filters order and not in the header order. This means that |
| 2827 | * each filter has to be validated among all headers. |
| 2828 | * |
| 2829 | * Filters are tried with ->be first, then with ->fe if it is |
| 2830 | * different from ->be. |
| 2831 | */ |
| 2832 | |
| 2833 | t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */ |
| 2834 | |
| 2835 | cur_proxy = t->be; |
| 2836 | while (1) { |
| 2837 | struct proxy *rule_set = cur_proxy; |
| 2838 | |
| 2839 | /* try headers filters */ |
| 2840 | if (rule_set->rsp_exp != NULL) { |
| 2841 | if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) { |
| 2842 | return_bad_resp: |
| 2843 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2844 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2845 | t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2846 | //sess_change_server(t, NULL); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2847 | } |
| 2848 | cur_proxy->failed_resp++; |
| 2849 | return_srv_prx_502: |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2850 | buffer_shutr_now(rep); |
| 2851 | buffer_shutw_now(req); |
| 2852 | //fd_delete(req->cons->fd); |
| 2853 | //req->cons->state = SI_ST_CLO; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2854 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2855 | txn->status = 502; |
Willy Tarreau | 81acfab | 2008-11-30 19:22:53 +0100 | [diff] [blame] | 2856 | stream_int_return(rep->cons, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2857 | if (!(t->flags & SN_ERR_MASK)) |
| 2858 | t->flags |= SN_ERR_PRXCOND; |
| 2859 | if (!(t->flags & SN_FINST_MASK)) |
| 2860 | t->flags |= SN_FINST_H; |
| 2861 | /* We used to have a free connection slot. Since we'll never use it, |
| 2862 | * we have to inform the server that it may be used by another session. |
| 2863 | */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2864 | //if (t->srv && may_dequeue_tasks(t->srv, cur_proxy)) |
| 2865 | // process_srv_queue(t->srv); |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2866 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2867 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2868 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2869 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2870 | /* has the response been denied ? */ |
| 2871 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | f899b94 | 2008-03-28 18:09:38 +0100 | [diff] [blame] | 2872 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2873 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2874 | t->srv->failed_secu++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2875 | //sess_change_server(t, NULL); |
Willy Tarreau | f899b94 | 2008-03-28 18:09:38 +0100 | [diff] [blame] | 2876 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2877 | cur_proxy->denied_resp++; |
| 2878 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 2879 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2880 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2881 | /* We might have to check for "Connection:" */ |
| 2882 | if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) && |
| 2883 | !(t->flags & SN_CONN_CLOSED)) { |
| 2884 | char *cur_ptr, *cur_end, *cur_next; |
| 2885 | int cur_idx, old_idx, delta, val; |
| 2886 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2887 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2888 | cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 2889 | old_idx = 0; |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 2890 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2891 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 2892 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 2893 | cur_ptr = cur_next; |
| 2894 | cur_end = cur_ptr + cur_hdr->len; |
| 2895 | cur_next = cur_end + cur_hdr->cr + 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2896 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2897 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 2898 | if (val) { |
| 2899 | /* 3 possibilities : |
| 2900 | * - we have already set Connection: close, |
| 2901 | * so we remove this line. |
| 2902 | * - we have not yet set Connection: close, |
| 2903 | * but this line indicates close. We leave |
| 2904 | * it untouched and set the flag. |
| 2905 | * - we have not yet set Connection: close, |
| 2906 | * and this line indicates non-close. We |
| 2907 | * replace it. |
| 2908 | */ |
| 2909 | if (t->flags & SN_CONN_CLOSED) { |
| 2910 | delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0); |
| 2911 | txn->rsp.eoh += delta; |
| 2912 | cur_next += delta; |
| 2913 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 2914 | txn->hdr_idx.used--; |
| 2915 | cur_hdr->len = 0; |
| 2916 | } else { |
| 2917 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 2918 | delta = buffer_replace2(rep, cur_ptr + val, cur_end, |
| 2919 | "close", 5); |
| 2920 | cur_next += delta; |
| 2921 | cur_hdr->len += delta; |
| 2922 | txn->rsp.eoh += delta; |
| 2923 | } |
| 2924 | t->flags |= SN_CONN_CLOSED; |
| 2925 | } |
| 2926 | } |
| 2927 | old_idx = cur_idx; |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 2928 | } |
| 2929 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2930 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2931 | /* add response headers from the rule sets in the same order */ |
| 2932 | for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) { |
| 2933 | if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, |
| 2934 | rule_set->rsp_add[cur_idx])) < 0) |
| 2935 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 2936 | } |
| 2937 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2938 | /* check whether we're already working on the frontend */ |
| 2939 | if (cur_proxy == t->fe) |
| 2940 | break; |
| 2941 | cur_proxy = t->fe; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2942 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2943 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2944 | /* |
| 2945 | * 4: check for server cookie. |
| 2946 | */ |
| 2947 | if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name |
| 2948 | || (t->be->options & PR_O_CHK_CACHE)) |
| 2949 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2950 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2951 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2952 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2953 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2954 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2955 | if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0) |
| 2956 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2957 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2958 | /* |
| 2959 | * 6: add server cookie in the response if needed |
| 2960 | */ |
| 2961 | if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) && |
| 2962 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) { |
| 2963 | int len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2964 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2965 | /* the server is known, it's not the one the client requested, we have to |
| 2966 | * insert a set-cookie here, except if we want to insert only on POST |
| 2967 | * requests and this one isn't. Note that servers which don't have cookies |
| 2968 | * (eg: some backup servers) will return a full cookie removal request. |
| 2969 | */ |
| 2970 | len = sprintf(trash, "Set-Cookie: %s=%s; path=/", |
| 2971 | t->be->cookie_name, |
| 2972 | t->srv->cookie ? t->srv->cookie : "; Expires=Thu, 01-Jan-1970 00:00:01 GMT"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2973 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2974 | if (t->be->cookie_domain) |
| 2975 | len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2976 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2977 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 2978 | trash, len)) < 0) |
| 2979 | goto return_bad_resp; |
| 2980 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2981 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2982 | /* Here, we will tell an eventual cache on the client side that we don't |
| 2983 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 2984 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 2985 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 2986 | */ |
| 2987 | if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2988 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2989 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 2990 | |
| 2991 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 2992 | "Cache-control: private", 22)) < 0) |
| 2993 | goto return_bad_resp; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2994 | } |
| 2995 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2996 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2997 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2998 | /* |
| 2999 | * 7: check if result will be cacheable with a cookie. |
| 3000 | * We'll block the response if security checks have caught |
| 3001 | * nasty things such as a cacheable cookie. |
| 3002 | */ |
| 3003 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) == |
| 3004 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) && |
| 3005 | (t->be->options & PR_O_CHK_CACHE)) { |
| 3006 | |
| 3007 | /* we're in presence of a cacheable response containing |
| 3008 | * a set-cookie header. We'll block it as requested by |
| 3009 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3010 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3011 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3012 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3013 | t->srv->failed_secu++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3014 | //sess_change_server(t, NULL); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3015 | } |
| 3016 | t->be->denied_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3017 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3018 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 3019 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 3020 | send_log(t->be, LOG_ALERT, |
| 3021 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 3022 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 3023 | goto return_srv_prx_502; |
| 3024 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3025 | |
| 3026 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3027 | * 8: add "Connection: close" if needed and not yet set. |
| 3028 | * Note that we do not need to add it in case of HTTP/1.0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3029 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3030 | if (!(t->flags & SN_CONN_CLOSED) && |
| 3031 | ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) { |
| 3032 | if ((unlikely(msg->sl.st.v_l != 8) || |
| 3033 | unlikely(req->data[msg->som + 7] != '0')) && |
| 3034 | unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3035 | "Connection: close", 17)) < 0) |
| 3036 | goto return_bad_resp; |
| 3037 | t->flags |= SN_CONN_CLOSED; |
| 3038 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3039 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3040 | /************************************************************* |
| 3041 | * OK, that's finished for the headers. We have done what we * |
| 3042 | * could. Let's switch to the DATA state. * |
| 3043 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3044 | |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 3045 | buffer_set_rlim(rep, BUFSIZE); /* no more rewrite needed */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3046 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3047 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3048 | #ifdef CONFIG_HAP_TCPSPLICE |
| 3049 | if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
| 3050 | /* TCP splicing supported by both FE and BE */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 3051 | tcp_splice_splicefd(rep->cons->fd, rep->prod->fd, 0); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3052 | } |
| 3053 | #endif |
| 3054 | /* if the user wants to log as soon as possible, without counting |
| 3055 | * bytes from the server, then this is the right moment. We have |
| 3056 | * to temporarily assign bytes_out to log what we currently have. |
| 3057 | */ |
| 3058 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 3059 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 3060 | t->logs.bytes_out = txn->rsp.eoh; |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 3061 | t->do_log(t); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3062 | t->logs.bytes_out = 0; |
| 3063 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3064 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3065 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 3066 | * otherwise we would not let the client side wake up. |
| 3067 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3068 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3069 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3070 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3071 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3072 | /* Note: eventhough nobody should set an unknown flag, clearing them right now will |
| 3073 | * probably reduce one day's debugging session. |
| 3074 | */ |
| 3075 | #ifdef DEBUG_DEV |
| 3076 | if (rep->analysers & ~(AN_RTR_HTTP_HDR)) { |
| 3077 | fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n", |
| 3078 | __FILE__, __LINE__, rep->analysers); |
| 3079 | ABORT_NOW(); |
| 3080 | } |
| 3081 | #endif |
| 3082 | rep->analysers &= AN_RTR_HTTP_HDR; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3083 | return 0; |
| 3084 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3085 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3086 | /* |
| 3087 | * Produces data for the session <s> depending on its source. Expects to be |
Willy Tarreau | 1ae3a05 | 2008-08-16 10:56:30 +0200 | [diff] [blame] | 3088 | * called with client socket shut down on input. Right now, only statistics can |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 3089 | * be produced. It stops by itself by unsetting the BF_HIJACK flag from the |
| 3090 | * buffer, which it uses to keep on being called when there is free space in |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3091 | * the buffer, or simply by letting an empty buffer upon return. It returns 1 |
Willy Tarreau | 1ae3a05 | 2008-08-16 10:56:30 +0200 | [diff] [blame] | 3092 | * when it wants to stop sending data, otherwise 0. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3093 | */ |
| 3094 | int produce_content(struct session *s) |
| 3095 | { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3096 | if (s->data_source == DATA_SRC_NONE) { |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 3097 | buffer_stop_hijack(s->rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3098 | return 1; |
| 3099 | } |
| 3100 | else if (s->data_source == DATA_SRC_STATS) { |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3101 | /* dump server statistics */ |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 3102 | int ret = stats_dump_http(s, s->be->uri_auth); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 3103 | if (ret >= 0) |
| 3104 | return ret; |
| 3105 | /* -1 indicates an error */ |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3106 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3107 | |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 3108 | /* unknown data source or internal error */ |
| 3109 | s->txn.status = 500; |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 3110 | stream_int_retnclose(s->rep->cons, error_message(s, HTTP_ERR_500)); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3111 | trace_term(s, TT_HTTP_CNT_1); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 3112 | if (!(s->flags & SN_ERR_MASK)) |
| 3113 | s->flags |= SN_ERR_PRXCOND; |
| 3114 | if (!(s->flags & SN_FINST_MASK)) |
| 3115 | s->flags |= SN_FINST_R; |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 3116 | buffer_stop_hijack(s->rep); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 3117 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3118 | } |
| 3119 | |
| 3120 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3121 | /* Iterate the same filter through all request headers. |
| 3122 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3123 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 3124 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3125 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3126 | 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] | 3127 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3128 | char term; |
| 3129 | char *cur_ptr, *cur_end, *cur_next; |
| 3130 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3131 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3132 | struct hdr_idx_elem *cur_hdr; |
| 3133 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 3134 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3135 | last_hdr = 0; |
| 3136 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3137 | cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3138 | old_idx = 0; |
| 3139 | |
| 3140 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3141 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3142 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3143 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3144 | (exp->action == ACT_ALLOW || |
| 3145 | exp->action == ACT_DENY || |
| 3146 | exp->action == ACT_TARPIT)) |
| 3147 | return 0; |
| 3148 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3149 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3150 | if (!cur_idx) |
| 3151 | break; |
| 3152 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3153 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3154 | cur_ptr = cur_next; |
| 3155 | cur_end = cur_ptr + cur_hdr->len; |
| 3156 | cur_next = cur_end + cur_hdr->cr + 1; |
| 3157 | |
| 3158 | /* Now we have one header between cur_ptr and cur_end, |
| 3159 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3160 | */ |
| 3161 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3162 | /* The annoying part is that pattern matching needs |
| 3163 | * that we modify the contents to null-terminate all |
| 3164 | * strings before testing them. |
| 3165 | */ |
| 3166 | |
| 3167 | term = *cur_end; |
| 3168 | *cur_end = '\0'; |
| 3169 | |
| 3170 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 3171 | switch (exp->action) { |
| 3172 | case ACT_SETBE: |
| 3173 | /* It is not possible to jump a second time. |
| 3174 | * FIXME: should we return an HTTP/500 here so that |
| 3175 | * the admin knows there's a problem ? |
| 3176 | */ |
| 3177 | if (t->be != t->fe) |
| 3178 | break; |
| 3179 | |
| 3180 | /* Swithing Proxy */ |
| 3181 | t->be = (struct proxy *) exp->replace; |
| 3182 | |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3183 | /* right now, the backend switch is not overly complicated |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3184 | * because we have associated req_cap and rsp_cap to the |
| 3185 | * frontend, and the beconn will be updated later. |
| 3186 | */ |
| 3187 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 3188 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 3189 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3190 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3191 | last_hdr = 1; |
| 3192 | break; |
| 3193 | |
| 3194 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3195 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3196 | last_hdr = 1; |
| 3197 | break; |
| 3198 | |
| 3199 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3200 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3201 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3202 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3203 | break; |
| 3204 | |
| 3205 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3206 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3207 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3208 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3209 | break; |
| 3210 | |
| 3211 | case ACT_REPLACE: |
| 3212 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 3213 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 3214 | /* FIXME: if the user adds a newline in the replacement, the |
| 3215 | * index will not be recalculated for now, and the new line |
| 3216 | * will not be counted as a new header. |
| 3217 | */ |
| 3218 | |
| 3219 | cur_end += delta; |
| 3220 | cur_next += delta; |
| 3221 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3222 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3223 | break; |
| 3224 | |
| 3225 | case ACT_REMOVE: |
| 3226 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 3227 | cur_next += delta; |
| 3228 | |
| 3229 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3230 | txn->req.eoh += delta; |
| 3231 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 3232 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3233 | cur_hdr->len = 0; |
| 3234 | cur_end = NULL; /* null-term has been rewritten */ |
| 3235 | break; |
| 3236 | |
| 3237 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3238 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3239 | if (cur_end) |
| 3240 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3241 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3242 | /* keep the link from this header to next one in case of later |
| 3243 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3244 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3245 | old_idx = cur_idx; |
| 3246 | } |
| 3247 | return 0; |
| 3248 | } |
| 3249 | |
| 3250 | |
| 3251 | /* Apply the filter to the request line. |
| 3252 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 3253 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3254 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 3255 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3256 | */ |
| 3257 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 3258 | { |
| 3259 | char term; |
| 3260 | char *cur_ptr, *cur_end; |
| 3261 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3262 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3263 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3264 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3265 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3266 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3267 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3268 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3269 | (exp->action == ACT_ALLOW || |
| 3270 | exp->action == ACT_DENY || |
| 3271 | exp->action == ACT_TARPIT)) |
| 3272 | return 0; |
| 3273 | else if (exp->action == ACT_REMOVE) |
| 3274 | return 0; |
| 3275 | |
| 3276 | done = 0; |
| 3277 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 3278 | cur_ptr = req->data + txn->req.som; /* should be equal to txn->sol */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3279 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3280 | |
| 3281 | /* Now we have the request line between cur_ptr and cur_end */ |
| 3282 | |
| 3283 | /* The annoying part is that pattern matching needs |
| 3284 | * that we modify the contents to null-terminate all |
| 3285 | * strings before testing them. |
| 3286 | */ |
| 3287 | |
| 3288 | term = *cur_end; |
| 3289 | *cur_end = '\0'; |
| 3290 | |
| 3291 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 3292 | switch (exp->action) { |
| 3293 | case ACT_SETBE: |
| 3294 | /* It is not possible to jump a second time. |
| 3295 | * FIXME: should we return an HTTP/500 here so that |
| 3296 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3297 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3298 | if (t->be != t->fe) |
| 3299 | break; |
| 3300 | |
| 3301 | /* Swithing Proxy */ |
| 3302 | t->be = (struct proxy *) exp->replace; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3303 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3304 | /* right now, the backend switch is not too much complicated |
| 3305 | * because we have associated req_cap and rsp_cap to the |
| 3306 | * frontend, and the beconn will be updated later. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3307 | */ |
| 3308 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 3309 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 3310 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3311 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3312 | done = 1; |
| 3313 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3314 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3315 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3316 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3317 | done = 1; |
| 3318 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3319 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3320 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3321 | txn->flags |= TX_CLDENY; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3322 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3323 | done = 1; |
| 3324 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3325 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3326 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3327 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3328 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3329 | done = 1; |
| 3330 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3331 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3332 | case ACT_REPLACE: |
| 3333 | *cur_end = term; /* restore the string terminator */ |
| 3334 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 3335 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 3336 | /* FIXME: if the user adds a newline in the replacement, the |
| 3337 | * index will not be recalculated for now, and the new line |
| 3338 | * will not be counted as a new header. |
| 3339 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3340 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3341 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3342 | cur_end += delta; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3343 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 3344 | txn->req.sol = req->data + txn->req.som; /* should be equal to txn->sol */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3345 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3346 | HTTP_MSG_RQMETH, |
| 3347 | cur_ptr, cur_end + 1, |
| 3348 | NULL, NULL); |
| 3349 | if (unlikely(!cur_end)) |
| 3350 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3351 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3352 | /* we have a full request and we know that we have either a CR |
| 3353 | * or an LF at <ptr>. |
| 3354 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3355 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 3356 | 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] | 3357 | /* there is no point trying this regex on headers */ |
| 3358 | return 1; |
| 3359 | } |
| 3360 | } |
| 3361 | *cur_end = term; /* restore the string terminator */ |
| 3362 | return done; |
| 3363 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 3364 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3365 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3366 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3367 | /* |
| 3368 | * Apply all the req filters <exp> to all headers in buffer <req> of session <t>. |
| 3369 | * 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] | 3370 | * unparsable request. Since it can manage the switch to another backend, it |
| 3371 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3372 | */ |
| 3373 | int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 3374 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3375 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3376 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3377 | while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3378 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3379 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3380 | /* |
| 3381 | * The interleaving of transformations and verdicts |
| 3382 | * makes it difficult to decide to continue or stop |
| 3383 | * the evaluation. |
| 3384 | */ |
| 3385 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3386 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3387 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 3388 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) { |
| 3389 | exp = exp->next; |
| 3390 | continue; |
| 3391 | } |
| 3392 | |
| 3393 | /* Apply the filter to the request line. */ |
| 3394 | ret = apply_filter_to_req_line(t, req, exp); |
| 3395 | if (unlikely(ret < 0)) |
| 3396 | return -1; |
| 3397 | |
| 3398 | if (likely(ret == 0)) { |
| 3399 | /* The filter did not match the request, it can be |
| 3400 | * iterated through all headers. |
| 3401 | */ |
| 3402 | apply_filter_to_req_headers(t, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3403 | } |
| 3404 | exp = exp->next; |
| 3405 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3406 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3407 | } |
| 3408 | |
| 3409 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3410 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3411 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 3412 | * Manage client-side cookie. It can impact performance by about 2% so it is |
| 3413 | * desirable to call it only when needed. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3414 | */ |
| 3415 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 3416 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3417 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3418 | char *p1, *p2, *p3, *p4; |
| 3419 | char *del_colon, *del_cookie, *colon; |
| 3420 | int app_cookies; |
| 3421 | |
| 3422 | appsess *asession_temp = NULL; |
| 3423 | appsess local_asession; |
| 3424 | |
| 3425 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3426 | int cur_idx, old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3427 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3428 | /* Iterate through the headers. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3429 | * we start with the start line. |
| 3430 | */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 3431 | old_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3432 | cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3433 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3434 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3435 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 3436 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3437 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3438 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3439 | cur_ptr = cur_next; |
| 3440 | cur_end = cur_ptr + cur_hdr->len; |
| 3441 | cur_next = cur_end + cur_hdr->cr + 1; |
| 3442 | |
| 3443 | /* We have one full header between cur_ptr and cur_end, and the |
| 3444 | * next header starts at cur_next. We're only interested in |
| 3445 | * "Cookie:" headers. |
| 3446 | */ |
| 3447 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 3448 | val = http_header_match2(cur_ptr, cur_end, "Cookie", 6); |
| 3449 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3450 | old_idx = cur_idx; |
| 3451 | continue; |
| 3452 | } |
| 3453 | |
| 3454 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 3455 | * attributes whose name begin with a '$', and associate them with |
| 3456 | * the right cookie, if we want to delete this cookie. |
| 3457 | * So there are 3 cases for each cookie read : |
| 3458 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 3459 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 3460 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 3461 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 3462 | * "special" cookie. |
| 3463 | * At the end of loop, if a "special" cookie remains, we may have to |
| 3464 | * remove it. If no application cookie persists in the header, we |
| 3465 | * *MUST* delete it |
| 3466 | */ |
| 3467 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 3468 | colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3469 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3470 | /* del_cookie == NULL => nothing to be deleted */ |
| 3471 | del_colon = del_cookie = NULL; |
| 3472 | app_cookies = 0; |
| 3473 | |
| 3474 | while (p1 < cur_end) { |
| 3475 | /* skip spaces and colons, but keep an eye on these ones */ |
| 3476 | while (p1 < cur_end) { |
| 3477 | if (*p1 == ';' || *p1 == ',') |
| 3478 | colon = p1; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 3479 | else if (!isspace((unsigned char)*p1)) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3480 | break; |
| 3481 | p1++; |
| 3482 | } |
| 3483 | |
| 3484 | if (p1 == cur_end) |
| 3485 | break; |
| 3486 | |
| 3487 | /* p1 is at the beginning of the cookie name */ |
| 3488 | p2 = p1; |
| 3489 | while (p2 < cur_end && *p2 != '=') |
| 3490 | p2++; |
| 3491 | |
| 3492 | if (p2 == cur_end) |
| 3493 | break; |
| 3494 | |
| 3495 | p3 = p2 + 1; /* skips the '=' sign */ |
| 3496 | if (p3 == cur_end) |
| 3497 | break; |
| 3498 | |
| 3499 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 3500 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3501 | p4++; |
| 3502 | |
| 3503 | /* here, we have the cookie name between p1 and p2, |
| 3504 | * and its value between p3 and p4. |
| 3505 | * we can process it : |
| 3506 | * |
| 3507 | * Cookie: NAME=VALUE; |
| 3508 | * | || || | |
| 3509 | * | || || +--> p4 |
| 3510 | * | || |+-------> p3 |
| 3511 | * | || +--------> p2 |
| 3512 | * | |+------------> p1 |
| 3513 | * | +-------------> colon |
| 3514 | * +--------------------> cur_ptr |
| 3515 | */ |
| 3516 | |
| 3517 | if (*p1 == '$') { |
| 3518 | /* skip this one */ |
| 3519 | } |
| 3520 | else { |
| 3521 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3522 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 3523 | txn->cli_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3524 | (p4 - p1 >= t->fe->capture_namelen) && |
| 3525 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3526 | int log_len = p4 - p1; |
| 3527 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 3528 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3529 | Alert("HTTP logging : out of memory.\n"); |
| 3530 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3531 | if (log_len > t->fe->capture_len) |
| 3532 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 3533 | memcpy(txn->cli_cookie, p1, log_len); |
| 3534 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3535 | } |
| 3536 | } |
| 3537 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3538 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 3539 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3540 | /* Cool... it's the right one */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3541 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3542 | char *delim; |
| 3543 | |
| 3544 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 3545 | * have the server ID betweek p3 and delim, and the original cookie between |
| 3546 | * delim+1 and p4. Otherwise, delim==p4 : |
| 3547 | * |
| 3548 | * Cookie: NAME=SRV~VALUE; |
| 3549 | * | || || | | |
| 3550 | * | || || | +--> p4 |
| 3551 | * | || || +--------> delim |
| 3552 | * | || |+-----------> p3 |
| 3553 | * | || +------------> p2 |
| 3554 | * | |+----------------> p1 |
| 3555 | * | +-----------------> colon |
| 3556 | * +------------------------> cur_ptr |
| 3557 | */ |
| 3558 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3559 | if (t->be->options & PR_O_COOK_PFX) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3560 | for (delim = p3; delim < p4; delim++) |
| 3561 | if (*delim == COOKIE_DELIM) |
| 3562 | break; |
| 3563 | } |
| 3564 | else |
| 3565 | delim = p4; |
| 3566 | |
| 3567 | |
| 3568 | /* Here, we'll look for the first running server which supports the cookie. |
| 3569 | * This allows to share a same cookie between several servers, for example |
| 3570 | * to dedicate backup servers to specific servers only. |
| 3571 | * However, to prevent clients from sticking to cookie-less backup server |
| 3572 | * when they have incidentely learned an empty cookie, we simply ignore |
| 3573 | * empty cookies and mark them as invalid. |
| 3574 | */ |
| 3575 | if (delim == p3) |
| 3576 | srv = NULL; |
| 3577 | |
| 3578 | while (srv) { |
Willy Tarreau | 92f2ab1 | 2007-02-02 22:14:47 +0100 | [diff] [blame] | 3579 | if (srv->cookie && (srv->cklen == delim - p3) && |
| 3580 | !memcmp(p3, srv->cookie, delim - p3)) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3581 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3582 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3583 | txn->flags &= ~TX_CK_MASK; |
| 3584 | txn->flags |= TX_CK_VALID; |
| 3585 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3586 | t->srv = srv; |
| 3587 | break; |
| 3588 | } else { |
| 3589 | /* we found a server, but it's down */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3590 | txn->flags &= ~TX_CK_MASK; |
| 3591 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3592 | } |
| 3593 | } |
| 3594 | srv = srv->next; |
| 3595 | } |
| 3596 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3597 | if (!srv && !(txn->flags & TX_CK_DOWN)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3598 | /* no server matched this cookie */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3599 | txn->flags &= ~TX_CK_MASK; |
| 3600 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3601 | } |
| 3602 | |
| 3603 | /* depending on the cookie mode, we may have to either : |
| 3604 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 3605 | * the server never sees it ; |
| 3606 | * - remove the server id from the cookie value, and tag the cookie as an |
| 3607 | * application cookie so that it does not get accidentely removed later, |
| 3608 | * if we're in cookie prefix mode |
| 3609 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3610 | if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3611 | int delta; /* negative */ |
| 3612 | |
| 3613 | delta = buffer_replace2(req, p3, delim + 1, NULL, 0); |
| 3614 | p4 += delta; |
| 3615 | cur_end += delta; |
| 3616 | cur_next += delta; |
| 3617 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3618 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3619 | |
| 3620 | del_cookie = del_colon = NULL; |
| 3621 | app_cookies++; /* protect the header from deletion */ |
| 3622 | } |
| 3623 | else if (del_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3624 | (t->be->options & (PR_O_COOK_INS | PR_O_COOK_IND)) == (PR_O_COOK_INS | PR_O_COOK_IND)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3625 | del_cookie = p1; |
| 3626 | del_colon = colon; |
| 3627 | } |
| 3628 | } else { |
| 3629 | /* now we know that we must keep this cookie since it's |
| 3630 | * not ours. But if we wanted to delete our cookie |
| 3631 | * earlier, we cannot remove the complete header, but we |
| 3632 | * can remove the previous block itself. |
| 3633 | */ |
| 3634 | app_cookies++; |
| 3635 | |
| 3636 | if (del_cookie != NULL) { |
| 3637 | int delta; /* negative */ |
| 3638 | |
| 3639 | delta = buffer_replace2(req, del_cookie, p1, NULL, 0); |
| 3640 | p4 += delta; |
| 3641 | cur_end += delta; |
| 3642 | cur_next += delta; |
| 3643 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3644 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3645 | del_cookie = del_colon = NULL; |
| 3646 | } |
| 3647 | } |
| 3648 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3649 | if ((t->be->appsession_name != NULL) && |
| 3650 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3651 | /* first, let's see if the cookie is our appcookie*/ |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 3652 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3653 | /* Cool... it's the right one */ |
| 3654 | |
| 3655 | asession_temp = &local_asession; |
| 3656 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 3657 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3658 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 3659 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 3660 | return; |
| 3661 | } |
| 3662 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3663 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 3664 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3665 | asession_temp->serverid = NULL; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 3666 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3667 | /* only do insert, if lookup fails */ |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 3668 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 3669 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 3670 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3671 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 3672 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3673 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 3674 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 3675 | return; |
| 3676 | } |
| 3677 | |
| 3678 | asession_temp->sessid = local_asession.sessid; |
| 3679 | asession_temp->serverid = local_asession.serverid; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 3680 | asession_temp->request_count = 0; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 3681 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3682 | } else { |
| 3683 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 3684 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3685 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3686 | if (asession_temp->serverid == NULL) { |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 3687 | /* TODO redispatch request */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3688 | Alert("Found Application Session without matching server.\n"); |
| 3689 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3690 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3691 | while (srv) { |
| 3692 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3693 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3694 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3695 | txn->flags &= ~TX_CK_MASK; |
| 3696 | txn->flags |= TX_CK_VALID; |
| 3697 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3698 | t->srv = srv; |
| 3699 | break; |
| 3700 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3701 | txn->flags &= ~TX_CK_MASK; |
| 3702 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3703 | } |
| 3704 | } |
| 3705 | srv = srv->next; |
| 3706 | }/* end while(srv) */ |
| 3707 | }/* end else if server == NULL */ |
| 3708 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3709 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 3710 | asession_temp->request_count++; |
| 3711 | #if defined(DEBUG_HASH) |
| 3712 | Alert("manage_client_side_cookies\n"); |
| 3713 | appsession_hash_dump(&(t->be->htbl_proxy)); |
| 3714 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3715 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 3716 | } |
| 3717 | |
| 3718 | /* we'll have to look for another cookie ... */ |
| 3719 | p1 = p4; |
| 3720 | } /* while (p1 < cur_end) */ |
| 3721 | |
| 3722 | /* There's no more cookie on this line. |
| 3723 | * We may have marked the last one(s) for deletion. |
| 3724 | * We must do this now in two ways : |
| 3725 | * - if there is no app cookie, we simply delete the header ; |
| 3726 | * - if there are app cookies, we must delete the end of the |
| 3727 | * string properly, including the colon/semi-colon before |
| 3728 | * the cookie name. |
| 3729 | */ |
| 3730 | if (del_cookie != NULL) { |
| 3731 | int delta; |
| 3732 | if (app_cookies) { |
| 3733 | delta = buffer_replace2(req, del_colon, cur_end, NULL, 0); |
| 3734 | cur_end = del_colon; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3735 | cur_hdr->len += delta; |
| 3736 | } else { |
| 3737 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3738 | |
| 3739 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3740 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 3741 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3742 | cur_hdr->len = 0; |
| 3743 | } |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 3744 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3745 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3746 | } |
| 3747 | |
| 3748 | /* keep the link from this header to next one */ |
| 3749 | old_idx = cur_idx; |
| 3750 | } /* end of cookie processing on this header */ |
| 3751 | } |
| 3752 | |
| 3753 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3754 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 3755 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 3756 | */ |
| 3757 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 3758 | { |
| 3759 | char term; |
| 3760 | char *cur_ptr, *cur_end, *cur_next; |
| 3761 | int cur_idx, old_idx, last_hdr; |
| 3762 | struct http_txn *txn = &t->txn; |
| 3763 | struct hdr_idx_elem *cur_hdr; |
| 3764 | int len, delta; |
| 3765 | |
| 3766 | last_hdr = 0; |
| 3767 | |
| 3768 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 3769 | old_idx = 0; |
| 3770 | |
| 3771 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3772 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3773 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3774 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3775 | (exp->action == ACT_ALLOW || |
| 3776 | exp->action == ACT_DENY)) |
| 3777 | return 0; |
| 3778 | |
| 3779 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 3780 | if (!cur_idx) |
| 3781 | break; |
| 3782 | |
| 3783 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 3784 | cur_ptr = cur_next; |
| 3785 | cur_end = cur_ptr + cur_hdr->len; |
| 3786 | cur_next = cur_end + cur_hdr->cr + 1; |
| 3787 | |
| 3788 | /* Now we have one header between cur_ptr and cur_end, |
| 3789 | * and the next header starts at cur_next. |
| 3790 | */ |
| 3791 | |
| 3792 | /* The annoying part is that pattern matching needs |
| 3793 | * that we modify the contents to null-terminate all |
| 3794 | * strings before testing them. |
| 3795 | */ |
| 3796 | |
| 3797 | term = *cur_end; |
| 3798 | *cur_end = '\0'; |
| 3799 | |
| 3800 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 3801 | switch (exp->action) { |
| 3802 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3803 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3804 | last_hdr = 1; |
| 3805 | break; |
| 3806 | |
| 3807 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3808 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3809 | last_hdr = 1; |
| 3810 | break; |
| 3811 | |
| 3812 | case ACT_REPLACE: |
| 3813 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 3814 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 3815 | /* FIXME: if the user adds a newline in the replacement, the |
| 3816 | * index will not be recalculated for now, and the new line |
| 3817 | * will not be counted as a new header. |
| 3818 | */ |
| 3819 | |
| 3820 | cur_end += delta; |
| 3821 | cur_next += delta; |
| 3822 | cur_hdr->len += delta; |
| 3823 | txn->rsp.eoh += delta; |
| 3824 | break; |
| 3825 | |
| 3826 | case ACT_REMOVE: |
| 3827 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 3828 | cur_next += delta; |
| 3829 | |
| 3830 | /* FIXME: this should be a separate function */ |
| 3831 | txn->rsp.eoh += delta; |
| 3832 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 3833 | txn->hdr_idx.used--; |
| 3834 | cur_hdr->len = 0; |
| 3835 | cur_end = NULL; /* null-term has been rewritten */ |
| 3836 | break; |
| 3837 | |
| 3838 | } |
| 3839 | } |
| 3840 | if (cur_end) |
| 3841 | *cur_end = term; /* restore the string terminator */ |
| 3842 | |
| 3843 | /* keep the link from this header to next one in case of later |
| 3844 | * removal of next header. |
| 3845 | */ |
| 3846 | old_idx = cur_idx; |
| 3847 | } |
| 3848 | return 0; |
| 3849 | } |
| 3850 | |
| 3851 | |
| 3852 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 3853 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 3854 | * or -1 if a replacement resulted in an invalid status line. |
| 3855 | */ |
| 3856 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 3857 | { |
| 3858 | char term; |
| 3859 | char *cur_ptr, *cur_end; |
| 3860 | int done; |
| 3861 | struct http_txn *txn = &t->txn; |
| 3862 | int len, delta; |
| 3863 | |
| 3864 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3865 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3866 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3867 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3868 | (exp->action == ACT_ALLOW || |
| 3869 | exp->action == ACT_DENY)) |
| 3870 | return 0; |
| 3871 | else if (exp->action == ACT_REMOVE) |
| 3872 | return 0; |
| 3873 | |
| 3874 | done = 0; |
| 3875 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 3876 | cur_ptr = rtr->data + txn->rsp.som; /* should be equal to txn->sol */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3877 | cur_end = cur_ptr + txn->rsp.sl.rq.l; |
| 3878 | |
| 3879 | /* Now we have the status line between cur_ptr and cur_end */ |
| 3880 | |
| 3881 | /* The annoying part is that pattern matching needs |
| 3882 | * that we modify the contents to null-terminate all |
| 3883 | * strings before testing them. |
| 3884 | */ |
| 3885 | |
| 3886 | term = *cur_end; |
| 3887 | *cur_end = '\0'; |
| 3888 | |
| 3889 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 3890 | switch (exp->action) { |
| 3891 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3892 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3893 | done = 1; |
| 3894 | break; |
| 3895 | |
| 3896 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3897 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3898 | done = 1; |
| 3899 | break; |
| 3900 | |
| 3901 | case ACT_REPLACE: |
| 3902 | *cur_end = term; /* restore the string terminator */ |
| 3903 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 3904 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 3905 | /* FIXME: if the user adds a newline in the replacement, the |
| 3906 | * index will not be recalculated for now, and the new line |
| 3907 | * will not be counted as a new header. |
| 3908 | */ |
| 3909 | |
| 3910 | txn->rsp.eoh += delta; |
| 3911 | cur_end += delta; |
| 3912 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 3913 | txn->rsp.sol = rtr->data + txn->rsp.som; /* should be equal to txn->sol */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3914 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 3915 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3916 | cur_ptr, cur_end + 1, |
| 3917 | NULL, NULL); |
| 3918 | if (unlikely(!cur_end)) |
| 3919 | return -1; |
| 3920 | |
| 3921 | /* we have a full respnse and we know that we have either a CR |
| 3922 | * or an LF at <ptr>. |
| 3923 | */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 3924 | txn->status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3925 | hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r'); |
| 3926 | /* there is no point trying this regex on headers */ |
| 3927 | return 1; |
| 3928 | } |
| 3929 | } |
| 3930 | *cur_end = term; /* restore the string terminator */ |
| 3931 | return done; |
| 3932 | } |
| 3933 | |
| 3934 | |
| 3935 | |
| 3936 | /* |
| 3937 | * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>. |
| 3938 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 3939 | * unparsable response. |
| 3940 | */ |
| 3941 | int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 3942 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3943 | struct http_txn *txn = &t->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3944 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3945 | while (exp && !(txn->flags & TX_SVDENY)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3946 | int ret; |
| 3947 | |
| 3948 | /* |
| 3949 | * The interleaving of transformations and verdicts |
| 3950 | * makes it difficult to decide to continue or stop |
| 3951 | * the evaluation. |
| 3952 | */ |
| 3953 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3954 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3955 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 3956 | exp->action == ACT_PASS)) { |
| 3957 | exp = exp->next; |
| 3958 | continue; |
| 3959 | } |
| 3960 | |
| 3961 | /* Apply the filter to the status line. */ |
| 3962 | ret = apply_filter_to_sts_line(t, rtr, exp); |
| 3963 | if (unlikely(ret < 0)) |
| 3964 | return -1; |
| 3965 | |
| 3966 | if (likely(ret == 0)) { |
| 3967 | /* The filter did not match the response, it can be |
| 3968 | * iterated through all headers. |
| 3969 | */ |
| 3970 | apply_filter_to_resp_headers(t, rtr, exp); |
| 3971 | } |
| 3972 | exp = exp->next; |
| 3973 | } |
| 3974 | return 0; |
| 3975 | } |
| 3976 | |
| 3977 | |
| 3978 | |
| 3979 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 3980 | * Manage server-side cookies. It can impact performance by about 2% so it is |
| 3981 | * desirable to call it only when needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3982 | */ |
| 3983 | void manage_server_side_cookies(struct session *t, struct buffer *rtr) |
| 3984 | { |
| 3985 | struct http_txn *txn = &t->txn; |
| 3986 | char *p1, *p2, *p3, *p4; |
| 3987 | |
| 3988 | appsess *asession_temp = NULL; |
| 3989 | appsess local_asession; |
| 3990 | |
| 3991 | char *cur_ptr, *cur_end, *cur_next; |
| 3992 | int cur_idx, old_idx, delta; |
| 3993 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3994 | /* Iterate through the headers. |
| 3995 | * we start with the start line. |
| 3996 | */ |
| 3997 | old_idx = 0; |
| 3998 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 3999 | |
| 4000 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 4001 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4002 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4003 | |
| 4004 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4005 | cur_ptr = cur_next; |
| 4006 | cur_end = cur_ptr + cur_hdr->len; |
| 4007 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4008 | |
| 4009 | /* We have one full header between cur_ptr and cur_end, and the |
| 4010 | * next header starts at cur_next. We're only interested in |
| 4011 | * "Cookie:" headers. |
| 4012 | */ |
| 4013 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4014 | val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10); |
| 4015 | if (!val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4016 | old_idx = cur_idx; |
| 4017 | continue; |
| 4018 | } |
| 4019 | |
| 4020 | /* OK, right now we know we have a set-cookie at cur_ptr */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4021 | txn->flags |= TX_SCK_ANY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4022 | |
| 4023 | |
| 4024 | /* maybe we only wanted to see if there was a set-cookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4025 | if (t->be->cookie_name == NULL && |
| 4026 | t->be->appsession_name == NULL && |
| 4027 | t->be->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4028 | return; |
| 4029 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4030 | p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4031 | |
| 4032 | while (p1 < cur_end) { /* in fact, we'll break after the first cookie */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4033 | if (p1 == cur_end || *p1 == ';') /* end of cookie */ |
| 4034 | break; |
| 4035 | |
| 4036 | /* p1 is at the beginning of the cookie name */ |
| 4037 | p2 = p1; |
| 4038 | |
| 4039 | while (p2 < cur_end && *p2 != '=' && *p2 != ';') |
| 4040 | p2++; |
| 4041 | |
| 4042 | if (p2 == cur_end || *p2 == ';') /* next cookie */ |
| 4043 | break; |
| 4044 | |
| 4045 | p3 = p2 + 1; /* skip the '=' sign */ |
| 4046 | if (p3 == cur_end) |
| 4047 | break; |
| 4048 | |
| 4049 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4050 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';') |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4051 | p4++; |
| 4052 | |
| 4053 | /* here, we have the cookie name between p1 and p2, |
| 4054 | * and its value between p3 and p4. |
| 4055 | * we can process it. |
| 4056 | */ |
| 4057 | |
| 4058 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4059 | if (t->be->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4060 | txn->srv_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4061 | (p4 - p1 >= t->be->capture_namelen) && |
| 4062 | memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4063 | int log_len = p4 - p1; |
| 4064 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 4065 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4066 | Alert("HTTP logging : out of memory.\n"); |
| 4067 | } |
| 4068 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4069 | if (log_len > t->be->capture_len) |
| 4070 | log_len = t->be->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4071 | memcpy(txn->srv_cookie, p1, log_len); |
| 4072 | txn->srv_cookie[log_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4073 | } |
| 4074 | |
| 4075 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4076 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 4077 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4078 | /* Cool... it's the right one */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4079 | txn->flags |= TX_SCK_SEEN; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4080 | |
| 4081 | /* If the cookie is in insert mode on a known server, we'll delete |
| 4082 | * this occurrence because we'll insert another one later. |
| 4083 | * We'll delete it too if the "indirect" option is set and we're in |
| 4084 | * a direct access. */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4085 | if (((t->srv) && (t->be->options & PR_O_COOK_INS)) || |
| 4086 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4087 | /* this header must be deleted */ |
| 4088 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 4089 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4090 | txn->hdr_idx.used--; |
| 4091 | cur_hdr->len = 0; |
| 4092 | cur_next += delta; |
| 4093 | txn->rsp.eoh += delta; |
| 4094 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4095 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4096 | } |
| 4097 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4098 | (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4099 | /* replace bytes p3->p4 with the cookie name associated |
| 4100 | * with this server since we know it. |
| 4101 | */ |
| 4102 | delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen); |
| 4103 | cur_hdr->len += delta; |
| 4104 | cur_next += delta; |
| 4105 | txn->rsp.eoh += delta; |
| 4106 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4107 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4108 | } |
| 4109 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4110 | (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4111 | /* insert the cookie name associated with this server |
| 4112 | * before existing cookie, and insert a delimitor between them.. |
| 4113 | */ |
| 4114 | delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1); |
| 4115 | cur_hdr->len += delta; |
| 4116 | cur_next += delta; |
| 4117 | txn->rsp.eoh += delta; |
| 4118 | |
| 4119 | p3[t->srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4120 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4121 | } |
| 4122 | } |
| 4123 | /* next, let's see if the cookie is our appcookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4124 | else if ((t->be->appsession_name != NULL) && |
| 4125 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4126 | |
| 4127 | /* Cool... it's the right one */ |
| 4128 | |
| 4129 | size_t server_id_len = strlen(t->srv->id) + 1; |
| 4130 | asession_temp = &local_asession; |
| 4131 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4132 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4133 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4134 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4135 | return; |
| 4136 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4137 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 4138 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4139 | asession_temp->serverid = NULL; |
| 4140 | |
| 4141 | /* only do insert, if lookup fails */ |
Ryan Warnick | 6d0b1fa | 2008-02-17 11:24:35 +0100 | [diff] [blame] | 4142 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 4143 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4144 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4145 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 4146 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 4147 | return; |
| 4148 | } |
| 4149 | asession_temp->sessid = local_asession.sessid; |
| 4150 | asession_temp->serverid = local_asession.serverid; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4151 | asession_temp->request_count = 0; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4152 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
| 4153 | } else { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4154 | /* free wasted memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4155 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4156 | } |
| 4157 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4158 | if (asession_temp->serverid == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4159 | if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4160 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4161 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4162 | return; |
| 4163 | } |
| 4164 | asession_temp->serverid[0] = '\0'; |
| 4165 | } |
| 4166 | |
| 4167 | if (asession_temp->serverid[0] == '\0') |
| 4168 | memcpy(asession_temp->serverid, t->srv->id, server_id_len); |
| 4169 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 4170 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4171 | asession_temp->request_count++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4172 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4173 | Alert("manage_server_side_cookies\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4174 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4175 | #endif |
| 4176 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 4177 | break; /* we don't want to loop again since there cannot be another cookie on the same line */ |
| 4178 | } /* we're now at the end of the cookie value */ |
| 4179 | |
| 4180 | /* keep the link from this header to next one */ |
| 4181 | old_idx = cur_idx; |
| 4182 | } /* end of cookie processing on this header */ |
| 4183 | } |
| 4184 | |
| 4185 | |
| 4186 | |
| 4187 | /* |
| 4188 | * Check if response is cacheable or not. Updates t->flags. |
| 4189 | */ |
| 4190 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 4191 | { |
| 4192 | struct http_txn *txn = &t->txn; |
| 4193 | char *p1, *p2; |
| 4194 | |
| 4195 | char *cur_ptr, *cur_end, *cur_next; |
| 4196 | int cur_idx; |
| 4197 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 4198 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4199 | return; |
| 4200 | |
| 4201 | /* Iterate through the headers. |
| 4202 | * we start with the start line. |
| 4203 | */ |
| 4204 | cur_idx = 0; |
| 4205 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4206 | |
| 4207 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 4208 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4209 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4210 | |
| 4211 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4212 | cur_ptr = cur_next; |
| 4213 | cur_end = cur_ptr + cur_hdr->len; |
| 4214 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4215 | |
| 4216 | /* We have one full header between cur_ptr and cur_end, and the |
| 4217 | * next header starts at cur_next. We're only interested in |
| 4218 | * "Cookie:" headers. |
| 4219 | */ |
| 4220 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4221 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 4222 | if (val) { |
| 4223 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 4224 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 4225 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 4226 | return; |
| 4227 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4228 | } |
| 4229 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4230 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 4231 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4232 | continue; |
| 4233 | |
| 4234 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 4235 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4236 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4237 | |
| 4238 | if (p1 >= cur_end) /* no more info */ |
| 4239 | continue; |
| 4240 | |
| 4241 | /* p1 is at the beginning of the value */ |
| 4242 | p2 = p1; |
| 4243 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4244 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4245 | p2++; |
| 4246 | |
| 4247 | /* we have a complete value between p1 and p2 */ |
| 4248 | if (p2 < cur_end && *p2 == '=') { |
| 4249 | /* we have something of the form no-cache="set-cookie" */ |
| 4250 | if ((cur_end - p1 >= 21) && |
| 4251 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 4252 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4253 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4254 | continue; |
| 4255 | } |
| 4256 | |
| 4257 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 4258 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 4259 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 4260 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 4261 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4262 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4263 | return; |
| 4264 | } |
| 4265 | |
| 4266 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4267 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4268 | continue; |
| 4269 | } |
| 4270 | } |
| 4271 | } |
| 4272 | |
| 4273 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4274 | /* |
| 4275 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 4276 | * If the server is found, it's assigned to the session. |
| 4277 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4278 | 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] | 4279 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4280 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4281 | appsess *asession_temp = NULL; |
| 4282 | appsess local_asession; |
| 4283 | char *request_line; |
| 4284 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4285 | if (t->be->appsession_name == NULL || |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 4286 | (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) || |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4287 | (request_line = memchr(begin, ';', len)) == NULL || |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4288 | ((1 + t->be->appsession_name_len + 1 + t->be->appsession_len) > (begin + len - request_line))) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4289 | return; |
| 4290 | |
| 4291 | /* skip ';' */ |
| 4292 | request_line++; |
| 4293 | |
| 4294 | /* look if we have a jsessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4295 | if (strncasecmp(request_line, t->be->appsession_name, t->be->appsession_name_len) != 0) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4296 | return; |
| 4297 | |
| 4298 | /* skip jsessionid= */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4299 | request_line += t->be->appsession_name_len + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4300 | |
| 4301 | /* First try if we already have an appsession */ |
| 4302 | asession_temp = &local_asession; |
| 4303 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4304 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4305 | Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n"); |
| 4306 | send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n"); |
| 4307 | return; |
| 4308 | } |
| 4309 | |
| 4310 | /* Copy the sessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4311 | memcpy(asession_temp->sessid, request_line, t->be->appsession_len); |
| 4312 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4313 | asession_temp->serverid = NULL; |
| 4314 | |
| 4315 | /* only do insert, if lookup fails */ |
Ryan Warnick | 6d0b1fa | 2008-02-17 11:24:35 +0100 | [diff] [blame] | 4316 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 4317 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4318 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4319 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4320 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4321 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 4322 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 4323 | return; |
| 4324 | } |
| 4325 | asession_temp->sessid = local_asession.sessid; |
| 4326 | asession_temp->serverid = local_asession.serverid; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4327 | asession_temp->request_count=0; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4328 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4329 | } |
| 4330 | else { |
| 4331 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4332 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4333 | } |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4334 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 4335 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4336 | asession_temp->request_count++; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4337 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4338 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4339 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4340 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4341 | #endif |
| 4342 | if (asession_temp->serverid == NULL) { |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4343 | /* TODO redispatch request */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4344 | Alert("Found Application Session without matching server.\n"); |
| 4345 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4346 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4347 | while (srv) { |
| 4348 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4349 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4350 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4351 | txn->flags &= ~TX_CK_MASK; |
| 4352 | txn->flags |= TX_CK_VALID; |
| 4353 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4354 | t->srv = srv; |
| 4355 | break; |
| 4356 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4357 | txn->flags &= ~TX_CK_MASK; |
| 4358 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4359 | } |
| 4360 | } |
| 4361 | srv = srv->next; |
| 4362 | } |
| 4363 | } |
| 4364 | } |
| 4365 | |
| 4366 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4367 | /* |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 4368 | * In a GET or HEAD request, check if the requested URI matches the stats uri |
| 4369 | * for the current backend, and if an authorization has been passed and is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4370 | * |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 4371 | * It is assumed that the request is either a HEAD or GET and that the |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4372 | * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 4373 | * produce_content() can be called to start sending data. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4374 | * |
| 4375 | * Returns 1 if the session's state changes, otherwise 0. |
| 4376 | */ |
| 4377 | int stats_check_uri_auth(struct session *t, struct proxy *backend) |
| 4378 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4379 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4380 | struct uri_auth *uri_auth = backend->uri_auth; |
| 4381 | struct user_auth *user; |
| 4382 | int authenticated, cur_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4383 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4384 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 4385 | memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats)); |
| 4386 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4387 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4388 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4389 | return 0; |
| 4390 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4391 | h = t->req->data + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4392 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 4393 | /* the URI is in h */ |
| 4394 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4395 | return 0; |
| 4396 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 4397 | h += uri_auth->uri_len; |
| 4398 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) { |
| 4399 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 4400 | t->data_ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 4401 | break; |
| 4402 | } |
| 4403 | h++; |
| 4404 | } |
| 4405 | |
| 4406 | if (uri_auth->refresh) { |
| 4407 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 4408 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) { |
| 4409 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 4410 | t->data_ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 4411 | break; |
| 4412 | } |
| 4413 | h++; |
| 4414 | } |
| 4415 | } |
| 4416 | |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 4417 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 4418 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) { |
| 4419 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 4420 | t->data_ctx.stats.flags |= STAT_FMT_CSV; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 4421 | break; |
| 4422 | } |
| 4423 | h++; |
| 4424 | } |
| 4425 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 4426 | t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO; |
| 4427 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4428 | /* we are in front of a interceptable URI. Let's check |
| 4429 | * if there's an authentication and if it's valid. |
| 4430 | */ |
| 4431 | user = uri_auth->users; |
| 4432 | if (!user) { |
| 4433 | /* no user auth required, it's OK */ |
| 4434 | authenticated = 1; |
| 4435 | } else { |
| 4436 | authenticated = 0; |
| 4437 | |
| 4438 | /* a user list is defined, we have to check. |
| 4439 | * skip 21 chars for "Authorization: Basic ". |
| 4440 | */ |
| 4441 | |
| 4442 | /* FIXME: this should move to an earlier place */ |
| 4443 | cur_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4444 | h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4445 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 4446 | int len = txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4447 | if (len > 14 && |
| 4448 | !strncasecmp("Authorization:", h, 14)) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4449 | txn->auth_hdr.str = h; |
| 4450 | txn->auth_hdr.len = len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4451 | break; |
| 4452 | } |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4453 | h += len + txn->hdr_idx.v[cur_idx].cr + 1; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4454 | } |
| 4455 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4456 | if (txn->auth_hdr.len < 21 || |
| 4457 | memcmp(txn->auth_hdr.str + 14, " Basic ", 7)) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4458 | user = NULL; |
| 4459 | |
| 4460 | while (user) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4461 | if ((txn->auth_hdr.len == user->user_len + 14 + 7) |
| 4462 | && !memcmp(txn->auth_hdr.str + 14 + 7, |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4463 | user->user_pwd, user->user_len)) { |
| 4464 | authenticated = 1; |
| 4465 | break; |
| 4466 | } |
| 4467 | user = user->next; |
| 4468 | } |
| 4469 | } |
| 4470 | |
| 4471 | if (!authenticated) { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 4472 | struct chunk msg; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4473 | |
| 4474 | /* no need to go further */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 4475 | msg.str = trash; |
| 4476 | msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm); |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4477 | txn->status = 401; |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 4478 | stream_int_retnclose(t->req->prod, &msg); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 4479 | trace_term(t, TT_HTTP_URI_1); |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 4480 | t->req->analysers = 0; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4481 | if (!(t->flags & SN_ERR_MASK)) |
| 4482 | t->flags |= SN_ERR_PRXCOND; |
| 4483 | if (!(t->flags & SN_FINST_MASK)) |
| 4484 | t->flags |= SN_FINST_R; |
| 4485 | return 1; |
| 4486 | } |
| 4487 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 4488 | /* The request is valid, the user is authenticated. Let's start sending |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4489 | * data. |
| 4490 | */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 4491 | buffer_write_dis(t->req); |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 4492 | buffer_shutw_now(t->req); |
| 4493 | buffer_shutr_now(t->rep); |
| 4494 | buffer_start_hijack(t->rep); |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 4495 | t->logs.tv_request = now; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4496 | t->data_source = DATA_SRC_STATS; |
| 4497 | t->data_state = DATA_ST_INIT; |
Willy Tarreau | 91e9993 | 2008-06-30 07:51:00 +0200 | [diff] [blame] | 4498 | t->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4499 | produce_content(t); |
| 4500 | return 1; |
| 4501 | } |
| 4502 | |
| 4503 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4504 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4505 | * Print a debug line with a header |
| 4506 | */ |
| 4507 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 4508 | { |
| 4509 | int len, max; |
| 4510 | 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] | 4511 | 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] | 4512 | max = end - start; |
| 4513 | UBOUND(max, sizeof(trash) - len - 1); |
| 4514 | len += strlcpy2(trash + len, start, max + 1); |
| 4515 | trash[len++] = '\n'; |
| 4516 | write(1, trash, len); |
| 4517 | } |
| 4518 | |
| 4519 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4520 | /************************************************************************/ |
| 4521 | /* The code below is dedicated to ACL parsing and matching */ |
| 4522 | /************************************************************************/ |
| 4523 | |
| 4524 | |
| 4525 | |
| 4526 | |
| 4527 | /* 1. Check on METHOD |
| 4528 | * We use the pre-parsed method if it is known, and store its number as an |
| 4529 | * integer. If it is unknown, we use the pointer and the length. |
| 4530 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4531 | 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] | 4532 | { |
| 4533 | int len, meth; |
| 4534 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4535 | len = strlen(*text); |
| 4536 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4537 | |
| 4538 | pattern->val.i = meth; |
| 4539 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4540 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4541 | if (!pattern->ptr.str) |
| 4542 | return 0; |
| 4543 | pattern->len = len; |
| 4544 | } |
| 4545 | return 1; |
| 4546 | } |
| 4547 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 4548 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 4549 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4550 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4551 | { |
| 4552 | int meth; |
| 4553 | struct http_txn *txn = l7; |
| 4554 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4555 | if (!txn) |
| 4556 | return 0; |
| 4557 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4558 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 4559 | return 0; |
| 4560 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4561 | meth = txn->meth; |
| 4562 | test->i = meth; |
| 4563 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4564 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 4565 | /* ensure the indexes are not affected */ |
| 4566 | return 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4567 | test->len = txn->req.sl.rq.m_l; |
| 4568 | test->ptr = txn->req.sol; |
| 4569 | } |
| 4570 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 4571 | return 1; |
| 4572 | } |
| 4573 | |
| 4574 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 4575 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 4576 | int icase; |
| 4577 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4578 | if (test->i != pattern->val.i) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 4579 | return ACL_PAT_FAIL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4580 | |
| 4581 | if (test->i != HTTP_METH_OTHER) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 4582 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4583 | |
| 4584 | /* Other method, we must compare the strings */ |
| 4585 | if (pattern->len != test->len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 4586 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 4587 | |
| 4588 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 4589 | if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) || |
| 4590 | (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 4591 | return ACL_PAT_FAIL; |
| 4592 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4593 | } |
| 4594 | |
| 4595 | /* 2. Check on Request/Status Version |
| 4596 | * We simply compare strings here. |
| 4597 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4598 | 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] | 4599 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4600 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4601 | if (!pattern->ptr.str) |
| 4602 | return 0; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4603 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4604 | return 1; |
| 4605 | } |
| 4606 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 4607 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 4608 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4609 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4610 | { |
| 4611 | struct http_txn *txn = l7; |
| 4612 | char *ptr; |
| 4613 | int len; |
| 4614 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4615 | if (!txn) |
| 4616 | return 0; |
| 4617 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4618 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 4619 | return 0; |
| 4620 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4621 | len = txn->req.sl.rq.v_l; |
| 4622 | ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som; |
| 4623 | |
| 4624 | while ((len-- > 0) && (*ptr++ != '/')); |
| 4625 | if (len <= 0) |
| 4626 | return 0; |
| 4627 | |
| 4628 | test->ptr = ptr; |
| 4629 | test->len = len; |
| 4630 | |
| 4631 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 4632 | return 1; |
| 4633 | } |
| 4634 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 4635 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 4636 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4637 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4638 | { |
| 4639 | struct http_txn *txn = l7; |
| 4640 | char *ptr; |
| 4641 | int len; |
| 4642 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4643 | if (!txn) |
| 4644 | return 0; |
| 4645 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4646 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 4647 | return 0; |
| 4648 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4649 | len = txn->rsp.sl.st.v_l; |
| 4650 | ptr = txn->rsp.sol; |
| 4651 | |
| 4652 | while ((len-- > 0) && (*ptr++ != '/')); |
| 4653 | if (len <= 0) |
| 4654 | return 0; |
| 4655 | |
| 4656 | test->ptr = ptr; |
| 4657 | test->len = len; |
| 4658 | |
| 4659 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 4660 | return 1; |
| 4661 | } |
| 4662 | |
| 4663 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 4664 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 4665 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4666 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4667 | { |
| 4668 | struct http_txn *txn = l7; |
| 4669 | char *ptr; |
| 4670 | int len; |
| 4671 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4672 | if (!txn) |
| 4673 | return 0; |
| 4674 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4675 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 4676 | return 0; |
| 4677 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4678 | len = txn->rsp.sl.st.c_l; |
| 4679 | ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som; |
| 4680 | |
| 4681 | test->i = __strl2ui(ptr, len); |
| 4682 | test->flags = ACL_TEST_F_VOL_1ST; |
| 4683 | return 1; |
| 4684 | } |
| 4685 | |
| 4686 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 4687 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 4688 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4689 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4690 | { |
| 4691 | struct http_txn *txn = l7; |
| 4692 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4693 | if (!txn) |
| 4694 | return 0; |
| 4695 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4696 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 4697 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4698 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4699 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 4700 | /* ensure the indexes are not affected */ |
| 4701 | return 0; |
| 4702 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4703 | test->len = txn->req.sl.rq.u_l; |
| 4704 | test->ptr = txn->req.sol + txn->req.sl.rq.u; |
| 4705 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 4706 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 4707 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4708 | return 1; |
| 4709 | } |
| 4710 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 4711 | static int |
| 4712 | acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4713 | struct acl_expr *expr, struct acl_test *test) |
| 4714 | { |
| 4715 | struct http_txn *txn = l7; |
| 4716 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4717 | if (!txn) |
| 4718 | return 0; |
| 4719 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 4720 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 4721 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4722 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 4723 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 4724 | /* ensure the indexes are not affected */ |
| 4725 | return 0; |
| 4726 | |
| 4727 | /* Parse HTTP request */ |
| 4728 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
| 4729 | test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr; |
| 4730 | test->i = AF_INET; |
| 4731 | |
| 4732 | /* |
| 4733 | * If we are parsing url in frontend space, we prepare backend stage |
| 4734 | * to not parse again the same url ! optimization lazyness... |
| 4735 | */ |
| 4736 | if (px->options & PR_O_HTTP_PROXY) |
| 4737 | l4->flags |= SN_ADDR_SET; |
| 4738 | |
| 4739 | test->flags = ACL_TEST_F_READ_ONLY; |
| 4740 | return 1; |
| 4741 | } |
| 4742 | |
| 4743 | static int |
| 4744 | acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4745 | struct acl_expr *expr, struct acl_test *test) |
| 4746 | { |
| 4747 | struct http_txn *txn = l7; |
| 4748 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4749 | if (!txn) |
| 4750 | return 0; |
| 4751 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 4752 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 4753 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4754 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 4755 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 4756 | /* ensure the indexes are not affected */ |
| 4757 | return 0; |
| 4758 | |
| 4759 | /* Same optimization as url_ip */ |
| 4760 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
| 4761 | test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port); |
| 4762 | |
| 4763 | if (px->options & PR_O_HTTP_PROXY) |
| 4764 | l4->flags |= SN_ADDR_SET; |
| 4765 | |
| 4766 | test->flags = ACL_TEST_F_READ_ONLY; |
| 4767 | return 1; |
| 4768 | } |
| 4769 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4770 | /* 5. Check on HTTP header. A pointer to the beginning of the value is returned. |
| 4771 | * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr(). |
| 4772 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 4773 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4774 | 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] | 4775 | struct acl_expr *expr, struct acl_test *test) |
| 4776 | { |
| 4777 | struct http_txn *txn = l7; |
| 4778 | struct hdr_idx *idx = &txn->hdr_idx; |
| 4779 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 4780 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4781 | if (!txn) |
| 4782 | return 0; |
| 4783 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 4784 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 4785 | /* search for header from the beginning */ |
| 4786 | ctx->idx = 0; |
| 4787 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 4788 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 4789 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 4790 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 4791 | test->len = ctx->vlen; |
| 4792 | test->ptr = (char *)ctx->line + ctx->val; |
| 4793 | return 1; |
| 4794 | } |
| 4795 | |
| 4796 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 4797 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 4798 | return 0; |
| 4799 | } |
| 4800 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 4801 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4802 | acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4803 | struct acl_expr *expr, struct acl_test *test) |
| 4804 | { |
| 4805 | struct http_txn *txn = l7; |
| 4806 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4807 | if (!txn) |
| 4808 | return 0; |
| 4809 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4810 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 4811 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4812 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4813 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 4814 | /* ensure the indexes are not affected */ |
| 4815 | return 0; |
| 4816 | |
| 4817 | return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test); |
| 4818 | } |
| 4819 | |
| 4820 | static int |
| 4821 | acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4822 | struct acl_expr *expr, struct acl_test *test) |
| 4823 | { |
| 4824 | struct http_txn *txn = l7; |
| 4825 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4826 | if (!txn) |
| 4827 | return 0; |
| 4828 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4829 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 4830 | return 0; |
| 4831 | |
| 4832 | return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test); |
| 4833 | } |
| 4834 | |
| 4835 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 4836 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 4837 | */ |
| 4838 | static int |
| 4839 | 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] | 4840 | struct acl_expr *expr, struct acl_test *test) |
| 4841 | { |
| 4842 | struct http_txn *txn = l7; |
| 4843 | struct hdr_idx *idx = &txn->hdr_idx; |
| 4844 | struct hdr_ctx ctx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 4845 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4846 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4847 | if (!txn) |
| 4848 | return 0; |
| 4849 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 4850 | ctx.idx = 0; |
| 4851 | cnt = 0; |
| 4852 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx)) |
| 4853 | cnt++; |
| 4854 | |
| 4855 | test->i = cnt; |
| 4856 | test->flags = ACL_TEST_F_VOL_HDR; |
| 4857 | return 1; |
| 4858 | } |
| 4859 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4860 | static int |
| 4861 | acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4862 | struct acl_expr *expr, struct acl_test *test) |
| 4863 | { |
| 4864 | struct http_txn *txn = l7; |
| 4865 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4866 | if (!txn) |
| 4867 | return 0; |
| 4868 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4869 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 4870 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4871 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4872 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 4873 | /* ensure the indexes are not affected */ |
| 4874 | return 0; |
| 4875 | |
| 4876 | return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test); |
| 4877 | } |
| 4878 | |
| 4879 | static int |
| 4880 | acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4881 | struct acl_expr *expr, struct acl_test *test) |
| 4882 | { |
| 4883 | struct http_txn *txn = l7; |
| 4884 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4885 | if (!txn) |
| 4886 | return 0; |
| 4887 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4888 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 4889 | return 0; |
| 4890 | |
| 4891 | return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test); |
| 4892 | } |
| 4893 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 4894 | /* 7. Check on HTTP header's integer value. The integer value is returned. |
| 4895 | * FIXME: the type is 'int', it may not be appropriate for everything. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4896 | * 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] | 4897 | */ |
| 4898 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4899 | 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] | 4900 | struct acl_expr *expr, struct acl_test *test) |
| 4901 | { |
| 4902 | struct http_txn *txn = l7; |
| 4903 | struct hdr_idx *idx = &txn->hdr_idx; |
| 4904 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 4905 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4906 | if (!txn) |
| 4907 | return 0; |
| 4908 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 4909 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 4910 | /* search for header from the beginning */ |
| 4911 | ctx->idx = 0; |
| 4912 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 4913 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 4914 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 4915 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 4916 | test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen); |
| 4917 | return 1; |
| 4918 | } |
| 4919 | |
| 4920 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 4921 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 4922 | return 0; |
| 4923 | } |
| 4924 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4925 | static int |
| 4926 | acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4927 | struct acl_expr *expr, struct acl_test *test) |
| 4928 | { |
| 4929 | struct http_txn *txn = l7; |
| 4930 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4931 | if (!txn) |
| 4932 | return 0; |
| 4933 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4934 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 4935 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4936 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4937 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 4938 | /* ensure the indexes are not affected */ |
| 4939 | return 0; |
| 4940 | |
| 4941 | return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test); |
| 4942 | } |
| 4943 | |
| 4944 | static int |
| 4945 | acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4946 | struct acl_expr *expr, struct acl_test *test) |
| 4947 | { |
| 4948 | struct http_txn *txn = l7; |
| 4949 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4950 | if (!txn) |
| 4951 | return 0; |
| 4952 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4953 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 4954 | return 0; |
| 4955 | |
| 4956 | return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test); |
| 4957 | } |
| 4958 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 4959 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 4960 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 4961 | */ |
| 4962 | static int |
| 4963 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4964 | struct acl_expr *expr, struct acl_test *test) |
| 4965 | { |
| 4966 | struct http_txn *txn = l7; |
| 4967 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 4968 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4969 | if (!txn) |
| 4970 | return 0; |
| 4971 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4972 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 4973 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4974 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4975 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 4976 | /* ensure the indexes are not affected */ |
| 4977 | return 0; |
| 4978 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4979 | end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 4980 | ptr = http_get_path(txn); |
| 4981 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 4982 | return 0; |
| 4983 | |
| 4984 | /* OK, we got the '/' ! */ |
| 4985 | test->ptr = ptr; |
| 4986 | |
| 4987 | while (ptr < end && *ptr != '?') |
| 4988 | ptr++; |
| 4989 | |
| 4990 | test->len = ptr - test->ptr; |
| 4991 | |
| 4992 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 4993 | test->flags = ACL_TEST_F_VOL_1ST; |
| 4994 | return 1; |
| 4995 | } |
| 4996 | |
| 4997 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4998 | |
| 4999 | /************************************************************************/ |
| 5000 | /* All supported keywords must be declared here. */ |
| 5001 | /************************************************************************/ |
| 5002 | |
| 5003 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 5004 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5005 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT }, |
| 5006 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5007 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 5008 | { "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] | 5009 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5010 | { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5011 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 5012 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 5013 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 5014 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 5015 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 5016 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 5017 | { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE }, |
| 5018 | { "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] | 5019 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5020 | /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */ |
| 5021 | { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5022 | { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 5023 | { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 5024 | { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 5025 | { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 5026 | { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 5027 | { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 5028 | { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
| 5029 | { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5030 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5031 | { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 5032 | { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE }, |
| 5033 | { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE }, |
| 5034 | { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE }, |
| 5035 | { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE }, |
| 5036 | { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE }, |
| 5037 | { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE }, |
| 5038 | { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
| 5039 | { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5040 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5041 | { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5042 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 5043 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 5044 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 5045 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 5046 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 5047 | { "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] | 5048 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 5049 | { NULL, NULL, NULL, NULL }, |
| 5050 | |
| 5051 | #if 0 |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5052 | { "line", acl_parse_str, acl_fetch_line, acl_match_str }, |
| 5053 | { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg }, |
| 5054 | { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg }, |
| 5055 | { "line_end", acl_parse_str, acl_fetch_line, acl_match_end }, |
| 5056 | { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub }, |
| 5057 | { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir }, |
| 5058 | { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom }, |
| 5059 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5060 | { "cook", acl_parse_str, acl_fetch_cook, acl_match_str }, |
| 5061 | { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg }, |
| 5062 | { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg }, |
| 5063 | { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end }, |
| 5064 | { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub }, |
| 5065 | { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir }, |
| 5066 | { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom }, |
| 5067 | { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst }, |
| 5068 | |
| 5069 | { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str }, |
| 5070 | { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg }, |
| 5071 | { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str }, |
| 5072 | { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 }, |
| 5073 | { NULL, NULL, NULL, NULL }, |
| 5074 | #endif |
| 5075 | }}; |
| 5076 | |
| 5077 | |
| 5078 | __attribute__((constructor)) |
| 5079 | static void __http_protocol_init(void) |
| 5080 | { |
| 5081 | acl_register_keywords(&acl_kws); |
| 5082 | } |
| 5083 | |
| 5084 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5085 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5086 | * Local variables: |
| 5087 | * c-indent-level: 8 |
| 5088 | * c-basic-offset: 8 |
| 5089 | * End: |
| 5090 | */ |