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> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 51 | #include <proto/senddata.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 52 | #include <proto/session.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 | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 372 | static void http_sess_log(struct session *s); |
| 373 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 374 | /* |
| 375 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 376 | * CRLF. Text length is measured first, so it cannot be NULL. |
| 377 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 378 | * of headers is automatically adjusted. The number of bytes added is returned |
| 379 | * on success, otherwise <0 is returned indicating an error. |
| 380 | */ |
| 381 | int http_header_add_tail(struct buffer *b, struct http_msg *msg, |
| 382 | struct hdr_idx *hdr_idx, const char *text) |
| 383 | { |
| 384 | int bytes, len; |
| 385 | |
| 386 | len = strlen(text); |
| 387 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 388 | if (!bytes) |
| 389 | return -1; |
| 390 | msg->eoh += bytes; |
| 391 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 392 | } |
| 393 | |
| 394 | /* |
| 395 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 396 | * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then |
| 397 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 398 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 399 | * of headers is automatically adjusted. The number of bytes added is returned |
| 400 | * on success, otherwise <0 is returned indicating an error. |
| 401 | */ |
| 402 | int http_header_add_tail2(struct buffer *b, struct http_msg *msg, |
| 403 | struct hdr_idx *hdr_idx, const char *text, int len) |
| 404 | { |
| 405 | int bytes; |
| 406 | |
| 407 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 408 | if (!bytes) |
| 409 | return -1; |
| 410 | msg->eoh += bytes; |
| 411 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 412 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 413 | |
| 414 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 415 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 416 | * If so, returns the position of the first non-space character relative to |
| 417 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 418 | * to return a pointer to the place after the first space. Returns 0 if the |
| 419 | * header name does not match. Checks are case-insensitive. |
| 420 | */ |
| 421 | int http_header_match2(const char *hdr, const char *end, |
| 422 | const char *name, int len) |
| 423 | { |
| 424 | const char *val; |
| 425 | |
| 426 | if (hdr + len >= end) |
| 427 | return 0; |
| 428 | if (hdr[len] != ':') |
| 429 | return 0; |
| 430 | if (strncasecmp(hdr, name, len) != 0) |
| 431 | return 0; |
| 432 | val = hdr + len + 1; |
| 433 | while (val < end && HTTP_IS_SPHT(*val)) |
| 434 | val++; |
| 435 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 436 | return len + 2; /* we may replace starting from second space */ |
| 437 | return val - hdr; |
| 438 | } |
| 439 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 440 | /* Find the end of the header value contained between <s> and <e>. |
| 441 | * See RFC2616, par 2.2 for more information. Note that it requires |
| 442 | * a valid header to return a valid result. |
| 443 | */ |
| 444 | const char *find_hdr_value_end(const char *s, const char *e) |
| 445 | { |
| 446 | int quoted, qdpair; |
| 447 | |
| 448 | quoted = qdpair = 0; |
| 449 | for (; s < e; s++) { |
| 450 | if (qdpair) qdpair = 0; |
| 451 | else if (quoted && *s == '\\') qdpair = 1; |
| 452 | else if (quoted && *s == '"') quoted = 0; |
| 453 | else if (*s == '"') quoted = 1; |
| 454 | else if (*s == ',') return s; |
| 455 | } |
| 456 | return s; |
| 457 | } |
| 458 | |
| 459 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 460 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 461 | * structure holds everything necessary to use the header and find next |
| 462 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 463 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 464 | * 1 when it finds a value, and 0 when there is no more. |
| 465 | */ |
| 466 | int http_find_header2(const char *name, int len, |
| 467 | const char *sol, struct hdr_idx *idx, |
| 468 | struct hdr_ctx *ctx) |
| 469 | { |
| 470 | __label__ return_hdr, next_hdr; |
| 471 | const char *eol, *sov; |
| 472 | int cur_idx; |
| 473 | |
| 474 | if (ctx->idx) { |
| 475 | /* We have previously returned a value, let's search |
| 476 | * another one on the same line. |
| 477 | */ |
| 478 | cur_idx = ctx->idx; |
| 479 | sol = ctx->line; |
| 480 | sov = sol + ctx->val + ctx->vlen; |
| 481 | eol = sol + idx->v[cur_idx].len; |
| 482 | |
| 483 | if (sov >= eol) |
| 484 | /* no more values in this header */ |
| 485 | goto next_hdr; |
| 486 | |
| 487 | /* values remaining for this header, skip the comma */ |
| 488 | sov++; |
| 489 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 490 | sov++; |
| 491 | |
| 492 | goto return_hdr; |
| 493 | } |
| 494 | |
| 495 | /* first request for this header */ |
| 496 | sol += hdr_idx_first_pos(idx); |
| 497 | cur_idx = hdr_idx_first_idx(idx); |
| 498 | |
| 499 | while (cur_idx) { |
| 500 | eol = sol + idx->v[cur_idx].len; |
| 501 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 502 | if (len == 0) { |
| 503 | /* No argument was passed, we want any header. |
| 504 | * To achieve this, we simply build a fake request. */ |
| 505 | while (sol + len < eol && sol[len] != ':') |
| 506 | len++; |
| 507 | name = sol; |
| 508 | } |
| 509 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 510 | if ((len < eol - sol) && |
| 511 | (sol[len] == ':') && |
| 512 | (strncasecmp(sol, name, len) == 0)) { |
| 513 | |
| 514 | sov = sol + len + 1; |
| 515 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 516 | sov++; |
| 517 | return_hdr: |
| 518 | ctx->line = sol; |
| 519 | ctx->idx = cur_idx; |
| 520 | ctx->val = sov - sol; |
| 521 | |
| 522 | eol = find_hdr_value_end(sov, eol); |
| 523 | ctx->vlen = eol - sov; |
| 524 | return 1; |
| 525 | } |
| 526 | next_hdr: |
| 527 | sol = eol + idx->v[cur_idx].cr + 1; |
| 528 | cur_idx = idx->v[cur_idx].next; |
| 529 | } |
| 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | int http_find_header(const char *name, |
| 534 | const char *sol, struct hdr_idx *idx, |
| 535 | struct hdr_ctx *ctx) |
| 536 | { |
| 537 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 538 | } |
| 539 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 540 | /* This function shuts down the buffers on the server side, and sets indicators |
| 541 | * accordingly. The server's fd is supposed to already be closed. Note that if |
| 542 | * <status> is 0, or if the message pointer is NULL, then no message is returned. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 543 | */ |
| 544 | void srv_close_with_err(struct session *t, int err, int finst, |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 545 | int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 546 | { |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 547 | buffer_write_ena(t->rep); |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 548 | buffer_shutw(t->req); |
| 549 | buffer_shutr(t->rep); |
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 | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 552 | if (t->fe->mode == PR_MODE_HTTP) |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 553 | client_return(t, msg); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 554 | } |
| 555 | if (!(t->flags & SN_ERR_MASK)) |
| 556 | t->flags |= err; |
| 557 | if (!(t->flags & SN_FINST_MASK)) |
| 558 | t->flags |= finst; |
| 559 | } |
| 560 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 561 | /* This function returns the appropriate error location for the given session |
| 562 | * and message. |
| 563 | */ |
| 564 | |
| 565 | struct chunk *error_message(struct session *s, int msgnum) |
| 566 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 567 | if (s->be->errmsg[msgnum].str) |
| 568 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 569 | else if (s->fe->errmsg[msgnum].str) |
| 570 | return &s->fe->errmsg[msgnum]; |
| 571 | else |
| 572 | return &http_err_chunks[msgnum]; |
| 573 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 574 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 575 | /* |
| 576 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 577 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 578 | */ |
| 579 | static http_meth_t find_http_meth(const char *str, const int len) |
| 580 | { |
| 581 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 582 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 583 | |
| 584 | m = ((unsigned)*str - 'A'); |
| 585 | |
| 586 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 587 | for (h = http_methods[m]; h->len > 0; h++) { |
| 588 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 589 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 590 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 591 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 592 | }; |
| 593 | return HTTP_METH_OTHER; |
| 594 | } |
| 595 | return HTTP_METH_NONE; |
| 596 | |
| 597 | } |
| 598 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 599 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 600 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 601 | * It is returned otherwise. |
| 602 | */ |
| 603 | static char * |
| 604 | http_get_path(struct http_txn *txn) |
| 605 | { |
| 606 | char *ptr, *end; |
| 607 | |
| 608 | ptr = txn->req.sol + txn->req.sl.rq.u; |
| 609 | end = ptr + txn->req.sl.rq.u_l; |
| 610 | |
| 611 | if (ptr >= end) |
| 612 | return NULL; |
| 613 | |
| 614 | /* RFC2616, par. 5.1.2 : |
| 615 | * Request-URI = "*" | absuri | abspath | authority |
| 616 | */ |
| 617 | |
| 618 | if (*ptr == '*') |
| 619 | return NULL; |
| 620 | |
| 621 | if (isalpha((unsigned char)*ptr)) { |
| 622 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 623 | ptr++; |
| 624 | while (ptr < end && |
| 625 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 626 | ptr++; |
| 627 | /* skip '://' */ |
| 628 | if (ptr == end || *ptr++ != ':') |
| 629 | return NULL; |
| 630 | if (ptr == end || *ptr++ != '/') |
| 631 | return NULL; |
| 632 | if (ptr == end || *ptr++ != '/') |
| 633 | return NULL; |
| 634 | } |
| 635 | /* skip [user[:passwd]@]host[:[port]] */ |
| 636 | |
| 637 | while (ptr < end && *ptr != '/') |
| 638 | ptr++; |
| 639 | |
| 640 | if (ptr == end) |
| 641 | return NULL; |
| 642 | |
| 643 | /* OK, we got the '/' ! */ |
| 644 | return ptr; |
| 645 | } |
| 646 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 647 | /* Processes the client, server, request and response jobs of a session task, |
| 648 | * then puts it back to the wait queue in a clean state, or cleans up its |
| 649 | * resources if it must be deleted. Returns in <next> the date the task wants |
| 650 | * to be woken up, or TICK_ETERNITY. In order not to call all functions for |
| 651 | * nothing too many times, the request and response buffers flags are monitored |
| 652 | * and each function is called only if at least another function has changed at |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 653 | * least one flag it is interested in. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 654 | */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 655 | void process_session(struct task *t, int *next) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 656 | { |
| 657 | struct session *s = t->context; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 658 | int resync; |
| 659 | unsigned int rqf_cli, rpf_cli; |
| 660 | unsigned int rqf_srv, rpf_srv; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 661 | |
Willy Tarreau | d7704b5 | 2008-09-04 11:51:16 +0200 | [diff] [blame^] | 662 | /* 1: Check timeouts only during data phase for now */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 663 | if (unlikely(t->state & TASK_WOKEN_TIMER)) { |
Willy Tarreau | 3c6ab2e | 2008-09-04 11:19:41 +0200 | [diff] [blame] | 664 | buffer_check_timeouts(s->req); |
| 665 | buffer_check_timeouts(s->rep); |
Willy Tarreau | d7704b5 | 2008-09-04 11:51:16 +0200 | [diff] [blame^] | 666 | stream_sock_check_timeouts(&s->si[0]); |
| 667 | stream_sock_check_timeouts(&s->si[1]); |
Willy Tarreau | 3c6ab2e | 2008-09-04 11:19:41 +0200 | [diff] [blame] | 668 | |
| 669 | if (unlikely(s->req->flags & (BF_READ_TIMEOUT|BF_WRITE_TIMEOUT))) { |
| 670 | if (s->req->flags & BF_READ_TIMEOUT) { |
| 671 | buffer_shutw(s->req); |
| 672 | s->req->cons->shutr(s->req->prod); |
| 673 | } |
| 674 | if (s->req->flags & BF_WRITE_TIMEOUT) { |
| 675 | buffer_shutw(s->req); |
| 676 | s->req->cons->shutw(s->req->cons); |
| 677 | } |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame] | 678 | } |
Willy Tarreau | 3c6ab2e | 2008-09-04 11:19:41 +0200 | [diff] [blame] | 679 | |
| 680 | if (unlikely(s->rep->flags & (BF_READ_TIMEOUT|BF_WRITE_TIMEOUT))) { |
| 681 | if (s->rep->flags & BF_READ_TIMEOUT) { |
| 682 | buffer_shutw(s->rep); |
| 683 | s->rep->cons->shutr(s->rep->prod); |
| 684 | } |
| 685 | if (s->rep->flags & BF_WRITE_TIMEOUT) { |
| 686 | buffer_shutw(s->rep); |
| 687 | s->rep->cons->shutw(s->rep->cons); |
| 688 | } |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame] | 689 | } |
Willy Tarreau | 3537467 | 2008-09-03 18:11:02 +0200 | [diff] [blame] | 690 | /* Note that we don't check nor indicate if we wake up because |
| 691 | * of a timeout on a stream interface. |
| 692 | */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 693 | } |
| 694 | |
Willy Tarreau | d7704b5 | 2008-09-04 11:51:16 +0200 | [diff] [blame^] | 695 | /* 2: Check if we need to close the write side. This can only happen |
Willy Tarreau | 48adac5 | 2008-08-30 04:58:38 +0200 | [diff] [blame] | 696 | * when either SHUTR or EMPTY appears, because WRITE_ENA cannot appear |
| 697 | * from low level, and neither HIJACK nor SHUTW can disappear from low |
| 698 | * level. |
| 699 | */ |
| 700 | if (unlikely((s->req->flags & (BF_SHUTW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) == (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR))) { |
| 701 | buffer_shutw(s->req); |
| 702 | s->req->cons->shutw(s->req->cons); |
| 703 | } |
| 704 | |
| 705 | if (unlikely((s->rep->flags & (BF_SHUTW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) == (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR))) { |
| 706 | buffer_shutw(s->rep); |
| 707 | s->rep->cons->shutw(s->rep->cons); |
| 708 | } |
| 709 | |
Willy Tarreau | d7704b5 | 2008-09-04 11:51:16 +0200 | [diff] [blame^] | 710 | /* 3: When a server-side connection is released, we have to |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 711 | * count it and check for pending connections on this server. |
| 712 | */ |
| 713 | if (unlikely(s->req->cons->state == SI_ST_CLO && |
| 714 | s->req->cons->prev_state == SI_ST_EST)) { |
| 715 | /* Count server-side errors (but not timeouts). */ |
| 716 | if (s->req->flags & BF_WRITE_ERROR) { |
| 717 | s->be->failed_resp++; |
| 718 | if (s->srv) |
| 719 | s->srv->failed_resp++; |
| 720 | } |
| 721 | |
| 722 | if (s->srv) { |
| 723 | s->srv->cur_sess--; |
| 724 | sess_change_server(s, NULL); |
| 725 | if (may_dequeue_tasks(s->srv, s->be)) |
| 726 | process_srv_queue(s->srv); |
| 727 | } |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 728 | } |
| 729 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 730 | /* Dirty trick: force one first pass everywhere */ |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame] | 731 | rqf_cli = rqf_srv = ~s->req->flags; |
| 732 | rpf_cli = rpf_srv = ~s->rep->flags; |
Willy Tarreau | 507385d | 2008-08-17 13:04:25 +0200 | [diff] [blame] | 733 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 734 | /* well, the ST_CONN state is already handled properly */ |
| 735 | if (s->req->prod->state == SI_ST_EST) { |
| 736 | rqf_cli = s->req->flags; |
| 737 | rpf_cli = s->rep->flags; |
| 738 | } |
| 739 | |
| 740 | if (s->req->cons->state == SI_ST_EST) { |
| 741 | rqf_srv = s->req->flags; |
| 742 | rpf_srv = s->rep->flags; |
| 743 | } |
| 744 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 745 | do { |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 746 | DPRINTF(stderr,"[%u] %s: task=%p rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rql=%d rpl=%d cs=%d ss=%d\n", |
| 747 | now_ms, __FUNCTION__, |
| 748 | t, |
| 749 | s->req, s->rep, |
| 750 | s->req->rex, s->rep->wex, |
| 751 | s->req->flags, s->rep->flags, |
| 752 | s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state); |
| 753 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 754 | resync = 0; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 755 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 756 | /* Maybe resync client FD state */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 757 | if (s->rep->cons->state != SI_ST_CLO) { |
| 758 | if (((rqf_cli ^ s->req->flags) & BF_MASK_INTERFACE_I) || |
| 759 | ((rpf_cli ^ s->rep->flags) & BF_MASK_INTERFACE_O)) { |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 760 | stream_sock_data_update(s->rep->cons->fd); |
| 761 | rqf_cli = s->req->flags; |
| 762 | rpf_cli = s->rep->flags; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 763 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 764 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 765 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 766 | /* Maybe resync server FD state */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 767 | if (s->req->cons->state != SI_ST_CLO) { |
| 768 | if (((rpf_srv ^ s->rep->flags) & BF_MASK_INTERFACE_I) || |
| 769 | ((rqf_srv ^ s->req->flags) & BF_MASK_INTERFACE_O)) { |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame] | 770 | if (s->req->cons->state < SI_ST_EST && s->req->flags & BF_WRITE_ENA) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 771 | process_srv_conn(s); |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame] | 772 | resync = 1; /* we might have to resync */ |
| 773 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 774 | |
| 775 | if (s->req->cons->state == SI_ST_EST) { |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 776 | if ((s->req->flags & (BF_SHUTW|BF_EMPTY|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA) && |
Willy Tarreau | 376580a | 2008-08-27 18:52:22 +0200 | [diff] [blame] | 777 | s->be->options & PR_O_FORCE_CLO && |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 778 | s->rep->flags & BF_READ_ACTIVITY) { |
Willy Tarreau | 376580a | 2008-08-27 18:52:22 +0200 | [diff] [blame] | 779 | /* We want to force the connection to the server to close, |
| 780 | * and the server has begun to respond. That's the right |
| 781 | * time. |
| 782 | */ |
| 783 | buffer_shutw_now(s->req); |
| 784 | } |
| 785 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 786 | stream_sock_data_update(s->req->cons->fd); |
Willy Tarreau | 376580a | 2008-08-27 18:52:22 +0200 | [diff] [blame] | 787 | |
| 788 | /* When a server-side connection is released, we have to |
| 789 | * count it and check for pending connections on this server. |
| 790 | */ |
| 791 | if (s->req->cons->state == SI_ST_CLO) { |
| 792 | if (s->srv) { |
| 793 | s->srv->cur_sess--; |
| 794 | sess_change_server(s, NULL); |
| 795 | if (may_dequeue_tasks(s->srv, s->be)) |
| 796 | process_srv_queue(s->srv); |
| 797 | } |
| 798 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 799 | } |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 800 | rqf_srv = s->req->flags; |
| 801 | rpf_srv = s->rep->flags; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 802 | } |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | /* we may have to resync because of pending connections */ |
| 806 | if (resync) |
| 807 | continue; |
| 808 | |
| 809 | /**** Process layer 7 below ****/ |
| 810 | |
| 811 | /* Analyse request */ |
| 812 | if (s->req->flags & BF_MASK_ANALYSER) { |
| 813 | unsigned int flags = s->req->flags; |
| 814 | |
| 815 | if (s->req->prod->state >= SI_ST_EST) { |
| 816 | /* it's up to the analysers to reset write_ena */ |
| 817 | buffer_write_ena(s->req); |
| 818 | if (s->req->analysers) |
| 819 | process_request(s); |
| 820 | } |
| 821 | s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 822 | flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 823 | if (s->req->flags != flags) |
| 824 | resync = 1; |
| 825 | } |
| 826 | |
| 827 | /* Analyse response */ |
| 828 | if (unlikely(s->rep->flags & BF_HIJACK)) { |
| 829 | /* In inject mode, we wake up everytime something has |
| 830 | * happened on the write side of the buffer. |
| 831 | */ |
| 832 | unsigned int flags = s->rep->flags; |
| 833 | |
| 834 | if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) && |
| 835 | !(s->rep->flags & BF_FULL)) { |
| 836 | produce_content(s); |
| 837 | } |
| 838 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 839 | flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 840 | if (s->rep->flags != flags) |
| 841 | resync = 1; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 842 | } |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame] | 843 | else if (s->rep->flags & BF_MASK_ANALYSER) { |
| 844 | unsigned int flags = s->rep->flags; |
| 845 | |
| 846 | if (s->rep->prod->state >= SI_ST_EST) { |
| 847 | /* it's up to the analysers to reset write_ena */ |
| 848 | buffer_write_ena(s->rep); |
| 849 | if (s->rep->analysers) |
| 850 | process_response(s); |
| 851 | } |
| 852 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 853 | flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 854 | if (s->rep->flags != flags) |
| 855 | resync = 1; |
| 856 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 857 | |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame] | 858 | /* For the moment, we need to clean the client and server flags that |
| 859 | * have vanished. This is just a temporary measure though. |
| 860 | */ |
| 861 | rqf_cli &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 862 | rqf_srv &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 863 | rpf_cli &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 864 | rpf_srv &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 865 | } while (resync); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 866 | |
Willy Tarreau | a37095b | 2008-09-03 11:37:47 +0200 | [diff] [blame] | 867 | /* This is needed only when debugging is enabled, to indicate |
| 868 | * client-side or server-side close. Please note that in the unlikely |
| 869 | * event where both sides would close at once, the sequence is reported |
| 870 | * on the server side first. |
| 871 | */ |
| 872 | if (unlikely((global.mode & MODE_DEBUG) && |
| 873 | (!(global.mode & MODE_QUIET) || |
| 874 | (global.mode & MODE_VERBOSE)))) { |
| 875 | int len; |
| 876 | |
| 877 | if (s->si[1].state == SI_ST_CLO && |
| 878 | s->si[1].prev_state == SI_ST_EST) { |
| 879 | len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n", |
| 880 | s->uniq_id, s->be->id, |
| 881 | (unsigned short)s->si[0].fd, |
| 882 | (unsigned short)s->si[1].fd); |
| 883 | write(1, trash, len); |
| 884 | } |
| 885 | |
| 886 | if (s->si[0].state == SI_ST_CLO && |
| 887 | s->si[0].prev_state == SI_ST_EST) { |
| 888 | len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", |
| 889 | s->uniq_id, s->be->id, |
| 890 | (unsigned short)s->si[0].fd, |
| 891 | (unsigned short)s->si[1].fd); |
| 892 | write(1, trash, len); |
| 893 | } |
| 894 | } |
| 895 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 896 | if (likely((s->rep->cons->state != SI_ST_CLO) || |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 897 | (s->req->cons->state != SI_ST_CLO && s->req->cons->state != SI_ST_INI))) { |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 898 | |
| 899 | if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED)) |
| 900 | session_process_counters(s); |
| 901 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 902 | if (s->rep->cons->state == SI_ST_EST) |
| 903 | stream_sock_data_finish(s->rep->cons->fd); |
| 904 | |
| 905 | if (s->req->cons->state == SI_ST_EST) |
| 906 | stream_sock_data_finish(s->req->cons->fd); |
| 907 | |
Willy Tarreau | 9a2d154 | 2008-08-30 12:31:07 +0200 | [diff] [blame] | 908 | s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 909 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 910 | s->si[0].prev_state = s->si[0].state; |
| 911 | s->si[1].prev_state = s->si[1].state; |
Willy Tarreau | d7704b5 | 2008-09-04 11:51:16 +0200 | [diff] [blame^] | 912 | s->si[0].flags = s->si[1].flags = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 913 | |
Willy Tarreau | 7f875f6 | 2008-08-11 17:35:01 +0200 | [diff] [blame] | 914 | /* Trick: if a request is being waiting for the server to respond, |
| 915 | * and if we know the server can timeout, we don't want the timeout |
| 916 | * to expire on the client side first, but we're still interested |
| 917 | * in passing data from the client to the server (eg: POST). Thus, |
| 918 | * we can cancel the client's request timeout if the server's |
| 919 | * request timeout is set and the server has not yet sent a response. |
| 920 | */ |
| 921 | |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 922 | if ((s->rep->flags & (BF_WRITE_ENA|BF_SHUTR)) == 0 && |
Willy Tarreau | 26ed74d | 2008-08-17 12:11:14 +0200 | [diff] [blame] | 923 | (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) |
Willy Tarreau | 7f875f6 | 2008-08-11 17:35:01 +0200 | [diff] [blame] | 924 | s->req->rex = TICK_ETERNITY; |
| 925 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 926 | t->expire = tick_first(tick_first(s->req->rex, s->req->wex), |
| 927 | tick_first(s->rep->rex, s->rep->wex)); |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 928 | if (s->req->analysers) |
| 929 | t->expire = tick_first(t->expire, s->req->analyse_exp); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 930 | |
Willy Tarreau | 3537467 | 2008-09-03 18:11:02 +0200 | [diff] [blame] | 931 | if (s->si[0].exp) |
| 932 | t->expire = tick_first(t->expire, s->si[0].exp); |
| 933 | |
| 934 | if (s->si[1].exp) |
| 935 | t->expire = tick_first(t->expire, s->si[1].exp); |
| 936 | |
Willy Tarreau | cb65125 | 2008-08-29 13:57:30 +0200 | [diff] [blame] | 937 | #ifdef DEBUG_FULL |
| 938 | fprintf(stderr, "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u rep->rex=%u rep->wex=%u\n", |
| 939 | now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp, s->rep->rex, s->rep->wex); |
| 940 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 941 | /* restore t to its place in the task list */ |
| 942 | task_queue(t); |
| 943 | |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 944 | #ifdef DEBUG_DEV |
| 945 | /* this may only happen when no timeout is set or in case of an FSM bug */ |
| 946 | if (!t->expire) |
| 947 | ABORT_NOW(); |
| 948 | #endif |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 949 | *next = t->expire; |
| 950 | return; /* nothing more to do */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 951 | } |
| 952 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 953 | s->fe->feconn--; |
| 954 | if (s->flags & SN_BE_ASSIGNED) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 955 | s->be->beconn--; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 956 | actconn--; |
| 957 | |
Willy Tarreau | f41d4b1 | 2007-04-28 23:26:14 +0200 | [diff] [blame] | 958 | if (unlikely((global.mode & MODE_DEBUG) && |
| 959 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 960 | int len; |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 961 | len = sprintf(trash, "%08x:%s.closed[%04x:%04x] (term_trace=0x%08x)\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 962 | s->uniq_id, s->be->id, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 963 | (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd, |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 964 | s->term_trace); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 965 | write(1, trash, len); |
| 966 | } |
| 967 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 968 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 969 | session_process_counters(s); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 970 | |
| 971 | /* let's do a final log if we need it */ |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 972 | if (s->logs.logwait && |
| 973 | !(s->flags & SN_MONITOR) && |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 974 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 975 | if (s->fe->to_log & LW_REQ) |
| 976 | http_sess_log(s); |
| 977 | else |
| 978 | tcp_sess_log(s); |
| 979 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 980 | |
| 981 | /* the task MUST not be in the run queue anymore */ |
| 982 | task_delete(t); |
| 983 | session_free(s); |
| 984 | task_free(t); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 985 | *next = TICK_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 989 | extern const char sess_term_cond[8]; |
| 990 | extern const char sess_fin_state[8]; |
| 991 | extern const char *monthname[12]; |
| 992 | const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */ |
| 993 | const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown, |
| 994 | Set-cookie seen and left unchanged (passive), Set-cookie Deleted, |
| 995 | unknown, Set-cookie Rewritten */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 996 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 997 | struct pool_head *pool2_capture; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 998 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 999 | /* |
| 1000 | * send a log for the session when we have enough info about it. |
| 1001 | * Will not log if the frontend has no log defined. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1002 | */ |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1003 | static void http_sess_log(struct session *s) |
| 1004 | { |
| 1005 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 1006 | struct proxy *fe = s->fe; |
| 1007 | struct proxy *be = s->be; |
| 1008 | struct proxy *prx_log; |
| 1009 | struct http_txn *txn = &s->txn; |
| 1010 | int tolog; |
| 1011 | char *uri, *h; |
| 1012 | char *svid; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1013 | struct tm tm; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1014 | static char tmpline[MAX_SYSLOG_LEN]; |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1015 | int t_request; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1016 | int hdr; |
| 1017 | |
| 1018 | if (fe->logfac1 < 0 && fe->logfac2 < 0) |
| 1019 | return; |
| 1020 | prx_log = fe; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1021 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1022 | if (s->cli_addr.ss_family == AF_INET) |
| 1023 | inet_ntop(AF_INET, |
| 1024 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 1025 | pn, sizeof(pn)); |
| 1026 | else |
| 1027 | inet_ntop(AF_INET6, |
| 1028 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 1029 | pn, sizeof(pn)); |
| 1030 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 1031 | get_localtime(s->logs.accept_date.tv_sec, &tm); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1032 | |
| 1033 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 1034 | tolog = fe->to_log; |
| 1035 | |
| 1036 | h = tmpline; |
| 1037 | if (fe->to_log & LW_REQHDR && |
| 1038 | txn->req.cap && |
| 1039 | (h < tmpline + sizeof(tmpline) - 10)) { |
| 1040 | *(h++) = ' '; |
| 1041 | *(h++) = '{'; |
| 1042 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 1043 | if (hdr) |
| 1044 | *(h++) = '|'; |
| 1045 | if (txn->req.cap[hdr] != NULL) |
| 1046 | h = encode_string(h, tmpline + sizeof(tmpline) - 7, |
| 1047 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 1048 | } |
| 1049 | *(h++) = '}'; |
| 1050 | } |
| 1051 | |
| 1052 | if (fe->to_log & LW_RSPHDR && |
| 1053 | txn->rsp.cap && |
| 1054 | (h < tmpline + sizeof(tmpline) - 7)) { |
| 1055 | *(h++) = ' '; |
| 1056 | *(h++) = '{'; |
| 1057 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 1058 | if (hdr) |
| 1059 | *(h++) = '|'; |
| 1060 | if (txn->rsp.cap[hdr] != NULL) |
| 1061 | h = encode_string(h, tmpline + sizeof(tmpline) - 4, |
| 1062 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 1063 | } |
| 1064 | *(h++) = '}'; |
| 1065 | } |
| 1066 | |
| 1067 | if (h < tmpline + sizeof(tmpline) - 4) { |
| 1068 | *(h++) = ' '; |
| 1069 | *(h++) = '"'; |
| 1070 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 1071 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 1072 | '#', url_encode_map, uri); |
| 1073 | *(h++) = '"'; |
| 1074 | } |
| 1075 | *h = '\0'; |
| 1076 | |
| 1077 | svid = (tolog & LW_SVID) ? |
| 1078 | (s->data_source != DATA_SRC_STATS) ? |
| 1079 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 1080 | |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1081 | t_request = -1; |
| 1082 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 1083 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 1084 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1085 | send_log(prx_log, LOG_INFO, |
| 1086 | "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]" |
| 1087 | " %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] | 1088 | " %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] | 1089 | pn, |
| 1090 | (s->cli_addr.ss_family == AF_INET) ? |
| 1091 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 1092 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1093 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 1094 | 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] | 1095 | fe->id, be->id, svid, |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1096 | t_request, |
| 1097 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1098 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 1099 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 1100 | (tolog & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 1101 | txn->status, |
Willy Tarreau | 8b3977f | 2008-01-18 11:16:32 +0100 | [diff] [blame] | 1102 | (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1103 | txn->cli_cookie ? txn->cli_cookie : "-", |
| 1104 | txn->srv_cookie ? txn->srv_cookie : "-", |
| 1105 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 1106 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 1107 | (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 1108 | (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-', |
| 1109 | 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] | 1110 | (s->flags & SN_REDISP)?"+":"", |
| 1111 | (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] | 1112 | s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline); |
| 1113 | |
| 1114 | s->logs.logwait = 0; |
| 1115 | } |
| 1116 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1117 | |
| 1118 | /* |
| 1119 | * Capture headers from message starting at <som> according to header list |
| 1120 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 1121 | */ |
| 1122 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1123 | char **cap, struct cap_hdr *cap_hdr) |
| 1124 | { |
| 1125 | char *eol, *sol, *col, *sov; |
| 1126 | int cur_idx; |
| 1127 | struct cap_hdr *h; |
| 1128 | int len; |
| 1129 | |
| 1130 | sol = som + hdr_idx_first_pos(idx); |
| 1131 | cur_idx = hdr_idx_first_idx(idx); |
| 1132 | |
| 1133 | while (cur_idx) { |
| 1134 | eol = sol + idx->v[cur_idx].len; |
| 1135 | |
| 1136 | col = sol; |
| 1137 | while (col < eol && *col != ':') |
| 1138 | col++; |
| 1139 | |
| 1140 | sov = col + 1; |
| 1141 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 1142 | sov++; |
| 1143 | |
| 1144 | for (h = cap_hdr; h; h = h->next) { |
| 1145 | if ((h->namelen == col - sol) && |
| 1146 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1147 | if (cap[h->index] == NULL) |
| 1148 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1149 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1150 | |
| 1151 | if (cap[h->index] == NULL) { |
| 1152 | Alert("HTTP capture : out of memory.\n"); |
| 1153 | continue; |
| 1154 | } |
| 1155 | |
| 1156 | len = eol - sov; |
| 1157 | if (len > h->len) |
| 1158 | len = h->len; |
| 1159 | |
| 1160 | memcpy(cap[h->index], sov, len); |
| 1161 | cap[h->index][len]=0; |
| 1162 | } |
| 1163 | } |
| 1164 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1165 | cur_idx = idx->v[cur_idx].next; |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1170 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1171 | */ |
| 1172 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1173 | |
| 1174 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1175 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1176 | */ |
| 1177 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1178 | ptr++; \ |
| 1179 | if (likely(ptr < end)) \ |
| 1180 | goto good; \ |
| 1181 | else { \ |
| 1182 | state = (st); \ |
| 1183 | goto http_msg_ood; \ |
| 1184 | } \ |
| 1185 | } while (0) |
| 1186 | |
| 1187 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1188 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1189 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1190 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1191 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1192 | * will give undefined results. |
| 1193 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1194 | * and that msg->sol points to the beginning of the response. |
| 1195 | * If a complete line is found (which implies that at least one CR or LF is |
| 1196 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1197 | * returned indicating an incomplete line (which does not mean that parts have |
| 1198 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1199 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1200 | * upon next call. |
| 1201 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1202 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1203 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1204 | * 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] | 1205 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1206 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1207 | const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, |
| 1208 | unsigned int state, const char *ptr, const char *end, |
| 1209 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1210 | { |
| 1211 | __label__ |
| 1212 | http_msg_rpver, |
| 1213 | http_msg_rpver_sp, |
| 1214 | http_msg_rpcode, |
| 1215 | http_msg_rpcode_sp, |
| 1216 | http_msg_rpreason, |
| 1217 | http_msg_rpline_eol, |
| 1218 | http_msg_ood, /* out of data */ |
| 1219 | http_msg_invalid; |
| 1220 | |
| 1221 | switch (state) { |
| 1222 | http_msg_rpver: |
| 1223 | case HTTP_MSG_RPVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1224 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1225 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1226 | |
| 1227 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1228 | msg->sl.st.v_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1229 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1230 | } |
| 1231 | goto http_msg_invalid; |
| 1232 | |
| 1233 | http_msg_rpver_sp: |
| 1234 | case HTTP_MSG_RPVER_SP: |
| 1235 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1236 | msg->sl.st.c = ptr - msg_buf; |
| 1237 | goto http_msg_rpcode; |
| 1238 | } |
| 1239 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1240 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1241 | /* so it's a CR/LF, this is invalid */ |
| 1242 | goto http_msg_invalid; |
| 1243 | |
| 1244 | http_msg_rpcode: |
| 1245 | case HTTP_MSG_RPCODE: |
| 1246 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1247 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1248 | |
| 1249 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1250 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 1251 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1252 | } |
| 1253 | |
| 1254 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1255 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 1256 | http_msg_rsp_reason: |
| 1257 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
| 1258 | msg->sl.st.r = ptr - msg_buf; |
| 1259 | msg->sl.st.r_l = 0; |
| 1260 | goto http_msg_rpline_eol; |
| 1261 | |
| 1262 | http_msg_rpcode_sp: |
| 1263 | case HTTP_MSG_RPCODE_SP: |
| 1264 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1265 | msg->sl.st.r = ptr - msg_buf; |
| 1266 | goto http_msg_rpreason; |
| 1267 | } |
| 1268 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1269 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1270 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1271 | goto http_msg_rsp_reason; |
| 1272 | |
| 1273 | http_msg_rpreason: |
| 1274 | case HTTP_MSG_RPREASON: |
| 1275 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1276 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
| 1277 | msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r; |
| 1278 | http_msg_rpline_eol: |
| 1279 | /* We have seen the end of line. Note that we do not |
| 1280 | * necessarily have the \n yet, but at least we know that we |
| 1281 | * have EITHER \r OR \n, otherwise the response would not be |
| 1282 | * complete. We can then record the response length and return |
| 1283 | * to the caller which will be able to register it. |
| 1284 | */ |
| 1285 | msg->sl.st.l = ptr - msg->sol; |
| 1286 | return ptr; |
| 1287 | |
| 1288 | #ifdef DEBUG_FULL |
| 1289 | default: |
| 1290 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1291 | exit(1); |
| 1292 | #endif |
| 1293 | } |
| 1294 | |
| 1295 | http_msg_ood: |
| 1296 | /* out of data */ |
| 1297 | if (ret_state) |
| 1298 | *ret_state = state; |
| 1299 | if (ret_ptr) |
| 1300 | *ret_ptr = (char *)ptr; |
| 1301 | return NULL; |
| 1302 | |
| 1303 | http_msg_invalid: |
| 1304 | /* invalid message */ |
| 1305 | if (ret_state) |
| 1306 | *ret_state = HTTP_MSG_ERROR; |
| 1307 | return NULL; |
| 1308 | } |
| 1309 | |
| 1310 | |
| 1311 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1312 | * This function parses a request line between <ptr> and <end>, starting with |
| 1313 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1314 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1315 | * will give undefined results. |
| 1316 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1317 | * and that msg->sol points to the beginning of the request. |
| 1318 | * If a complete line is found (which implies that at least one CR or LF is |
| 1319 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1320 | * returned indicating an incomplete line (which does not mean that parts have |
| 1321 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1322 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1323 | * upon next call. |
| 1324 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1325 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1326 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1327 | * 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] | 1328 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1329 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1330 | const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, |
| 1331 | unsigned int state, const char *ptr, const char *end, |
| 1332 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1333 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1334 | __label__ |
| 1335 | http_msg_rqmeth, |
| 1336 | http_msg_rqmeth_sp, |
| 1337 | http_msg_rquri, |
| 1338 | http_msg_rquri_sp, |
| 1339 | http_msg_rqver, |
| 1340 | http_msg_rqline_eol, |
| 1341 | http_msg_ood, /* out of data */ |
| 1342 | http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1343 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1344 | switch (state) { |
| 1345 | http_msg_rqmeth: |
| 1346 | case HTTP_MSG_RQMETH: |
| 1347 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1348 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1349 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1350 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1351 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1352 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1353 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1354 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1355 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1356 | /* HTTP 0.9 request */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1357 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1358 | http_msg_req09_uri: |
| 1359 | msg->sl.rq.u = ptr - msg_buf; |
| 1360 | http_msg_req09_uri_e: |
| 1361 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1362 | http_msg_req09_ver: |
| 1363 | msg->sl.rq.v = ptr - msg_buf; |
| 1364 | msg->sl.rq.v_l = 0; |
| 1365 | goto http_msg_rqline_eol; |
| 1366 | } |
| 1367 | goto http_msg_invalid; |
| 1368 | |
| 1369 | http_msg_rqmeth_sp: |
| 1370 | case HTTP_MSG_RQMETH_SP: |
| 1371 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1372 | msg->sl.rq.u = ptr - msg_buf; |
| 1373 | goto http_msg_rquri; |
| 1374 | } |
| 1375 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1376 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1377 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1378 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1379 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1380 | http_msg_rquri: |
| 1381 | case HTTP_MSG_RQURI: |
| 1382 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1383 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1384 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1385 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1386 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1387 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1388 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1389 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1390 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1391 | goto http_msg_req09_uri_e; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1392 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1393 | http_msg_rquri_sp: |
| 1394 | case HTTP_MSG_RQURI_SP: |
| 1395 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1396 | msg->sl.rq.v = ptr - msg_buf; |
| 1397 | goto http_msg_rqver; |
| 1398 | } |
| 1399 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1400 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1401 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1402 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1403 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1404 | http_msg_rqver: |
| 1405 | case HTTP_MSG_RQVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1406 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1407 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1408 | |
| 1409 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1410 | msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v; |
| 1411 | http_msg_rqline_eol: |
| 1412 | /* We have seen the end of line. Note that we do not |
| 1413 | * necessarily have the \n yet, but at least we know that we |
| 1414 | * have EITHER \r OR \n, otherwise the request would not be |
| 1415 | * complete. We can then record the request length and return |
| 1416 | * to the caller which will be able to register it. |
| 1417 | */ |
| 1418 | msg->sl.rq.l = ptr - msg->sol; |
| 1419 | return ptr; |
| 1420 | } |
| 1421 | |
| 1422 | /* neither an HTTP_VER token nor a CRLF */ |
| 1423 | goto http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1424 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1425 | #ifdef DEBUG_FULL |
| 1426 | default: |
| 1427 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1428 | exit(1); |
| 1429 | #endif |
| 1430 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1431 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1432 | http_msg_ood: |
| 1433 | /* out of data */ |
| 1434 | if (ret_state) |
| 1435 | *ret_state = state; |
| 1436 | if (ret_ptr) |
| 1437 | *ret_ptr = (char *)ptr; |
| 1438 | return NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1439 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1440 | http_msg_invalid: |
| 1441 | /* invalid message */ |
| 1442 | if (ret_state) |
| 1443 | *ret_state = HTTP_MSG_ERROR; |
| 1444 | return NULL; |
| 1445 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1446 | |
| 1447 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1448 | /* |
| 1449 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1450 | * depending on the initial msg->msg_state. It can be preempted everywhere |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1451 | * when data are missing and recalled at the exact same location with no |
| 1452 | * information loss. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1453 | * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other |
| 1454 | * fields. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1455 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1456 | void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx) |
| 1457 | { |
| 1458 | __label__ |
| 1459 | http_msg_rqbefore, |
| 1460 | http_msg_rqbefore_cr, |
| 1461 | http_msg_rqmeth, |
| 1462 | http_msg_rqline_end, |
| 1463 | http_msg_hdr_first, |
| 1464 | http_msg_hdr_name, |
| 1465 | http_msg_hdr_l1_sp, |
| 1466 | http_msg_hdr_l1_lf, |
| 1467 | http_msg_hdr_l1_lws, |
| 1468 | http_msg_hdr_val, |
| 1469 | http_msg_hdr_l2_lf, |
| 1470 | http_msg_hdr_l2_lws, |
| 1471 | http_msg_complete_header, |
| 1472 | http_msg_last_lf, |
| 1473 | http_msg_ood, /* out of data */ |
| 1474 | http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1475 | |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1476 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1477 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1478 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1479 | state = msg->msg_state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1480 | ptr = buf->lr; |
| 1481 | end = buf->r; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1482 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1483 | if (unlikely(ptr >= end)) |
| 1484 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1485 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1486 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1487 | /* |
| 1488 | * First, states that are specific to the response only. |
| 1489 | * We check them first so that request and headers are |
| 1490 | * closer to each other (accessed more often). |
| 1491 | */ |
| 1492 | http_msg_rpbefore: |
| 1493 | case HTTP_MSG_RPBEFORE: |
| 1494 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1495 | if (likely(ptr == buf->data)) { |
| 1496 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1497 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1498 | } else { |
| 1499 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1500 | /* only skip empty leading lines, don't remove them */ |
| 1501 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1502 | msg->som = ptr - buf->data; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1503 | #else |
| 1504 | /* Remove empty leading lines, as recommended by |
| 1505 | * RFC2616. This takes a lot of time because we |
| 1506 | * must move all the buffer backwards, but this |
| 1507 | * is rarely needed. The method above will be |
| 1508 | * cleaner when we'll be able to start sending |
| 1509 | * the request from any place in the buffer. |
| 1510 | */ |
| 1511 | buf->lr = ptr; |
| 1512 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1513 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1514 | msg->sol = buf->data; |
| 1515 | ptr = buf->data; |
| 1516 | end = buf->r; |
| 1517 | #endif |
| 1518 | } |
| 1519 | hdr_idx_init(idx); |
| 1520 | state = HTTP_MSG_RPVER; |
| 1521 | goto http_msg_rpver; |
| 1522 | } |
| 1523 | |
| 1524 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1525 | goto http_msg_invalid; |
| 1526 | |
| 1527 | if (unlikely(*ptr == '\n')) |
| 1528 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1529 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1530 | /* stop here */ |
| 1531 | |
| 1532 | http_msg_rpbefore_cr: |
| 1533 | case HTTP_MSG_RPBEFORE_CR: |
| 1534 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1535 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1536 | /* stop here */ |
| 1537 | |
| 1538 | http_msg_rpver: |
| 1539 | case HTTP_MSG_RPVER: |
| 1540 | case HTTP_MSG_RPVER_SP: |
| 1541 | case HTTP_MSG_RPCODE: |
| 1542 | case HTTP_MSG_RPCODE_SP: |
| 1543 | case HTTP_MSG_RPREASON: |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1544 | ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1545 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1546 | if (unlikely(!ptr)) |
| 1547 | return; |
| 1548 | |
| 1549 | /* we have a full response and we know that we have either a CR |
| 1550 | * or an LF at <ptr>. |
| 1551 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1552 | //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] | 1553 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1554 | |
| 1555 | msg->sol = ptr; |
| 1556 | if (likely(*ptr == '\r')) |
| 1557 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1558 | goto http_msg_rpline_end; |
| 1559 | |
| 1560 | http_msg_rpline_end: |
| 1561 | case HTTP_MSG_RPLINE_END: |
| 1562 | /* msg->sol must point to the first of CR or LF. */ |
| 1563 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1564 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1565 | /* stop here */ |
| 1566 | |
| 1567 | /* |
| 1568 | * Second, states that are specific to the request only |
| 1569 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1570 | http_msg_rqbefore: |
| 1571 | case HTTP_MSG_RQBEFORE: |
| 1572 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1573 | if (likely(ptr == buf->data)) { |
| 1574 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1575 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1576 | } else { |
| 1577 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1578 | /* only skip empty leading lines, don't remove them */ |
| 1579 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1580 | msg->som = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1581 | #else |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1582 | /* Remove empty leading lines, as recommended by |
| 1583 | * RFC2616. This takes a lot of time because we |
| 1584 | * must move all the buffer backwards, but this |
| 1585 | * is rarely needed. The method above will be |
| 1586 | * cleaner when we'll be able to start sending |
| 1587 | * the request from any place in the buffer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1588 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1589 | buf->lr = ptr; |
| 1590 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1591 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1592 | msg->sol = buf->data; |
| 1593 | ptr = buf->data; |
| 1594 | end = buf->r; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1595 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1596 | } |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1597 | /* we will need this when keep-alive will be supported |
| 1598 | hdr_idx_init(idx); |
| 1599 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1600 | state = HTTP_MSG_RQMETH; |
| 1601 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1602 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1603 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1604 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1605 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1606 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1607 | if (unlikely(*ptr == '\n')) |
| 1608 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1609 | 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] | 1610 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1611 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1612 | http_msg_rqbefore_cr: |
| 1613 | case HTTP_MSG_RQBEFORE_CR: |
| 1614 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1615 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1616 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1617 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1618 | http_msg_rqmeth: |
| 1619 | case HTTP_MSG_RQMETH: |
| 1620 | case HTTP_MSG_RQMETH_SP: |
| 1621 | case HTTP_MSG_RQURI: |
| 1622 | case HTTP_MSG_RQURI_SP: |
| 1623 | case HTTP_MSG_RQVER: |
| 1624 | ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1625 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1626 | if (unlikely(!ptr)) |
| 1627 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1628 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1629 | /* we have a full request and we know that we have either a CR |
| 1630 | * or an LF at <ptr>. |
| 1631 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1632 | //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] | 1633 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1634 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1635 | msg->sol = ptr; |
| 1636 | if (likely(*ptr == '\r')) |
| 1637 | 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] | 1638 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1639 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1640 | http_msg_rqline_end: |
| 1641 | case HTTP_MSG_RQLINE_END: |
| 1642 | /* check for HTTP/0.9 request : no version information available. |
| 1643 | * msg->sol must point to the first of CR or LF. |
| 1644 | */ |
| 1645 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1646 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1647 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1648 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1649 | 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] | 1650 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1651 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1652 | /* |
| 1653 | * Common states below |
| 1654 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1655 | http_msg_hdr_first: |
| 1656 | case HTTP_MSG_HDR_FIRST: |
| 1657 | msg->sol = ptr; |
| 1658 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1659 | goto http_msg_hdr_name; |
| 1660 | } |
| 1661 | |
| 1662 | if (likely(*ptr == '\r')) |
| 1663 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1664 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1665 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1666 | http_msg_hdr_name: |
| 1667 | case HTTP_MSG_HDR_NAME: |
| 1668 | /* assumes msg->sol points to the first char */ |
| 1669 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1670 | 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] | 1671 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1672 | if (likely(*ptr == ':')) { |
| 1673 | msg->col = ptr - buf->data; |
| 1674 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| 1675 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1676 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1677 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1678 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1679 | http_msg_hdr_l1_sp: |
| 1680 | case HTTP_MSG_HDR_L1_SP: |
| 1681 | /* assumes msg->sol points to the first char and msg->col to the colon */ |
| 1682 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1683 | 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] | 1684 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1685 | /* header value can be basically anything except CR/LF */ |
| 1686 | msg->sov = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1687 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1688 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1689 | goto http_msg_hdr_val; |
| 1690 | } |
| 1691 | |
| 1692 | if (likely(*ptr == '\r')) |
| 1693 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1694 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1695 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1696 | http_msg_hdr_l1_lf: |
| 1697 | case HTTP_MSG_HDR_L1_LF: |
| 1698 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1699 | 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] | 1700 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1701 | http_msg_hdr_l1_lws: |
| 1702 | case HTTP_MSG_HDR_L1_LWS: |
| 1703 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1704 | /* replace HT,CR,LF with spaces */ |
| 1705 | for (; buf->data+msg->sov < ptr; msg->sov++) |
| 1706 | buf->data[msg->sov] = ' '; |
| 1707 | goto http_msg_hdr_l1_sp; |
| 1708 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1709 | /* we had a header consisting only in spaces ! */ |
| 1710 | msg->eol = buf->data + msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1711 | goto http_msg_complete_header; |
| 1712 | |
| 1713 | http_msg_hdr_val: |
| 1714 | case HTTP_MSG_HDR_VAL: |
| 1715 | /* assumes msg->sol points to the first char, msg->col to the |
| 1716 | * colon, and msg->sov points to the first character of the |
| 1717 | * value. |
| 1718 | */ |
| 1719 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1720 | 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] | 1721 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1722 | msg->eol = ptr; |
| 1723 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1724 | * real header end in case it ends with lots of LWS, but is this |
| 1725 | * really needed ? |
| 1726 | */ |
| 1727 | if (likely(*ptr == '\r')) |
| 1728 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1729 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1730 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1731 | http_msg_hdr_l2_lf: |
| 1732 | case HTTP_MSG_HDR_L2_LF: |
| 1733 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1734 | 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] | 1735 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1736 | http_msg_hdr_l2_lws: |
| 1737 | case HTTP_MSG_HDR_L2_LWS: |
| 1738 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1739 | /* LWS: replace HT,CR,LF with spaces */ |
| 1740 | for (; msg->eol < ptr; msg->eol++) |
| 1741 | *msg->eol = ' '; |
| 1742 | goto http_msg_hdr_val; |
| 1743 | } |
| 1744 | http_msg_complete_header: |
| 1745 | /* |
| 1746 | * It was a new header, so the last one is finished. |
| 1747 | * Assumes msg->sol points to the first char, msg->col to the |
| 1748 | * colon, msg->sov points to the first character of the value |
| 1749 | * and msg->eol to the first CR or LF so we know how the line |
| 1750 | * ends. We insert last header into the index. |
| 1751 | */ |
| 1752 | /* |
| 1753 | fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol); |
| 1754 | write(2, msg->sol, msg->eol-msg->sol); |
| 1755 | fprintf(stderr,"\n"); |
| 1756 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1757 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1758 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r', |
| 1759 | idx, idx->tail) < 0)) |
| 1760 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1761 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1762 | msg->sol = ptr; |
| 1763 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1764 | goto http_msg_hdr_name; |
| 1765 | } |
| 1766 | |
| 1767 | if (likely(*ptr == '\r')) |
| 1768 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1769 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1770 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1771 | http_msg_last_lf: |
| 1772 | case HTTP_MSG_LAST_LF: |
| 1773 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1774 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1775 | ptr++; |
| 1776 | buf->lr = ptr; |
| 1777 | msg->eoh = msg->sol - buf->data; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1778 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1779 | return; |
| 1780 | #ifdef DEBUG_FULL |
| 1781 | default: |
| 1782 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1783 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1784 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1785 | } |
| 1786 | http_msg_ood: |
| 1787 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1788 | msg->msg_state = state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1789 | buf->lr = ptr; |
| 1790 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1791 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1792 | http_msg_invalid: |
| 1793 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1794 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1795 | return; |
| 1796 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1797 | |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1798 | /* This function performs all the processing enabled for the current request. |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1799 | * It normally returns zero, but may return 1 if it absolutely needs to be |
| 1800 | * called again after other functions. It relies on buffers flags, and updates |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1801 | * 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] | 1802 | * functions. Its behaviour is rather simple : |
| 1803 | * - all enabled analysers are called in turn from the lower to the higher |
| 1804 | * bit. |
| 1805 | * - 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] | 1806 | * 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] | 1807 | * that unprocessed data will not be forwarded. But that probably depends on |
| 1808 | * the protocol. Generally it is not reset in case of errors. |
| 1809 | * - 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] | 1810 | * analyser without touching BF_WRITE_ENA (it is enabled prior to |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 1811 | * analysis). |
| 1812 | * - if an analyser thinks it has no added value anymore staying here, it must |
| 1813 | * reset its bit from the analysers flags in order not to be called anymore. |
| 1814 | * |
| 1815 | * In the future, analysers should be able to indicate that they want to be |
| 1816 | * called after XXX bytes have been received (or transfered), and the min of |
| 1817 | * all's wishes will be used to ring back (unless a special condition occurs). |
| 1818 | * |
| 1819 | * |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1820 | */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1821 | int process_request(struct session *t) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1822 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1823 | struct buffer *req = t->req; |
| 1824 | struct buffer *rep = t->rep; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1825 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 1826 | 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] | 1827 | now_ms, __FUNCTION__, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 1828 | t, |
| 1829 | req, |
| 1830 | req->rex, req->wex, |
| 1831 | req->flags, |
| 1832 | req->l, |
| 1833 | req->analysers); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1834 | |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 1835 | /* The tcp-inspect analyser is always called alone */ |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1836 | if (req->analysers & AN_REQ_INSPECT) { |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1837 | struct tcp_rule *rule; |
| 1838 | int partial; |
| 1839 | |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 1840 | /* We will abort if we encounter a read error. In theory, we |
| 1841 | * should not abort if we get a close, it might be valid, |
| 1842 | * although very unlikely. FIXME: we'll abort for now, this |
| 1843 | * will be easier to change later. |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1844 | */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1845 | if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1846 | req->analysers = 0; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 1847 | //t->fe->failed_req++; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1848 | if (!(t->flags & SN_ERR_MASK)) |
| 1849 | t->flags |= SN_ERR_CLICL; |
| 1850 | if (!(t->flags & SN_FINST_MASK)) |
| 1851 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1852 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1853 | } |
| 1854 | |
| 1855 | /* Abort if client read timeout has expired */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1856 | else if (req->flags & BF_READ_TIMEOUT) { |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1857 | req->analysers = 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1858 | t->fe->failed_req++; |
| 1859 | if (!(t->flags & SN_ERR_MASK)) |
| 1860 | t->flags |= SN_ERR_CLITO; |
| 1861 | if (!(t->flags & SN_FINST_MASK)) |
| 1862 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1863 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1864 | } |
| 1865 | |
| 1866 | /* We don't know whether we have enough data, so must proceed |
| 1867 | * this way : |
| 1868 | * - iterate through all rules in their declaration order |
| 1869 | * - if one rule returns MISS, it means the inspect delay is |
| 1870 | * not over yet, then return immediately, otherwise consider |
| 1871 | * it as a non-match. |
| 1872 | * - if one rule returns OK, then return OK |
| 1873 | * - if one rule returns KO, then return KO |
| 1874 | */ |
| 1875 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 1876 | 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] | 1877 | partial = 0; |
| 1878 | else |
| 1879 | partial = ACL_PARTIAL; |
| 1880 | |
| 1881 | list_for_each_entry(rule, &t->fe->tcp_req.inspect_rules, list) { |
| 1882 | int ret = ACL_PAT_PASS; |
| 1883 | |
| 1884 | if (rule->cond) { |
| 1885 | ret = acl_exec_cond(rule->cond, t->fe, t, NULL, ACL_DIR_REQ | partial); |
| 1886 | if (ret == ACL_PAT_MISS) { |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 1887 | buffer_write_dis(req); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1888 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 1889 | if (!tick_isset(req->analyse_exp)) |
| 1890 | 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] | 1891 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1892 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1893 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1894 | ret = acl_pass(ret); |
| 1895 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 1896 | ret = !ret; |
| 1897 | } |
| 1898 | |
| 1899 | if (ret) { |
| 1900 | /* we have a matching rule. */ |
| 1901 | if (rule->action == TCP_ACT_REJECT) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1902 | buffer_abort(req); |
| 1903 | buffer_abort(rep); |
| 1904 | //FIXME: this delete this |
| 1905 | //fd_delete(t->cli_fd); |
| 1906 | //t->cli_state = CL_STCLOSE; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1907 | req->analysers = 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1908 | t->fe->failed_req++; |
| 1909 | if (!(t->flags & SN_ERR_MASK)) |
| 1910 | t->flags |= SN_ERR_PRXCOND; |
| 1911 | if (!(t->flags & SN_FINST_MASK)) |
| 1912 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1913 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1914 | } |
| 1915 | /* otherwise accept */ |
| 1916 | break; |
| 1917 | } |
| 1918 | } |
| 1919 | |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1920 | /* if we get there, it means we have no rule which matches, or |
| 1921 | * we have an explicit accept, so we apply the default accept. |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1922 | */ |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1923 | req->analysers &= ~AN_REQ_INSPECT; |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 1924 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1925 | } |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1926 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1927 | if (req->analysers & AN_REQ_HTTP_HDR) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1928 | /* |
| 1929 | * Now parse the partial (or complete) lines. |
| 1930 | * We will check the request syntax, and also join multi-line |
| 1931 | * headers. An index of all the lines will be elaborated while |
| 1932 | * parsing. |
| 1933 | * |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1934 | * For the parsing, we use a 28 states FSM. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1935 | * |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1936 | * Here is the information we currently have : |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1937 | * req->data + req->som = beginning of request |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1938 | * req->data + req->eoh = end of processed headers / start of current one |
| 1939 | * req->data + req->eol = end of current header or line (LF or CRLF) |
| 1940 | * req->lr = first non-visited byte |
| 1941 | * req->r = end of data |
| 1942 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1943 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1944 | int cur_idx; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1945 | struct http_txn *txn = &t->txn; |
| 1946 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1947 | struct proxy *cur_proxy; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1948 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1949 | if (likely(req->lr < req->r)) |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1950 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1951 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1952 | /* 1: we might have to print this header in debug mode */ |
| 1953 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1954 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1955 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1956 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1957 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1958 | sol = req->data + msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1959 | eol = sol + msg->sl.rq.l; |
| 1960 | debug_hdr("clireq", t, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 1961 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1962 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 1963 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1964 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1965 | while (cur_idx) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1966 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1967 | debug_hdr("clihdr", t, sol, eol); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1968 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 1969 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1970 | } |
| 1971 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1972 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1973 | |
| 1974 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1975 | * Now we quickly check if we have found a full valid request. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1976 | * If not so, we check the FD and buffer states before leaving. |
| 1977 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1978 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 1979 | * requests are checked first. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1980 | * |
| 1981 | */ |
| 1982 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1983 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1984 | /* |
| 1985 | * First, let's catch bad requests. |
| 1986 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1987 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1988 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1989 | |
| 1990 | /* 1: Since we are in header mode, if there's no space |
| 1991 | * left for headers, we won't be able to free more |
| 1992 | * later, so the session will never terminate. We |
| 1993 | * must terminate it now. |
| 1994 | */ |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 1995 | if (unlikely(req->flags & BF_FULL)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1996 | /* FIXME: check if URI is set and return Status |
| 1997 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1998 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1999 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2000 | } |
| 2001 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2002 | /* 2: have we encountered a read error ? */ |
| 2003 | else if (req->flags & BF_READ_ERROR) { |
| 2004 | /* we cannot return any message on error */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2005 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2006 | req->analysers = 0; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2007 | //t->fe->failed_req++; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2008 | if (!(t->flags & SN_ERR_MASK)) |
| 2009 | t->flags |= SN_ERR_CLICL; |
| 2010 | if (!(t->flags & SN_FINST_MASK)) |
| 2011 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2012 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2013 | } |
| 2014 | |
| 2015 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 2016 | 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] | 2017 | /* read timeout : give up with an error message. */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2018 | txn->status = 408; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2019 | client_retnclose(t, error_message(t, HTTP_ERR_408)); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2020 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2021 | req->analysers = 0; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 2022 | t->fe->failed_req++; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2023 | if (!(t->flags & SN_ERR_MASK)) |
| 2024 | t->flags |= SN_ERR_CLITO; |
| 2025 | if (!(t->flags & SN_FINST_MASK)) |
| 2026 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2027 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2028 | } |
| 2029 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2030 | /* 4: have we encountered a close ? */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 2031 | else if (req->flags & BF_SHUTR) { |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2032 | txn->status = 400; |
| 2033 | client_retnclose(t, error_message(t, HTTP_ERR_400)); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2034 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2035 | req->analysers = 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2036 | t->fe->failed_req++; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2037 | |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2038 | if (!(t->flags & SN_ERR_MASK)) |
| 2039 | t->flags |= SN_ERR_CLICL; |
| 2040 | if (!(t->flags & SN_FINST_MASK)) |
| 2041 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2042 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2043 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2044 | |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 2045 | buffer_write_dis(req); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2046 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 2047 | if (!tick_isset(req->analyse_exp)) |
| 2048 | req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2049 | |
| 2050 | /* we're not ready yet */ |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2051 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2052 | } |
| 2053 | |
| 2054 | |
| 2055 | /**************************************************************** |
| 2056 | * More interesting part now : we know that we have a complete * |
| 2057 | * request which at least looks like HTTP. We have an indicator * |
| 2058 | * of each header's length, so we can parse them quickly. * |
| 2059 | ****************************************************************/ |
| 2060 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2061 | req->analysers &= ~AN_REQ_HTTP_HDR; |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 2062 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 2063 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2064 | /* ensure we keep this pointer to the beginning of the message */ |
| 2065 | msg->sol = req->data + msg->som; |
| 2066 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2067 | /* |
| 2068 | * 1: identify the method |
| 2069 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2070 | 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] | 2071 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2072 | /* we can make use of server redirect on GET and HEAD */ |
| 2073 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2074 | t->flags |= SN_REDIRECTABLE; |
| 2075 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2076 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2077 | * 2: check if the URI matches the monitor_uri. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2078 | * We have to do this for every request which gets in, because |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2079 | * the monitor-uri is defined by the frontend. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2080 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2081 | if (unlikely((t->fe->monitor_uri_len != 0) && |
| 2082 | (t->fe->monitor_uri_len == msg->sl.rq.u_l) && |
| 2083 | !memcmp(&req->data[msg->sl.rq.u], |
| 2084 | t->fe->monitor_uri, |
| 2085 | t->fe->monitor_uri_len))) { |
| 2086 | /* |
| 2087 | * We have found the monitor URI |
| 2088 | */ |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2089 | struct acl_cond *cond; |
| 2090 | cur_proxy = t->fe; |
| 2091 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2092 | t->flags |= SN_MONITOR; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2093 | |
| 2094 | /* Check if we want to fail this monitor request or not */ |
| 2095 | list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) { |
| 2096 | 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] | 2097 | |
| 2098 | ret = acl_pass(ret); |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2099 | if (cond->pol == ACL_COND_UNLESS) |
| 2100 | ret = !ret; |
| 2101 | |
| 2102 | if (ret) { |
| 2103 | /* we fail this request, let's return 503 service unavail */ |
| 2104 | txn->status = 503; |
| 2105 | client_retnclose(t, error_message(t, HTTP_ERR_503)); |
| 2106 | goto return_prx_cond; |
| 2107 | } |
| 2108 | } |
| 2109 | |
| 2110 | /* nothing to fail, let's reply normaly */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2111 | txn->status = 200; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2112 | client_retnclose(t, &http_200_chunk); |
| 2113 | goto return_prx_cond; |
| 2114 | } |
| 2115 | |
| 2116 | /* |
| 2117 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2118 | * Note: we cannot log anymore if the request has been |
| 2119 | * classified as invalid. |
| 2120 | */ |
| 2121 | if (unlikely(t->logs.logwait & LW_REQ)) { |
| 2122 | /* we have a complete HTTP request that we must log */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 2123 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2124 | int urilen = msg->sl.rq.l; |
| 2125 | |
| 2126 | if (urilen >= REQURI_LEN) |
| 2127 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2128 | memcpy(txn->uri, &req->data[msg->som], urilen); |
| 2129 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2130 | |
| 2131 | if (!(t->logs.logwait &= ~LW_REQ)) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 2132 | http_sess_log(t); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2133 | } else { |
| 2134 | Alert("HTTP logging : out of memory.\n"); |
| 2135 | } |
| 2136 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2137 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2138 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2139 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 2140 | if (unlikely(msg->sl.rq.v_l == 0)) { |
| 2141 | int delta; |
| 2142 | char *cur_end; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2143 | msg->sol = req->data + msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2144 | cur_end = msg->sol + msg->sl.rq.l; |
| 2145 | delta = 0; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2146 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2147 | if (msg->sl.rq.u_l == 0) { |
| 2148 | /* if no URI was set, add "/" */ |
| 2149 | delta = buffer_replace2(req, cur_end, cur_end, " /", 2); |
| 2150 | cur_end += delta; |
| 2151 | msg->eoh += delta; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2152 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2153 | /* add HTTP version */ |
| 2154 | delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11); |
| 2155 | msg->eoh += delta; |
| 2156 | cur_end += delta; |
| 2157 | cur_end = (char *)http_parse_reqline(msg, req->data, |
| 2158 | HTTP_MSG_RQMETH, |
| 2159 | msg->sol, cur_end + 1, |
| 2160 | NULL, NULL); |
| 2161 | if (unlikely(!cur_end)) |
| 2162 | goto return_bad_req; |
| 2163 | |
| 2164 | /* we have a full HTTP/1.0 request now and we know that |
| 2165 | * we have either a CR or an LF at <ptr>. |
| 2166 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2167 | 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] | 2168 | } |
| 2169 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2170 | |
| 2171 | /* 5: we may need to capture headers */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2172 | if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap)) |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 2173 | capture_headers(req->data + msg->som, &txn->hdr_idx, |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2174 | txn->req.cap, t->fe->req_cap); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2175 | |
| 2176 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2177 | * 6: we will have to evaluate the filters. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2178 | * As opposed to version 1.2, now they will be evaluated in the |
| 2179 | * filters order and not in the header order. This means that |
| 2180 | * each filter has to be validated among all headers. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2181 | * |
| 2182 | * We can now check whether we want to switch to another |
| 2183 | * backend, in which case we will re-check the backend's |
| 2184 | * filters and various options. In order to support 3-level |
| 2185 | * switching, here's how we should proceed : |
| 2186 | * |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2187 | * a) run be. |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2188 | * if (switch) then switch ->be to the new backend. |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2189 | * b) run be if (be != fe). |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2190 | * There cannot be any switch from there, so ->be cannot be |
| 2191 | * changed anymore. |
| 2192 | * |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2193 | * => filters always apply to ->be, then ->be may change. |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2194 | * |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2195 | * The response path will be able to apply either ->be, or |
| 2196 | * ->be then ->fe filters in order to match the reverse of |
| 2197 | * the forward sequence. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2198 | */ |
| 2199 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2200 | do { |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2201 | struct acl_cond *cond; |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2202 | struct redirect_rule *rule; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2203 | struct proxy *rule_set = t->be; |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2204 | cur_proxy = t->be; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2205 | |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2206 | /* first check whether we have some ACLs set to redirect this request */ |
| 2207 | list_for_each_entry(rule, &cur_proxy->redirect_rules, list) { |
| 2208 | 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] | 2209 | |
| 2210 | ret = acl_pass(ret); |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2211 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2212 | ret = !ret; |
| 2213 | |
| 2214 | if (ret) { |
| 2215 | struct chunk rdr = { trash, 0 }; |
| 2216 | const char *msg_fmt; |
| 2217 | |
| 2218 | /* build redirect message */ |
| 2219 | switch(rule->code) { |
| 2220 | case 303: |
| 2221 | rdr.len = strlen(HTTP_303); |
| 2222 | msg_fmt = HTTP_303; |
| 2223 | break; |
| 2224 | case 301: |
| 2225 | rdr.len = strlen(HTTP_301); |
| 2226 | msg_fmt = HTTP_301; |
| 2227 | break; |
| 2228 | case 302: |
| 2229 | default: |
| 2230 | rdr.len = strlen(HTTP_302); |
| 2231 | msg_fmt = HTTP_302; |
| 2232 | break; |
| 2233 | } |
| 2234 | |
| 2235 | if (unlikely(rdr.len > sizeof(trash))) |
| 2236 | goto return_bad_req; |
| 2237 | memcpy(rdr.str, msg_fmt, rdr.len); |
| 2238 | |
| 2239 | switch(rule->type) { |
| 2240 | case REDIRECT_TYPE_PREFIX: { |
| 2241 | const char *path; |
| 2242 | int pathlen; |
| 2243 | |
| 2244 | path = http_get_path(txn); |
| 2245 | /* build message using path */ |
| 2246 | if (path) { |
| 2247 | pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path; |
| 2248 | } else { |
| 2249 | path = "/"; |
| 2250 | pathlen = 1; |
| 2251 | } |
| 2252 | |
| 2253 | if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4) |
| 2254 | goto return_bad_req; |
| 2255 | |
| 2256 | /* add prefix */ |
| 2257 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 2258 | rdr.len += rule->rdr_len; |
| 2259 | |
| 2260 | /* add path */ |
| 2261 | memcpy(rdr.str + rdr.len, path, pathlen); |
| 2262 | rdr.len += pathlen; |
| 2263 | break; |
| 2264 | } |
| 2265 | case REDIRECT_TYPE_LOCATION: |
| 2266 | default: |
| 2267 | if (rdr.len + rule->rdr_len > sizeof(trash) - 4) |
| 2268 | goto return_bad_req; |
| 2269 | |
| 2270 | /* add location */ |
| 2271 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 2272 | rdr.len += rule->rdr_len; |
| 2273 | break; |
| 2274 | } |
| 2275 | |
| 2276 | /* add end of headers */ |
| 2277 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 2278 | rdr.len += 4; |
| 2279 | |
| 2280 | txn->status = rule->code; |
| 2281 | /* let's log the request time */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2282 | t->logs.tv_request = now; |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2283 | client_retnclose(t, &rdr); |
| 2284 | goto return_prx_cond; |
| 2285 | } |
| 2286 | } |
| 2287 | |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2288 | /* first check whether we have some ACLs set to block this request */ |
| 2289 | list_for_each_entry(cond, &cur_proxy->block_cond, list) { |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 2290 | 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] | 2291 | |
| 2292 | ret = acl_pass(ret); |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2293 | if (cond->pol == ACL_COND_UNLESS) |
| 2294 | ret = !ret; |
| 2295 | |
| 2296 | if (ret) { |
| 2297 | txn->status = 403; |
| 2298 | /* let's log the request time */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2299 | t->logs.tv_request = now; |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2300 | client_retnclose(t, error_message(t, HTTP_ERR_403)); |
| 2301 | goto return_prx_cond; |
| 2302 | } |
| 2303 | } |
| 2304 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2305 | /* try headers filters */ |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2306 | if (rule_set->req_exp != NULL) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2307 | if (apply_filters_to_request(t, req, rule_set->req_exp) < 0) |
| 2308 | goto return_bad_req; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2309 | } |
| 2310 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2311 | if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) { |
| 2312 | /* to ensure correct connection accounting on |
| 2313 | * the backend, we count the connection for the |
| 2314 | * one managing the queue. |
| 2315 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2316 | t->be->beconn++; |
| 2317 | if (t->be->beconn > t->be->beconn_max) |
| 2318 | t->be->beconn_max = t->be->beconn; |
| 2319 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2320 | t->flags |= SN_BE_ASSIGNED; |
| 2321 | } |
| 2322 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2323 | /* has the request been denied ? */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2324 | if (txn->flags & TX_CLDENY) { |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2325 | /* no need to go further */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2326 | txn->status = 403; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2327 | /* let's log the request time */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2328 | t->logs.tv_request = now; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2329 | client_retnclose(t, error_message(t, HTTP_ERR_403)); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2330 | goto return_prx_cond; |
| 2331 | } |
| 2332 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2333 | /* We might have to check for "Connection:" */ |
Krzysztof Oledzki | 336d475 | 2007-12-25 02:40:22 +0100 | [diff] [blame] | 2334 | 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] | 2335 | !(t->flags & SN_CONN_CLOSED)) { |
| 2336 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2337 | int cur_idx, old_idx, delta, val; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2338 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2339 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2340 | 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] | 2341 | old_idx = 0; |
| 2342 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2343 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 2344 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2345 | cur_ptr = cur_next; |
| 2346 | cur_end = cur_ptr + cur_hdr->len; |
| 2347 | cur_next = cur_end + cur_hdr->cr + 1; |
| 2348 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2349 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 2350 | if (val) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2351 | /* 3 possibilities : |
| 2352 | * - we have already set Connection: close, |
| 2353 | * so we remove this line. |
| 2354 | * - we have not yet set Connection: close, |
| 2355 | * but this line indicates close. We leave |
| 2356 | * it untouched and set the flag. |
| 2357 | * - we have not yet set Connection: close, |
| 2358 | * and this line indicates non-close. We |
| 2359 | * replace it. |
| 2360 | */ |
| 2361 | if (t->flags & SN_CONN_CLOSED) { |
| 2362 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2363 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2364 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2365 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 2366 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2367 | cur_hdr->len = 0; |
| 2368 | } else { |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2369 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 2370 | delta = buffer_replace2(req, cur_ptr + val, cur_end, |
| 2371 | "close", 5); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2372 | cur_next += delta; |
| 2373 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2374 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2375 | } |
| 2376 | t->flags |= SN_CONN_CLOSED; |
| 2377 | } |
| 2378 | } |
| 2379 | old_idx = cur_idx; |
| 2380 | } |
Willy Tarreau | f2f0ee8 | 2007-03-30 12:02:43 +0200 | [diff] [blame] | 2381 | } |
| 2382 | /* add request headers from the rule sets in the same order */ |
| 2383 | for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) { |
| 2384 | if (unlikely(http_header_add_tail(req, |
| 2385 | &txn->req, |
| 2386 | &txn->hdr_idx, |
| 2387 | rule_set->req_add[cur_idx])) < 0) |
| 2388 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2389 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2390 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2391 | /* check if stats URI was requested, and if an auth is needed */ |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 2392 | if (rule_set->uri_auth != NULL && |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2393 | (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) { |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2394 | /* we have to check the URI and auth for this request. |
| 2395 | * FIXME!!! that one is rather dangerous, we want to |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2396 | * make it follow standard rules (eg: clear req->analysers). |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2397 | */ |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2398 | if (stats_check_uri_auth(t, rule_set)) |
| 2399 | return 1; |
| 2400 | } |
| 2401 | |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2402 | /* now check whether we have some switching rules for this request */ |
| 2403 | if (!(t->flags & SN_BE_ASSIGNED)) { |
| 2404 | struct switching_rule *rule; |
| 2405 | |
| 2406 | list_for_each_entry(rule, &cur_proxy->switching_rules, list) { |
| 2407 | int ret; |
| 2408 | |
| 2409 | 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] | 2410 | |
| 2411 | ret = acl_pass(ret); |
Willy Tarreau | a8cfa34 | 2008-07-09 11:23:31 +0200 | [diff] [blame] | 2412 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2413 | ret = !ret; |
| 2414 | |
| 2415 | if (ret) { |
| 2416 | t->be = rule->be.backend; |
| 2417 | t->be->beconn++; |
| 2418 | if (t->be->beconn > t->be->beconn_max) |
| 2419 | t->be->beconn_max = t->be->beconn; |
| 2420 | t->be->cum_beconn++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2421 | |
| 2422 | /* assign new parameters to the session from the new backend */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2423 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 2424 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2425 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2426 | t->flags |= SN_BE_ASSIGNED; |
| 2427 | break; |
| 2428 | } |
| 2429 | } |
| 2430 | } |
| 2431 | |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 2432 | if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) { |
| 2433 | /* No backend was set, but there was a default |
| 2434 | * backend set in the frontend, so we use it and |
| 2435 | * loop again. |
| 2436 | */ |
| 2437 | t->be = cur_proxy->defbe.be; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2438 | t->be->beconn++; |
| 2439 | if (t->be->beconn > t->be->beconn_max) |
| 2440 | t->be->beconn_max = t->be->beconn; |
| 2441 | t->be->cum_beconn++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2442 | |
| 2443 | /* assign new parameters to the session from the new backend */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2444 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 2445 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2446 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 2447 | t->flags |= SN_BE_ASSIGNED; |
| 2448 | } |
| 2449 | } 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] | 2450 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2451 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2452 | if (!(t->flags & SN_BE_ASSIGNED)) { |
| 2453 | /* To ensure correct connection accounting on |
| 2454 | * the backend, we count the connection for the |
| 2455 | * one managing the queue. |
| 2456 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2457 | t->be->beconn++; |
| 2458 | if (t->be->beconn > t->be->beconn_max) |
| 2459 | t->be->beconn_max = t->be->beconn; |
| 2460 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2461 | t->flags |= SN_BE_ASSIGNED; |
| 2462 | } |
| 2463 | |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2464 | /* |
| 2465 | * Right now, we know that we have processed the entire headers |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2466 | * and that unwanted requests have been filtered out. We can do |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2467 | * whatever we want with the remaining request. Also, now we |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2468 | * may have separate values for ->fe, ->be. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2469 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2470 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 2471 | /* |
| 2472 | * If HTTP PROXY is set we simply get remote server address |
| 2473 | * parsing incoming request. |
| 2474 | */ |
| 2475 | if ((t->be->options & PR_O_HTTP_PROXY) && !(t->flags & SN_ADDR_SET)) { |
| 2476 | url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &t->srv_addr); |
| 2477 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2478 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2479 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2480 | * 7: the appsession cookie was looked up very early in 1.2, |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2481 | * so let's do the same now. |
| 2482 | */ |
| 2483 | |
| 2484 | /* It needs to look into the URI */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2485 | if (t->be->appsession_name) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2486 | 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] | 2487 | } |
| 2488 | |
| 2489 | |
| 2490 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2491 | * 8: Now we can work with the cookies. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2492 | * Note that doing so might move headers in the request, but |
| 2493 | * the fields will stay coherent and the URI will not move. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2494 | * This should only be performed in the backend. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2495 | */ |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 2496 | if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name) |
| 2497 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2498 | manage_client_side_cookies(t, req); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2499 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2500 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2501 | /* |
Willy Tarreau | bb046ac | 2007-03-03 19:17:03 +0100 | [diff] [blame] | 2502 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 2503 | * asks for it. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2504 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2505 | if ((t->fe->options | t->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2506 | if (t->cli_addr.ss_family == AF_INET) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2507 | /* Add an X-Forwarded-For header unless the source IP is |
| 2508 | * in the 'except' network range. |
| 2509 | */ |
| 2510 | if ((!t->fe->except_mask.s_addr || |
| 2511 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr) |
| 2512 | != t->fe->except_net.s_addr) && |
| 2513 | (!t->be->except_mask.s_addr || |
| 2514 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr) |
| 2515 | != t->be->except_net.s_addr)) { |
| 2516 | int len; |
| 2517 | unsigned char *pn; |
| 2518 | pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr; |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2519 | |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2520 | /* Note: we rely on the backend to get the header name to be used for |
| 2521 | * x-forwarded-for, because the header is really meant for the backends. |
| 2522 | * However, if the backend did not specify any option, we have to rely |
| 2523 | * on the frontend's header name. |
| 2524 | */ |
| 2525 | if (t->be->fwdfor_hdr_len) { |
| 2526 | len = t->be->fwdfor_hdr_len; |
| 2527 | memcpy(trash, t->be->fwdfor_hdr_name, len); |
| 2528 | } else { |
| 2529 | len = t->fe->fwdfor_hdr_len; |
| 2530 | memcpy(trash, t->fe->fwdfor_hdr_name, len); |
| 2531 | } |
| 2532 | 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] | 2533 | |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2534 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2535 | &txn->hdr_idx, trash, len)) < 0) |
| 2536 | goto return_bad_req; |
| 2537 | } |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2538 | } |
| 2539 | else if (t->cli_addr.ss_family == AF_INET6) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2540 | /* FIXME: for the sake of completeness, we should also support |
| 2541 | * 'except' here, although it is mostly useless in this case. |
| 2542 | */ |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2543 | int len; |
| 2544 | char pn[INET6_ADDRSTRLEN]; |
| 2545 | inet_ntop(AF_INET6, |
| 2546 | (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr, |
| 2547 | pn, sizeof(pn)); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2548 | |
| 2549 | /* Note: we rely on the backend to get the header name to be used for |
| 2550 | * x-forwarded-for, because the header is really meant for the backends. |
| 2551 | * However, if the backend did not specify any option, we have to rely |
| 2552 | * on the frontend's header name. |
| 2553 | */ |
| 2554 | if (t->be->fwdfor_hdr_len) { |
| 2555 | len = t->be->fwdfor_hdr_len; |
| 2556 | memcpy(trash, t->be->fwdfor_hdr_name, len); |
| 2557 | } else { |
| 2558 | len = t->fe->fwdfor_hdr_len; |
| 2559 | memcpy(trash, t->fe->fwdfor_hdr_name, len); |
| 2560 | } |
| 2561 | len += sprintf(trash + len, ": %s", pn); |
| 2562 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2563 | if (unlikely(http_header_add_tail2(req, &txn->req, |
| 2564 | &txn->hdr_idx, trash, len)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2565 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2566 | } |
| 2567 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2568 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2569 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2570 | * 10: add "Connection: close" if needed and not yet set. |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2571 | * 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] | 2572 | */ |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2573 | if (!(t->flags & SN_CONN_CLOSED) && |
Krzysztof Oledzki | 336d475 | 2007-12-25 02:40:22 +0100 | [diff] [blame] | 2574 | ((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] | 2575 | if ((unlikely(msg->sl.rq.v_l != 8) || |
| 2576 | unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) && |
| 2577 | unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx, |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2578 | "Connection: close", 17)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2579 | goto return_bad_req; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2580 | t->flags |= SN_CONN_CLOSED; |
Willy Tarreau | e15d913 | 2006-12-14 22:26:42 +0100 | [diff] [blame] | 2581 | } |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2582 | /* Before we switch to data, was assignment set in manage_client_side_cookie? |
| 2583 | * If not assigned, perhaps we are balancing on url_param, but this is a |
| 2584 | * POST; and the parameters are in the body, maybe scan there to find our server. |
| 2585 | * (unless headers overflowed the buffer?) |
| 2586 | */ |
| 2587 | if (!(t->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 2588 | t->txn.meth == HTTP_METH_POST && t->be->url_param_name != NULL && |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2589 | 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] | 2590 | memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) { |
| 2591 | /* are there enough bytes here? total == l || r || rlim ? |
| 2592 | * len is unsigned, but eoh is int, |
| 2593 | * how many bytes of body have we received? |
| 2594 | * eoh is the first empty line of the header |
| 2595 | */ |
| 2596 | /* 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] | 2597 | 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] | 2598 | |
| 2599 | /* If we have HTTP/1.1 and Expect: 100-continue, then abort. |
| 2600 | * We can't assume responsibility for the server's decision, |
| 2601 | * on this URI and header set. See rfc2616: 14.20, 8.2.3, |
| 2602 | * We also can't change our mind later, about which server to choose, so round robin. |
| 2603 | */ |
| 2604 | if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) { |
| 2605 | struct hdr_ctx ctx; |
| 2606 | ctx.idx = 0; |
| 2607 | /* Expect is allowed in 1.1, look for it */ |
| 2608 | http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx); |
| 2609 | if (ctx.idx != 0 && |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2610 | 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] | 2611 | /* We can't reliablly stall and wait for data, because of |
| 2612 | * .NET clients that don't conform to rfc2616; so, no need for |
| 2613 | * the next block to check length expectations. |
| 2614 | * We could send 100 status back to the client, but then we need to |
| 2615 | * re-write headers, and send the message. And this isn't the right |
| 2616 | * place for that action. |
| 2617 | * TODO: support Expect elsewhere and delete this block. |
| 2618 | */ |
| 2619 | goto end_check_maybe_wait_for_body; |
| 2620 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2621 | |
| 2622 | if (likely(len > t->be->url_param_post_limit)) { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2623 | /* nothing to do, we got enough */ |
| 2624 | } else { |
| 2625 | /* limit implies we are supposed to need this many bytes |
| 2626 | * to find the parameter. Let's see how many bytes we can wait for. |
| 2627 | */ |
| 2628 | long long hint = len; |
| 2629 | struct hdr_ctx ctx; |
| 2630 | ctx.idx = 0; |
| 2631 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx); |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2632 | 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] | 2633 | buffer_write_dis(req); |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2634 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2635 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2636 | else { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2637 | ctx.idx = 0; |
| 2638 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx); |
| 2639 | /* now if we have a length, we'll take the hint */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2640 | if (ctx.idx) { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2641 | /* We have Content-Length */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2642 | if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint)) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2643 | hint = 0; /* parse failure, untrusted client */ |
| 2644 | else { |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2645 | if (hint > 0) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2646 | msg->hdr_content_len = hint; |
| 2647 | else |
| 2648 | hint = 0; /* bad client, sent negative length */ |
| 2649 | } |
| 2650 | } |
| 2651 | /* 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] | 2652 | if (t->be->url_param_post_limit < hint) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2653 | hint = t->be->url_param_post_limit; |
| 2654 | /* now do we really need to buffer more data? */ |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2655 | if (len < hint) { |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 2656 | buffer_write_dis(req); |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2657 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2658 | } |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2659 | /* else... There are no body bytes to wait for */ |
| 2660 | } |
| 2661 | } |
| 2662 | } |
| 2663 | end_check_maybe_wait_for_body: |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2664 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2665 | /************************************************************* |
| 2666 | * OK, that's finished for the headers. We have done what we * |
| 2667 | * could. Let's switch to the DATA state. * |
| 2668 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2669 | |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2670 | buffer_set_rlim(req, BUFSIZE); /* no more rewrite needed */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2671 | t->logs.tv_request = now; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2672 | |
Willy Tarreau | 1fa3126 | 2007-12-03 00:36:16 +0100 | [diff] [blame] | 2673 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 2674 | * which may be the same as the connect timeout if unspecified. |
| 2675 | * 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] | 2676 | * eventually expire. We build the tarpit as an analyser. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2677 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2678 | if (txn->flags & TX_CLTARPIT) { |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2679 | buffer_flush(t->req); |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2680 | /* flush the request so that we can drop the connection early |
| 2681 | * if the client closes first. |
| 2682 | */ |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 2683 | buffer_write_dis(req); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2684 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 2685 | req->analyse_exp = tick_add_ifset(now_ms, t->be->timeout.tarpit); |
| 2686 | if (!req->analyse_exp) |
| 2687 | req->analyse_exp = now_ms; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2688 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2689 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2690 | /* OK let's go on with the BODY now */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2691 | goto end_of_headers; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2692 | |
| 2693 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2694 | txn->req.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2695 | txn->status = 400; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2696 | req->analysers = 0; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2697 | client_retnclose(t, error_message(t, HTTP_ERR_400)); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 2698 | t->fe->failed_req++; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2699 | return_prx_cond: |
| 2700 | if (!(t->flags & SN_ERR_MASK)) |
| 2701 | t->flags |= SN_ERR_PRXCOND; |
| 2702 | if (!(t->flags & SN_FINST_MASK)) |
| 2703 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2704 | return 0; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2705 | end_of_headers: |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2706 | ; // to keep gcc happy |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2707 | } |
| 2708 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2709 | if (req->analysers & AN_REQ_HTTP_TARPIT) { |
| 2710 | struct http_txn *txn = &t->txn; |
| 2711 | |
| 2712 | /* This connection is being tarpitted. The CLIENT side has |
| 2713 | * already set the connect expiration date to the right |
| 2714 | * timeout. We just have to check that the client is still |
| 2715 | * there and that the timeout has not expired. |
| 2716 | */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 2717 | if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 && |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2718 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 2719 | return 0; |
| 2720 | |
| 2721 | /* We will set the queue timer to the time spent, just for |
| 2722 | * logging purposes. We fake a 500 server error, so that the |
| 2723 | * attacker will not suspect his connection has been tarpitted. |
| 2724 | * It will not cause trouble to the logs because we can exclude |
| 2725 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 2726 | */ |
| 2727 | trace_term(t, TT_HTTP_SRV_2); |
| 2728 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 2729 | |
| 2730 | txn->status = 500; |
| 2731 | if (req->flags != BF_READ_ERROR) |
| 2732 | client_retnclose(t, error_message(t, HTTP_ERR_500)); |
| 2733 | |
| 2734 | req->analysers = 0; |
| 2735 | req->analyse_exp = TICK_ETERNITY; |
| 2736 | |
| 2737 | t->fe->failed_req++; |
| 2738 | if (!(t->flags & SN_ERR_MASK)) |
| 2739 | t->flags |= SN_ERR_PRXCOND; |
| 2740 | if (!(t->flags & SN_FINST_MASK)) |
| 2741 | t->flags |= SN_FINST_T; |
| 2742 | return 0; |
| 2743 | } |
| 2744 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2745 | if (req->analysers & AN_REQ_HTTP_BODY) { |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2746 | /* We have to parse the HTTP request body to find any required data. |
| 2747 | * "balance url_param check_post" should have been the only way to get |
| 2748 | * into this. We were brought here after HTTP header analysis, so all |
| 2749 | * related structures are ready. |
| 2750 | */ |
| 2751 | struct http_msg *msg = &t->txn.req; |
| 2752 | unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1; |
| 2753 | long long limit = t->be->url_param_post_limit; |
| 2754 | struct hdr_ctx ctx; |
| 2755 | |
| 2756 | ctx.idx = 0; |
| 2757 | |
| 2758 | /* now if we have a length, we'll take the hint */ |
| 2759 | http_find_header2("Transfer-Encoding", 17, msg->sol, &t->txn.hdr_idx, &ctx); |
| 2760 | if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) { |
| 2761 | unsigned int chunk = 0; |
| 2762 | while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) { |
| 2763 | char c = msg->sol[body]; |
| 2764 | if (ishex(c)) { |
| 2765 | unsigned int hex = toupper(c) - '0'; |
| 2766 | if (hex > 9) |
| 2767 | hex -= 'A' - '9' - 1; |
| 2768 | chunk = (chunk << 4) | hex; |
| 2769 | } else |
| 2770 | break; |
| 2771 | body++; |
| 2772 | } |
| 2773 | if (body + 2 >= req->l) /* we want CRLF too */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2774 | goto http_body_end; /* end of buffer? data missing! */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2775 | |
| 2776 | if (memcmp(msg->sol+body, "\r\n", 2) != 0) |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2777 | 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] | 2778 | |
| 2779 | body += 2; // skip CRLF |
| 2780 | |
| 2781 | /* if we support more then one chunk here, we have to do it again when assigning server |
| 2782 | * 1. how much entity data do we have? new var |
| 2783 | * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here |
| 2784 | * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found) |
| 2785 | */ |
| 2786 | |
| 2787 | if (chunk < limit) |
| 2788 | limit = chunk; /* only reading one chunk */ |
| 2789 | } else { |
| 2790 | if (msg->hdr_content_len < limit) |
| 2791 | limit = msg->hdr_content_len; |
| 2792 | } |
| 2793 | |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2794 | http_body_end: |
| 2795 | /* we leave once we know we have nothing left to do. This means that we have |
| 2796 | * enough bytes, or that we know we'll not get any more (buffer full, read |
| 2797 | * buffer closed). |
| 2798 | */ |
| 2799 | if (req->l - body >= limit || /* enough bytes! */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 2800 | req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR | BF_READ_TIMEOUT) || |
Willy Tarreau | c52164a | 2008-08-17 19:17:57 +0200 | [diff] [blame] | 2801 | tick_is_expired(req->analyse_exp, now_ms)) { |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2802 | /* 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] | 2803 | 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] | 2804 | req->analysers &= ~AN_REQ_HTTP_BODY; |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 2805 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2806 | } |
Willy Tarreau | c52164a | 2008-08-17 19:17:57 +0200 | [diff] [blame] | 2807 | else { |
| 2808 | /* Not enough data. We'll re-use the http-request |
| 2809 | * timeout here. Ideally, we should set the timeout |
| 2810 | * relative to the accept() date. We just set the |
| 2811 | * request timeout once at the beginning of the |
| 2812 | * request. |
| 2813 | */ |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 2814 | buffer_write_dis(req); |
Willy Tarreau | c52164a | 2008-08-17 19:17:57 +0200 | [diff] [blame] | 2815 | if (!tick_isset(req->analyse_exp)) |
| 2816 | req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq); |
| 2817 | return 0; |
| 2818 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2819 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2820 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2821 | /* Note: eventhough nobody should set an unknown flag, clearing them right now will |
| 2822 | * probably reduce one day's debugging session. |
| 2823 | */ |
| 2824 | #ifdef DEBUG_DEV |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2825 | 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] | 2826 | fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n", |
| 2827 | __FILE__, __LINE__, req->analysers); |
| 2828 | ABORT_NOW(); |
| 2829 | } |
| 2830 | #endif |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2831 | 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] | 2832 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2833 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2834 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2835 | /* This function performs all the processing enabled for the current response. |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2836 | * It normally returns zero, but may return 1 if it absolutely needs to be |
| 2837 | * called again after other functions. It relies on buffers flags, and updates |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2838 | * 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] | 2839 | * functions. It works like process_request (see indications above). |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2840 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2841 | int process_response(struct session *t) |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2842 | { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2843 | struct http_txn *txn = &t->txn; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2844 | struct buffer *req = t->req; |
| 2845 | struct buffer *rep = t->rep; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2846 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 2847 | 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] | 2848 | now_ms, __FUNCTION__, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 2849 | t, |
| 2850 | rep, |
| 2851 | rep->rex, rep->wex, |
| 2852 | rep->flags, |
| 2853 | rep->l, |
| 2854 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 2855 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2856 | if (rep->analysers & AN_RTR_HTTP_HDR) { /* receiving server headers */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2857 | /* |
| 2858 | * Now parse the partial (or complete) lines. |
| 2859 | * We will check the response syntax, and also join multi-line |
| 2860 | * headers. An index of all the lines will be elaborated while |
| 2861 | * parsing. |
| 2862 | * |
| 2863 | * For the parsing, we use a 28 states FSM. |
| 2864 | * |
| 2865 | * Here is the information we currently have : |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2866 | * rep->data + rep->som = beginning of response |
| 2867 | * rep->data + rep->eoh = end of processed headers / start of current one |
| 2868 | * 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] | 2869 | * rep->lr = first non-visited byte |
| 2870 | * rep->r = end of data |
| 2871 | */ |
Willy Tarreau | 7f875f6 | 2008-08-11 17:35:01 +0200 | [diff] [blame] | 2872 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2873 | int cur_idx; |
| 2874 | struct http_msg *msg = &txn->rsp; |
| 2875 | struct proxy *cur_proxy; |
| 2876 | |
| 2877 | if (likely(rep->lr < rep->r)) |
| 2878 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
| 2879 | |
| 2880 | /* 1: we might have to print this header in debug mode */ |
| 2881 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2882 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
| 2883 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
| 2884 | char *eol, *sol; |
| 2885 | |
| 2886 | sol = rep->data + msg->som; |
| 2887 | eol = sol + msg->sl.rq.l; |
| 2888 | debug_hdr("srvrep", t, sol, eol); |
| 2889 | |
| 2890 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2891 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
| 2892 | |
| 2893 | while (cur_idx) { |
| 2894 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2895 | debug_hdr("srvhdr", t, sol, eol); |
| 2896 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2897 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2898 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2899 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2900 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2901 | /* |
| 2902 | * Now we quickly check if we have found a full valid response. |
| 2903 | * If not so, we check the FD and buffer states before leaving. |
| 2904 | * A full response is indicated by the fact that we have seen |
| 2905 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 2906 | * responses are checked first. |
| 2907 | * |
| 2908 | * Depending on whether the client is still there or not, we |
| 2909 | * may send an error response back or not. Note that normally |
| 2910 | * we should only check for HTTP status there, and check I/O |
| 2911 | * errors somewhere else. |
| 2912 | */ |
| 2913 | |
| 2914 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2915 | /* Invalid response */ |
| 2916 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 2917 | hdr_response_bad: |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2918 | //buffer_shutr(rep); |
| 2919 | //buffer_shutw(req); |
| 2920 | //fd_delete(req->cons->fd); |
| 2921 | //req->cons->state = SI_ST_CLO; |
| 2922 | buffer_shutr_now(rep); |
| 2923 | buffer_shutw_now(req); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2924 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2925 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2926 | t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2927 | //sess_change_server(t, NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2928 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2929 | t->be->failed_resp++; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2930 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2931 | txn->status = 502; |
| 2932 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 2933 | if (!(t->flags & SN_ERR_MASK)) |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2934 | t->flags |= SN_ERR_PRXCOND; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2935 | if (!(t->flags & SN_FINST_MASK)) |
| 2936 | t->flags |= SN_FINST_H; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 2937 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2938 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 2939 | // process_srv_queue(t->srv); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2940 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2941 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2942 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2943 | /* too large response does not fit in buffer. */ |
| 2944 | else if (rep->flags & BF_FULL) { |
| 2945 | goto hdr_response_bad; |
| 2946 | } |
| 2947 | /* read error */ |
| 2948 | else if (rep->flags & BF_READ_ERROR) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2949 | buffer_shutr_now(rep); |
| 2950 | buffer_shutw_now(req); |
| 2951 | //fd_delete(req->cons->fd); |
| 2952 | //req->cons->state = SI_ST_CLO; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2953 | //if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2954 | //t->srv->cur_sess--; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2955 | //t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2956 | //sess_change_server(t, NULL); |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2957 | //} |
| 2958 | //t->be->failed_resp++; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2959 | rep->analysers = 0; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2960 | txn->status = 502; |
| 2961 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 2962 | if (!(t->flags & SN_ERR_MASK)) |
| 2963 | t->flags |= SN_ERR_SRVCL; |
| 2964 | if (!(t->flags & SN_FINST_MASK)) |
| 2965 | t->flags |= SN_FINST_H; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 2966 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2967 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 2968 | // process_srv_queue(t->srv); |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2969 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2970 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2971 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2972 | /* read timeout : return a 504 to the client. */ |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2973 | else if (rep->flags & BF_READ_TIMEOUT) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2974 | buffer_shutr_now(rep); |
| 2975 | buffer_shutw_now(req); |
| 2976 | //fd_delete(req->cons->fd); |
| 2977 | //req->cons->state = SI_ST_CLO; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2978 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2979 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2980 | t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2981 | //sess_change_server(t, NULL); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2982 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2983 | t->be->failed_resp++; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2984 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2985 | txn->status = 504; |
| 2986 | client_return(t, error_message(t, HTTP_ERR_504)); |
| 2987 | if (!(t->flags & SN_ERR_MASK)) |
| 2988 | t->flags |= SN_ERR_SRVTO; |
| 2989 | if (!(t->flags & SN_FINST_MASK)) |
| 2990 | t->flags |= SN_FINST_H; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 2991 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2992 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 2993 | // process_srv_queue(t->srv); |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2994 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2995 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2996 | /* write error to client, or close from server */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 2997 | else if (rep->flags & (BF_WRITE_ERROR|BF_SHUTR)) { |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2998 | buffer_shutr_now(rep); |
| 2999 | buffer_shutw_now(req); |
| 3000 | //fd_delete(req->cons->fd); |
| 3001 | //req->cons->state = SI_ST_CLO; |
| 3002 | if (t->srv) { |
| 3003 | //t->srv->cur_sess--; |
| 3004 | t->srv->failed_resp++; |
| 3005 | //sess_change_server(t, NULL); |
| 3006 | } |
| 3007 | t->be->failed_resp++; |
| 3008 | rep->analysers = 0; |
| 3009 | txn->status = 502; |
| 3010 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 3011 | if (!(t->flags & SN_ERR_MASK)) |
| 3012 | t->flags |= SN_ERR_SRVCL; |
| 3013 | if (!(t->flags & SN_FINST_MASK)) |
| 3014 | t->flags |= SN_FINST_H; |
| 3015 | |
| 3016 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 3017 | // process_srv_queue(t->srv); |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 3018 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 3019 | return 0; |
| 3020 | } |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 3021 | buffer_write_dis(rep); |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 3022 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3023 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3024 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3025 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3026 | /***************************************************************** |
| 3027 | * More interesting part now : we know that we have a complete * |
| 3028 | * response which at least looks like HTTP. We have an indicator * |
| 3029 | * of each header's length, so we can parse them quickly. * |
| 3030 | ****************************************************************/ |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3031 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3032 | rep->analysers &= ~AN_RTR_HTTP_HDR; |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 3033 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3034 | /* ensure we keep this pointer to the beginning of the message */ |
| 3035 | msg->sol = rep->data + msg->som; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3036 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3037 | /* |
| 3038 | * 1: get the status code and check for cacheability. |
| 3039 | */ |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3040 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3041 | t->logs.logwait &= ~LW_RESP; |
| 3042 | 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] | 3043 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3044 | switch (txn->status) { |
| 3045 | case 200: |
| 3046 | case 203: |
| 3047 | case 206: |
| 3048 | case 300: |
| 3049 | case 301: |
| 3050 | case 410: |
| 3051 | /* RFC2616 @13.4: |
| 3052 | * "A response received with a status code of |
| 3053 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 3054 | * by a cache (...) unless a cache-control |
| 3055 | * directive prohibits caching." |
| 3056 | * |
| 3057 | * RFC2616 @9.5: POST method : |
| 3058 | * "Responses to this method are not cacheable, |
| 3059 | * unless the response includes appropriate |
| 3060 | * Cache-Control or Expires header fields." |
| 3061 | */ |
| 3062 | if (likely(txn->meth != HTTP_METH_POST) && |
| 3063 | (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
| 3064 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 3065 | break; |
| 3066 | default: |
| 3067 | break; |
| 3068 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3069 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3070 | /* |
| 3071 | * 2: we may need to capture headers |
| 3072 | */ |
| 3073 | if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap)) |
| 3074 | capture_headers(rep->data + msg->som, &txn->hdr_idx, |
| 3075 | txn->rsp.cap, t->fe->rsp_cap); |
| 3076 | |
| 3077 | /* |
| 3078 | * 3: we will have to evaluate the filters. |
| 3079 | * As opposed to version 1.2, now they will be evaluated in the |
| 3080 | * filters order and not in the header order. This means that |
| 3081 | * each filter has to be validated among all headers. |
| 3082 | * |
| 3083 | * Filters are tried with ->be first, then with ->fe if it is |
| 3084 | * different from ->be. |
| 3085 | */ |
| 3086 | |
| 3087 | t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */ |
| 3088 | |
| 3089 | cur_proxy = t->be; |
| 3090 | while (1) { |
| 3091 | struct proxy *rule_set = cur_proxy; |
| 3092 | |
| 3093 | /* try headers filters */ |
| 3094 | if (rule_set->rsp_exp != NULL) { |
| 3095 | if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) { |
| 3096 | return_bad_resp: |
| 3097 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3098 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3099 | t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3100 | //sess_change_server(t, NULL); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3101 | } |
| 3102 | cur_proxy->failed_resp++; |
| 3103 | return_srv_prx_502: |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3104 | buffer_shutr_now(rep); |
| 3105 | buffer_shutw_now(req); |
| 3106 | //fd_delete(req->cons->fd); |
| 3107 | //req->cons->state = SI_ST_CLO; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3108 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3109 | txn->status = 502; |
| 3110 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 3111 | if (!(t->flags & SN_ERR_MASK)) |
| 3112 | t->flags |= SN_ERR_PRXCOND; |
| 3113 | if (!(t->flags & SN_FINST_MASK)) |
| 3114 | t->flags |= SN_FINST_H; |
| 3115 | /* We used to have a free connection slot. Since we'll never use it, |
| 3116 | * we have to inform the server that it may be used by another session. |
| 3117 | */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3118 | //if (t->srv && may_dequeue_tasks(t->srv, cur_proxy)) |
| 3119 | // process_srv_queue(t->srv); |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3120 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3121 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3122 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3123 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3124 | /* has the response been denied ? */ |
| 3125 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | f899b94 | 2008-03-28 18:09:38 +0100 | [diff] [blame] | 3126 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3127 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3128 | t->srv->failed_secu++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3129 | //sess_change_server(t, NULL); |
Willy Tarreau | f899b94 | 2008-03-28 18:09:38 +0100 | [diff] [blame] | 3130 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3131 | cur_proxy->denied_resp++; |
| 3132 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 3133 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3134 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3135 | /* We might have to check for "Connection:" */ |
| 3136 | if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) && |
| 3137 | !(t->flags & SN_CONN_CLOSED)) { |
| 3138 | char *cur_ptr, *cur_end, *cur_next; |
| 3139 | int cur_idx, old_idx, delta, val; |
| 3140 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3141 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3142 | cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 3143 | old_idx = 0; |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 3144 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3145 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 3146 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 3147 | cur_ptr = cur_next; |
| 3148 | cur_end = cur_ptr + cur_hdr->len; |
| 3149 | cur_next = cur_end + cur_hdr->cr + 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3150 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3151 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 3152 | if (val) { |
| 3153 | /* 3 possibilities : |
| 3154 | * - we have already set Connection: close, |
| 3155 | * so we remove this line. |
| 3156 | * - we have not yet set Connection: close, |
| 3157 | * but this line indicates close. We leave |
| 3158 | * it untouched and set the flag. |
| 3159 | * - we have not yet set Connection: close, |
| 3160 | * and this line indicates non-close. We |
| 3161 | * replace it. |
| 3162 | */ |
| 3163 | if (t->flags & SN_CONN_CLOSED) { |
| 3164 | delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0); |
| 3165 | txn->rsp.eoh += delta; |
| 3166 | cur_next += delta; |
| 3167 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 3168 | txn->hdr_idx.used--; |
| 3169 | cur_hdr->len = 0; |
| 3170 | } else { |
| 3171 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 3172 | delta = buffer_replace2(rep, cur_ptr + val, cur_end, |
| 3173 | "close", 5); |
| 3174 | cur_next += delta; |
| 3175 | cur_hdr->len += delta; |
| 3176 | txn->rsp.eoh += delta; |
| 3177 | } |
| 3178 | t->flags |= SN_CONN_CLOSED; |
| 3179 | } |
| 3180 | } |
| 3181 | old_idx = cur_idx; |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 3182 | } |
| 3183 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3184 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3185 | /* add response headers from the rule sets in the same order */ |
| 3186 | for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) { |
| 3187 | if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, |
| 3188 | rule_set->rsp_add[cur_idx])) < 0) |
| 3189 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 3190 | } |
| 3191 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3192 | /* check whether we're already working on the frontend */ |
| 3193 | if (cur_proxy == t->fe) |
| 3194 | break; |
| 3195 | cur_proxy = t->fe; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3196 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3197 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3198 | /* |
| 3199 | * 4: check for server cookie. |
| 3200 | */ |
| 3201 | if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name |
| 3202 | || (t->be->options & PR_O_CHK_CACHE)) |
| 3203 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3204 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3205 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3206 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3207 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3208 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3209 | if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0) |
| 3210 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3211 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3212 | /* |
| 3213 | * 6: add server cookie in the response if needed |
| 3214 | */ |
| 3215 | if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) && |
| 3216 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) { |
| 3217 | int len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3218 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3219 | /* the server is known, it's not the one the client requested, we have to |
| 3220 | * insert a set-cookie here, except if we want to insert only on POST |
| 3221 | * requests and this one isn't. Note that servers which don't have cookies |
| 3222 | * (eg: some backup servers) will return a full cookie removal request. |
| 3223 | */ |
| 3224 | len = sprintf(trash, "Set-Cookie: %s=%s; path=/", |
| 3225 | t->be->cookie_name, |
| 3226 | 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] | 3227 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3228 | if (t->be->cookie_domain) |
| 3229 | len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3230 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3231 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3232 | trash, len)) < 0) |
| 3233 | goto return_bad_resp; |
| 3234 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3235 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3236 | /* Here, we will tell an eventual cache on the client side that we don't |
| 3237 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 3238 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 3239 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 3240 | */ |
| 3241 | if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3242 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3243 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3244 | |
| 3245 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3246 | "Cache-control: private", 22)) < 0) |
| 3247 | goto return_bad_resp; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3248 | } |
| 3249 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3250 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3251 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3252 | /* |
| 3253 | * 7: check if result will be cacheable with a cookie. |
| 3254 | * We'll block the response if security checks have caught |
| 3255 | * nasty things such as a cacheable cookie. |
| 3256 | */ |
| 3257 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) == |
| 3258 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) && |
| 3259 | (t->be->options & PR_O_CHK_CACHE)) { |
| 3260 | |
| 3261 | /* we're in presence of a cacheable response containing |
| 3262 | * a set-cookie header. We'll block it as requested by |
| 3263 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3264 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3265 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3266 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3267 | t->srv->failed_secu++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3268 | //sess_change_server(t, NULL); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3269 | } |
| 3270 | t->be->denied_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3271 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3272 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 3273 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 3274 | send_log(t->be, LOG_ALERT, |
| 3275 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 3276 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 3277 | goto return_srv_prx_502; |
| 3278 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3279 | |
| 3280 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3281 | * 8: add "Connection: close" if needed and not yet set. |
| 3282 | * 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] | 3283 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3284 | if (!(t->flags & SN_CONN_CLOSED) && |
| 3285 | ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) { |
| 3286 | if ((unlikely(msg->sl.st.v_l != 8) || |
| 3287 | unlikely(req->data[msg->som + 7] != '0')) && |
| 3288 | unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3289 | "Connection: close", 17)) < 0) |
| 3290 | goto return_bad_resp; |
| 3291 | t->flags |= SN_CONN_CLOSED; |
| 3292 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3293 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3294 | /************************************************************* |
| 3295 | * OK, that's finished for the headers. We have done what we * |
| 3296 | * could. Let's switch to the DATA state. * |
| 3297 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3298 | |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 3299 | buffer_set_rlim(rep, BUFSIZE); /* no more rewrite needed */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3300 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3301 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3302 | #ifdef CONFIG_HAP_TCPSPLICE |
| 3303 | if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
| 3304 | /* TCP splicing supported by both FE and BE */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 3305 | tcp_splice_splicefd(rep->cons->fd, rep->prod->fd, 0); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3306 | } |
| 3307 | #endif |
| 3308 | /* if the user wants to log as soon as possible, without counting |
| 3309 | * bytes from the server, then this is the right moment. We have |
| 3310 | * to temporarily assign bytes_out to log what we currently have. |
| 3311 | */ |
| 3312 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 3313 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 3314 | t->logs.bytes_out = txn->rsp.eoh; |
| 3315 | if (t->fe->to_log & LW_REQ) |
| 3316 | http_sess_log(t); |
| 3317 | else |
| 3318 | tcp_sess_log(t); |
| 3319 | t->logs.bytes_out = 0; |
| 3320 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3321 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3322 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 3323 | * otherwise we would not let the client side wake up. |
| 3324 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3325 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3326 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3327 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3328 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3329 | /* Note: eventhough nobody should set an unknown flag, clearing them right now will |
| 3330 | * probably reduce one day's debugging session. |
| 3331 | */ |
| 3332 | #ifdef DEBUG_DEV |
| 3333 | if (rep->analysers & ~(AN_RTR_HTTP_HDR)) { |
| 3334 | fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n", |
| 3335 | __FILE__, __LINE__, rep->analysers); |
| 3336 | ABORT_NOW(); |
| 3337 | } |
| 3338 | #endif |
| 3339 | rep->analysers &= AN_RTR_HTTP_HDR; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3340 | return 0; |
| 3341 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3342 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 3343 | ///* |
| 3344 | // * Manages the client FSM and its socket. It normally returns zero, but may |
| 3345 | // * return 1 if it absolutely wants to be called again. |
| 3346 | // * |
| 3347 | // * Note: process_cli is the ONLY function allowed to set cli_state to anything |
| 3348 | // * but CL_STCLOSE. |
| 3349 | // */ |
| 3350 | //int process_cli(struct session *t) |
| 3351 | //{ |
| 3352 | // struct buffer *req = t->req; |
| 3353 | // struct buffer *rep = t->rep; |
| 3354 | // |
| 3355 | // DPRINTF(stderr,"[%u] %s: fd=%d[%d] c=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n", |
| 3356 | // now_ms, __FUNCTION__, |
| 3357 | // t->cli_fd, t->cli_fd >= 0 ? fdtab[t->cli_fd].state : 0, /* fd,state*/ |
| 3358 | // cli_stnames[t->cli_state], |
| 3359 | // t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_RD) : 0, |
| 3360 | // t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_WR) : 0, |
| 3361 | // req->rex, rep->wex, |
| 3362 | // req->flags, rep->flags, |
| 3363 | // req->l, rep->l); |
| 3364 | // |
| 3365 | // update_state: |
| 3366 | // /* FIXME: we still have to check for CL_STSHUTR because client_retnclose |
| 3367 | // * still set this state (and will do until unix sockets are converted). |
| 3368 | // */ |
| 3369 | // if (t->cli_state == CL_STDATA || t->cli_state == CL_STSHUTR) { |
| 3370 | // /* we can skip most of the tests at once if some conditions are not met */ |
| 3371 | // if (!((fdtab[t->cli_fd].state == FD_STERROR) || |
| 3372 | // (req->flags & (BF_READ_TIMEOUT|BF_READ_ERROR|BF_SHUTR_NOW)) || |
| 3373 | // (rep->flags & (BF_WRITE_TIMEOUT|BF_WRITE_ERROR|BF_SHUTW_NOW)) || |
| 3374 | // (!(req->flags & BF_SHUTR) && req->flags & (BF_READ_NULL|BF_SHUTW)) || |
| 3375 | // (!(rep->flags & BF_SHUTW) && |
| 3376 | // (rep->flags & (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR)) == (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR)))) |
| 3377 | // goto update_timeouts; |
| 3378 | // |
| 3379 | // /* read or write error */ |
| 3380 | // if (fdtab[t->cli_fd].state == FD_STERROR) { |
| 3381 | // buffer_shutr(req); |
| 3382 | // req->flags |= BF_READ_ERROR; |
| 3383 | // buffer_shutw(rep); |
| 3384 | // rep->flags |= BF_WRITE_ERROR; |
| 3385 | // fd_delete(t->cli_fd); |
| 3386 | // t->cli_state = CL_STCLOSE; |
| 3387 | // trace_term(t, TT_HTTP_CLI_1); |
| 3388 | // if (!req->analysers) { |
| 3389 | // if (!(t->flags & SN_ERR_MASK)) |
| 3390 | // t->flags |= SN_ERR_CLICL; |
| 3391 | // if (!(t->flags & SN_FINST_MASK)) { |
| 3392 | // if (req->cons->err_type <= SI_ET_QUEUE_ABRT) |
| 3393 | // t->flags |= SN_FINST_Q; |
| 3394 | // else if (req->cons->err_type <= SI_ET_CONN_OTHER) |
| 3395 | // t->flags |= SN_FINST_C; |
| 3396 | // else |
| 3397 | // t->flags |= SN_FINST_D; |
| 3398 | // } |
| 3399 | // } |
| 3400 | // goto update_state; |
| 3401 | // } |
| 3402 | // /* last read, or end of server write */ |
| 3403 | // else if (!(req->flags & BF_SHUTR) && /* not already done */ |
| 3404 | // req->flags & (BF_READ_NULL|BF_SHUTR_NOW|BF_SHUTW)) { |
| 3405 | // buffer_shutr(req); |
| 3406 | // if (!(rep->flags & BF_SHUTW)) { |
| 3407 | // EV_FD_CLR(t->cli_fd, DIR_RD); |
| 3408 | // trace_term(t, TT_HTTP_CLI_2); |
| 3409 | // } else { |
| 3410 | // /* output was already closed */ |
| 3411 | // fd_delete(t->cli_fd); |
| 3412 | // t->cli_state = CL_STCLOSE; |
| 3413 | // trace_term(t, TT_HTTP_CLI_3); |
| 3414 | // } |
| 3415 | // goto update_state; |
| 3416 | // } |
| 3417 | // /* last server read and buffer empty : we only check them when we're |
| 3418 | // * allowed to forward the data. |
| 3419 | // */ |
| 3420 | // else if (!(rep->flags & BF_SHUTW) && /* not already done */ |
| 3421 | // ((rep->flags & BF_SHUTW_NOW) || |
| 3422 | // (rep->flags & BF_EMPTY && rep->flags & BF_MAY_FORWARD && |
| 3423 | // rep->flags & BF_SHUTR && !(t->flags & SN_SELF_GEN)))) { |
| 3424 | // buffer_shutw(rep); |
| 3425 | // if (!(req->flags & BF_SHUTR)) { |
| 3426 | // EV_FD_CLR(t->cli_fd, DIR_WR); |
| 3427 | // shutdown(t->cli_fd, SHUT_WR); |
| 3428 | // trace_term(t, TT_HTTP_CLI_4); |
| 3429 | // } else { |
| 3430 | // fd_delete(t->cli_fd); |
| 3431 | // t->cli_state = CL_STCLOSE; |
| 3432 | // trace_term(t, TT_HTTP_CLI_5); |
| 3433 | // } |
| 3434 | // goto update_state; |
| 3435 | // } |
| 3436 | // /* read timeout */ |
| 3437 | // else if ((req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT) { |
| 3438 | // buffer_shutr(req); |
| 3439 | // if (!(rep->flags & BF_SHUTW)) { |
| 3440 | // EV_FD_CLR(t->cli_fd, DIR_RD); |
| 3441 | // trace_term(t, TT_HTTP_CLI_6); |
| 3442 | // } else { |
| 3443 | // /* output was already closed */ |
| 3444 | // fd_delete(t->cli_fd); |
| 3445 | // t->cli_state = CL_STCLOSE; |
| 3446 | // trace_term(t, TT_HTTP_CLI_7); |
| 3447 | // } |
| 3448 | // if (!req->analysers) { |
| 3449 | // if (!(t->flags & SN_ERR_MASK)) |
| 3450 | // t->flags |= SN_ERR_CLITO; |
| 3451 | // if (!(t->flags & SN_FINST_MASK)) { |
| 3452 | // if (req->cons->err_type <= SI_ET_QUEUE_ABRT) |
| 3453 | // t->flags |= SN_FINST_Q; |
| 3454 | // else if (req->cons->err_type <= SI_ET_CONN_OTHER) |
| 3455 | // t->flags |= SN_FINST_C; |
| 3456 | // else |
| 3457 | // t->flags |= SN_FINST_D; |
| 3458 | // } |
| 3459 | // } |
| 3460 | // goto update_state; |
| 3461 | // } |
| 3462 | // /* write timeout */ |
| 3463 | // else if ((rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT) { |
| 3464 | // buffer_shutw(rep); |
| 3465 | // if (!(req->flags & BF_SHUTR)) { |
| 3466 | // EV_FD_CLR(t->cli_fd, DIR_WR); |
| 3467 | // shutdown(t->cli_fd, SHUT_WR); |
| 3468 | // trace_term(t, TT_HTTP_CLI_8); |
| 3469 | // } else { |
| 3470 | // fd_delete(t->cli_fd); |
| 3471 | // t->cli_state = CL_STCLOSE; |
| 3472 | // trace_term(t, TT_HTTP_CLI_9); |
| 3473 | // } |
| 3474 | // if (!req->analysers) { |
| 3475 | // if (!(t->flags & SN_ERR_MASK)) |
| 3476 | // t->flags |= SN_ERR_CLITO; |
| 3477 | // if (!(t->flags & SN_FINST_MASK)) { |
| 3478 | // if (req->cons->err_type <= SI_ET_QUEUE_ABRT) |
| 3479 | // t->flags |= SN_FINST_Q; |
| 3480 | // else if (req->cons->err_type <= SI_ET_CONN_OTHER) |
| 3481 | // t->flags |= SN_FINST_C; |
| 3482 | // else |
| 3483 | // t->flags |= SN_FINST_D; |
| 3484 | // } |
| 3485 | // } |
| 3486 | // goto update_state; |
| 3487 | // } |
| 3488 | // |
| 3489 | // update_timeouts: |
| 3490 | // /* manage read timeout */ |
| 3491 | // if (!(req->flags & BF_SHUTR)) { |
| 3492 | // if (req->flags & BF_FULL) { |
| 3493 | // /* no room to read more data */ |
| 3494 | // if (EV_FD_COND_C(t->cli_fd, DIR_RD)) { |
| 3495 | // /* stop reading until we get some space */ |
| 3496 | // req->rex = TICK_ETERNITY; |
| 3497 | // } |
| 3498 | // } else { |
| 3499 | // EV_FD_COND_S(t->cli_fd, DIR_RD); |
| 3500 | // req->rex = tick_add_ifset(now_ms, t->fe->timeout.client); |
| 3501 | // } |
| 3502 | // } |
| 3503 | // |
| 3504 | // /* manage write timeout */ |
| 3505 | // if (!(rep->flags & BF_SHUTW)) { |
| 3506 | // /* first, we may have to produce data (eg: stats). |
| 3507 | // * right now, this is limited to the SHUTR state. |
| 3508 | // */ |
| 3509 | // if (req->flags & BF_SHUTR && t->flags & SN_SELF_GEN) { |
| 3510 | // produce_content(t); |
| 3511 | // if (rep->flags & BF_EMPTY) { |
| 3512 | // buffer_shutw(rep); |
| 3513 | // fd_delete(t->cli_fd); |
| 3514 | // t->cli_state = CL_STCLOSE; |
| 3515 | // trace_term(t, TT_HTTP_CLI_10); |
| 3516 | // goto update_state; |
| 3517 | // } |
| 3518 | // } |
| 3519 | // |
| 3520 | // /* we don't enable client write if the buffer is empty, nor if the server has to analyze it */ |
| 3521 | // if ((rep->flags & (BF_EMPTY|BF_MAY_FORWARD)) != BF_MAY_FORWARD) { |
| 3522 | // if (EV_FD_COND_C(t->cli_fd, DIR_WR)) { |
| 3523 | // /* stop writing */ |
| 3524 | // rep->wex = TICK_ETERNITY; |
| 3525 | // } |
| 3526 | // } else { |
| 3527 | // /* buffer not empty */ |
| 3528 | // EV_FD_COND_S(t->cli_fd, DIR_WR); |
| 3529 | // if (!tick_isset(rep->wex)) { |
| 3530 | // /* restart writing */ |
| 3531 | // rep->wex = tick_add_ifset(now_ms, t->fe->timeout.client); |
| 3532 | // if (!(req->flags & BF_SHUTR) && tick_isset(rep->wex) && tick_isset(req->rex)) { |
| 3533 | // /* FIXME: to prevent the client from expiring read timeouts during writes, |
| 3534 | // * we refresh it, except if it was already infinite. */ |
| 3535 | // req->rex = rep->wex; |
| 3536 | // } |
| 3537 | // } |
| 3538 | // } |
| 3539 | // } |
| 3540 | // return 0; /* other cases change nothing */ |
| 3541 | // } |
| 3542 | // else if (t->cli_state == CL_STCLOSE) { /* CL_STCLOSE: nothing to do */ |
| 3543 | // if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 3544 | // int len; |
| 3545 | // len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)req->cons->fd); |
| 3546 | // write(1, trash, len); |
| 3547 | // } |
| 3548 | // return 0; |
| 3549 | // } |
| 3550 | //#ifdef DEBUG_DEV |
| 3551 | // fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, t->cli_state); |
| 3552 | // ABORT_NOW(); |
| 3553 | //#endif |
| 3554 | // return 0; |
| 3555 | //} |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3556 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3557 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3558 | /* Return 1 if the pending connection has failed and should be retried, |
| 3559 | * otherwise zero. We may only come here in SI_ST_CON state, which means that |
| 3560 | * the socket's file descriptor is known. |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3561 | */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3562 | int tcp_connection_status(struct session *t) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3563 | { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3564 | struct buffer *req = t->req; |
| 3565 | struct buffer *rep = t->rep; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3566 | int conn_err = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3567 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3568 | DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n", |
| 3569 | now_ms, __FUNCTION__, |
| 3570 | cli_stnames[t->cli_state], |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3571 | rep->rex, req->wex, |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 3572 | req->flags, rep->flags, |
| 3573 | req->l, rep->l); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3574 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3575 | if ((req->flags & BF_SHUTW_NOW) || |
| 3576 | (rep->flags & BF_SHUTW) || |
| 3577 | ((req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */ |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 3578 | ((req->flags & BF_EMPTY && !(req->flags & BF_WRITE_ACTIVITY)) || |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3579 | t->be->options & PR_O_ABRT_CLOSE))) { /* give up */ |
| 3580 | |
| 3581 | trace_term(t, TT_HTTP_SRV_5); |
| 3582 | req->wex = TICK_ETERNITY; |
| 3583 | fd_delete(req->cons->fd); |
| 3584 | if (t->srv) { |
| 3585 | t->srv->cur_sess--; |
| 3586 | sess_change_server(t, NULL); |
| 3587 | } |
| 3588 | /* note that this must not return any error because it would be able to |
| 3589 | * overwrite the client_retnclose() output. |
| 3590 | */ |
| 3591 | //srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL); |
| 3592 | |
| 3593 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3594 | buffer_shutw(req); |
| 3595 | buffer_shutr(rep); |
| 3596 | req->cons->state = SI_ST_CLO; |
| 3597 | if (!req->cons->err_type) |
| 3598 | req->cons->err_type = SI_ET_CONN_ABRT; |
| 3599 | req->cons->err_loc = t->srv; |
| 3600 | return 0; |
| 3601 | } |
| 3602 | |
| 3603 | /* check for timeouts and asynchronous connect errors */ |
| 3604 | if (fdtab[req->cons->fd].state == FD_STERROR) { |
| 3605 | conn_err = SI_ET_CONN_ERR; |
| 3606 | if (!req->cons->err_type) |
| 3607 | req->cons->err_type = SI_ET_CONN_ERR; |
| 3608 | } |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 3609 | else if (!(req->flags & BF_WRITE_ACTIVITY)) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3610 | /* nothing happened, maybe we timed out */ |
| 3611 | if (tick_is_expired(req->wex, now_ms)) { |
| 3612 | conn_err = SI_ET_CONN_TO; |
| 3613 | if (!req->cons->err_type) |
| 3614 | req->cons->err_type = SI_ET_CONN_TO; |
| 3615 | } |
| 3616 | else |
| 3617 | return 0; /* let's wait a bit more */ |
| 3618 | } |
| 3619 | |
| 3620 | if (conn_err) { |
| 3621 | fd_delete(req->cons->fd); |
| 3622 | req->cons->state = SI_ST_CLO; |
| 3623 | |
| 3624 | if (t->srv) { |
| 3625 | t->srv->cur_sess--; |
| 3626 | sess_change_server(t, NULL); |
| 3627 | req->cons->err_loc = t->srv; |
| 3628 | } |
| 3629 | |
| 3630 | /* ensure that we have enough retries left */ |
| 3631 | if (srv_count_retry_down(t, conn_err)) |
| 3632 | return 0; |
| 3633 | |
| 3634 | if (conn_err == SI_ET_CONN_ERR) { |
| 3635 | /* we encountered an immediate connection error, and we |
| 3636 | * will have to retry connecting to the same server, most |
| 3637 | * likely leading to the same result. To avoid this, we |
| 3638 | * fake a connection timeout to retry after a turn-around |
| 3639 | * time of 1 second. We will wait in the previous if block. |
| 3640 | */ |
| 3641 | req->cons->state = SI_ST_TAR; |
Willy Tarreau | 3537467 | 2008-09-03 18:11:02 +0200 | [diff] [blame] | 3642 | req->cons->exp = tick_add(now_ms, MS_TO_TICKS(1000)); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3643 | return 0; |
| 3644 | } |
| 3645 | |
| 3646 | if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) { |
| 3647 | /* We're on our last chance, and the REDISP option was specified. |
| 3648 | * We will ignore cookie and force to balance or use the dispatcher. |
| 3649 | */ |
| 3650 | /* let's try to offer this slot to anybody */ |
| 3651 | if (may_dequeue_tasks(t->srv, t->be)) |
| 3652 | process_srv_queue(t->srv); |
| 3653 | |
| 3654 | /* it's left to the dispatcher to choose a server */ |
| 3655 | t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
| 3656 | t->prev_srv = t->srv; |
| 3657 | } else { |
| 3658 | /* we just want to retry */ |
| 3659 | if (t->srv) |
| 3660 | t->srv->retries++; |
| 3661 | t->be->retries++; |
| 3662 | |
| 3663 | /* Now we will try to either reconnect to the same server or |
| 3664 | * connect to another server. If the connection gets queued |
| 3665 | * because all servers are saturated, then we will go back to |
| 3666 | * the idle state where the buffer's consumer is marked as |
| 3667 | * unknown. |
| 3668 | */ |
| 3669 | if (srv_retryable_connect(t)) { |
| 3670 | /* success or unrecoverable error */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3671 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3672 | return 0; |
| 3673 | } |
| 3674 | } |
| 3675 | |
| 3676 | /* We'll rely on the caller to try to get a connection again */ |
| 3677 | return 1; |
| 3678 | } |
| 3679 | else { |
| 3680 | /* no error and write OK : connection succeeded */ |
| 3681 | t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 3682 | req->cons->state = SI_ST_EST; |
| 3683 | req->cons->err_type = SI_ET_NONE; |
| 3684 | req->cons->err_loc = NULL; |
| 3685 | |
| 3686 | if (req->flags & BF_EMPTY) { |
| 3687 | EV_FD_CLR(req->cons->fd, DIR_WR); |
| 3688 | req->wex = TICK_ETERNITY; |
| 3689 | } else { |
| 3690 | EV_FD_SET(req->cons->fd, DIR_WR); |
| 3691 | req->wex = tick_add_ifset(now_ms, t->be->timeout.server); |
| 3692 | if (tick_isset(req->wex)) { |
| 3693 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 3694 | * we refresh it. */ |
| 3695 | rep->rex = req->wex; |
| 3696 | } |
| 3697 | } |
| 3698 | |
| 3699 | if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */ |
| 3700 | if (!(rep->flags & BF_HIJACK)) { |
| 3701 | EV_FD_SET(req->cons->fd, DIR_RD); |
| 3702 | rep->rex = tick_add_ifset(now_ms, t->be->timeout.server); |
| 3703 | } |
| 3704 | buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */ |
| 3705 | |
| 3706 | /* if the user wants to log as soon as possible, without counting |
| 3707 | bytes from the server, then this is the right moment. */ |
| 3708 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 3709 | t->logs.t_close = t->logs.t_connect; /* to get a valid end date */ |
| 3710 | tcp_sess_log(t); |
| 3711 | } |
| 3712 | #ifdef CONFIG_HAP_TCPSPLICE |
| 3713 | if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
| 3714 | /* TCP splicing supported by both FE and BE */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 3715 | tcp_splice_splicefd(req->prod->fd, req->cons->fd, 0); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3716 | } |
| 3717 | #endif |
| 3718 | } |
| 3719 | else { |
| 3720 | rep->analysers |= AN_RTR_HTTP_HDR; |
| 3721 | buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */ |
| 3722 | t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE; |
| 3723 | /* reset hdr_idx which was already initialized by the request. |
| 3724 | * right now, the http parser does it. |
| 3725 | * hdr_idx_init(&t->txn.hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3726 | */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3727 | } |
| 3728 | |
Willy Tarreau | 9a2d154 | 2008-08-30 12:31:07 +0200 | [diff] [blame] | 3729 | rep->flags |= BF_READ_ATTACHED; /* producer is now attached */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3730 | req->wex = TICK_ETERNITY; |
| 3731 | return 0; |
| 3732 | } |
| 3733 | } |
| 3734 | |
| 3735 | |
| 3736 | /* |
| 3737 | * This function tries to assign a server to a stream_sock interface. |
| 3738 | * It may be called only for t->req->cons->state = one of { SI_ST_INI, |
| 3739 | * SI_ST_TAR, SI_ST_QUE }. It returns one of those states, SI_ST_ASS |
| 3740 | * in case of success, or SI_ST_CLO in case of failure. It returns 1 if |
| 3741 | * it returns SI_ST_ASS, otherwise zero. |
| 3742 | */ |
| 3743 | int stream_sock_assign_server(struct session *t) |
| 3744 | { |
| 3745 | DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n", |
| 3746 | now_ms, __FUNCTION__, |
| 3747 | cli_stnames[t->cli_state], |
| 3748 | t->rep->rex, t->req->wex, |
| 3749 | t->req->flags, t->rep->flags, |
| 3750 | t->req->l, t->rep->l); |
| 3751 | |
| 3752 | if (t->req->cons->state == SI_ST_TAR) { |
| 3753 | /* connection might be aborted */ |
| 3754 | if ((t->req->flags & BF_SHUTW_NOW) || |
| 3755 | (t->rep->flags & BF_SHUTW) || |
| 3756 | ((t->req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */ |
| 3757 | (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3758 | |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3759 | trace_term(t, TT_HTTP_SRV_1); |
Willy Tarreau | 3537467 | 2008-09-03 18:11:02 +0200 | [diff] [blame] | 3760 | t->req->cons->exp = TICK_ETERNITY; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3761 | |
| 3762 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3763 | buffer_shutr(t->rep); |
| 3764 | buffer_shutw(t->req); |
| 3765 | if (!t->req->cons->err_type) |
| 3766 | t->req->cons->err_type = SI_ET_CONN_ABRT; |
| 3767 | t->req->cons->state = SI_ST_CLO; |
| 3768 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3769 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3770 | |
Willy Tarreau | 3537467 | 2008-09-03 18:11:02 +0200 | [diff] [blame] | 3771 | if (!tick_is_expired(t->req->cons->exp, now_ms)) |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3772 | return 0; /* still in turn-around */ |
| 3773 | |
| 3774 | t->req->cons->state = SI_ST_INI; |
Willy Tarreau | 3537467 | 2008-09-03 18:11:02 +0200 | [diff] [blame] | 3775 | t->req->cons->exp = TICK_ETERNITY; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3776 | } |
| 3777 | else if (t->req->cons->state == SI_ST_QUE) { |
| 3778 | if (t->pend_pos) { |
| 3779 | /* request still in queue... */ |
Willy Tarreau | 3537467 | 2008-09-03 18:11:02 +0200 | [diff] [blame] | 3780 | if (tick_is_expired(t->req->cons->exp, now_ms)) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3781 | /* ... and timeout expired */ |
| 3782 | trace_term(t, TT_HTTP_SRV_3); |
Willy Tarreau | 3537467 | 2008-09-03 18:11:02 +0200 | [diff] [blame] | 3783 | t->req->cons->exp = TICK_ETERNITY; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3784 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3785 | if (t->srv) |
| 3786 | t->srv->failed_conns++; |
| 3787 | t->be->failed_conns++; |
| 3788 | |
| 3789 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3790 | buffer_shutr(t->rep); |
| 3791 | buffer_shutw(t->req); |
| 3792 | t->req->flags |= BF_WRITE_TIMEOUT; |
| 3793 | if (!t->req->cons->err_type) |
| 3794 | t->req->cons->err_type = SI_ET_QUEUE_TO; |
| 3795 | t->req->cons->state = SI_ST_CLO; |
| 3796 | return 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3797 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3798 | /* connection remains in queue, check if we have to abort it */ |
| 3799 | if ((t->req->flags & BF_SHUTW_NOW) || |
| 3800 | (t->rep->flags & BF_SHUTW) || |
| 3801 | ((t->req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */ |
| 3802 | (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { |
| 3803 | /* give up */ |
| 3804 | trace_term(t, TT_HTTP_SRV_1); |
Willy Tarreau | 3537467 | 2008-09-03 18:11:02 +0200 | [diff] [blame] | 3805 | t->req->cons->exp = TICK_ETERNITY; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3806 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3807 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3808 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3809 | buffer_shutr(t->rep); |
| 3810 | buffer_shutw(t->req); |
| 3811 | if (!t->req->cons->err_type) |
| 3812 | t->req->cons->err_type = SI_ET_QUEUE_ABRT; |
| 3813 | t->req->cons->state = SI_ST_CLO; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3814 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3815 | return 0; |
| 3816 | } |
| 3817 | /* The connection is not in the queue anymore */ |
| 3818 | t->req->cons->state = SI_ST_INI; |
Willy Tarreau | 3537467 | 2008-09-03 18:11:02 +0200 | [diff] [blame] | 3819 | t->req->cons->exp = TICK_ETERNITY; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3820 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3821 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3822 | /* we may get here from above */ |
| 3823 | if (t->req->cons->state == SI_ST_INI) { |
| 3824 | /* no connection in progress, we have to get a new one */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3825 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3826 | /* first, check if the connection has been aborted */ |
| 3827 | if ((t->req->flags & BF_SHUTW_NOW) || |
| 3828 | (t->rep->flags & BF_SHUTW) || |
| 3829 | ((t->req->flags & BF_SHUTR) && |
| 3830 | (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3831 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3832 | trace_term(t, TT_HTTP_SRV_1); |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3833 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3834 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3835 | buffer_shutr(t->rep); |
| 3836 | buffer_shutw(t->req); |
| 3837 | if (!t->req->cons->err_type) |
| 3838 | t->req->cons->err_type = SI_ET_CONN_ABRT; |
| 3839 | t->req->cons->state = SI_ST_CLO; |
| 3840 | return 0; |
| 3841 | } |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3842 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3843 | /* try to get a server assigned */ |
| 3844 | if (srv_redispatch_connect(t) != 0) { |
| 3845 | /* we did not get any server, let's check the cause */ |
| 3846 | if (t->req->cons->state == SI_ST_QUE) { |
| 3847 | /* the connection was queued, that's OK */ |
| 3848 | return 0; |
| 3849 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3850 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3851 | trace_term(t, TT_HTTP_SRV_2); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3852 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3853 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3854 | buffer_shutr(t->rep); |
| 3855 | buffer_shutw(t->req); |
| 3856 | t->req->flags |= BF_WRITE_ERROR; |
| 3857 | if (!t->req->cons->err_type) |
| 3858 | t->req->cons->err_type = SI_ET_CONN_OTHER; |
| 3859 | t->req->cons->state = SI_ST_CLO; |
| 3860 | return 0; |
| 3861 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3862 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3863 | t->req->cons->state = SI_ST_ASS; |
| 3864 | /* Once the server is assigned, we have to return because |
| 3865 | * the caller might be interested in checking several |
| 3866 | * things before connecting. |
| 3867 | */ |
| 3868 | return 1; |
| 3869 | } |
| 3870 | return 0; |
| 3871 | } |
Krzysztof Piotr Oledzki | efe3b6f | 2008-05-23 23:49:32 +0200 | [diff] [blame] | 3872 | |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3873 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3874 | /* |
| 3875 | * This function tries to establish a connection to an assigned server. It also |
| 3876 | * performs connection retries. It may only be called with t->req->cons->state |
| 3877 | * in { SI_ST_ASS, SI_ST_CON }. It may also set the state to SI_ST_INI, |
| 3878 | * SI_ST_EST, or SI_ST_CLO. |
| 3879 | */ |
| 3880 | int stream_sock_connect_server(struct session *t) |
| 3881 | { |
| 3882 | if (t->req->cons->state == SI_ST_ASS) { |
| 3883 | /* server assigned to request, we have to try to connect now */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3884 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3885 | if (!srv_retryable_connect(t)) { |
| 3886 | /* we need to redispatch */ |
| 3887 | t->req->cons->state = SI_ST_INI; |
| 3888 | return 0; |
| 3889 | } |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3890 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3891 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 3892 | if (t->req->cons->state != SI_ST_CON) { |
| 3893 | /* it was an error */ |
| 3894 | trace_term(t, TT_HTTP_SRV_4); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3895 | |
| 3896 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3897 | buffer_shutr(t->rep); |
| 3898 | buffer_shutw(t->req); |
| 3899 | t->req->flags |= BF_WRITE_ERROR; |
| 3900 | if (!t->req->cons->err_type) |
| 3901 | t->req->cons->err_type = SI_ET_CONN_OTHER; |
| 3902 | t->req->cons->state = SI_ST_CLO; |
| 3903 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3904 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3905 | /* We have a socket and switched to SI_ST_CON */ |
| 3906 | } |
| 3907 | |
| 3908 | /* we may also get here from above */ |
| 3909 | if (t->req->cons->state == SI_ST_CON) { |
| 3910 | /* connection in progress or just completed */ |
| 3911 | if (!tcp_connection_status(t)) |
| 3912 | return 0; |
| 3913 | } |
| 3914 | return 0; |
| 3915 | } |
| 3916 | |
| 3917 | |
| 3918 | /* |
| 3919 | * Tries to establish a connection to the server and associate it to the |
| 3920 | * request buffer's consumer side. It is assumed that this function will not be |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 3921 | * be called with SI_ST_EST nor with BF_WRITE_ENA cleared. It normally |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3922 | * returns zero, but may return 1 if it absolutely wants to be called again. |
| 3923 | */ |
| 3924 | int process_srv_conn(struct session *t) |
| 3925 | { |
| 3926 | DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n", |
| 3927 | now_ms, __FUNCTION__, |
| 3928 | cli_stnames[t->cli_state], |
| 3929 | t->rep->rex, t->req->wex, |
| 3930 | t->req->flags, t->rep->flags, |
| 3931 | t->req->l, t->rep->l); |
| 3932 | |
| 3933 | do { |
| 3934 | if (t->req->cons->state == SI_ST_INI || |
| 3935 | t->req->cons->state == SI_ST_TAR || |
| 3936 | t->req->cons->state == SI_ST_QUE) { |
| 3937 | /* try to assign a server */ |
| 3938 | if (!stream_sock_assign_server(t)) |
| 3939 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3940 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3941 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3942 | if (t->req->cons->state == SI_ST_ASS && |
| 3943 | t->srv && t->srv->rdr_len && t->flags & SN_REDIRECTABLE) { |
| 3944 | /* Server supporting redirection and it is possible. |
| 3945 | * Invalid requests are reported as such. It concerns all |
| 3946 | * the largest ones. |
| 3947 | */ |
| 3948 | struct http_txn *txn = &t->txn; |
| 3949 | struct chunk rdr; |
| 3950 | char *path; |
| 3951 | int len; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3952 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3953 | /* 1: create the response header */ |
| 3954 | rdr.len = strlen(HTTP_302); |
| 3955 | rdr.str = trash; |
| 3956 | memcpy(rdr.str, HTTP_302, rdr.len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3957 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3958 | /* 2: add the server's prefix */ |
| 3959 | if (rdr.len + t->srv->rdr_len > sizeof(trash)) |
| 3960 | goto cancel_redir; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3961 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3962 | memcpy(rdr.str + rdr.len, t->srv->rdr_pfx, t->srv->rdr_len); |
| 3963 | rdr.len += t->srv->rdr_len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3964 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3965 | /* 3: add the request URI */ |
| 3966 | path = http_get_path(txn); |
| 3967 | if (!path) |
| 3968 | goto cancel_redir; |
| 3969 | len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path; |
| 3970 | if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */ |
| 3971 | goto cancel_redir; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3972 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3973 | memcpy(rdr.str + rdr.len, path, len); |
| 3974 | rdr.len += len; |
| 3975 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 3976 | rdr.len += 4; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3977 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3978 | srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr); |
| 3979 | trace_term(t, TT_HTTP_SRV_3); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3980 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3981 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
| 3982 | if (t->srv) |
| 3983 | t->srv->cum_sess++; |
| 3984 | |
| 3985 | t->req->cons->state = SI_ST_CLO; |
| 3986 | return 0; |
| 3987 | cancel_redir: |
| 3988 | //txn->status = 400; |
| 3989 | //t->fe->failed_req++; |
| 3990 | //srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, |
| 3991 | // 400, error_message(t, HTTP_ERR_400)); |
| 3992 | trace_term(t, TT_HTTP_SRV_4); |
| 3993 | |
| 3994 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3995 | buffer_shutw(t->req); |
| 3996 | buffer_shutr(t->rep); |
| 3997 | if (!t->req->cons->err_type) |
| 3998 | t->req->cons->err_type = SI_ET_CONN_OTHER; |
| 3999 | t->req->cons->state = SI_ST_CLO; |
| 4000 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4001 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4002 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 4003 | if (t->req->cons->state == SI_ST_CON || |
| 4004 | t->req->cons->state == SI_ST_ASS) { |
| 4005 | stream_sock_connect_server(t); |
| 4006 | } |
| 4007 | } while (t->req->cons->state != SI_ST_CLO && |
| 4008 | t->req->cons->state != SI_ST_CON && |
| 4009 | t->req->cons->state != SI_ST_EST); |
| 4010 | return 0; |
| 4011 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4012 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 4013 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4014 | /* |
| 4015 | * 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] | 4016 | * 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] | 4017 | * be produced. It stops by itself by unsetting the BF_HIJACK flag from the |
| 4018 | * 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] | 4019 | * 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] | 4020 | * when it wants to stop sending data, otherwise 0. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4021 | */ |
| 4022 | int produce_content(struct session *s) |
| 4023 | { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4024 | if (s->data_source == DATA_SRC_NONE) { |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 4025 | buffer_stop_hijack(s->rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4026 | return 1; |
| 4027 | } |
| 4028 | else if (s->data_source == DATA_SRC_STATS) { |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 4029 | /* dump server statistics */ |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 4030 | int ret = stats_dump_http(s, s->be->uri_auth); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 4031 | if (ret >= 0) |
| 4032 | return ret; |
| 4033 | /* -1 indicates an error */ |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 4034 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4035 | |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 4036 | /* unknown data source or internal error */ |
| 4037 | s->txn.status = 500; |
| 4038 | client_retnclose(s, error_message(s, HTTP_ERR_500)); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 4039 | trace_term(s, TT_HTTP_CNT_1); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 4040 | if (!(s->flags & SN_ERR_MASK)) |
| 4041 | s->flags |= SN_ERR_PRXCOND; |
| 4042 | if (!(s->flags & SN_FINST_MASK)) |
| 4043 | s->flags |= SN_FINST_R; |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 4044 | buffer_stop_hijack(s->rep); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 4045 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4046 | } |
| 4047 | |
| 4048 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4049 | /* Iterate the same filter through all request headers. |
| 4050 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4051 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 4052 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4053 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4054 | 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] | 4055 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4056 | char term; |
| 4057 | char *cur_ptr, *cur_end, *cur_next; |
| 4058 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4059 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4060 | struct hdr_idx_elem *cur_hdr; |
| 4061 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 4062 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4063 | last_hdr = 0; |
| 4064 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4065 | 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] | 4066 | old_idx = 0; |
| 4067 | |
| 4068 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4069 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4070 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4071 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4072 | (exp->action == ACT_ALLOW || |
| 4073 | exp->action == ACT_DENY || |
| 4074 | exp->action == ACT_TARPIT)) |
| 4075 | return 0; |
| 4076 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4077 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4078 | if (!cur_idx) |
| 4079 | break; |
| 4080 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4081 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4082 | cur_ptr = cur_next; |
| 4083 | cur_end = cur_ptr + cur_hdr->len; |
| 4084 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4085 | |
| 4086 | /* Now we have one header between cur_ptr and cur_end, |
| 4087 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4088 | */ |
| 4089 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4090 | /* The annoying part is that pattern matching needs |
| 4091 | * that we modify the contents to null-terminate all |
| 4092 | * strings before testing them. |
| 4093 | */ |
| 4094 | |
| 4095 | term = *cur_end; |
| 4096 | *cur_end = '\0'; |
| 4097 | |
| 4098 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4099 | switch (exp->action) { |
| 4100 | case ACT_SETBE: |
| 4101 | /* It is not possible to jump a second time. |
| 4102 | * FIXME: should we return an HTTP/500 here so that |
| 4103 | * the admin knows there's a problem ? |
| 4104 | */ |
| 4105 | if (t->be != t->fe) |
| 4106 | break; |
| 4107 | |
| 4108 | /* Swithing Proxy */ |
| 4109 | t->be = (struct proxy *) exp->replace; |
| 4110 | |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4111 | /* right now, the backend switch is not overly complicated |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4112 | * because we have associated req_cap and rsp_cap to the |
| 4113 | * frontend, and the beconn will be updated later. |
| 4114 | */ |
| 4115 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 4116 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 4117 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 4118 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4119 | last_hdr = 1; |
| 4120 | break; |
| 4121 | |
| 4122 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4123 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4124 | last_hdr = 1; |
| 4125 | break; |
| 4126 | |
| 4127 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4128 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4129 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4130 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4131 | break; |
| 4132 | |
| 4133 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4134 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4135 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4136 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4137 | break; |
| 4138 | |
| 4139 | case ACT_REPLACE: |
| 4140 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4141 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 4142 | /* FIXME: if the user adds a newline in the replacement, the |
| 4143 | * index will not be recalculated for now, and the new line |
| 4144 | * will not be counted as a new header. |
| 4145 | */ |
| 4146 | |
| 4147 | cur_end += delta; |
| 4148 | cur_next += delta; |
| 4149 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4150 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4151 | break; |
| 4152 | |
| 4153 | case ACT_REMOVE: |
| 4154 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 4155 | cur_next += delta; |
| 4156 | |
| 4157 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4158 | txn->req.eoh += delta; |
| 4159 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4160 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4161 | cur_hdr->len = 0; |
| 4162 | cur_end = NULL; /* null-term has been rewritten */ |
| 4163 | break; |
| 4164 | |
| 4165 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4166 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4167 | if (cur_end) |
| 4168 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4169 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4170 | /* keep the link from this header to next one in case of later |
| 4171 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4172 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4173 | old_idx = cur_idx; |
| 4174 | } |
| 4175 | return 0; |
| 4176 | } |
| 4177 | |
| 4178 | |
| 4179 | /* Apply the filter to the request line. |
| 4180 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 4181 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4182 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 4183 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4184 | */ |
| 4185 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 4186 | { |
| 4187 | char term; |
| 4188 | char *cur_ptr, *cur_end; |
| 4189 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4190 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4191 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4192 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4193 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4194 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4195 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4196 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4197 | (exp->action == ACT_ALLOW || |
| 4198 | exp->action == ACT_DENY || |
| 4199 | exp->action == ACT_TARPIT)) |
| 4200 | return 0; |
| 4201 | else if (exp->action == ACT_REMOVE) |
| 4202 | return 0; |
| 4203 | |
| 4204 | done = 0; |
| 4205 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4206 | 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] | 4207 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4208 | |
| 4209 | /* Now we have the request line between cur_ptr and cur_end */ |
| 4210 | |
| 4211 | /* The annoying part is that pattern matching needs |
| 4212 | * that we modify the contents to null-terminate all |
| 4213 | * strings before testing them. |
| 4214 | */ |
| 4215 | |
| 4216 | term = *cur_end; |
| 4217 | *cur_end = '\0'; |
| 4218 | |
| 4219 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4220 | switch (exp->action) { |
| 4221 | case ACT_SETBE: |
| 4222 | /* It is not possible to jump a second time. |
| 4223 | * FIXME: should we return an HTTP/500 here so that |
| 4224 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4225 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4226 | if (t->be != t->fe) |
| 4227 | break; |
| 4228 | |
| 4229 | /* Swithing Proxy */ |
| 4230 | t->be = (struct proxy *) exp->replace; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4231 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4232 | /* right now, the backend switch is not too much complicated |
| 4233 | * because we have associated req_cap and rsp_cap to the |
| 4234 | * frontend, and the beconn will be updated later. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4235 | */ |
| 4236 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 4237 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 4238 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 4239 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4240 | done = 1; |
| 4241 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4242 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4243 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4244 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4245 | done = 1; |
| 4246 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4247 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4248 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4249 | txn->flags |= TX_CLDENY; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4250 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4251 | done = 1; |
| 4252 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4253 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4254 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4255 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4256 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4257 | done = 1; |
| 4258 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4259 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4260 | case ACT_REPLACE: |
| 4261 | *cur_end = term; /* restore the string terminator */ |
| 4262 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4263 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 4264 | /* FIXME: if the user adds a newline in the replacement, the |
| 4265 | * index will not be recalculated for now, and the new line |
| 4266 | * will not be counted as a new header. |
| 4267 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4268 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4269 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4270 | cur_end += delta; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4271 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4272 | 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] | 4273 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4274 | HTTP_MSG_RQMETH, |
| 4275 | cur_ptr, cur_end + 1, |
| 4276 | NULL, NULL); |
| 4277 | if (unlikely(!cur_end)) |
| 4278 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4279 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4280 | /* we have a full request and we know that we have either a CR |
| 4281 | * or an LF at <ptr>. |
| 4282 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4283 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 4284 | 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] | 4285 | /* there is no point trying this regex on headers */ |
| 4286 | return 1; |
| 4287 | } |
| 4288 | } |
| 4289 | *cur_end = term; /* restore the string terminator */ |
| 4290 | return done; |
| 4291 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 4292 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4293 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4294 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4295 | /* |
| 4296 | * Apply all the req filters <exp> to all headers in buffer <req> of session <t>. |
| 4297 | * 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] | 4298 | * unparsable request. Since it can manage the switch to another backend, it |
| 4299 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4300 | */ |
| 4301 | int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 4302 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4303 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4304 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4305 | while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4306 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4307 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4308 | /* |
| 4309 | * The interleaving of transformations and verdicts |
| 4310 | * makes it difficult to decide to continue or stop |
| 4311 | * the evaluation. |
| 4312 | */ |
| 4313 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4314 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4315 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 4316 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) { |
| 4317 | exp = exp->next; |
| 4318 | continue; |
| 4319 | } |
| 4320 | |
| 4321 | /* Apply the filter to the request line. */ |
| 4322 | ret = apply_filter_to_req_line(t, req, exp); |
| 4323 | if (unlikely(ret < 0)) |
| 4324 | return -1; |
| 4325 | |
| 4326 | if (likely(ret == 0)) { |
| 4327 | /* The filter did not match the request, it can be |
| 4328 | * iterated through all headers. |
| 4329 | */ |
| 4330 | apply_filter_to_req_headers(t, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4331 | } |
| 4332 | exp = exp->next; |
| 4333 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4334 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4335 | } |
| 4336 | |
| 4337 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4338 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4339 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 4340 | * Manage client-side cookie. It can impact performance by about 2% so it is |
| 4341 | * desirable to call it only when needed. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4342 | */ |
| 4343 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 4344 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4345 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4346 | char *p1, *p2, *p3, *p4; |
| 4347 | char *del_colon, *del_cookie, *colon; |
| 4348 | int app_cookies; |
| 4349 | |
| 4350 | appsess *asession_temp = NULL; |
| 4351 | appsess local_asession; |
| 4352 | |
| 4353 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4354 | int cur_idx, old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4355 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4356 | /* Iterate through the headers. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4357 | * we start with the start line. |
| 4358 | */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 4359 | old_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4360 | 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] | 4361 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4362 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4363 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4364 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4365 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4366 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4367 | cur_ptr = cur_next; |
| 4368 | cur_end = cur_ptr + cur_hdr->len; |
| 4369 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4370 | |
| 4371 | /* We have one full header between cur_ptr and cur_end, and the |
| 4372 | * next header starts at cur_next. We're only interested in |
| 4373 | * "Cookie:" headers. |
| 4374 | */ |
| 4375 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4376 | val = http_header_match2(cur_ptr, cur_end, "Cookie", 6); |
| 4377 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4378 | old_idx = cur_idx; |
| 4379 | continue; |
| 4380 | } |
| 4381 | |
| 4382 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 4383 | * attributes whose name begin with a '$', and associate them with |
| 4384 | * the right cookie, if we want to delete this cookie. |
| 4385 | * So there are 3 cases for each cookie read : |
| 4386 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 4387 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 4388 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 4389 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 4390 | * "special" cookie. |
| 4391 | * At the end of loop, if a "special" cookie remains, we may have to |
| 4392 | * remove it. If no application cookie persists in the header, we |
| 4393 | * *MUST* delete it |
| 4394 | */ |
| 4395 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4396 | colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4397 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4398 | /* del_cookie == NULL => nothing to be deleted */ |
| 4399 | del_colon = del_cookie = NULL; |
| 4400 | app_cookies = 0; |
| 4401 | |
| 4402 | while (p1 < cur_end) { |
| 4403 | /* skip spaces and colons, but keep an eye on these ones */ |
| 4404 | while (p1 < cur_end) { |
| 4405 | if (*p1 == ';' || *p1 == ',') |
| 4406 | colon = p1; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4407 | else if (!isspace((unsigned char)*p1)) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4408 | break; |
| 4409 | p1++; |
| 4410 | } |
| 4411 | |
| 4412 | if (p1 == cur_end) |
| 4413 | break; |
| 4414 | |
| 4415 | /* p1 is at the beginning of the cookie name */ |
| 4416 | p2 = p1; |
| 4417 | while (p2 < cur_end && *p2 != '=') |
| 4418 | p2++; |
| 4419 | |
| 4420 | if (p2 == cur_end) |
| 4421 | break; |
| 4422 | |
| 4423 | p3 = p2 + 1; /* skips the '=' sign */ |
| 4424 | if (p3 == cur_end) |
| 4425 | break; |
| 4426 | |
| 4427 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4428 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4429 | p4++; |
| 4430 | |
| 4431 | /* here, we have the cookie name between p1 and p2, |
| 4432 | * and its value between p3 and p4. |
| 4433 | * we can process it : |
| 4434 | * |
| 4435 | * Cookie: NAME=VALUE; |
| 4436 | * | || || | |
| 4437 | * | || || +--> p4 |
| 4438 | * | || |+-------> p3 |
| 4439 | * | || +--------> p2 |
| 4440 | * | |+------------> p1 |
| 4441 | * | +-------------> colon |
| 4442 | * +--------------------> cur_ptr |
| 4443 | */ |
| 4444 | |
| 4445 | if (*p1 == '$') { |
| 4446 | /* skip this one */ |
| 4447 | } |
| 4448 | else { |
| 4449 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4450 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4451 | txn->cli_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4452 | (p4 - p1 >= t->fe->capture_namelen) && |
| 4453 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4454 | int log_len = p4 - p1; |
| 4455 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 4456 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4457 | Alert("HTTP logging : out of memory.\n"); |
| 4458 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4459 | if (log_len > t->fe->capture_len) |
| 4460 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4461 | memcpy(txn->cli_cookie, p1, log_len); |
| 4462 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4463 | } |
| 4464 | } |
| 4465 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4466 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 4467 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4468 | /* Cool... it's the right one */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4469 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4470 | char *delim; |
| 4471 | |
| 4472 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 4473 | * have the server ID betweek p3 and delim, and the original cookie between |
| 4474 | * delim+1 and p4. Otherwise, delim==p4 : |
| 4475 | * |
| 4476 | * Cookie: NAME=SRV~VALUE; |
| 4477 | * | || || | | |
| 4478 | * | || || | +--> p4 |
| 4479 | * | || || +--------> delim |
| 4480 | * | || |+-----------> p3 |
| 4481 | * | || +------------> p2 |
| 4482 | * | |+----------------> p1 |
| 4483 | * | +-----------------> colon |
| 4484 | * +------------------------> cur_ptr |
| 4485 | */ |
| 4486 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4487 | if (t->be->options & PR_O_COOK_PFX) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4488 | for (delim = p3; delim < p4; delim++) |
| 4489 | if (*delim == COOKIE_DELIM) |
| 4490 | break; |
| 4491 | } |
| 4492 | else |
| 4493 | delim = p4; |
| 4494 | |
| 4495 | |
| 4496 | /* Here, we'll look for the first running server which supports the cookie. |
| 4497 | * This allows to share a same cookie between several servers, for example |
| 4498 | * to dedicate backup servers to specific servers only. |
| 4499 | * However, to prevent clients from sticking to cookie-less backup server |
| 4500 | * when they have incidentely learned an empty cookie, we simply ignore |
| 4501 | * empty cookies and mark them as invalid. |
| 4502 | */ |
| 4503 | if (delim == p3) |
| 4504 | srv = NULL; |
| 4505 | |
| 4506 | while (srv) { |
Willy Tarreau | 92f2ab1 | 2007-02-02 22:14:47 +0100 | [diff] [blame] | 4507 | if (srv->cookie && (srv->cklen == delim - p3) && |
| 4508 | !memcmp(p3, srv->cookie, delim - p3)) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4509 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4510 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4511 | txn->flags &= ~TX_CK_MASK; |
| 4512 | txn->flags |= TX_CK_VALID; |
| 4513 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4514 | t->srv = srv; |
| 4515 | break; |
| 4516 | } else { |
| 4517 | /* we found a server, but it's down */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4518 | txn->flags &= ~TX_CK_MASK; |
| 4519 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4520 | } |
| 4521 | } |
| 4522 | srv = srv->next; |
| 4523 | } |
| 4524 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4525 | if (!srv && !(txn->flags & TX_CK_DOWN)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4526 | /* no server matched this cookie */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4527 | txn->flags &= ~TX_CK_MASK; |
| 4528 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4529 | } |
| 4530 | |
| 4531 | /* depending on the cookie mode, we may have to either : |
| 4532 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 4533 | * the server never sees it ; |
| 4534 | * - remove the server id from the cookie value, and tag the cookie as an |
| 4535 | * application cookie so that it does not get accidentely removed later, |
| 4536 | * if we're in cookie prefix mode |
| 4537 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4538 | if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4539 | int delta; /* negative */ |
| 4540 | |
| 4541 | delta = buffer_replace2(req, p3, delim + 1, NULL, 0); |
| 4542 | p4 += delta; |
| 4543 | cur_end += delta; |
| 4544 | cur_next += delta; |
| 4545 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4546 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4547 | |
| 4548 | del_cookie = del_colon = NULL; |
| 4549 | app_cookies++; /* protect the header from deletion */ |
| 4550 | } |
| 4551 | else if (del_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4552 | (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] | 4553 | del_cookie = p1; |
| 4554 | del_colon = colon; |
| 4555 | } |
| 4556 | } else { |
| 4557 | /* now we know that we must keep this cookie since it's |
| 4558 | * not ours. But if we wanted to delete our cookie |
| 4559 | * earlier, we cannot remove the complete header, but we |
| 4560 | * can remove the previous block itself. |
| 4561 | */ |
| 4562 | app_cookies++; |
| 4563 | |
| 4564 | if (del_cookie != NULL) { |
| 4565 | int delta; /* negative */ |
| 4566 | |
| 4567 | delta = buffer_replace2(req, del_cookie, p1, NULL, 0); |
| 4568 | p4 += delta; |
| 4569 | cur_end += delta; |
| 4570 | cur_next += delta; |
| 4571 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4572 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4573 | del_cookie = del_colon = NULL; |
| 4574 | } |
| 4575 | } |
| 4576 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4577 | if ((t->be->appsession_name != NULL) && |
| 4578 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4579 | /* first, let's see if the cookie is our appcookie*/ |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4580 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4581 | /* Cool... it's the right one */ |
| 4582 | |
| 4583 | asession_temp = &local_asession; |
| 4584 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4585 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4586 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 4587 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 4588 | return; |
| 4589 | } |
| 4590 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4591 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 4592 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4593 | asession_temp->serverid = NULL; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4594 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4595 | /* only do insert, if lookup fails */ |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4596 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 4597 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4598 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4599 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4600 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4601 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 4602 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 4603 | return; |
| 4604 | } |
| 4605 | |
| 4606 | asession_temp->sessid = local_asession.sessid; |
| 4607 | asession_temp->serverid = local_asession.serverid; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4608 | asession_temp->request_count = 0; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4609 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4610 | } else { |
| 4611 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4612 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4613 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4614 | if (asession_temp->serverid == NULL) { |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4615 | /* TODO redispatch request */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4616 | Alert("Found Application Session without matching server.\n"); |
| 4617 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4618 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4619 | while (srv) { |
| 4620 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4621 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4622 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4623 | txn->flags &= ~TX_CK_MASK; |
| 4624 | txn->flags |= TX_CK_VALID; |
| 4625 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4626 | t->srv = srv; |
| 4627 | break; |
| 4628 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4629 | txn->flags &= ~TX_CK_MASK; |
| 4630 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4631 | } |
| 4632 | } |
| 4633 | srv = srv->next; |
| 4634 | }/* end while(srv) */ |
| 4635 | }/* end else if server == NULL */ |
| 4636 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 4637 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4638 | asession_temp->request_count++; |
| 4639 | #if defined(DEBUG_HASH) |
| 4640 | Alert("manage_client_side_cookies\n"); |
| 4641 | appsession_hash_dump(&(t->be->htbl_proxy)); |
| 4642 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4643 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 4644 | } |
| 4645 | |
| 4646 | /* we'll have to look for another cookie ... */ |
| 4647 | p1 = p4; |
| 4648 | } /* while (p1 < cur_end) */ |
| 4649 | |
| 4650 | /* There's no more cookie on this line. |
| 4651 | * We may have marked the last one(s) for deletion. |
| 4652 | * We must do this now in two ways : |
| 4653 | * - if there is no app cookie, we simply delete the header ; |
| 4654 | * - if there are app cookies, we must delete the end of the |
| 4655 | * string properly, including the colon/semi-colon before |
| 4656 | * the cookie name. |
| 4657 | */ |
| 4658 | if (del_cookie != NULL) { |
| 4659 | int delta; |
| 4660 | if (app_cookies) { |
| 4661 | delta = buffer_replace2(req, del_colon, cur_end, NULL, 0); |
| 4662 | cur_end = del_colon; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4663 | cur_hdr->len += delta; |
| 4664 | } else { |
| 4665 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4666 | |
| 4667 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4668 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4669 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4670 | cur_hdr->len = 0; |
| 4671 | } |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 4672 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4673 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4674 | } |
| 4675 | |
| 4676 | /* keep the link from this header to next one */ |
| 4677 | old_idx = cur_idx; |
| 4678 | } /* end of cookie processing on this header */ |
| 4679 | } |
| 4680 | |
| 4681 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4682 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 4683 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 4684 | */ |
| 4685 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4686 | { |
| 4687 | char term; |
| 4688 | char *cur_ptr, *cur_end, *cur_next; |
| 4689 | int cur_idx, old_idx, last_hdr; |
| 4690 | struct http_txn *txn = &t->txn; |
| 4691 | struct hdr_idx_elem *cur_hdr; |
| 4692 | int len, delta; |
| 4693 | |
| 4694 | last_hdr = 0; |
| 4695 | |
| 4696 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4697 | old_idx = 0; |
| 4698 | |
| 4699 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4700 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4701 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4702 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4703 | (exp->action == ACT_ALLOW || |
| 4704 | exp->action == ACT_DENY)) |
| 4705 | return 0; |
| 4706 | |
| 4707 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 4708 | if (!cur_idx) |
| 4709 | break; |
| 4710 | |
| 4711 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4712 | cur_ptr = cur_next; |
| 4713 | cur_end = cur_ptr + cur_hdr->len; |
| 4714 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4715 | |
| 4716 | /* Now we have one header between cur_ptr and cur_end, |
| 4717 | * and the next header starts at cur_next. |
| 4718 | */ |
| 4719 | |
| 4720 | /* The annoying part is that pattern matching needs |
| 4721 | * that we modify the contents to null-terminate all |
| 4722 | * strings before testing them. |
| 4723 | */ |
| 4724 | |
| 4725 | term = *cur_end; |
| 4726 | *cur_end = '\0'; |
| 4727 | |
| 4728 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4729 | switch (exp->action) { |
| 4730 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4731 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4732 | last_hdr = 1; |
| 4733 | break; |
| 4734 | |
| 4735 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4736 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4737 | last_hdr = 1; |
| 4738 | break; |
| 4739 | |
| 4740 | case ACT_REPLACE: |
| 4741 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4742 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 4743 | /* FIXME: if the user adds a newline in the replacement, the |
| 4744 | * index will not be recalculated for now, and the new line |
| 4745 | * will not be counted as a new header. |
| 4746 | */ |
| 4747 | |
| 4748 | cur_end += delta; |
| 4749 | cur_next += delta; |
| 4750 | cur_hdr->len += delta; |
| 4751 | txn->rsp.eoh += delta; |
| 4752 | break; |
| 4753 | |
| 4754 | case ACT_REMOVE: |
| 4755 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 4756 | cur_next += delta; |
| 4757 | |
| 4758 | /* FIXME: this should be a separate function */ |
| 4759 | txn->rsp.eoh += delta; |
| 4760 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4761 | txn->hdr_idx.used--; |
| 4762 | cur_hdr->len = 0; |
| 4763 | cur_end = NULL; /* null-term has been rewritten */ |
| 4764 | break; |
| 4765 | |
| 4766 | } |
| 4767 | } |
| 4768 | if (cur_end) |
| 4769 | *cur_end = term; /* restore the string terminator */ |
| 4770 | |
| 4771 | /* keep the link from this header to next one in case of later |
| 4772 | * removal of next header. |
| 4773 | */ |
| 4774 | old_idx = cur_idx; |
| 4775 | } |
| 4776 | return 0; |
| 4777 | } |
| 4778 | |
| 4779 | |
| 4780 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 4781 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 4782 | * or -1 if a replacement resulted in an invalid status line. |
| 4783 | */ |
| 4784 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4785 | { |
| 4786 | char term; |
| 4787 | char *cur_ptr, *cur_end; |
| 4788 | int done; |
| 4789 | struct http_txn *txn = &t->txn; |
| 4790 | int len, delta; |
| 4791 | |
| 4792 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4793 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4794 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4795 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4796 | (exp->action == ACT_ALLOW || |
| 4797 | exp->action == ACT_DENY)) |
| 4798 | return 0; |
| 4799 | else if (exp->action == ACT_REMOVE) |
| 4800 | return 0; |
| 4801 | |
| 4802 | done = 0; |
| 4803 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4804 | 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] | 4805 | cur_end = cur_ptr + txn->rsp.sl.rq.l; |
| 4806 | |
| 4807 | /* Now we have the status line between cur_ptr and cur_end */ |
| 4808 | |
| 4809 | /* The annoying part is that pattern matching needs |
| 4810 | * that we modify the contents to null-terminate all |
| 4811 | * strings before testing them. |
| 4812 | */ |
| 4813 | |
| 4814 | term = *cur_end; |
| 4815 | *cur_end = '\0'; |
| 4816 | |
| 4817 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4818 | switch (exp->action) { |
| 4819 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4820 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4821 | done = 1; |
| 4822 | break; |
| 4823 | |
| 4824 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4825 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4826 | done = 1; |
| 4827 | break; |
| 4828 | |
| 4829 | case ACT_REPLACE: |
| 4830 | *cur_end = term; /* restore the string terminator */ |
| 4831 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4832 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 4833 | /* FIXME: if the user adds a newline in the replacement, the |
| 4834 | * index will not be recalculated for now, and the new line |
| 4835 | * will not be counted as a new header. |
| 4836 | */ |
| 4837 | |
| 4838 | txn->rsp.eoh += delta; |
| 4839 | cur_end += delta; |
| 4840 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4841 | 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] | 4842 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 4843 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4844 | cur_ptr, cur_end + 1, |
| 4845 | NULL, NULL); |
| 4846 | if (unlikely(!cur_end)) |
| 4847 | return -1; |
| 4848 | |
| 4849 | /* we have a full respnse and we know that we have either a CR |
| 4850 | * or an LF at <ptr>. |
| 4851 | */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4852 | 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] | 4853 | hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r'); |
| 4854 | /* there is no point trying this regex on headers */ |
| 4855 | return 1; |
| 4856 | } |
| 4857 | } |
| 4858 | *cur_end = term; /* restore the string terminator */ |
| 4859 | return done; |
| 4860 | } |
| 4861 | |
| 4862 | |
| 4863 | |
| 4864 | /* |
| 4865 | * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>. |
| 4866 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 4867 | * unparsable response. |
| 4868 | */ |
| 4869 | int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4870 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4871 | struct http_txn *txn = &t->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4872 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4873 | while (exp && !(txn->flags & TX_SVDENY)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4874 | int ret; |
| 4875 | |
| 4876 | /* |
| 4877 | * The interleaving of transformations and verdicts |
| 4878 | * makes it difficult to decide to continue or stop |
| 4879 | * the evaluation. |
| 4880 | */ |
| 4881 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4882 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4883 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 4884 | exp->action == ACT_PASS)) { |
| 4885 | exp = exp->next; |
| 4886 | continue; |
| 4887 | } |
| 4888 | |
| 4889 | /* Apply the filter to the status line. */ |
| 4890 | ret = apply_filter_to_sts_line(t, rtr, exp); |
| 4891 | if (unlikely(ret < 0)) |
| 4892 | return -1; |
| 4893 | |
| 4894 | if (likely(ret == 0)) { |
| 4895 | /* The filter did not match the response, it can be |
| 4896 | * iterated through all headers. |
| 4897 | */ |
| 4898 | apply_filter_to_resp_headers(t, rtr, exp); |
| 4899 | } |
| 4900 | exp = exp->next; |
| 4901 | } |
| 4902 | return 0; |
| 4903 | } |
| 4904 | |
| 4905 | |
| 4906 | |
| 4907 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 4908 | * Manage server-side cookies. It can impact performance by about 2% so it is |
| 4909 | * desirable to call it only when needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4910 | */ |
| 4911 | void manage_server_side_cookies(struct session *t, struct buffer *rtr) |
| 4912 | { |
| 4913 | struct http_txn *txn = &t->txn; |
| 4914 | char *p1, *p2, *p3, *p4; |
| 4915 | |
| 4916 | appsess *asession_temp = NULL; |
| 4917 | appsess local_asession; |
| 4918 | |
| 4919 | char *cur_ptr, *cur_end, *cur_next; |
| 4920 | int cur_idx, old_idx, delta; |
| 4921 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4922 | /* Iterate through the headers. |
| 4923 | * we start with the start line. |
| 4924 | */ |
| 4925 | old_idx = 0; |
| 4926 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4927 | |
| 4928 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 4929 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4930 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4931 | |
| 4932 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4933 | cur_ptr = cur_next; |
| 4934 | cur_end = cur_ptr + cur_hdr->len; |
| 4935 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4936 | |
| 4937 | /* We have one full header between cur_ptr and cur_end, and the |
| 4938 | * next header starts at cur_next. We're only interested in |
| 4939 | * "Cookie:" headers. |
| 4940 | */ |
| 4941 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4942 | val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10); |
| 4943 | if (!val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4944 | old_idx = cur_idx; |
| 4945 | continue; |
| 4946 | } |
| 4947 | |
| 4948 | /* 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] | 4949 | txn->flags |= TX_SCK_ANY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4950 | |
| 4951 | |
| 4952 | /* maybe we only wanted to see if there was a set-cookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4953 | if (t->be->cookie_name == NULL && |
| 4954 | t->be->appsession_name == NULL && |
| 4955 | t->be->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4956 | return; |
| 4957 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4958 | p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4959 | |
| 4960 | 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] | 4961 | if (p1 == cur_end || *p1 == ';') /* end of cookie */ |
| 4962 | break; |
| 4963 | |
| 4964 | /* p1 is at the beginning of the cookie name */ |
| 4965 | p2 = p1; |
| 4966 | |
| 4967 | while (p2 < cur_end && *p2 != '=' && *p2 != ';') |
| 4968 | p2++; |
| 4969 | |
| 4970 | if (p2 == cur_end || *p2 == ';') /* next cookie */ |
| 4971 | break; |
| 4972 | |
| 4973 | p3 = p2 + 1; /* skip the '=' sign */ |
| 4974 | if (p3 == cur_end) |
| 4975 | break; |
| 4976 | |
| 4977 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4978 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';') |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4979 | p4++; |
| 4980 | |
| 4981 | /* here, we have the cookie name between p1 and p2, |
| 4982 | * and its value between p3 and p4. |
| 4983 | * we can process it. |
| 4984 | */ |
| 4985 | |
| 4986 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4987 | if (t->be->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4988 | txn->srv_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4989 | (p4 - p1 >= t->be->capture_namelen) && |
| 4990 | memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4991 | int log_len = p4 - p1; |
| 4992 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 4993 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4994 | Alert("HTTP logging : out of memory.\n"); |
| 4995 | } |
| 4996 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4997 | if (log_len > t->be->capture_len) |
| 4998 | log_len = t->be->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4999 | memcpy(txn->srv_cookie, p1, log_len); |
| 5000 | txn->srv_cookie[log_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5001 | } |
| 5002 | |
| 5003 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5004 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 5005 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5006 | /* Cool... it's the right one */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5007 | txn->flags |= TX_SCK_SEEN; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5008 | |
| 5009 | /* If the cookie is in insert mode on a known server, we'll delete |
| 5010 | * this occurrence because we'll insert another one later. |
| 5011 | * We'll delete it too if the "indirect" option is set and we're in |
| 5012 | * a direct access. */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5013 | if (((t->srv) && (t->be->options & PR_O_COOK_INS)) || |
| 5014 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5015 | /* this header must be deleted */ |
| 5016 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 5017 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5018 | txn->hdr_idx.used--; |
| 5019 | cur_hdr->len = 0; |
| 5020 | cur_next += delta; |
| 5021 | txn->rsp.eoh += delta; |
| 5022 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5023 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5024 | } |
| 5025 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5026 | (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5027 | /* replace bytes p3->p4 with the cookie name associated |
| 5028 | * with this server since we know it. |
| 5029 | */ |
| 5030 | delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen); |
| 5031 | cur_hdr->len += delta; |
| 5032 | cur_next += delta; |
| 5033 | txn->rsp.eoh += delta; |
| 5034 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5035 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5036 | } |
| 5037 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5038 | (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5039 | /* insert the cookie name associated with this server |
| 5040 | * before existing cookie, and insert a delimitor between them.. |
| 5041 | */ |
| 5042 | delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1); |
| 5043 | cur_hdr->len += delta; |
| 5044 | cur_next += delta; |
| 5045 | txn->rsp.eoh += delta; |
| 5046 | |
| 5047 | p3[t->srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5048 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5049 | } |
| 5050 | } |
| 5051 | /* next, let's see if the cookie is our appcookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5052 | else if ((t->be->appsession_name != NULL) && |
| 5053 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5054 | |
| 5055 | /* Cool... it's the right one */ |
| 5056 | |
| 5057 | size_t server_id_len = strlen(t->srv->id) + 1; |
| 5058 | asession_temp = &local_asession; |
| 5059 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5060 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5061 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5062 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5063 | return; |
| 5064 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5065 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 5066 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5067 | asession_temp->serverid = NULL; |
| 5068 | |
| 5069 | /* only do insert, if lookup fails */ |
Ryan Warnick | 6d0b1fa | 2008-02-17 11:24:35 +0100 | [diff] [blame] | 5070 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 5071 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5072 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5073 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 5074 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 5075 | return; |
| 5076 | } |
| 5077 | asession_temp->sessid = local_asession.sessid; |
| 5078 | asession_temp->serverid = local_asession.serverid; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5079 | asession_temp->request_count = 0; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5080 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
| 5081 | } else { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5082 | /* free wasted memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5083 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5084 | } |
| 5085 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5086 | if (asession_temp->serverid == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5087 | if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5088 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5089 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5090 | return; |
| 5091 | } |
| 5092 | asession_temp->serverid[0] = '\0'; |
| 5093 | } |
| 5094 | |
| 5095 | if (asession_temp->serverid[0] == '\0') |
| 5096 | memcpy(asession_temp->serverid, t->srv->id, server_id_len); |
| 5097 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 5098 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5099 | asession_temp->request_count++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5100 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5101 | Alert("manage_server_side_cookies\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5102 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5103 | #endif |
| 5104 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 5105 | break; /* we don't want to loop again since there cannot be another cookie on the same line */ |
| 5106 | } /* we're now at the end of the cookie value */ |
| 5107 | |
| 5108 | /* keep the link from this header to next one */ |
| 5109 | old_idx = cur_idx; |
| 5110 | } /* end of cookie processing on this header */ |
| 5111 | } |
| 5112 | |
| 5113 | |
| 5114 | |
| 5115 | /* |
| 5116 | * Check if response is cacheable or not. Updates t->flags. |
| 5117 | */ |
| 5118 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 5119 | { |
| 5120 | struct http_txn *txn = &t->txn; |
| 5121 | char *p1, *p2; |
| 5122 | |
| 5123 | char *cur_ptr, *cur_end, *cur_next; |
| 5124 | int cur_idx; |
| 5125 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 5126 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5127 | return; |
| 5128 | |
| 5129 | /* Iterate through the headers. |
| 5130 | * we start with the start line. |
| 5131 | */ |
| 5132 | cur_idx = 0; |
| 5133 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 5134 | |
| 5135 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 5136 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5137 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5138 | |
| 5139 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 5140 | cur_ptr = cur_next; |
| 5141 | cur_end = cur_ptr + cur_hdr->len; |
| 5142 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5143 | |
| 5144 | /* We have one full header between cur_ptr and cur_end, and the |
| 5145 | * next header starts at cur_next. We're only interested in |
| 5146 | * "Cookie:" headers. |
| 5147 | */ |
| 5148 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5149 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 5150 | if (val) { |
| 5151 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 5152 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 5153 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 5154 | return; |
| 5155 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5156 | } |
| 5157 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5158 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 5159 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5160 | continue; |
| 5161 | |
| 5162 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 5163 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5164 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5165 | |
| 5166 | if (p1 >= cur_end) /* no more info */ |
| 5167 | continue; |
| 5168 | |
| 5169 | /* p1 is at the beginning of the value */ |
| 5170 | p2 = p1; |
| 5171 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 5172 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5173 | p2++; |
| 5174 | |
| 5175 | /* we have a complete value between p1 and p2 */ |
| 5176 | if (p2 < cur_end && *p2 == '=') { |
| 5177 | /* we have something of the form no-cache="set-cookie" */ |
| 5178 | if ((cur_end - p1 >= 21) && |
| 5179 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 5180 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5181 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5182 | continue; |
| 5183 | } |
| 5184 | |
| 5185 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 5186 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 5187 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 5188 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 5189 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5190 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5191 | return; |
| 5192 | } |
| 5193 | |
| 5194 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5195 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5196 | continue; |
| 5197 | } |
| 5198 | } |
| 5199 | } |
| 5200 | |
| 5201 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5202 | /* |
| 5203 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 5204 | * If the server is found, it's assigned to the session. |
| 5205 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5206 | 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] | 5207 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5208 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5209 | appsess *asession_temp = NULL; |
| 5210 | appsess local_asession; |
| 5211 | char *request_line; |
| 5212 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5213 | if (t->be->appsession_name == NULL || |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 5214 | (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) || |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5215 | (request_line = memchr(begin, ';', len)) == NULL || |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5216 | ((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] | 5217 | return; |
| 5218 | |
| 5219 | /* skip ';' */ |
| 5220 | request_line++; |
| 5221 | |
| 5222 | /* look if we have a jsessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5223 | 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] | 5224 | return; |
| 5225 | |
| 5226 | /* skip jsessionid= */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5227 | request_line += t->be->appsession_name_len + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5228 | |
| 5229 | /* First try if we already have an appsession */ |
| 5230 | asession_temp = &local_asession; |
| 5231 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5232 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5233 | Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n"); |
| 5234 | send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n"); |
| 5235 | return; |
| 5236 | } |
| 5237 | |
| 5238 | /* Copy the sessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5239 | memcpy(asession_temp->sessid, request_line, t->be->appsession_len); |
| 5240 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5241 | asession_temp->serverid = NULL; |
| 5242 | |
| 5243 | /* only do insert, if lookup fails */ |
Ryan Warnick | 6d0b1fa | 2008-02-17 11:24:35 +0100 | [diff] [blame] | 5244 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 5245 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5246 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5247 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5248 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5249 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 5250 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 5251 | return; |
| 5252 | } |
| 5253 | asession_temp->sessid = local_asession.sessid; |
| 5254 | asession_temp->serverid = local_asession.serverid; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5255 | asession_temp->request_count=0; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5256 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5257 | } |
| 5258 | else { |
| 5259 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5260 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5261 | } |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5262 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 5263 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5264 | asession_temp->request_count++; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5265 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5266 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5267 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5268 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5269 | #endif |
| 5270 | if (asession_temp->serverid == NULL) { |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5271 | /* TODO redispatch request */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5272 | Alert("Found Application Session without matching server.\n"); |
| 5273 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5274 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5275 | while (srv) { |
| 5276 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5277 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5278 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5279 | txn->flags &= ~TX_CK_MASK; |
| 5280 | txn->flags |= TX_CK_VALID; |
| 5281 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5282 | t->srv = srv; |
| 5283 | break; |
| 5284 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5285 | txn->flags &= ~TX_CK_MASK; |
| 5286 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5287 | } |
| 5288 | } |
| 5289 | srv = srv->next; |
| 5290 | } |
| 5291 | } |
| 5292 | } |
| 5293 | |
| 5294 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5295 | /* |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5296 | * In a GET or HEAD request, check if the requested URI matches the stats uri |
| 5297 | * 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] | 5298 | * |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5299 | * 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] | 5300 | * 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] | 5301 | * produce_content() can be called to start sending data. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5302 | * |
| 5303 | * Returns 1 if the session's state changes, otherwise 0. |
| 5304 | */ |
| 5305 | int stats_check_uri_auth(struct session *t, struct proxy *backend) |
| 5306 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5307 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5308 | struct uri_auth *uri_auth = backend->uri_auth; |
| 5309 | struct user_auth *user; |
| 5310 | int authenticated, cur_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5311 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5312 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5313 | memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats)); |
| 5314 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5315 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5316 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5317 | return 0; |
| 5318 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5319 | h = t->req->data + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5320 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5321 | /* the URI is in h */ |
| 5322 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5323 | return 0; |
| 5324 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 5325 | h += uri_auth->uri_len; |
| 5326 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) { |
| 5327 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5328 | t->data_ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 5329 | break; |
| 5330 | } |
| 5331 | h++; |
| 5332 | } |
| 5333 | |
| 5334 | if (uri_auth->refresh) { |
| 5335 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 5336 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) { |
| 5337 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5338 | t->data_ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 5339 | break; |
| 5340 | } |
| 5341 | h++; |
| 5342 | } |
| 5343 | } |
| 5344 | |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 5345 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 5346 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) { |
| 5347 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5348 | t->data_ctx.stats.flags |= STAT_FMT_CSV; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 5349 | break; |
| 5350 | } |
| 5351 | h++; |
| 5352 | } |
| 5353 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5354 | t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO; |
| 5355 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5356 | /* we are in front of a interceptable URI. Let's check |
| 5357 | * if there's an authentication and if it's valid. |
| 5358 | */ |
| 5359 | user = uri_auth->users; |
| 5360 | if (!user) { |
| 5361 | /* no user auth required, it's OK */ |
| 5362 | authenticated = 1; |
| 5363 | } else { |
| 5364 | authenticated = 0; |
| 5365 | |
| 5366 | /* a user list is defined, we have to check. |
| 5367 | * skip 21 chars for "Authorization: Basic ". |
| 5368 | */ |
| 5369 | |
| 5370 | /* FIXME: this should move to an earlier place */ |
| 5371 | cur_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5372 | h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 5373 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 5374 | int len = txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5375 | if (len > 14 && |
| 5376 | !strncasecmp("Authorization:", h, 14)) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5377 | txn->auth_hdr.str = h; |
| 5378 | txn->auth_hdr.len = len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5379 | break; |
| 5380 | } |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5381 | h += len + txn->hdr_idx.v[cur_idx].cr + 1; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5382 | } |
| 5383 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5384 | if (txn->auth_hdr.len < 21 || |
| 5385 | memcmp(txn->auth_hdr.str + 14, " Basic ", 7)) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5386 | user = NULL; |
| 5387 | |
| 5388 | while (user) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5389 | if ((txn->auth_hdr.len == user->user_len + 14 + 7) |
| 5390 | && !memcmp(txn->auth_hdr.str + 14 + 7, |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5391 | user->user_pwd, user->user_len)) { |
| 5392 | authenticated = 1; |
| 5393 | break; |
| 5394 | } |
| 5395 | user = user->next; |
| 5396 | } |
| 5397 | } |
| 5398 | |
| 5399 | if (!authenticated) { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5400 | struct chunk msg; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5401 | |
| 5402 | /* no need to go further */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5403 | msg.str = trash; |
| 5404 | msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm); |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5405 | txn->status = 401; |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5406 | client_retnclose(t, &msg); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 5407 | trace_term(t, TT_HTTP_URI_1); |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 5408 | t->req->analysers = 0; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5409 | if (!(t->flags & SN_ERR_MASK)) |
| 5410 | t->flags |= SN_ERR_PRXCOND; |
| 5411 | if (!(t->flags & SN_FINST_MASK)) |
| 5412 | t->flags |= SN_FINST_R; |
| 5413 | return 1; |
| 5414 | } |
| 5415 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5416 | /* The request is valid, the user is authenticated. Let's start sending |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5417 | * data. |
| 5418 | */ |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 5419 | buffer_shutw_now(t->req); |
| 5420 | buffer_shutr_now(t->rep); |
| 5421 | buffer_start_hijack(t->rep); |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 5422 | t->logs.tv_request = now; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5423 | t->data_source = DATA_SRC_STATS; |
| 5424 | t->data_state = DATA_ST_INIT; |
Willy Tarreau | 91e9993 | 2008-06-30 07:51:00 +0200 | [diff] [blame] | 5425 | t->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5426 | produce_content(t); |
| 5427 | return 1; |
| 5428 | } |
| 5429 | |
| 5430 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5431 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5432 | * Print a debug line with a header |
| 5433 | */ |
| 5434 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 5435 | { |
| 5436 | int len, max; |
| 5437 | 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] | 5438 | 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] | 5439 | max = end - start; |
| 5440 | UBOUND(max, sizeof(trash) - len - 1); |
| 5441 | len += strlcpy2(trash + len, start, max + 1); |
| 5442 | trash[len++] = '\n'; |
| 5443 | write(1, trash, len); |
| 5444 | } |
| 5445 | |
| 5446 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5447 | /************************************************************************/ |
| 5448 | /* The code below is dedicated to ACL parsing and matching */ |
| 5449 | /************************************************************************/ |
| 5450 | |
| 5451 | |
| 5452 | |
| 5453 | |
| 5454 | /* 1. Check on METHOD |
| 5455 | * We use the pre-parsed method if it is known, and store its number as an |
| 5456 | * integer. If it is unknown, we use the pointer and the length. |
| 5457 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5458 | 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] | 5459 | { |
| 5460 | int len, meth; |
| 5461 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5462 | len = strlen(*text); |
| 5463 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5464 | |
| 5465 | pattern->val.i = meth; |
| 5466 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5467 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5468 | if (!pattern->ptr.str) |
| 5469 | return 0; |
| 5470 | pattern->len = len; |
| 5471 | } |
| 5472 | return 1; |
| 5473 | } |
| 5474 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5475 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5476 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5477 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5478 | { |
| 5479 | int meth; |
| 5480 | struct http_txn *txn = l7; |
| 5481 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5482 | if (!txn) |
| 5483 | return 0; |
| 5484 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5485 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5486 | return 0; |
| 5487 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5488 | meth = txn->meth; |
| 5489 | test->i = meth; |
| 5490 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5491 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5492 | /* ensure the indexes are not affected */ |
| 5493 | return 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5494 | test->len = txn->req.sl.rq.m_l; |
| 5495 | test->ptr = txn->req.sol; |
| 5496 | } |
| 5497 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5498 | return 1; |
| 5499 | } |
| 5500 | |
| 5501 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 5502 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 5503 | int icase; |
| 5504 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5505 | if (test->i != pattern->val.i) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5506 | return ACL_PAT_FAIL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5507 | |
| 5508 | if (test->i != HTTP_METH_OTHER) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5509 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5510 | |
| 5511 | /* Other method, we must compare the strings */ |
| 5512 | if (pattern->len != test->len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5513 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 5514 | |
| 5515 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 5516 | if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) || |
| 5517 | (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5518 | return ACL_PAT_FAIL; |
| 5519 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5520 | } |
| 5521 | |
| 5522 | /* 2. Check on Request/Status Version |
| 5523 | * We simply compare strings here. |
| 5524 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5525 | 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] | 5526 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5527 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5528 | if (!pattern->ptr.str) |
| 5529 | return 0; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5530 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5531 | return 1; |
| 5532 | } |
| 5533 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5534 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5535 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5536 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5537 | { |
| 5538 | struct http_txn *txn = l7; |
| 5539 | char *ptr; |
| 5540 | int len; |
| 5541 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5542 | if (!txn) |
| 5543 | return 0; |
| 5544 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5545 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5546 | return 0; |
| 5547 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5548 | len = txn->req.sl.rq.v_l; |
| 5549 | ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som; |
| 5550 | |
| 5551 | while ((len-- > 0) && (*ptr++ != '/')); |
| 5552 | if (len <= 0) |
| 5553 | return 0; |
| 5554 | |
| 5555 | test->ptr = ptr; |
| 5556 | test->len = len; |
| 5557 | |
| 5558 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5559 | return 1; |
| 5560 | } |
| 5561 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5562 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5563 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5564 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5565 | { |
| 5566 | struct http_txn *txn = l7; |
| 5567 | char *ptr; |
| 5568 | int len; |
| 5569 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5570 | if (!txn) |
| 5571 | return 0; |
| 5572 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5573 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5574 | return 0; |
| 5575 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5576 | len = txn->rsp.sl.st.v_l; |
| 5577 | ptr = txn->rsp.sol; |
| 5578 | |
| 5579 | while ((len-- > 0) && (*ptr++ != '/')); |
| 5580 | if (len <= 0) |
| 5581 | return 0; |
| 5582 | |
| 5583 | test->ptr = ptr; |
| 5584 | test->len = len; |
| 5585 | |
| 5586 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5587 | return 1; |
| 5588 | } |
| 5589 | |
| 5590 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5591 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5592 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5593 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5594 | { |
| 5595 | struct http_txn *txn = l7; |
| 5596 | char *ptr; |
| 5597 | int len; |
| 5598 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5599 | if (!txn) |
| 5600 | return 0; |
| 5601 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5602 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5603 | return 0; |
| 5604 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5605 | len = txn->rsp.sl.st.c_l; |
| 5606 | ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som; |
| 5607 | |
| 5608 | test->i = __strl2ui(ptr, len); |
| 5609 | test->flags = ACL_TEST_F_VOL_1ST; |
| 5610 | return 1; |
| 5611 | } |
| 5612 | |
| 5613 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5614 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5615 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5616 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5617 | { |
| 5618 | struct http_txn *txn = l7; |
| 5619 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5620 | if (!txn) |
| 5621 | return 0; |
| 5622 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5623 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5624 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5625 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5626 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5627 | /* ensure the indexes are not affected */ |
| 5628 | return 0; |
| 5629 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5630 | test->len = txn->req.sl.rq.u_l; |
| 5631 | test->ptr = txn->req.sol + txn->req.sl.rq.u; |
| 5632 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 5633 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 5634 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5635 | return 1; |
| 5636 | } |
| 5637 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5638 | static int |
| 5639 | acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5640 | struct acl_expr *expr, struct acl_test *test) |
| 5641 | { |
| 5642 | struct http_txn *txn = l7; |
| 5643 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5644 | if (!txn) |
| 5645 | return 0; |
| 5646 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5647 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5648 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5649 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5650 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5651 | /* ensure the indexes are not affected */ |
| 5652 | return 0; |
| 5653 | |
| 5654 | /* Parse HTTP request */ |
| 5655 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
| 5656 | test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr; |
| 5657 | test->i = AF_INET; |
| 5658 | |
| 5659 | /* |
| 5660 | * If we are parsing url in frontend space, we prepare backend stage |
| 5661 | * to not parse again the same url ! optimization lazyness... |
| 5662 | */ |
| 5663 | if (px->options & PR_O_HTTP_PROXY) |
| 5664 | l4->flags |= SN_ADDR_SET; |
| 5665 | |
| 5666 | test->flags = ACL_TEST_F_READ_ONLY; |
| 5667 | return 1; |
| 5668 | } |
| 5669 | |
| 5670 | static int |
| 5671 | acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5672 | struct acl_expr *expr, struct acl_test *test) |
| 5673 | { |
| 5674 | struct http_txn *txn = l7; |
| 5675 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5676 | if (!txn) |
| 5677 | return 0; |
| 5678 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5679 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5680 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5681 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5682 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5683 | /* ensure the indexes are not affected */ |
| 5684 | return 0; |
| 5685 | |
| 5686 | /* Same optimization as url_ip */ |
| 5687 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
| 5688 | test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port); |
| 5689 | |
| 5690 | if (px->options & PR_O_HTTP_PROXY) |
| 5691 | l4->flags |= SN_ADDR_SET; |
| 5692 | |
| 5693 | test->flags = ACL_TEST_F_READ_ONLY; |
| 5694 | return 1; |
| 5695 | } |
| 5696 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5697 | /* 5. Check on HTTP header. A pointer to the beginning of the value is returned. |
| 5698 | * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr(). |
| 5699 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5700 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5701 | 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] | 5702 | struct acl_expr *expr, struct acl_test *test) |
| 5703 | { |
| 5704 | struct http_txn *txn = l7; |
| 5705 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5706 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5707 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5708 | if (!txn) |
| 5709 | return 0; |
| 5710 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5711 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 5712 | /* search for header from the beginning */ |
| 5713 | ctx->idx = 0; |
| 5714 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5715 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 5716 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 5717 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5718 | test->len = ctx->vlen; |
| 5719 | test->ptr = (char *)ctx->line + ctx->val; |
| 5720 | return 1; |
| 5721 | } |
| 5722 | |
| 5723 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 5724 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5725 | return 0; |
| 5726 | } |
| 5727 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5728 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5729 | acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5730 | struct acl_expr *expr, struct acl_test *test) |
| 5731 | { |
| 5732 | struct http_txn *txn = l7; |
| 5733 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5734 | if (!txn) |
| 5735 | return 0; |
| 5736 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5737 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5738 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5739 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5740 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5741 | /* ensure the indexes are not affected */ |
| 5742 | return 0; |
| 5743 | |
| 5744 | return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test); |
| 5745 | } |
| 5746 | |
| 5747 | static int |
| 5748 | acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5749 | struct acl_expr *expr, struct acl_test *test) |
| 5750 | { |
| 5751 | struct http_txn *txn = l7; |
| 5752 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5753 | if (!txn) |
| 5754 | return 0; |
| 5755 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5756 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5757 | return 0; |
| 5758 | |
| 5759 | return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test); |
| 5760 | } |
| 5761 | |
| 5762 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 5763 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 5764 | */ |
| 5765 | static int |
| 5766 | 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] | 5767 | struct acl_expr *expr, struct acl_test *test) |
| 5768 | { |
| 5769 | struct http_txn *txn = l7; |
| 5770 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5771 | struct hdr_ctx ctx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5772 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5773 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5774 | if (!txn) |
| 5775 | return 0; |
| 5776 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5777 | ctx.idx = 0; |
| 5778 | cnt = 0; |
| 5779 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx)) |
| 5780 | cnt++; |
| 5781 | |
| 5782 | test->i = cnt; |
| 5783 | test->flags = ACL_TEST_F_VOL_HDR; |
| 5784 | return 1; |
| 5785 | } |
| 5786 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5787 | static int |
| 5788 | acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5789 | struct acl_expr *expr, struct acl_test *test) |
| 5790 | { |
| 5791 | struct http_txn *txn = l7; |
| 5792 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5793 | if (!txn) |
| 5794 | return 0; |
| 5795 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5796 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5797 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5798 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5799 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5800 | /* ensure the indexes are not affected */ |
| 5801 | return 0; |
| 5802 | |
| 5803 | return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test); |
| 5804 | } |
| 5805 | |
| 5806 | static int |
| 5807 | acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5808 | struct acl_expr *expr, struct acl_test *test) |
| 5809 | { |
| 5810 | struct http_txn *txn = l7; |
| 5811 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5812 | if (!txn) |
| 5813 | return 0; |
| 5814 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5815 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5816 | return 0; |
| 5817 | |
| 5818 | return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test); |
| 5819 | } |
| 5820 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5821 | /* 7. Check on HTTP header's integer value. The integer value is returned. |
| 5822 | * FIXME: the type is 'int', it may not be appropriate for everything. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5823 | * 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] | 5824 | */ |
| 5825 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5826 | 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] | 5827 | struct acl_expr *expr, struct acl_test *test) |
| 5828 | { |
| 5829 | struct http_txn *txn = l7; |
| 5830 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5831 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5832 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5833 | if (!txn) |
| 5834 | return 0; |
| 5835 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5836 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 5837 | /* search for header from the beginning */ |
| 5838 | ctx->idx = 0; |
| 5839 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5840 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 5841 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 5842 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5843 | test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen); |
| 5844 | return 1; |
| 5845 | } |
| 5846 | |
| 5847 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 5848 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5849 | return 0; |
| 5850 | } |
| 5851 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5852 | static int |
| 5853 | acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5854 | struct acl_expr *expr, struct acl_test *test) |
| 5855 | { |
| 5856 | struct http_txn *txn = l7; |
| 5857 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5858 | if (!txn) |
| 5859 | return 0; |
| 5860 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5861 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5862 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5863 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5864 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5865 | /* ensure the indexes are not affected */ |
| 5866 | return 0; |
| 5867 | |
| 5868 | return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test); |
| 5869 | } |
| 5870 | |
| 5871 | static int |
| 5872 | acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5873 | struct acl_expr *expr, struct acl_test *test) |
| 5874 | { |
| 5875 | struct http_txn *txn = l7; |
| 5876 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5877 | if (!txn) |
| 5878 | return 0; |
| 5879 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5880 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5881 | return 0; |
| 5882 | |
| 5883 | return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test); |
| 5884 | } |
| 5885 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5886 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 5887 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 5888 | */ |
| 5889 | static int |
| 5890 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5891 | struct acl_expr *expr, struct acl_test *test) |
| 5892 | { |
| 5893 | struct http_txn *txn = l7; |
| 5894 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5895 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5896 | if (!txn) |
| 5897 | return 0; |
| 5898 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5899 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5900 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5901 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5902 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5903 | /* ensure the indexes are not affected */ |
| 5904 | return 0; |
| 5905 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5906 | end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 5907 | ptr = http_get_path(txn); |
| 5908 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5909 | return 0; |
| 5910 | |
| 5911 | /* OK, we got the '/' ! */ |
| 5912 | test->ptr = ptr; |
| 5913 | |
| 5914 | while (ptr < end && *ptr != '?') |
| 5915 | ptr++; |
| 5916 | |
| 5917 | test->len = ptr - test->ptr; |
| 5918 | |
| 5919 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 5920 | test->flags = ACL_TEST_F_VOL_1ST; |
| 5921 | return 1; |
| 5922 | } |
| 5923 | |
| 5924 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5925 | |
| 5926 | /************************************************************************/ |
| 5927 | /* All supported keywords must be declared here. */ |
| 5928 | /************************************************************************/ |
| 5929 | |
| 5930 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 5931 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5932 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT }, |
| 5933 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5934 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 5935 | { "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] | 5936 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5937 | { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5938 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 5939 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 5940 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 5941 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 5942 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 5943 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 5944 | { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE }, |
| 5945 | { "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] | 5946 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5947 | /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */ |
| 5948 | { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5949 | { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 5950 | { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 5951 | { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 5952 | { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 5953 | { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 5954 | { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 5955 | { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
| 5956 | { "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] | 5957 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5958 | { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 5959 | { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE }, |
| 5960 | { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE }, |
| 5961 | { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE }, |
| 5962 | { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE }, |
| 5963 | { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE }, |
| 5964 | { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE }, |
| 5965 | { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
| 5966 | { "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] | 5967 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5968 | { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5969 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 5970 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 5971 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 5972 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 5973 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 5974 | { "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] | 5975 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 5976 | { NULL, NULL, NULL, NULL }, |
| 5977 | |
| 5978 | #if 0 |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5979 | { "line", acl_parse_str, acl_fetch_line, acl_match_str }, |
| 5980 | { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg }, |
| 5981 | { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg }, |
| 5982 | { "line_end", acl_parse_str, acl_fetch_line, acl_match_end }, |
| 5983 | { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub }, |
| 5984 | { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir }, |
| 5985 | { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom }, |
| 5986 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5987 | { "cook", acl_parse_str, acl_fetch_cook, acl_match_str }, |
| 5988 | { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg }, |
| 5989 | { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg }, |
| 5990 | { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end }, |
| 5991 | { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub }, |
| 5992 | { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir }, |
| 5993 | { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom }, |
| 5994 | { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst }, |
| 5995 | |
| 5996 | { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str }, |
| 5997 | { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg }, |
| 5998 | { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str }, |
| 5999 | { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 }, |
| 6000 | { NULL, NULL, NULL, NULL }, |
| 6001 | #endif |
| 6002 | }}; |
| 6003 | |
| 6004 | |
| 6005 | __attribute__((constructor)) |
| 6006 | static void __http_protocol_init(void) |
| 6007 | { |
| 6008 | acl_register_keywords(&acl_kws); |
| 6009 | } |
| 6010 | |
| 6011 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6012 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6013 | * Local variables: |
| 6014 | * c-indent-level: 8 |
| 6015 | * c-basic-offset: 8 |
| 6016 | * End: |
| 6017 | */ |