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; |
| 661 | unsigned int rqf_req, rpf_rep; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 662 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 663 | /* Check timeouts only during data phase for now */ |
| 664 | if (unlikely(t->state & TASK_WOKEN_TIMER)) { |
| 665 | if (s->rep->cons->state == SI_ST_EST) |
| 666 | stream_sock_data_check_timeouts(s->rep->cons->fd); |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 667 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 668 | if (s->req->cons->state == SI_ST_EST) |
| 669 | stream_sock_data_check_timeouts(s->req->cons->fd); |
| 670 | } |
| 671 | |
Willy Tarreau | 48adac5 | 2008-08-30 04:58:38 +0200 | [diff] [blame] | 672 | /* Check if we need to close the write side. This can only happen |
| 673 | * when either SHUTR or EMPTY appears, because WRITE_ENA cannot appear |
| 674 | * from low level, and neither HIJACK nor SHUTW can disappear from low |
| 675 | * level. |
| 676 | */ |
| 677 | if (unlikely((s->req->flags & (BF_SHUTW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) == (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR))) { |
| 678 | buffer_shutw(s->req); |
| 679 | s->req->cons->shutw(s->req->cons); |
| 680 | } |
| 681 | |
| 682 | if (unlikely((s->rep->flags & (BF_SHUTW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) == (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR))) { |
| 683 | buffer_shutw(s->rep); |
| 684 | s->rep->cons->shutw(s->rep->cons); |
| 685 | } |
| 686 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 687 | /* When a server-side connection is released, we have to |
| 688 | * count it and check for pending connections on this server. |
| 689 | */ |
| 690 | if (unlikely(s->req->cons->state == SI_ST_CLO && |
| 691 | s->req->cons->prev_state == SI_ST_EST)) { |
| 692 | /* Count server-side errors (but not timeouts). */ |
| 693 | if (s->req->flags & BF_WRITE_ERROR) { |
| 694 | s->be->failed_resp++; |
| 695 | if (s->srv) |
| 696 | s->srv->failed_resp++; |
| 697 | } |
| 698 | |
| 699 | if (s->srv) { |
| 700 | s->srv->cur_sess--; |
| 701 | sess_change_server(s, NULL); |
| 702 | if (may_dequeue_tasks(s->srv, s->be)) |
| 703 | process_srv_queue(s->srv); |
| 704 | } |
| 705 | |
| 706 | if (unlikely((s->req->cons->state == SI_ST_CLO) && |
| 707 | (global.mode & MODE_DEBUG) && |
| 708 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
| 709 | int len; |
| 710 | len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n", |
| 711 | s->uniq_id, s->be->id, (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd); |
| 712 | write(1, trash, len); |
| 713 | } |
| 714 | } |
| 715 | |
Willy Tarreau | 48adac5 | 2008-08-30 04:58:38 +0200 | [diff] [blame] | 716 | /* This is needed when debugging is enabled, to indicate client-side close */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 717 | if (unlikely(s->rep->cons->state == SI_ST_CLO && |
| 718 | s->rep->cons->prev_state == SI_ST_EST)) { |
| 719 | if (unlikely((s->rep->cons->state == SI_ST_CLO) && |
| 720 | (global.mode & MODE_DEBUG) && |
| 721 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
| 722 | int len; |
| 723 | len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", |
| 724 | s->uniq_id, s->be->id, (unsigned short)s->rep->prod->fd, (unsigned short)s->req->cons->fd); |
| 725 | write(1, trash, len); |
| 726 | } |
| 727 | } |
| 728 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 729 | /* Dirty trick: force one first pass everywhere */ |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 730 | rqf_cli = rqf_srv = rqf_req = ~s->req->flags; |
| 731 | rpf_cli = rpf_srv = rpf_rep = ~s->rep->flags; |
Willy Tarreau | 507385d | 2008-08-17 13:04:25 +0200 | [diff] [blame] | 732 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 733 | /* well, the ST_CONN state is already handled properly */ |
| 734 | if (s->req->prod->state == SI_ST_EST) { |
| 735 | rqf_cli = s->req->flags; |
| 736 | rpf_cli = s->rep->flags; |
| 737 | } |
| 738 | |
| 739 | if (s->req->cons->state == SI_ST_EST) { |
| 740 | rqf_srv = s->req->flags; |
| 741 | rpf_srv = s->rep->flags; |
| 742 | } |
| 743 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 744 | do { |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 745 | 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", |
| 746 | now_ms, __FUNCTION__, |
| 747 | t, |
| 748 | s->req, s->rep, |
| 749 | s->req->rex, s->rep->wex, |
| 750 | s->req->flags, s->rep->flags, |
| 751 | s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state); |
| 752 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 753 | resync = 0; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 754 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 755 | /* Analyse request */ |
| 756 | if ((rqf_req ^ s->req->flags) & BF_MASK_ANALYSER) { |
| 757 | if (s->req->prod->state >= SI_ST_EST) { |
| 758 | resync = 1; |
| 759 | /* it's up to the analysers to reset write_ena */ |
| 760 | buffer_write_ena(s->req); |
| 761 | if (s->req->analysers) |
| 762 | process_request(s); |
| 763 | rqf_req = s->req->flags; |
| 764 | } |
| 765 | |
| 766 | } |
| 767 | |
| 768 | /* Analyse response */ |
| 769 | if (unlikely(s->rep->flags & BF_HIJACK)) { |
| 770 | /* In inject mode, we wake up everytime something has |
| 771 | * happened on the write side of the buffer. |
| 772 | */ |
| 773 | if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) && |
| 774 | !(s->rep->flags & BF_FULL)) { |
| 775 | if (produce_content(s) != 0) |
| 776 | resync = 1; /* completed, better re-check flags */ |
| 777 | } |
| 778 | } |
| 779 | else if (s->rep->prod->state >= SI_ST_EST) { |
| 780 | if ((rpf_rep ^ s->rep->flags) & BF_MASK_ANALYSER) { |
| 781 | resync = 1; |
| 782 | /* it's up to the analysers to reset write_ena */ |
| 783 | buffer_write_ena(s->rep); |
| 784 | if (s->rep->analysers) |
| 785 | process_response(s); |
| 786 | rpf_rep = s->rep->flags; |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | /* Maybe resync client FD state */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 791 | if (s->rep->cons->state != SI_ST_CLO) { |
| 792 | if (((rqf_cli ^ s->req->flags) & BF_MASK_INTERFACE_I) || |
| 793 | ((rpf_cli ^ s->rep->flags) & BF_MASK_INTERFACE_O)) { |
| 794 | resync = 1; |
| 795 | stream_sock_data_update(s->rep->cons->fd); |
| 796 | rqf_cli = s->req->flags; |
| 797 | rpf_cli = s->rep->flags; |
| 798 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 799 | if (unlikely((s->rep->cons->state == SI_ST_CLO) && |
| 800 | (global.mode & MODE_DEBUG) && |
| 801 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
| 802 | int len; |
| 803 | len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 804 | s->uniq_id, s->be->id, (unsigned short)s->rep->prod->fd, (unsigned short)s->req->cons->fd); |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 805 | write(1, trash, len); |
| 806 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 807 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 808 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 809 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 810 | /* Maybe resync server FD state */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 811 | if (s->req->cons->state != SI_ST_CLO) { |
| 812 | if (((rpf_srv ^ s->rep->flags) & BF_MASK_INTERFACE_I) || |
| 813 | ((rqf_srv ^ s->req->flags) & BF_MASK_INTERFACE_O)) { |
| 814 | resync = 1; |
| 815 | |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 816 | 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] | 817 | process_srv_conn(s); |
| 818 | |
| 819 | if (s->req->cons->state == SI_ST_EST) { |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 820 | 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] | 821 | s->be->options & PR_O_FORCE_CLO && |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 822 | s->rep->flags & BF_READ_ACTIVITY) { |
Willy Tarreau | 376580a | 2008-08-27 18:52:22 +0200 | [diff] [blame] | 823 | /* We want to force the connection to the server to close, |
| 824 | * and the server has begun to respond. That's the right |
| 825 | * time. |
| 826 | */ |
| 827 | buffer_shutw_now(s->req); |
| 828 | } |
| 829 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 830 | stream_sock_data_update(s->req->cons->fd); |
Willy Tarreau | 376580a | 2008-08-27 18:52:22 +0200 | [diff] [blame] | 831 | |
| 832 | /* When a server-side connection is released, we have to |
| 833 | * count it and check for pending connections on this server. |
| 834 | */ |
| 835 | if (s->req->cons->state == SI_ST_CLO) { |
| 836 | if (s->srv) { |
| 837 | s->srv->cur_sess--; |
| 838 | sess_change_server(s, NULL); |
| 839 | if (may_dequeue_tasks(s->srv, s->be)) |
| 840 | process_srv_queue(s->srv); |
| 841 | } |
| 842 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 843 | } |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 844 | rqf_srv = s->req->flags; |
| 845 | rpf_srv = s->rep->flags; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 846 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 847 | if (unlikely((s->req->cons->state == SI_ST_CLO) && |
| 848 | (global.mode & MODE_DEBUG) && |
| 849 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
| 850 | int len; |
| 851 | len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n", |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 852 | s->uniq_id, s->be->id, (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 853 | write(1, trash, len); |
| 854 | } |
| 855 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 856 | } |
| 857 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 858 | } while (resync); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 859 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 860 | if (likely((s->rep->cons->state != SI_ST_CLO) || |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 861 | (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] | 862 | |
| 863 | if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED)) |
| 864 | session_process_counters(s); |
| 865 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 866 | if (s->rep->cons->state == SI_ST_EST) |
| 867 | stream_sock_data_finish(s->rep->cons->fd); |
| 868 | |
| 869 | if (s->req->cons->state == SI_ST_EST) |
| 870 | stream_sock_data_finish(s->req->cons->fd); |
| 871 | |
Willy Tarreau | 9a2d154 | 2008-08-30 12:31:07 +0200 | [diff] [blame^] | 872 | s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 873 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 874 | s->si[0].prev_state = s->si[0].state; |
| 875 | s->si[1].prev_state = s->si[1].state; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 876 | |
Willy Tarreau | 7f875f6 | 2008-08-11 17:35:01 +0200 | [diff] [blame] | 877 | /* Trick: if a request is being waiting for the server to respond, |
| 878 | * and if we know the server can timeout, we don't want the timeout |
| 879 | * to expire on the client side first, but we're still interested |
| 880 | * in passing data from the client to the server (eg: POST). Thus, |
| 881 | * we can cancel the client's request timeout if the server's |
| 882 | * request timeout is set and the server has not yet sent a response. |
| 883 | */ |
| 884 | |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 885 | if ((s->rep->flags & (BF_WRITE_ENA|BF_SHUTR)) == 0 && |
Willy Tarreau | 26ed74d | 2008-08-17 12:11:14 +0200 | [diff] [blame] | 886 | (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) |
Willy Tarreau | 7f875f6 | 2008-08-11 17:35:01 +0200 | [diff] [blame] | 887 | s->req->rex = TICK_ETERNITY; |
| 888 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 889 | t->expire = tick_first(tick_first(s->req->rex, s->req->wex), |
| 890 | tick_first(s->rep->rex, s->rep->wex)); |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 891 | if (s->req->analysers) |
| 892 | t->expire = tick_first(t->expire, s->req->analyse_exp); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 893 | |
Willy Tarreau | cb65125 | 2008-08-29 13:57:30 +0200 | [diff] [blame] | 894 | #ifdef DEBUG_FULL |
| 895 | fprintf(stderr, "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u rep->rex=%u rep->wex=%u\n", |
| 896 | now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp, s->rep->rex, s->rep->wex); |
| 897 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 898 | /* restore t to its place in the task list */ |
| 899 | task_queue(t); |
| 900 | |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 901 | #ifdef DEBUG_DEV |
| 902 | /* this may only happen when no timeout is set or in case of an FSM bug */ |
| 903 | if (!t->expire) |
| 904 | ABORT_NOW(); |
| 905 | #endif |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 906 | *next = t->expire; |
| 907 | return; /* nothing more to do */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 908 | } |
| 909 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 910 | s->fe->feconn--; |
| 911 | if (s->flags & SN_BE_ASSIGNED) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 912 | s->be->beconn--; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 913 | actconn--; |
| 914 | |
Willy Tarreau | f41d4b1 | 2007-04-28 23:26:14 +0200 | [diff] [blame] | 915 | if (unlikely((global.mode & MODE_DEBUG) && |
| 916 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 917 | int len; |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 918 | 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] | 919 | s->uniq_id, s->be->id, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 920 | (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd, |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 921 | s->term_trace); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 922 | write(1, trash, len); |
| 923 | } |
| 924 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 925 | 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] | 926 | session_process_counters(s); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 927 | |
| 928 | /* let's do a final log if we need it */ |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 929 | if (s->logs.logwait && |
| 930 | !(s->flags & SN_MONITOR) && |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 931 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 932 | if (s->fe->to_log & LW_REQ) |
| 933 | http_sess_log(s); |
| 934 | else |
| 935 | tcp_sess_log(s); |
| 936 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 937 | |
| 938 | /* the task MUST not be in the run queue anymore */ |
| 939 | task_delete(t); |
| 940 | session_free(s); |
| 941 | task_free(t); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 942 | *next = TICK_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 946 | extern const char sess_term_cond[8]; |
| 947 | extern const char sess_fin_state[8]; |
| 948 | extern const char *monthname[12]; |
| 949 | const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */ |
| 950 | const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown, |
| 951 | Set-cookie seen and left unchanged (passive), Set-cookie Deleted, |
| 952 | unknown, Set-cookie Rewritten */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 953 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 954 | struct pool_head *pool2_capture; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 955 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 956 | /* |
| 957 | * send a log for the session when we have enough info about it. |
| 958 | * Will not log if the frontend has no log defined. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 959 | */ |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 960 | static void http_sess_log(struct session *s) |
| 961 | { |
| 962 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 963 | struct proxy *fe = s->fe; |
| 964 | struct proxy *be = s->be; |
| 965 | struct proxy *prx_log; |
| 966 | struct http_txn *txn = &s->txn; |
| 967 | int tolog; |
| 968 | char *uri, *h; |
| 969 | char *svid; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 970 | struct tm tm; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 971 | static char tmpline[MAX_SYSLOG_LEN]; |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 972 | int t_request; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 973 | int hdr; |
| 974 | |
| 975 | if (fe->logfac1 < 0 && fe->logfac2 < 0) |
| 976 | return; |
| 977 | prx_log = fe; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 978 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 979 | if (s->cli_addr.ss_family == AF_INET) |
| 980 | inet_ntop(AF_INET, |
| 981 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 982 | pn, sizeof(pn)); |
| 983 | else |
| 984 | inet_ntop(AF_INET6, |
| 985 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 986 | pn, sizeof(pn)); |
| 987 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 988 | get_localtime(s->logs.accept_date.tv_sec, &tm); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 989 | |
| 990 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 991 | tolog = fe->to_log; |
| 992 | |
| 993 | h = tmpline; |
| 994 | if (fe->to_log & LW_REQHDR && |
| 995 | txn->req.cap && |
| 996 | (h < tmpline + sizeof(tmpline) - 10)) { |
| 997 | *(h++) = ' '; |
| 998 | *(h++) = '{'; |
| 999 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 1000 | if (hdr) |
| 1001 | *(h++) = '|'; |
| 1002 | if (txn->req.cap[hdr] != NULL) |
| 1003 | h = encode_string(h, tmpline + sizeof(tmpline) - 7, |
| 1004 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 1005 | } |
| 1006 | *(h++) = '}'; |
| 1007 | } |
| 1008 | |
| 1009 | if (fe->to_log & LW_RSPHDR && |
| 1010 | txn->rsp.cap && |
| 1011 | (h < tmpline + sizeof(tmpline) - 7)) { |
| 1012 | *(h++) = ' '; |
| 1013 | *(h++) = '{'; |
| 1014 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 1015 | if (hdr) |
| 1016 | *(h++) = '|'; |
| 1017 | if (txn->rsp.cap[hdr] != NULL) |
| 1018 | h = encode_string(h, tmpline + sizeof(tmpline) - 4, |
| 1019 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 1020 | } |
| 1021 | *(h++) = '}'; |
| 1022 | } |
| 1023 | |
| 1024 | if (h < tmpline + sizeof(tmpline) - 4) { |
| 1025 | *(h++) = ' '; |
| 1026 | *(h++) = '"'; |
| 1027 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 1028 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 1029 | '#', url_encode_map, uri); |
| 1030 | *(h++) = '"'; |
| 1031 | } |
| 1032 | *h = '\0'; |
| 1033 | |
| 1034 | svid = (tolog & LW_SVID) ? |
| 1035 | (s->data_source != DATA_SRC_STATS) ? |
| 1036 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 1037 | |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1038 | t_request = -1; |
| 1039 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 1040 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 1041 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1042 | send_log(prx_log, LOG_INFO, |
| 1043 | "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]" |
| 1044 | " %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] | 1045 | " %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] | 1046 | pn, |
| 1047 | (s->cli_addr.ss_family == AF_INET) ? |
| 1048 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 1049 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1050 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 1051 | 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] | 1052 | fe->id, be->id, svid, |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1053 | t_request, |
| 1054 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1055 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 1056 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 1057 | (tolog & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 1058 | txn->status, |
Willy Tarreau | 8b3977f | 2008-01-18 11:16:32 +0100 | [diff] [blame] | 1059 | (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1060 | txn->cli_cookie ? txn->cli_cookie : "-", |
| 1061 | txn->srv_cookie ? txn->srv_cookie : "-", |
| 1062 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 1063 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 1064 | (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 1065 | (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-', |
| 1066 | 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] | 1067 | (s->flags & SN_REDISP)?"+":"", |
| 1068 | (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] | 1069 | s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline); |
| 1070 | |
| 1071 | s->logs.logwait = 0; |
| 1072 | } |
| 1073 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1074 | |
| 1075 | /* |
| 1076 | * Capture headers from message starting at <som> according to header list |
| 1077 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 1078 | */ |
| 1079 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1080 | char **cap, struct cap_hdr *cap_hdr) |
| 1081 | { |
| 1082 | char *eol, *sol, *col, *sov; |
| 1083 | int cur_idx; |
| 1084 | struct cap_hdr *h; |
| 1085 | int len; |
| 1086 | |
| 1087 | sol = som + hdr_idx_first_pos(idx); |
| 1088 | cur_idx = hdr_idx_first_idx(idx); |
| 1089 | |
| 1090 | while (cur_idx) { |
| 1091 | eol = sol + idx->v[cur_idx].len; |
| 1092 | |
| 1093 | col = sol; |
| 1094 | while (col < eol && *col != ':') |
| 1095 | col++; |
| 1096 | |
| 1097 | sov = col + 1; |
| 1098 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 1099 | sov++; |
| 1100 | |
| 1101 | for (h = cap_hdr; h; h = h->next) { |
| 1102 | if ((h->namelen == col - sol) && |
| 1103 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1104 | if (cap[h->index] == NULL) |
| 1105 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1106 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1107 | |
| 1108 | if (cap[h->index] == NULL) { |
| 1109 | Alert("HTTP capture : out of memory.\n"); |
| 1110 | continue; |
| 1111 | } |
| 1112 | |
| 1113 | len = eol - sov; |
| 1114 | if (len > h->len) |
| 1115 | len = h->len; |
| 1116 | |
| 1117 | memcpy(cap[h->index], sov, len); |
| 1118 | cap[h->index][len]=0; |
| 1119 | } |
| 1120 | } |
| 1121 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1122 | cur_idx = idx->v[cur_idx].next; |
| 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1127 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1128 | */ |
| 1129 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1130 | |
| 1131 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1132 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1133 | */ |
| 1134 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1135 | ptr++; \ |
| 1136 | if (likely(ptr < end)) \ |
| 1137 | goto good; \ |
| 1138 | else { \ |
| 1139 | state = (st); \ |
| 1140 | goto http_msg_ood; \ |
| 1141 | } \ |
| 1142 | } while (0) |
| 1143 | |
| 1144 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1145 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1146 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1147 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1148 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1149 | * will give undefined results. |
| 1150 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1151 | * and that msg->sol points to the beginning of the response. |
| 1152 | * If a complete line is found (which implies that at least one CR or LF is |
| 1153 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1154 | * returned indicating an incomplete line (which does not mean that parts have |
| 1155 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1156 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1157 | * upon next call. |
| 1158 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1159 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1160 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1161 | * 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] | 1162 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1163 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1164 | const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, |
| 1165 | unsigned int state, const char *ptr, const char *end, |
| 1166 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1167 | { |
| 1168 | __label__ |
| 1169 | http_msg_rpver, |
| 1170 | http_msg_rpver_sp, |
| 1171 | http_msg_rpcode, |
| 1172 | http_msg_rpcode_sp, |
| 1173 | http_msg_rpreason, |
| 1174 | http_msg_rpline_eol, |
| 1175 | http_msg_ood, /* out of data */ |
| 1176 | http_msg_invalid; |
| 1177 | |
| 1178 | switch (state) { |
| 1179 | http_msg_rpver: |
| 1180 | case HTTP_MSG_RPVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1181 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1182 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1183 | |
| 1184 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1185 | msg->sl.st.v_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1186 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1187 | } |
| 1188 | goto http_msg_invalid; |
| 1189 | |
| 1190 | http_msg_rpver_sp: |
| 1191 | case HTTP_MSG_RPVER_SP: |
| 1192 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1193 | msg->sl.st.c = ptr - msg_buf; |
| 1194 | goto http_msg_rpcode; |
| 1195 | } |
| 1196 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1197 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1198 | /* so it's a CR/LF, this is invalid */ |
| 1199 | goto http_msg_invalid; |
| 1200 | |
| 1201 | http_msg_rpcode: |
| 1202 | case HTTP_MSG_RPCODE: |
| 1203 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1204 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1205 | |
| 1206 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1207 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 1208 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1209 | } |
| 1210 | |
| 1211 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1212 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 1213 | http_msg_rsp_reason: |
| 1214 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
| 1215 | msg->sl.st.r = ptr - msg_buf; |
| 1216 | msg->sl.st.r_l = 0; |
| 1217 | goto http_msg_rpline_eol; |
| 1218 | |
| 1219 | http_msg_rpcode_sp: |
| 1220 | case HTTP_MSG_RPCODE_SP: |
| 1221 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1222 | msg->sl.st.r = ptr - msg_buf; |
| 1223 | goto http_msg_rpreason; |
| 1224 | } |
| 1225 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1226 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1227 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1228 | goto http_msg_rsp_reason; |
| 1229 | |
| 1230 | http_msg_rpreason: |
| 1231 | case HTTP_MSG_RPREASON: |
| 1232 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1233 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
| 1234 | msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r; |
| 1235 | http_msg_rpline_eol: |
| 1236 | /* We have seen the end of line. Note that we do not |
| 1237 | * necessarily have the \n yet, but at least we know that we |
| 1238 | * have EITHER \r OR \n, otherwise the response would not be |
| 1239 | * complete. We can then record the response length and return |
| 1240 | * to the caller which will be able to register it. |
| 1241 | */ |
| 1242 | msg->sl.st.l = ptr - msg->sol; |
| 1243 | return ptr; |
| 1244 | |
| 1245 | #ifdef DEBUG_FULL |
| 1246 | default: |
| 1247 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1248 | exit(1); |
| 1249 | #endif |
| 1250 | } |
| 1251 | |
| 1252 | http_msg_ood: |
| 1253 | /* out of data */ |
| 1254 | if (ret_state) |
| 1255 | *ret_state = state; |
| 1256 | if (ret_ptr) |
| 1257 | *ret_ptr = (char *)ptr; |
| 1258 | return NULL; |
| 1259 | |
| 1260 | http_msg_invalid: |
| 1261 | /* invalid message */ |
| 1262 | if (ret_state) |
| 1263 | *ret_state = HTTP_MSG_ERROR; |
| 1264 | return NULL; |
| 1265 | } |
| 1266 | |
| 1267 | |
| 1268 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1269 | * This function parses a request line between <ptr> and <end>, starting with |
| 1270 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1271 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1272 | * will give undefined results. |
| 1273 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1274 | * and that msg->sol points to the beginning of the request. |
| 1275 | * If a complete line is found (which implies that at least one CR or LF is |
| 1276 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1277 | * returned indicating an incomplete line (which does not mean that parts have |
| 1278 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1279 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1280 | * upon next call. |
| 1281 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1282 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1283 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1284 | * 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] | 1285 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1286 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1287 | const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, |
| 1288 | unsigned int state, const char *ptr, const char *end, |
| 1289 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1290 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1291 | __label__ |
| 1292 | http_msg_rqmeth, |
| 1293 | http_msg_rqmeth_sp, |
| 1294 | http_msg_rquri, |
| 1295 | http_msg_rquri_sp, |
| 1296 | http_msg_rqver, |
| 1297 | http_msg_rqline_eol, |
| 1298 | http_msg_ood, /* out of data */ |
| 1299 | http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1300 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1301 | switch (state) { |
| 1302 | http_msg_rqmeth: |
| 1303 | case HTTP_MSG_RQMETH: |
| 1304 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1305 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1306 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1307 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1308 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1309 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1310 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1311 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1312 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1313 | /* HTTP 0.9 request */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1314 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1315 | http_msg_req09_uri: |
| 1316 | msg->sl.rq.u = ptr - msg_buf; |
| 1317 | http_msg_req09_uri_e: |
| 1318 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1319 | http_msg_req09_ver: |
| 1320 | msg->sl.rq.v = ptr - msg_buf; |
| 1321 | msg->sl.rq.v_l = 0; |
| 1322 | goto http_msg_rqline_eol; |
| 1323 | } |
| 1324 | goto http_msg_invalid; |
| 1325 | |
| 1326 | http_msg_rqmeth_sp: |
| 1327 | case HTTP_MSG_RQMETH_SP: |
| 1328 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1329 | msg->sl.rq.u = ptr - msg_buf; |
| 1330 | goto http_msg_rquri; |
| 1331 | } |
| 1332 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1333 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1334 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1335 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1336 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1337 | http_msg_rquri: |
| 1338 | case HTTP_MSG_RQURI: |
| 1339 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1340 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1341 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1342 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1343 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1344 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1345 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1346 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1347 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1348 | goto http_msg_req09_uri_e; |
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 | http_msg_rquri_sp: |
| 1351 | case HTTP_MSG_RQURI_SP: |
| 1352 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1353 | msg->sl.rq.v = ptr - msg_buf; |
| 1354 | goto http_msg_rqver; |
| 1355 | } |
| 1356 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1357 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1358 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1359 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1360 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1361 | http_msg_rqver: |
| 1362 | case HTTP_MSG_RQVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1363 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1364 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1365 | |
| 1366 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1367 | msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v; |
| 1368 | http_msg_rqline_eol: |
| 1369 | /* We have seen the end of line. Note that we do not |
| 1370 | * necessarily have the \n yet, but at least we know that we |
| 1371 | * have EITHER \r OR \n, otherwise the request would not be |
| 1372 | * complete. We can then record the request length and return |
| 1373 | * to the caller which will be able to register it. |
| 1374 | */ |
| 1375 | msg->sl.rq.l = ptr - msg->sol; |
| 1376 | return ptr; |
| 1377 | } |
| 1378 | |
| 1379 | /* neither an HTTP_VER token nor a CRLF */ |
| 1380 | goto http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1381 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1382 | #ifdef DEBUG_FULL |
| 1383 | default: |
| 1384 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1385 | exit(1); |
| 1386 | #endif |
| 1387 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1388 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1389 | http_msg_ood: |
| 1390 | /* out of data */ |
| 1391 | if (ret_state) |
| 1392 | *ret_state = state; |
| 1393 | if (ret_ptr) |
| 1394 | *ret_ptr = (char *)ptr; |
| 1395 | return NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1396 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1397 | http_msg_invalid: |
| 1398 | /* invalid message */ |
| 1399 | if (ret_state) |
| 1400 | *ret_state = HTTP_MSG_ERROR; |
| 1401 | return NULL; |
| 1402 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1403 | |
| 1404 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1405 | /* |
| 1406 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1407 | * depending on the initial msg->msg_state. It can be preempted everywhere |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1408 | * when data are missing and recalled at the exact same location with no |
| 1409 | * information loss. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1410 | * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other |
| 1411 | * fields. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1412 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1413 | void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx) |
| 1414 | { |
| 1415 | __label__ |
| 1416 | http_msg_rqbefore, |
| 1417 | http_msg_rqbefore_cr, |
| 1418 | http_msg_rqmeth, |
| 1419 | http_msg_rqline_end, |
| 1420 | http_msg_hdr_first, |
| 1421 | http_msg_hdr_name, |
| 1422 | http_msg_hdr_l1_sp, |
| 1423 | http_msg_hdr_l1_lf, |
| 1424 | http_msg_hdr_l1_lws, |
| 1425 | http_msg_hdr_val, |
| 1426 | http_msg_hdr_l2_lf, |
| 1427 | http_msg_hdr_l2_lws, |
| 1428 | http_msg_complete_header, |
| 1429 | http_msg_last_lf, |
| 1430 | http_msg_ood, /* out of data */ |
| 1431 | http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1432 | |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1433 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1434 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1435 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1436 | state = msg->msg_state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1437 | ptr = buf->lr; |
| 1438 | end = buf->r; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1439 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1440 | if (unlikely(ptr >= end)) |
| 1441 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1442 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1443 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1444 | /* |
| 1445 | * First, states that are specific to the response only. |
| 1446 | * We check them first so that request and headers are |
| 1447 | * closer to each other (accessed more often). |
| 1448 | */ |
| 1449 | http_msg_rpbefore: |
| 1450 | case HTTP_MSG_RPBEFORE: |
| 1451 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1452 | if (likely(ptr == buf->data)) { |
| 1453 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1454 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1455 | } else { |
| 1456 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1457 | /* only skip empty leading lines, don't remove them */ |
| 1458 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1459 | msg->som = ptr - buf->data; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1460 | #else |
| 1461 | /* Remove empty leading lines, as recommended by |
| 1462 | * RFC2616. This takes a lot of time because we |
| 1463 | * must move all the buffer backwards, but this |
| 1464 | * is rarely needed. The method above will be |
| 1465 | * cleaner when we'll be able to start sending |
| 1466 | * the request from any place in the buffer. |
| 1467 | */ |
| 1468 | buf->lr = ptr; |
| 1469 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1470 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1471 | msg->sol = buf->data; |
| 1472 | ptr = buf->data; |
| 1473 | end = buf->r; |
| 1474 | #endif |
| 1475 | } |
| 1476 | hdr_idx_init(idx); |
| 1477 | state = HTTP_MSG_RPVER; |
| 1478 | goto http_msg_rpver; |
| 1479 | } |
| 1480 | |
| 1481 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1482 | goto http_msg_invalid; |
| 1483 | |
| 1484 | if (unlikely(*ptr == '\n')) |
| 1485 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1486 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1487 | /* stop here */ |
| 1488 | |
| 1489 | http_msg_rpbefore_cr: |
| 1490 | case HTTP_MSG_RPBEFORE_CR: |
| 1491 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1492 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1493 | /* stop here */ |
| 1494 | |
| 1495 | http_msg_rpver: |
| 1496 | case HTTP_MSG_RPVER: |
| 1497 | case HTTP_MSG_RPVER_SP: |
| 1498 | case HTTP_MSG_RPCODE: |
| 1499 | case HTTP_MSG_RPCODE_SP: |
| 1500 | case HTTP_MSG_RPREASON: |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1501 | ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1502 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1503 | if (unlikely(!ptr)) |
| 1504 | return; |
| 1505 | |
| 1506 | /* we have a full response and we know that we have either a CR |
| 1507 | * or an LF at <ptr>. |
| 1508 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1509 | //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] | 1510 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1511 | |
| 1512 | msg->sol = ptr; |
| 1513 | if (likely(*ptr == '\r')) |
| 1514 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1515 | goto http_msg_rpline_end; |
| 1516 | |
| 1517 | http_msg_rpline_end: |
| 1518 | case HTTP_MSG_RPLINE_END: |
| 1519 | /* msg->sol must point to the first of CR or LF. */ |
| 1520 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1521 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1522 | /* stop here */ |
| 1523 | |
| 1524 | /* |
| 1525 | * Second, states that are specific to the request only |
| 1526 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1527 | http_msg_rqbefore: |
| 1528 | case HTTP_MSG_RQBEFORE: |
| 1529 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1530 | if (likely(ptr == buf->data)) { |
| 1531 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1532 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1533 | } else { |
| 1534 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1535 | /* only skip empty leading lines, don't remove them */ |
| 1536 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1537 | msg->som = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1538 | #else |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1539 | /* Remove empty leading lines, as recommended by |
| 1540 | * RFC2616. This takes a lot of time because we |
| 1541 | * must move all the buffer backwards, but this |
| 1542 | * is rarely needed. The method above will be |
| 1543 | * cleaner when we'll be able to start sending |
| 1544 | * the request from any place in the buffer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1545 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1546 | buf->lr = ptr; |
| 1547 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1548 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1549 | msg->sol = buf->data; |
| 1550 | ptr = buf->data; |
| 1551 | end = buf->r; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1552 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1553 | } |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1554 | /* we will need this when keep-alive will be supported |
| 1555 | hdr_idx_init(idx); |
| 1556 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1557 | state = HTTP_MSG_RQMETH; |
| 1558 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1559 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1560 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1561 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1562 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1563 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1564 | if (unlikely(*ptr == '\n')) |
| 1565 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1566 | 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] | 1567 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1568 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1569 | http_msg_rqbefore_cr: |
| 1570 | case HTTP_MSG_RQBEFORE_CR: |
| 1571 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1572 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1573 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1574 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1575 | http_msg_rqmeth: |
| 1576 | case HTTP_MSG_RQMETH: |
| 1577 | case HTTP_MSG_RQMETH_SP: |
| 1578 | case HTTP_MSG_RQURI: |
| 1579 | case HTTP_MSG_RQURI_SP: |
| 1580 | case HTTP_MSG_RQVER: |
| 1581 | ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1582 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1583 | if (unlikely(!ptr)) |
| 1584 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1585 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1586 | /* we have a full request and we know that we have either a CR |
| 1587 | * or an LF at <ptr>. |
| 1588 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1589 | //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] | 1590 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1591 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1592 | msg->sol = ptr; |
| 1593 | if (likely(*ptr == '\r')) |
| 1594 | 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] | 1595 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1596 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1597 | http_msg_rqline_end: |
| 1598 | case HTTP_MSG_RQLINE_END: |
| 1599 | /* check for HTTP/0.9 request : no version information available. |
| 1600 | * msg->sol must point to the first of CR or LF. |
| 1601 | */ |
| 1602 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1603 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1604 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1605 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1606 | 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] | 1607 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1608 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1609 | /* |
| 1610 | * Common states below |
| 1611 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1612 | http_msg_hdr_first: |
| 1613 | case HTTP_MSG_HDR_FIRST: |
| 1614 | msg->sol = ptr; |
| 1615 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1616 | goto http_msg_hdr_name; |
| 1617 | } |
| 1618 | |
| 1619 | if (likely(*ptr == '\r')) |
| 1620 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1621 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1622 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1623 | http_msg_hdr_name: |
| 1624 | case HTTP_MSG_HDR_NAME: |
| 1625 | /* assumes msg->sol points to the first char */ |
| 1626 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1627 | 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] | 1628 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1629 | if (likely(*ptr == ':')) { |
| 1630 | msg->col = ptr - buf->data; |
| 1631 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| 1632 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1633 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1634 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1635 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1636 | http_msg_hdr_l1_sp: |
| 1637 | case HTTP_MSG_HDR_L1_SP: |
| 1638 | /* assumes msg->sol points to the first char and msg->col to the colon */ |
| 1639 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1640 | 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] | 1641 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1642 | /* header value can be basically anything except CR/LF */ |
| 1643 | msg->sov = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1644 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1645 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1646 | goto http_msg_hdr_val; |
| 1647 | } |
| 1648 | |
| 1649 | if (likely(*ptr == '\r')) |
| 1650 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1651 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1652 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1653 | http_msg_hdr_l1_lf: |
| 1654 | case HTTP_MSG_HDR_L1_LF: |
| 1655 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1656 | 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] | 1657 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1658 | http_msg_hdr_l1_lws: |
| 1659 | case HTTP_MSG_HDR_L1_LWS: |
| 1660 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1661 | /* replace HT,CR,LF with spaces */ |
| 1662 | for (; buf->data+msg->sov < ptr; msg->sov++) |
| 1663 | buf->data[msg->sov] = ' '; |
| 1664 | goto http_msg_hdr_l1_sp; |
| 1665 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1666 | /* we had a header consisting only in spaces ! */ |
| 1667 | msg->eol = buf->data + msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1668 | goto http_msg_complete_header; |
| 1669 | |
| 1670 | http_msg_hdr_val: |
| 1671 | case HTTP_MSG_HDR_VAL: |
| 1672 | /* assumes msg->sol points to the first char, msg->col to the |
| 1673 | * colon, and msg->sov points to the first character of the |
| 1674 | * value. |
| 1675 | */ |
| 1676 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1677 | 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] | 1678 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1679 | msg->eol = ptr; |
| 1680 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1681 | * real header end in case it ends with lots of LWS, but is this |
| 1682 | * really needed ? |
| 1683 | */ |
| 1684 | if (likely(*ptr == '\r')) |
| 1685 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1686 | goto http_msg_hdr_l2_lf; |
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 | http_msg_hdr_l2_lf: |
| 1689 | case HTTP_MSG_HDR_L2_LF: |
| 1690 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1691 | 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] | 1692 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1693 | http_msg_hdr_l2_lws: |
| 1694 | case HTTP_MSG_HDR_L2_LWS: |
| 1695 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1696 | /* LWS: replace HT,CR,LF with spaces */ |
| 1697 | for (; msg->eol < ptr; msg->eol++) |
| 1698 | *msg->eol = ' '; |
| 1699 | goto http_msg_hdr_val; |
| 1700 | } |
| 1701 | http_msg_complete_header: |
| 1702 | /* |
| 1703 | * It was a new header, so the last one is finished. |
| 1704 | * Assumes msg->sol points to the first char, msg->col to the |
| 1705 | * colon, msg->sov points to the first character of the value |
| 1706 | * and msg->eol to the first CR or LF so we know how the line |
| 1707 | * ends. We insert last header into the index. |
| 1708 | */ |
| 1709 | /* |
| 1710 | fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol); |
| 1711 | write(2, msg->sol, msg->eol-msg->sol); |
| 1712 | fprintf(stderr,"\n"); |
| 1713 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1714 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1715 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r', |
| 1716 | idx, idx->tail) < 0)) |
| 1717 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1718 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1719 | msg->sol = ptr; |
| 1720 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1721 | goto http_msg_hdr_name; |
| 1722 | } |
| 1723 | |
| 1724 | if (likely(*ptr == '\r')) |
| 1725 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1726 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1727 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1728 | http_msg_last_lf: |
| 1729 | case HTTP_MSG_LAST_LF: |
| 1730 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1731 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1732 | ptr++; |
| 1733 | buf->lr = ptr; |
| 1734 | msg->eoh = msg->sol - buf->data; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1735 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1736 | return; |
| 1737 | #ifdef DEBUG_FULL |
| 1738 | default: |
| 1739 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1740 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1741 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1742 | } |
| 1743 | http_msg_ood: |
| 1744 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1745 | msg->msg_state = state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1746 | buf->lr = ptr; |
| 1747 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1748 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1749 | http_msg_invalid: |
| 1750 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1751 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1752 | return; |
| 1753 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1754 | |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1755 | /* This function performs all the processing enabled for the current request. |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1756 | * It normally returns zero, but may return 1 if it absolutely needs to be |
| 1757 | * called again after other functions. It relies on buffers flags, and updates |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1758 | * 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] | 1759 | * functions. Its behaviour is rather simple : |
| 1760 | * - all enabled analysers are called in turn from the lower to the higher |
| 1761 | * bit. |
| 1762 | * - 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] | 1763 | * 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] | 1764 | * that unprocessed data will not be forwarded. But that probably depends on |
| 1765 | * the protocol. Generally it is not reset in case of errors. |
| 1766 | * - 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] | 1767 | * analyser without touching BF_WRITE_ENA (it is enabled prior to |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 1768 | * analysis). |
| 1769 | * - if an analyser thinks it has no added value anymore staying here, it must |
| 1770 | * reset its bit from the analysers flags in order not to be called anymore. |
| 1771 | * |
| 1772 | * In the future, analysers should be able to indicate that they want to be |
| 1773 | * called after XXX bytes have been received (or transfered), and the min of |
| 1774 | * all's wishes will be used to ring back (unless a special condition occurs). |
| 1775 | * |
| 1776 | * |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1777 | */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1778 | int process_request(struct session *t) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1779 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1780 | struct buffer *req = t->req; |
| 1781 | struct buffer *rep = t->rep; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1782 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 1783 | 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] | 1784 | now_ms, __FUNCTION__, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 1785 | t, |
| 1786 | req, |
| 1787 | req->rex, req->wex, |
| 1788 | req->flags, |
| 1789 | req->l, |
| 1790 | req->analysers); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1791 | |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 1792 | /* The tcp-inspect analyser is always called alone */ |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1793 | if (req->analysers & AN_REQ_INSPECT) { |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1794 | struct tcp_rule *rule; |
| 1795 | int partial; |
| 1796 | |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 1797 | /* We will abort if we encounter a read error. In theory, we |
| 1798 | * should not abort if we get a close, it might be valid, |
| 1799 | * although very unlikely. FIXME: we'll abort for now, this |
| 1800 | * will be easier to change later. |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1801 | */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1802 | if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1803 | req->analysers = 0; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 1804 | //t->fe->failed_req++; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1805 | if (!(t->flags & SN_ERR_MASK)) |
| 1806 | t->flags |= SN_ERR_CLICL; |
| 1807 | if (!(t->flags & SN_FINST_MASK)) |
| 1808 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1809 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1810 | } |
| 1811 | |
| 1812 | /* Abort if client read timeout has expired */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1813 | else if (req->flags & BF_READ_TIMEOUT) { |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1814 | req->analysers = 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1815 | t->fe->failed_req++; |
| 1816 | if (!(t->flags & SN_ERR_MASK)) |
| 1817 | t->flags |= SN_ERR_CLITO; |
| 1818 | if (!(t->flags & SN_FINST_MASK)) |
| 1819 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1820 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1821 | } |
| 1822 | |
| 1823 | /* We don't know whether we have enough data, so must proceed |
| 1824 | * this way : |
| 1825 | * - iterate through all rules in their declaration order |
| 1826 | * - if one rule returns MISS, it means the inspect delay is |
| 1827 | * not over yet, then return immediately, otherwise consider |
| 1828 | * it as a non-match. |
| 1829 | * - if one rule returns OK, then return OK |
| 1830 | * - if one rule returns KO, then return KO |
| 1831 | */ |
| 1832 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 1833 | 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] | 1834 | partial = 0; |
| 1835 | else |
| 1836 | partial = ACL_PARTIAL; |
| 1837 | |
| 1838 | list_for_each_entry(rule, &t->fe->tcp_req.inspect_rules, list) { |
| 1839 | int ret = ACL_PAT_PASS; |
| 1840 | |
| 1841 | if (rule->cond) { |
| 1842 | ret = acl_exec_cond(rule->cond, t->fe, t, NULL, ACL_DIR_REQ | partial); |
| 1843 | if (ret == ACL_PAT_MISS) { |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 1844 | buffer_write_dis(req); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1845 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 1846 | if (!tick_isset(req->analyse_exp)) |
| 1847 | 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] | 1848 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1849 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1850 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1851 | ret = acl_pass(ret); |
| 1852 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 1853 | ret = !ret; |
| 1854 | } |
| 1855 | |
| 1856 | if (ret) { |
| 1857 | /* we have a matching rule. */ |
| 1858 | if (rule->action == TCP_ACT_REJECT) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1859 | buffer_abort(req); |
| 1860 | buffer_abort(rep); |
| 1861 | //FIXME: this delete this |
| 1862 | //fd_delete(t->cli_fd); |
| 1863 | //t->cli_state = CL_STCLOSE; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1864 | req->analysers = 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1865 | t->fe->failed_req++; |
| 1866 | if (!(t->flags & SN_ERR_MASK)) |
| 1867 | t->flags |= SN_ERR_PRXCOND; |
| 1868 | if (!(t->flags & SN_FINST_MASK)) |
| 1869 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1870 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1871 | } |
| 1872 | /* otherwise accept */ |
| 1873 | break; |
| 1874 | } |
| 1875 | } |
| 1876 | |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1877 | /* if we get there, it means we have no rule which matches, or |
| 1878 | * we have an explicit accept, so we apply the default accept. |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1879 | */ |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1880 | req->analysers &= ~AN_REQ_INSPECT; |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 1881 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1882 | } |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1883 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1884 | if (req->analysers & AN_REQ_HTTP_HDR) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1885 | /* |
| 1886 | * Now parse the partial (or complete) lines. |
| 1887 | * We will check the request syntax, and also join multi-line |
| 1888 | * headers. An index of all the lines will be elaborated while |
| 1889 | * parsing. |
| 1890 | * |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1891 | * For the parsing, we use a 28 states FSM. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1892 | * |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1893 | * Here is the information we currently have : |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1894 | * req->data + req->som = beginning of request |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1895 | * req->data + req->eoh = end of processed headers / start of current one |
| 1896 | * req->data + req->eol = end of current header or line (LF or CRLF) |
| 1897 | * req->lr = first non-visited byte |
| 1898 | * req->r = end of data |
| 1899 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1900 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1901 | int cur_idx; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1902 | struct http_txn *txn = &t->txn; |
| 1903 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1904 | struct proxy *cur_proxy; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1905 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1906 | if (likely(req->lr < req->r)) |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1907 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1908 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1909 | /* 1: we might have to print this header in debug mode */ |
| 1910 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1911 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1912 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1913 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1914 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1915 | sol = req->data + msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1916 | eol = sol + msg->sl.rq.l; |
| 1917 | debug_hdr("clireq", t, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 1918 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1919 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 1920 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1921 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1922 | while (cur_idx) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1923 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1924 | debug_hdr("clihdr", t, sol, eol); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1925 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 1926 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1927 | } |
| 1928 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1929 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1930 | |
| 1931 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1932 | * Now we quickly check if we have found a full valid request. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1933 | * If not so, we check the FD and buffer states before leaving. |
| 1934 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1935 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 1936 | * requests are checked first. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1937 | * |
| 1938 | */ |
| 1939 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1940 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1941 | /* |
| 1942 | * First, let's catch bad requests. |
| 1943 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1944 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1945 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1946 | |
| 1947 | /* 1: Since we are in header mode, if there's no space |
| 1948 | * left for headers, we won't be able to free more |
| 1949 | * later, so the session will never terminate. We |
| 1950 | * must terminate it now. |
| 1951 | */ |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 1952 | if (unlikely(req->flags & BF_FULL)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1953 | /* FIXME: check if URI is set and return Status |
| 1954 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1955 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1956 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1957 | } |
| 1958 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 1959 | /* 2: have we encountered a read error ? */ |
| 1960 | else if (req->flags & BF_READ_ERROR) { |
| 1961 | /* we cannot return any message on error */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1962 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1963 | req->analysers = 0; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 1964 | //t->fe->failed_req++; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1965 | if (!(t->flags & SN_ERR_MASK)) |
| 1966 | t->flags |= SN_ERR_CLICL; |
| 1967 | if (!(t->flags & SN_FINST_MASK)) |
| 1968 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1969 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1970 | } |
| 1971 | |
| 1972 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 1973 | 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] | 1974 | /* read timeout : give up with an error message. */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1975 | txn->status = 408; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 1976 | client_retnclose(t, error_message(t, HTTP_ERR_408)); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1977 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1978 | req->analysers = 0; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 1979 | t->fe->failed_req++; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1980 | if (!(t->flags & SN_ERR_MASK)) |
| 1981 | t->flags |= SN_ERR_CLITO; |
| 1982 | if (!(t->flags & SN_FINST_MASK)) |
| 1983 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1984 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1985 | } |
| 1986 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 1987 | /* 4: have we encountered a close ? */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 1988 | else if (req->flags & BF_SHUTR) { |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 1989 | txn->status = 400; |
| 1990 | client_retnclose(t, error_message(t, HTTP_ERR_400)); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1991 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1992 | req->analysers = 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1993 | t->fe->failed_req++; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 1994 | |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1995 | if (!(t->flags & SN_ERR_MASK)) |
| 1996 | t->flags |= SN_ERR_CLICL; |
| 1997 | if (!(t->flags & SN_FINST_MASK)) |
| 1998 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1999 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2000 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2001 | |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 2002 | buffer_write_dis(req); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2003 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 2004 | if (!tick_isset(req->analyse_exp)) |
| 2005 | req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2006 | |
| 2007 | /* we're not ready yet */ |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2008 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2009 | } |
| 2010 | |
| 2011 | |
| 2012 | /**************************************************************** |
| 2013 | * More interesting part now : we know that we have a complete * |
| 2014 | * request which at least looks like HTTP. We have an indicator * |
| 2015 | * of each header's length, so we can parse them quickly. * |
| 2016 | ****************************************************************/ |
| 2017 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2018 | req->analysers &= ~AN_REQ_HTTP_HDR; |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 2019 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 2020 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2021 | /* ensure we keep this pointer to the beginning of the message */ |
| 2022 | msg->sol = req->data + msg->som; |
| 2023 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2024 | /* |
| 2025 | * 1: identify the method |
| 2026 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2027 | 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] | 2028 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2029 | /* we can make use of server redirect on GET and HEAD */ |
| 2030 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2031 | t->flags |= SN_REDIRECTABLE; |
| 2032 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2033 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2034 | * 2: check if the URI matches the monitor_uri. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2035 | * We have to do this for every request which gets in, because |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2036 | * the monitor-uri is defined by the frontend. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2037 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2038 | if (unlikely((t->fe->monitor_uri_len != 0) && |
| 2039 | (t->fe->monitor_uri_len == msg->sl.rq.u_l) && |
| 2040 | !memcmp(&req->data[msg->sl.rq.u], |
| 2041 | t->fe->monitor_uri, |
| 2042 | t->fe->monitor_uri_len))) { |
| 2043 | /* |
| 2044 | * We have found the monitor URI |
| 2045 | */ |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2046 | struct acl_cond *cond; |
| 2047 | cur_proxy = t->fe; |
| 2048 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2049 | t->flags |= SN_MONITOR; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2050 | |
| 2051 | /* Check if we want to fail this monitor request or not */ |
| 2052 | list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) { |
| 2053 | 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] | 2054 | |
| 2055 | ret = acl_pass(ret); |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2056 | if (cond->pol == ACL_COND_UNLESS) |
| 2057 | ret = !ret; |
| 2058 | |
| 2059 | if (ret) { |
| 2060 | /* we fail this request, let's return 503 service unavail */ |
| 2061 | txn->status = 503; |
| 2062 | client_retnclose(t, error_message(t, HTTP_ERR_503)); |
| 2063 | goto return_prx_cond; |
| 2064 | } |
| 2065 | } |
| 2066 | |
| 2067 | /* nothing to fail, let's reply normaly */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2068 | txn->status = 200; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2069 | client_retnclose(t, &http_200_chunk); |
| 2070 | goto return_prx_cond; |
| 2071 | } |
| 2072 | |
| 2073 | /* |
| 2074 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2075 | * Note: we cannot log anymore if the request has been |
| 2076 | * classified as invalid. |
| 2077 | */ |
| 2078 | if (unlikely(t->logs.logwait & LW_REQ)) { |
| 2079 | /* we have a complete HTTP request that we must log */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 2080 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2081 | int urilen = msg->sl.rq.l; |
| 2082 | |
| 2083 | if (urilen >= REQURI_LEN) |
| 2084 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2085 | memcpy(txn->uri, &req->data[msg->som], urilen); |
| 2086 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2087 | |
| 2088 | if (!(t->logs.logwait &= ~LW_REQ)) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 2089 | http_sess_log(t); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2090 | } else { |
| 2091 | Alert("HTTP logging : out of memory.\n"); |
| 2092 | } |
| 2093 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2094 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2095 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2096 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 2097 | if (unlikely(msg->sl.rq.v_l == 0)) { |
| 2098 | int delta; |
| 2099 | char *cur_end; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2100 | msg->sol = req->data + msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2101 | cur_end = msg->sol + msg->sl.rq.l; |
| 2102 | delta = 0; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2103 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2104 | if (msg->sl.rq.u_l == 0) { |
| 2105 | /* if no URI was set, add "/" */ |
| 2106 | delta = buffer_replace2(req, cur_end, cur_end, " /", 2); |
| 2107 | cur_end += delta; |
| 2108 | msg->eoh += delta; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2109 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2110 | /* add HTTP version */ |
| 2111 | delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11); |
| 2112 | msg->eoh += delta; |
| 2113 | cur_end += delta; |
| 2114 | cur_end = (char *)http_parse_reqline(msg, req->data, |
| 2115 | HTTP_MSG_RQMETH, |
| 2116 | msg->sol, cur_end + 1, |
| 2117 | NULL, NULL); |
| 2118 | if (unlikely(!cur_end)) |
| 2119 | goto return_bad_req; |
| 2120 | |
| 2121 | /* we have a full HTTP/1.0 request now and we know that |
| 2122 | * we have either a CR or an LF at <ptr>. |
| 2123 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2124 | 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] | 2125 | } |
| 2126 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2127 | |
| 2128 | /* 5: we may need to capture headers */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2129 | if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap)) |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 2130 | capture_headers(req->data + msg->som, &txn->hdr_idx, |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2131 | txn->req.cap, t->fe->req_cap); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2132 | |
| 2133 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2134 | * 6: we will have to evaluate the filters. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2135 | * As opposed to version 1.2, now they will be evaluated in the |
| 2136 | * filters order and not in the header order. This means that |
| 2137 | * each filter has to be validated among all headers. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2138 | * |
| 2139 | * We can now check whether we want to switch to another |
| 2140 | * backend, in which case we will re-check the backend's |
| 2141 | * filters and various options. In order to support 3-level |
| 2142 | * switching, here's how we should proceed : |
| 2143 | * |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2144 | * a) run be. |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2145 | * if (switch) then switch ->be to the new backend. |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2146 | * b) run be if (be != fe). |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2147 | * There cannot be any switch from there, so ->be cannot be |
| 2148 | * changed anymore. |
| 2149 | * |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2150 | * => filters always apply to ->be, then ->be may change. |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2151 | * |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2152 | * The response path will be able to apply either ->be, or |
| 2153 | * ->be then ->fe filters in order to match the reverse of |
| 2154 | * the forward sequence. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2155 | */ |
| 2156 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2157 | do { |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2158 | struct acl_cond *cond; |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2159 | struct redirect_rule *rule; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2160 | struct proxy *rule_set = t->be; |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2161 | cur_proxy = t->be; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2162 | |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2163 | /* first check whether we have some ACLs set to redirect this request */ |
| 2164 | list_for_each_entry(rule, &cur_proxy->redirect_rules, list) { |
| 2165 | 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] | 2166 | |
| 2167 | ret = acl_pass(ret); |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2168 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2169 | ret = !ret; |
| 2170 | |
| 2171 | if (ret) { |
| 2172 | struct chunk rdr = { trash, 0 }; |
| 2173 | const char *msg_fmt; |
| 2174 | |
| 2175 | /* build redirect message */ |
| 2176 | switch(rule->code) { |
| 2177 | case 303: |
| 2178 | rdr.len = strlen(HTTP_303); |
| 2179 | msg_fmt = HTTP_303; |
| 2180 | break; |
| 2181 | case 301: |
| 2182 | rdr.len = strlen(HTTP_301); |
| 2183 | msg_fmt = HTTP_301; |
| 2184 | break; |
| 2185 | case 302: |
| 2186 | default: |
| 2187 | rdr.len = strlen(HTTP_302); |
| 2188 | msg_fmt = HTTP_302; |
| 2189 | break; |
| 2190 | } |
| 2191 | |
| 2192 | if (unlikely(rdr.len > sizeof(trash))) |
| 2193 | goto return_bad_req; |
| 2194 | memcpy(rdr.str, msg_fmt, rdr.len); |
| 2195 | |
| 2196 | switch(rule->type) { |
| 2197 | case REDIRECT_TYPE_PREFIX: { |
| 2198 | const char *path; |
| 2199 | int pathlen; |
| 2200 | |
| 2201 | path = http_get_path(txn); |
| 2202 | /* build message using path */ |
| 2203 | if (path) { |
| 2204 | pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path; |
| 2205 | } else { |
| 2206 | path = "/"; |
| 2207 | pathlen = 1; |
| 2208 | } |
| 2209 | |
| 2210 | if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4) |
| 2211 | goto return_bad_req; |
| 2212 | |
| 2213 | /* add prefix */ |
| 2214 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 2215 | rdr.len += rule->rdr_len; |
| 2216 | |
| 2217 | /* add path */ |
| 2218 | memcpy(rdr.str + rdr.len, path, pathlen); |
| 2219 | rdr.len += pathlen; |
| 2220 | break; |
| 2221 | } |
| 2222 | case REDIRECT_TYPE_LOCATION: |
| 2223 | default: |
| 2224 | if (rdr.len + rule->rdr_len > sizeof(trash) - 4) |
| 2225 | goto return_bad_req; |
| 2226 | |
| 2227 | /* add location */ |
| 2228 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 2229 | rdr.len += rule->rdr_len; |
| 2230 | break; |
| 2231 | } |
| 2232 | |
| 2233 | /* add end of headers */ |
| 2234 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 2235 | rdr.len += 4; |
| 2236 | |
| 2237 | txn->status = rule->code; |
| 2238 | /* let's log the request time */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2239 | t->logs.tv_request = now; |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2240 | client_retnclose(t, &rdr); |
| 2241 | goto return_prx_cond; |
| 2242 | } |
| 2243 | } |
| 2244 | |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2245 | /* first check whether we have some ACLs set to block this request */ |
| 2246 | list_for_each_entry(cond, &cur_proxy->block_cond, list) { |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 2247 | 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] | 2248 | |
| 2249 | ret = acl_pass(ret); |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2250 | if (cond->pol == ACL_COND_UNLESS) |
| 2251 | ret = !ret; |
| 2252 | |
| 2253 | if (ret) { |
| 2254 | txn->status = 403; |
| 2255 | /* let's log the request time */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2256 | t->logs.tv_request = now; |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2257 | client_retnclose(t, error_message(t, HTTP_ERR_403)); |
| 2258 | goto return_prx_cond; |
| 2259 | } |
| 2260 | } |
| 2261 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2262 | /* try headers filters */ |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2263 | if (rule_set->req_exp != NULL) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2264 | if (apply_filters_to_request(t, req, rule_set->req_exp) < 0) |
| 2265 | goto return_bad_req; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2266 | } |
| 2267 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2268 | if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) { |
| 2269 | /* to ensure correct connection accounting on |
| 2270 | * the backend, we count the connection for the |
| 2271 | * one managing the queue. |
| 2272 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2273 | t->be->beconn++; |
| 2274 | if (t->be->beconn > t->be->beconn_max) |
| 2275 | t->be->beconn_max = t->be->beconn; |
| 2276 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2277 | t->flags |= SN_BE_ASSIGNED; |
| 2278 | } |
| 2279 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2280 | /* has the request been denied ? */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2281 | if (txn->flags & TX_CLDENY) { |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2282 | /* no need to go further */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2283 | txn->status = 403; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2284 | /* let's log the request time */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2285 | t->logs.tv_request = now; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2286 | client_retnclose(t, error_message(t, HTTP_ERR_403)); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2287 | goto return_prx_cond; |
| 2288 | } |
| 2289 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2290 | /* We might have to check for "Connection:" */ |
Krzysztof Oledzki | 336d475 | 2007-12-25 02:40:22 +0100 | [diff] [blame] | 2291 | 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] | 2292 | !(t->flags & SN_CONN_CLOSED)) { |
| 2293 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2294 | int cur_idx, old_idx, delta, val; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2295 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2296 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2297 | 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] | 2298 | old_idx = 0; |
| 2299 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2300 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 2301 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2302 | cur_ptr = cur_next; |
| 2303 | cur_end = cur_ptr + cur_hdr->len; |
| 2304 | cur_next = cur_end + cur_hdr->cr + 1; |
| 2305 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2306 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 2307 | if (val) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2308 | /* 3 possibilities : |
| 2309 | * - we have already set Connection: close, |
| 2310 | * so we remove this line. |
| 2311 | * - we have not yet set Connection: close, |
| 2312 | * but this line indicates close. We leave |
| 2313 | * it untouched and set the flag. |
| 2314 | * - we have not yet set Connection: close, |
| 2315 | * and this line indicates non-close. We |
| 2316 | * replace it. |
| 2317 | */ |
| 2318 | if (t->flags & SN_CONN_CLOSED) { |
| 2319 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2320 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2321 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2322 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 2323 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2324 | cur_hdr->len = 0; |
| 2325 | } else { |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2326 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 2327 | delta = buffer_replace2(req, cur_ptr + val, cur_end, |
| 2328 | "close", 5); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2329 | cur_next += delta; |
| 2330 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2331 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2332 | } |
| 2333 | t->flags |= SN_CONN_CLOSED; |
| 2334 | } |
| 2335 | } |
| 2336 | old_idx = cur_idx; |
| 2337 | } |
Willy Tarreau | f2f0ee8 | 2007-03-30 12:02:43 +0200 | [diff] [blame] | 2338 | } |
| 2339 | /* add request headers from the rule sets in the same order */ |
| 2340 | for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) { |
| 2341 | if (unlikely(http_header_add_tail(req, |
| 2342 | &txn->req, |
| 2343 | &txn->hdr_idx, |
| 2344 | rule_set->req_add[cur_idx])) < 0) |
| 2345 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2346 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2347 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2348 | /* check if stats URI was requested, and if an auth is needed */ |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 2349 | if (rule_set->uri_auth != NULL && |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2350 | (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) { |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2351 | /* we have to check the URI and auth for this request. |
| 2352 | * FIXME!!! that one is rather dangerous, we want to |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2353 | * make it follow standard rules (eg: clear req->analysers). |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2354 | */ |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2355 | if (stats_check_uri_auth(t, rule_set)) |
| 2356 | return 1; |
| 2357 | } |
| 2358 | |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2359 | /* now check whether we have some switching rules for this request */ |
| 2360 | if (!(t->flags & SN_BE_ASSIGNED)) { |
| 2361 | struct switching_rule *rule; |
| 2362 | |
| 2363 | list_for_each_entry(rule, &cur_proxy->switching_rules, list) { |
| 2364 | int ret; |
| 2365 | |
| 2366 | 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] | 2367 | |
| 2368 | ret = acl_pass(ret); |
Willy Tarreau | a8cfa34 | 2008-07-09 11:23:31 +0200 | [diff] [blame] | 2369 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2370 | ret = !ret; |
| 2371 | |
| 2372 | if (ret) { |
| 2373 | t->be = rule->be.backend; |
| 2374 | t->be->beconn++; |
| 2375 | if (t->be->beconn > t->be->beconn_max) |
| 2376 | t->be->beconn_max = t->be->beconn; |
| 2377 | t->be->cum_beconn++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2378 | |
| 2379 | /* assign new parameters to the session from the new backend */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2380 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 2381 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2382 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2383 | t->flags |= SN_BE_ASSIGNED; |
| 2384 | break; |
| 2385 | } |
| 2386 | } |
| 2387 | } |
| 2388 | |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 2389 | if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) { |
| 2390 | /* No backend was set, but there was a default |
| 2391 | * backend set in the frontend, so we use it and |
| 2392 | * loop again. |
| 2393 | */ |
| 2394 | t->be = cur_proxy->defbe.be; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2395 | t->be->beconn++; |
| 2396 | if (t->be->beconn > t->be->beconn_max) |
| 2397 | t->be->beconn_max = t->be->beconn; |
| 2398 | t->be->cum_beconn++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2399 | |
| 2400 | /* assign new parameters to the session from the new backend */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2401 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 2402 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2403 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 2404 | t->flags |= SN_BE_ASSIGNED; |
| 2405 | } |
| 2406 | } 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] | 2407 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2408 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2409 | if (!(t->flags & SN_BE_ASSIGNED)) { |
| 2410 | /* To ensure correct connection accounting on |
| 2411 | * the backend, we count the connection for the |
| 2412 | * one managing the queue. |
| 2413 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2414 | t->be->beconn++; |
| 2415 | if (t->be->beconn > t->be->beconn_max) |
| 2416 | t->be->beconn_max = t->be->beconn; |
| 2417 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2418 | t->flags |= SN_BE_ASSIGNED; |
| 2419 | } |
| 2420 | |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2421 | /* |
| 2422 | * Right now, we know that we have processed the entire headers |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2423 | * and that unwanted requests have been filtered out. We can do |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2424 | * whatever we want with the remaining request. Also, now we |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2425 | * may have separate values for ->fe, ->be. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2426 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2427 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 2428 | /* |
| 2429 | * If HTTP PROXY is set we simply get remote server address |
| 2430 | * parsing incoming request. |
| 2431 | */ |
| 2432 | if ((t->be->options & PR_O_HTTP_PROXY) && !(t->flags & SN_ADDR_SET)) { |
| 2433 | url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &t->srv_addr); |
| 2434 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2435 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2436 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2437 | * 7: the appsession cookie was looked up very early in 1.2, |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2438 | * so let's do the same now. |
| 2439 | */ |
| 2440 | |
| 2441 | /* It needs to look into the URI */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2442 | if (t->be->appsession_name) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2443 | 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] | 2444 | } |
| 2445 | |
| 2446 | |
| 2447 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2448 | * 8: Now we can work with the cookies. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2449 | * Note that doing so might move headers in the request, but |
| 2450 | * the fields will stay coherent and the URI will not move. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2451 | * This should only be performed in the backend. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2452 | */ |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 2453 | if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name) |
| 2454 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2455 | manage_client_side_cookies(t, req); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2456 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2457 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2458 | /* |
Willy Tarreau | bb046ac | 2007-03-03 19:17:03 +0100 | [diff] [blame] | 2459 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 2460 | * asks for it. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2461 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2462 | if ((t->fe->options | t->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2463 | if (t->cli_addr.ss_family == AF_INET) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2464 | /* Add an X-Forwarded-For header unless the source IP is |
| 2465 | * in the 'except' network range. |
| 2466 | */ |
| 2467 | if ((!t->fe->except_mask.s_addr || |
| 2468 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr) |
| 2469 | != t->fe->except_net.s_addr) && |
| 2470 | (!t->be->except_mask.s_addr || |
| 2471 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr) |
| 2472 | != t->be->except_net.s_addr)) { |
| 2473 | int len; |
| 2474 | unsigned char *pn; |
| 2475 | pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr; |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2476 | |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2477 | /* Note: we rely on the backend to get the header name to be used for |
| 2478 | * x-forwarded-for, because the header is really meant for the backends. |
| 2479 | * However, if the backend did not specify any option, we have to rely |
| 2480 | * on the frontend's header name. |
| 2481 | */ |
| 2482 | if (t->be->fwdfor_hdr_len) { |
| 2483 | len = t->be->fwdfor_hdr_len; |
| 2484 | memcpy(trash, t->be->fwdfor_hdr_name, len); |
| 2485 | } else { |
| 2486 | len = t->fe->fwdfor_hdr_len; |
| 2487 | memcpy(trash, t->fe->fwdfor_hdr_name, len); |
| 2488 | } |
| 2489 | 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] | 2490 | |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2491 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2492 | &txn->hdr_idx, trash, len)) < 0) |
| 2493 | goto return_bad_req; |
| 2494 | } |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2495 | } |
| 2496 | else if (t->cli_addr.ss_family == AF_INET6) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2497 | /* FIXME: for the sake of completeness, we should also support |
| 2498 | * 'except' here, although it is mostly useless in this case. |
| 2499 | */ |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2500 | int len; |
| 2501 | char pn[INET6_ADDRSTRLEN]; |
| 2502 | inet_ntop(AF_INET6, |
| 2503 | (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr, |
| 2504 | pn, sizeof(pn)); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2505 | |
| 2506 | /* Note: we rely on the backend to get the header name to be used for |
| 2507 | * x-forwarded-for, because the header is really meant for the backends. |
| 2508 | * However, if the backend did not specify any option, we have to rely |
| 2509 | * on the frontend's header name. |
| 2510 | */ |
| 2511 | if (t->be->fwdfor_hdr_len) { |
| 2512 | len = t->be->fwdfor_hdr_len; |
| 2513 | memcpy(trash, t->be->fwdfor_hdr_name, len); |
| 2514 | } else { |
| 2515 | len = t->fe->fwdfor_hdr_len; |
| 2516 | memcpy(trash, t->fe->fwdfor_hdr_name, len); |
| 2517 | } |
| 2518 | len += sprintf(trash + len, ": %s", pn); |
| 2519 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2520 | if (unlikely(http_header_add_tail2(req, &txn->req, |
| 2521 | &txn->hdr_idx, trash, len)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2522 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2523 | } |
| 2524 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2525 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2526 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2527 | * 10: add "Connection: close" if needed and not yet set. |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2528 | * 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] | 2529 | */ |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2530 | if (!(t->flags & SN_CONN_CLOSED) && |
Krzysztof Oledzki | 336d475 | 2007-12-25 02:40:22 +0100 | [diff] [blame] | 2531 | ((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] | 2532 | if ((unlikely(msg->sl.rq.v_l != 8) || |
| 2533 | unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) && |
| 2534 | unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx, |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2535 | "Connection: close", 17)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2536 | goto return_bad_req; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2537 | t->flags |= SN_CONN_CLOSED; |
Willy Tarreau | e15d913 | 2006-12-14 22:26:42 +0100 | [diff] [blame] | 2538 | } |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2539 | /* Before we switch to data, was assignment set in manage_client_side_cookie? |
| 2540 | * If not assigned, perhaps we are balancing on url_param, but this is a |
| 2541 | * POST; and the parameters are in the body, maybe scan there to find our server. |
| 2542 | * (unless headers overflowed the buffer?) |
| 2543 | */ |
| 2544 | if (!(t->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 2545 | t->txn.meth == HTTP_METH_POST && t->be->url_param_name != NULL && |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2546 | 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] | 2547 | memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) { |
| 2548 | /* are there enough bytes here? total == l || r || rlim ? |
| 2549 | * len is unsigned, but eoh is int, |
| 2550 | * how many bytes of body have we received? |
| 2551 | * eoh is the first empty line of the header |
| 2552 | */ |
| 2553 | /* 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] | 2554 | 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] | 2555 | |
| 2556 | /* If we have HTTP/1.1 and Expect: 100-continue, then abort. |
| 2557 | * We can't assume responsibility for the server's decision, |
| 2558 | * on this URI and header set. See rfc2616: 14.20, 8.2.3, |
| 2559 | * We also can't change our mind later, about which server to choose, so round robin. |
| 2560 | */ |
| 2561 | if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) { |
| 2562 | struct hdr_ctx ctx; |
| 2563 | ctx.idx = 0; |
| 2564 | /* Expect is allowed in 1.1, look for it */ |
| 2565 | http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx); |
| 2566 | if (ctx.idx != 0 && |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2567 | 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] | 2568 | /* We can't reliablly stall and wait for data, because of |
| 2569 | * .NET clients that don't conform to rfc2616; so, no need for |
| 2570 | * the next block to check length expectations. |
| 2571 | * We could send 100 status back to the client, but then we need to |
| 2572 | * re-write headers, and send the message. And this isn't the right |
| 2573 | * place for that action. |
| 2574 | * TODO: support Expect elsewhere and delete this block. |
| 2575 | */ |
| 2576 | goto end_check_maybe_wait_for_body; |
| 2577 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2578 | |
| 2579 | if (likely(len > t->be->url_param_post_limit)) { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2580 | /* nothing to do, we got enough */ |
| 2581 | } else { |
| 2582 | /* limit implies we are supposed to need this many bytes |
| 2583 | * to find the parameter. Let's see how many bytes we can wait for. |
| 2584 | */ |
| 2585 | long long hint = len; |
| 2586 | struct hdr_ctx ctx; |
| 2587 | ctx.idx = 0; |
| 2588 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx); |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2589 | 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] | 2590 | buffer_write_dis(req); |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2591 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2592 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2593 | else { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2594 | ctx.idx = 0; |
| 2595 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx); |
| 2596 | /* now if we have a length, we'll take the hint */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2597 | if (ctx.idx) { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2598 | /* We have Content-Length */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2599 | if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint)) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2600 | hint = 0; /* parse failure, untrusted client */ |
| 2601 | else { |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2602 | if (hint > 0) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2603 | msg->hdr_content_len = hint; |
| 2604 | else |
| 2605 | hint = 0; /* bad client, sent negative length */ |
| 2606 | } |
| 2607 | } |
| 2608 | /* 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] | 2609 | if (t->be->url_param_post_limit < hint) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2610 | hint = t->be->url_param_post_limit; |
| 2611 | /* now do we really need to buffer more data? */ |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2612 | if (len < hint) { |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 2613 | buffer_write_dis(req); |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2614 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2615 | } |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2616 | /* else... There are no body bytes to wait for */ |
| 2617 | } |
| 2618 | } |
| 2619 | } |
| 2620 | end_check_maybe_wait_for_body: |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2621 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2622 | /************************************************************* |
| 2623 | * OK, that's finished for the headers. We have done what we * |
| 2624 | * could. Let's switch to the DATA state. * |
| 2625 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2626 | |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2627 | buffer_set_rlim(req, BUFSIZE); /* no more rewrite needed */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2628 | t->logs.tv_request = now; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2629 | |
Willy Tarreau | 1fa3126 | 2007-12-03 00:36:16 +0100 | [diff] [blame] | 2630 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 2631 | * which may be the same as the connect timeout if unspecified. |
| 2632 | * 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] | 2633 | * eventually expire. We build the tarpit as an analyser. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2634 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2635 | if (txn->flags & TX_CLTARPIT) { |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2636 | buffer_flush(t->req); |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2637 | /* flush the request so that we can drop the connection early |
| 2638 | * if the client closes first. |
| 2639 | */ |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 2640 | buffer_write_dis(req); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2641 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 2642 | req->analyse_exp = tick_add_ifset(now_ms, t->be->timeout.tarpit); |
| 2643 | if (!req->analyse_exp) |
| 2644 | req->analyse_exp = now_ms; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2645 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2646 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2647 | /* OK let's go on with the BODY now */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2648 | goto end_of_headers; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2649 | |
| 2650 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2651 | txn->req.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2652 | txn->status = 400; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2653 | req->analysers = 0; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2654 | client_retnclose(t, error_message(t, HTTP_ERR_400)); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 2655 | t->fe->failed_req++; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2656 | return_prx_cond: |
| 2657 | if (!(t->flags & SN_ERR_MASK)) |
| 2658 | t->flags |= SN_ERR_PRXCOND; |
| 2659 | if (!(t->flags & SN_FINST_MASK)) |
| 2660 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2661 | return 0; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2662 | end_of_headers: |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2663 | ; // to keep gcc happy |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2664 | } |
| 2665 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2666 | if (req->analysers & AN_REQ_HTTP_TARPIT) { |
| 2667 | struct http_txn *txn = &t->txn; |
| 2668 | |
| 2669 | /* This connection is being tarpitted. The CLIENT side has |
| 2670 | * already set the connect expiration date to the right |
| 2671 | * timeout. We just have to check that the client is still |
| 2672 | * there and that the timeout has not expired. |
| 2673 | */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 2674 | if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 && |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2675 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 2676 | return 0; |
| 2677 | |
| 2678 | /* We will set the queue timer to the time spent, just for |
| 2679 | * logging purposes. We fake a 500 server error, so that the |
| 2680 | * attacker will not suspect his connection has been tarpitted. |
| 2681 | * It will not cause trouble to the logs because we can exclude |
| 2682 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 2683 | */ |
| 2684 | trace_term(t, TT_HTTP_SRV_2); |
| 2685 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 2686 | |
| 2687 | txn->status = 500; |
| 2688 | if (req->flags != BF_READ_ERROR) |
| 2689 | client_retnclose(t, error_message(t, HTTP_ERR_500)); |
| 2690 | |
| 2691 | req->analysers = 0; |
| 2692 | req->analyse_exp = TICK_ETERNITY; |
| 2693 | |
| 2694 | t->fe->failed_req++; |
| 2695 | if (!(t->flags & SN_ERR_MASK)) |
| 2696 | t->flags |= SN_ERR_PRXCOND; |
| 2697 | if (!(t->flags & SN_FINST_MASK)) |
| 2698 | t->flags |= SN_FINST_T; |
| 2699 | return 0; |
| 2700 | } |
| 2701 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2702 | if (req->analysers & AN_REQ_HTTP_BODY) { |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2703 | /* We have to parse the HTTP request body to find any required data. |
| 2704 | * "balance url_param check_post" should have been the only way to get |
| 2705 | * into this. We were brought here after HTTP header analysis, so all |
| 2706 | * related structures are ready. |
| 2707 | */ |
| 2708 | struct http_msg *msg = &t->txn.req; |
| 2709 | unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1; |
| 2710 | long long limit = t->be->url_param_post_limit; |
| 2711 | struct hdr_ctx ctx; |
| 2712 | |
| 2713 | ctx.idx = 0; |
| 2714 | |
| 2715 | /* now if we have a length, we'll take the hint */ |
| 2716 | http_find_header2("Transfer-Encoding", 17, msg->sol, &t->txn.hdr_idx, &ctx); |
| 2717 | if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) { |
| 2718 | unsigned int chunk = 0; |
| 2719 | while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) { |
| 2720 | char c = msg->sol[body]; |
| 2721 | if (ishex(c)) { |
| 2722 | unsigned int hex = toupper(c) - '0'; |
| 2723 | if (hex > 9) |
| 2724 | hex -= 'A' - '9' - 1; |
| 2725 | chunk = (chunk << 4) | hex; |
| 2726 | } else |
| 2727 | break; |
| 2728 | body++; |
| 2729 | } |
| 2730 | if (body + 2 >= req->l) /* we want CRLF too */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2731 | goto http_body_end; /* end of buffer? data missing! */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2732 | |
| 2733 | if (memcmp(msg->sol+body, "\r\n", 2) != 0) |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2734 | 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] | 2735 | |
| 2736 | body += 2; // skip CRLF |
| 2737 | |
| 2738 | /* if we support more then one chunk here, we have to do it again when assigning server |
| 2739 | * 1. how much entity data do we have? new var |
| 2740 | * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here |
| 2741 | * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found) |
| 2742 | */ |
| 2743 | |
| 2744 | if (chunk < limit) |
| 2745 | limit = chunk; /* only reading one chunk */ |
| 2746 | } else { |
| 2747 | if (msg->hdr_content_len < limit) |
| 2748 | limit = msg->hdr_content_len; |
| 2749 | } |
| 2750 | |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2751 | http_body_end: |
| 2752 | /* we leave once we know we have nothing left to do. This means that we have |
| 2753 | * enough bytes, or that we know we'll not get any more (buffer full, read |
| 2754 | * buffer closed). |
| 2755 | */ |
| 2756 | if (req->l - body >= limit || /* enough bytes! */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 2757 | req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR | BF_READ_TIMEOUT) || |
Willy Tarreau | c52164a | 2008-08-17 19:17:57 +0200 | [diff] [blame] | 2758 | tick_is_expired(req->analyse_exp, now_ms)) { |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2759 | /* 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] | 2760 | 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] | 2761 | req->analysers &= ~AN_REQ_HTTP_BODY; |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 2762 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2763 | } |
Willy Tarreau | c52164a | 2008-08-17 19:17:57 +0200 | [diff] [blame] | 2764 | else { |
| 2765 | /* Not enough data. We'll re-use the http-request |
| 2766 | * timeout here. Ideally, we should set the timeout |
| 2767 | * relative to the accept() date. We just set the |
| 2768 | * request timeout once at the beginning of the |
| 2769 | * request. |
| 2770 | */ |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 2771 | buffer_write_dis(req); |
Willy Tarreau | c52164a | 2008-08-17 19:17:57 +0200 | [diff] [blame] | 2772 | if (!tick_isset(req->analyse_exp)) |
| 2773 | req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq); |
| 2774 | return 0; |
| 2775 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2776 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2777 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2778 | /* Note: eventhough nobody should set an unknown flag, clearing them right now will |
| 2779 | * probably reduce one day's debugging session. |
| 2780 | */ |
| 2781 | #ifdef DEBUG_DEV |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2782 | 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] | 2783 | fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n", |
| 2784 | __FILE__, __LINE__, req->analysers); |
| 2785 | ABORT_NOW(); |
| 2786 | } |
| 2787 | #endif |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2788 | 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] | 2789 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2790 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2791 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2792 | /* This function performs all the processing enabled for the current response. |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2793 | * It normally returns zero, but may return 1 if it absolutely needs to be |
| 2794 | * called again after other functions. It relies on buffers flags, and updates |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2795 | * 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] | 2796 | * functions. It works like process_request (see indications above). |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2797 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2798 | int process_response(struct session *t) |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2799 | { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2800 | struct http_txn *txn = &t->txn; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2801 | struct buffer *req = t->req; |
| 2802 | struct buffer *rep = t->rep; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2803 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 2804 | 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] | 2805 | now_ms, __FUNCTION__, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 2806 | t, |
| 2807 | rep, |
| 2808 | rep->rex, rep->wex, |
| 2809 | rep->flags, |
| 2810 | rep->l, |
| 2811 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 2812 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2813 | if (rep->analysers & AN_RTR_HTTP_HDR) { /* receiving server headers */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2814 | /* |
| 2815 | * Now parse the partial (or complete) lines. |
| 2816 | * We will check the response syntax, and also join multi-line |
| 2817 | * headers. An index of all the lines will be elaborated while |
| 2818 | * parsing. |
| 2819 | * |
| 2820 | * For the parsing, we use a 28 states FSM. |
| 2821 | * |
| 2822 | * Here is the information we currently have : |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2823 | * rep->data + rep->som = beginning of response |
| 2824 | * rep->data + rep->eoh = end of processed headers / start of current one |
| 2825 | * 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] | 2826 | * rep->lr = first non-visited byte |
| 2827 | * rep->r = end of data |
| 2828 | */ |
Willy Tarreau | 7f875f6 | 2008-08-11 17:35:01 +0200 | [diff] [blame] | 2829 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2830 | int cur_idx; |
| 2831 | struct http_msg *msg = &txn->rsp; |
| 2832 | struct proxy *cur_proxy; |
| 2833 | |
| 2834 | if (likely(rep->lr < rep->r)) |
| 2835 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
| 2836 | |
| 2837 | /* 1: we might have to print this header in debug mode */ |
| 2838 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2839 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
| 2840 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
| 2841 | char *eol, *sol; |
| 2842 | |
| 2843 | sol = rep->data + msg->som; |
| 2844 | eol = sol + msg->sl.rq.l; |
| 2845 | debug_hdr("srvrep", t, sol, eol); |
| 2846 | |
| 2847 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2848 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
| 2849 | |
| 2850 | while (cur_idx) { |
| 2851 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2852 | debug_hdr("srvhdr", t, sol, eol); |
| 2853 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2854 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2855 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2856 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2857 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2858 | /* |
| 2859 | * Now we quickly check if we have found a full valid response. |
| 2860 | * If not so, we check the FD and buffer states before leaving. |
| 2861 | * A full response is indicated by the fact that we have seen |
| 2862 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 2863 | * responses are checked first. |
| 2864 | * |
| 2865 | * Depending on whether the client is still there or not, we |
| 2866 | * may send an error response back or not. Note that normally |
| 2867 | * we should only check for HTTP status there, and check I/O |
| 2868 | * errors somewhere else. |
| 2869 | */ |
| 2870 | |
| 2871 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2872 | /* Invalid response */ |
| 2873 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 2874 | hdr_response_bad: |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2875 | //buffer_shutr(rep); |
| 2876 | //buffer_shutw(req); |
| 2877 | //fd_delete(req->cons->fd); |
| 2878 | //req->cons->state = SI_ST_CLO; |
| 2879 | buffer_shutr_now(rep); |
| 2880 | buffer_shutw_now(req); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2881 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2882 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2883 | t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2884 | //sess_change_server(t, NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2885 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2886 | t->be->failed_resp++; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2887 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2888 | txn->status = 502; |
| 2889 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 2890 | if (!(t->flags & SN_ERR_MASK)) |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2891 | t->flags |= SN_ERR_PRXCOND; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2892 | if (!(t->flags & SN_FINST_MASK)) |
| 2893 | t->flags |= SN_FINST_H; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 2894 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2895 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 2896 | // process_srv_queue(t->srv); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2897 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2898 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2899 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2900 | /* too large response does not fit in buffer. */ |
| 2901 | else if (rep->flags & BF_FULL) { |
| 2902 | goto hdr_response_bad; |
| 2903 | } |
| 2904 | /* read error */ |
| 2905 | else if (rep->flags & BF_READ_ERROR) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2906 | buffer_shutr_now(rep); |
| 2907 | buffer_shutw_now(req); |
| 2908 | //fd_delete(req->cons->fd); |
| 2909 | //req->cons->state = SI_ST_CLO; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2910 | //if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2911 | //t->srv->cur_sess--; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2912 | //t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2913 | //sess_change_server(t, NULL); |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2914 | //} |
| 2915 | //t->be->failed_resp++; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2916 | rep->analysers = 0; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2917 | txn->status = 502; |
| 2918 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 2919 | if (!(t->flags & SN_ERR_MASK)) |
| 2920 | t->flags |= SN_ERR_SRVCL; |
| 2921 | if (!(t->flags & SN_FINST_MASK)) |
| 2922 | t->flags |= SN_FINST_H; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 2923 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2924 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 2925 | // process_srv_queue(t->srv); |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2926 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2927 | return 0; |
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 | /* read timeout : return a 504 to the client. */ |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2930 | else if (rep->flags & BF_READ_TIMEOUT) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2931 | buffer_shutr_now(rep); |
| 2932 | buffer_shutw_now(req); |
| 2933 | //fd_delete(req->cons->fd); |
| 2934 | //req->cons->state = SI_ST_CLO; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2935 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2936 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2937 | t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2938 | //sess_change_server(t, NULL); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2939 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2940 | t->be->failed_resp++; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2941 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2942 | txn->status = 504; |
| 2943 | client_return(t, error_message(t, HTTP_ERR_504)); |
| 2944 | if (!(t->flags & SN_ERR_MASK)) |
| 2945 | t->flags |= SN_ERR_SRVTO; |
| 2946 | if (!(t->flags & SN_FINST_MASK)) |
| 2947 | t->flags |= SN_FINST_H; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 2948 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2949 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 2950 | // process_srv_queue(t->srv); |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2951 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2952 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2953 | /* write error to client, or close from server */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 2954 | else if (rep->flags & (BF_WRITE_ERROR|BF_SHUTR)) { |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2955 | buffer_shutr_now(rep); |
| 2956 | buffer_shutw_now(req); |
| 2957 | //fd_delete(req->cons->fd); |
| 2958 | //req->cons->state = SI_ST_CLO; |
| 2959 | if (t->srv) { |
| 2960 | //t->srv->cur_sess--; |
| 2961 | t->srv->failed_resp++; |
| 2962 | //sess_change_server(t, NULL); |
| 2963 | } |
| 2964 | t->be->failed_resp++; |
| 2965 | rep->analysers = 0; |
| 2966 | txn->status = 502; |
| 2967 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 2968 | if (!(t->flags & SN_ERR_MASK)) |
| 2969 | t->flags |= SN_ERR_SRVCL; |
| 2970 | if (!(t->flags & SN_FINST_MASK)) |
| 2971 | t->flags |= SN_FINST_H; |
| 2972 | |
| 2973 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 2974 | // process_srv_queue(t->srv); |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2975 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2976 | return 0; |
| 2977 | } |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 2978 | buffer_write_dis(rep); |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2979 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2980 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2981 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2982 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2983 | /***************************************************************** |
| 2984 | * More interesting part now : we know that we have a complete * |
| 2985 | * response which at least looks like HTTP. We have an indicator * |
| 2986 | * of each header's length, so we can parse them quickly. * |
| 2987 | ****************************************************************/ |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2988 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2989 | rep->analysers &= ~AN_RTR_HTTP_HDR; |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 2990 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2991 | /* ensure we keep this pointer to the beginning of the message */ |
| 2992 | msg->sol = rep->data + msg->som; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2993 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2994 | /* |
| 2995 | * 1: get the status code and check for cacheability. |
| 2996 | */ |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2997 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2998 | t->logs.logwait &= ~LW_RESP; |
| 2999 | 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] | 3000 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3001 | switch (txn->status) { |
| 3002 | case 200: |
| 3003 | case 203: |
| 3004 | case 206: |
| 3005 | case 300: |
| 3006 | case 301: |
| 3007 | case 410: |
| 3008 | /* RFC2616 @13.4: |
| 3009 | * "A response received with a status code of |
| 3010 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 3011 | * by a cache (...) unless a cache-control |
| 3012 | * directive prohibits caching." |
| 3013 | * |
| 3014 | * RFC2616 @9.5: POST method : |
| 3015 | * "Responses to this method are not cacheable, |
| 3016 | * unless the response includes appropriate |
| 3017 | * Cache-Control or Expires header fields." |
| 3018 | */ |
| 3019 | if (likely(txn->meth != HTTP_METH_POST) && |
| 3020 | (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
| 3021 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 3022 | break; |
| 3023 | default: |
| 3024 | break; |
| 3025 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3026 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3027 | /* |
| 3028 | * 2: we may need to capture headers |
| 3029 | */ |
| 3030 | if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap)) |
| 3031 | capture_headers(rep->data + msg->som, &txn->hdr_idx, |
| 3032 | txn->rsp.cap, t->fe->rsp_cap); |
| 3033 | |
| 3034 | /* |
| 3035 | * 3: we will have to evaluate the filters. |
| 3036 | * As opposed to version 1.2, now they will be evaluated in the |
| 3037 | * filters order and not in the header order. This means that |
| 3038 | * each filter has to be validated among all headers. |
| 3039 | * |
| 3040 | * Filters are tried with ->be first, then with ->fe if it is |
| 3041 | * different from ->be. |
| 3042 | */ |
| 3043 | |
| 3044 | t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */ |
| 3045 | |
| 3046 | cur_proxy = t->be; |
| 3047 | while (1) { |
| 3048 | struct proxy *rule_set = cur_proxy; |
| 3049 | |
| 3050 | /* try headers filters */ |
| 3051 | if (rule_set->rsp_exp != NULL) { |
| 3052 | if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) { |
| 3053 | return_bad_resp: |
| 3054 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3055 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3056 | t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3057 | //sess_change_server(t, NULL); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3058 | } |
| 3059 | cur_proxy->failed_resp++; |
| 3060 | return_srv_prx_502: |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3061 | buffer_shutr_now(rep); |
| 3062 | buffer_shutw_now(req); |
| 3063 | //fd_delete(req->cons->fd); |
| 3064 | //req->cons->state = SI_ST_CLO; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3065 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3066 | txn->status = 502; |
| 3067 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 3068 | if (!(t->flags & SN_ERR_MASK)) |
| 3069 | t->flags |= SN_ERR_PRXCOND; |
| 3070 | if (!(t->flags & SN_FINST_MASK)) |
| 3071 | t->flags |= SN_FINST_H; |
| 3072 | /* We used to have a free connection slot. Since we'll never use it, |
| 3073 | * we have to inform the server that it may be used by another session. |
| 3074 | */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3075 | //if (t->srv && may_dequeue_tasks(t->srv, cur_proxy)) |
| 3076 | // process_srv_queue(t->srv); |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3077 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3078 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3079 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3080 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3081 | /* has the response been denied ? */ |
| 3082 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | f899b94 | 2008-03-28 18:09:38 +0100 | [diff] [blame] | 3083 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3084 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3085 | t->srv->failed_secu++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3086 | //sess_change_server(t, NULL); |
Willy Tarreau | f899b94 | 2008-03-28 18:09:38 +0100 | [diff] [blame] | 3087 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3088 | cur_proxy->denied_resp++; |
| 3089 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 3090 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3091 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3092 | /* We might have to check for "Connection:" */ |
| 3093 | if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) && |
| 3094 | !(t->flags & SN_CONN_CLOSED)) { |
| 3095 | char *cur_ptr, *cur_end, *cur_next; |
| 3096 | int cur_idx, old_idx, delta, val; |
| 3097 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3098 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3099 | cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 3100 | old_idx = 0; |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 3101 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3102 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 3103 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 3104 | cur_ptr = cur_next; |
| 3105 | cur_end = cur_ptr + cur_hdr->len; |
| 3106 | cur_next = cur_end + cur_hdr->cr + 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3107 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3108 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 3109 | if (val) { |
| 3110 | /* 3 possibilities : |
| 3111 | * - we have already set Connection: close, |
| 3112 | * so we remove this line. |
| 3113 | * - we have not yet set Connection: close, |
| 3114 | * but this line indicates close. We leave |
| 3115 | * it untouched and set the flag. |
| 3116 | * - we have not yet set Connection: close, |
| 3117 | * and this line indicates non-close. We |
| 3118 | * replace it. |
| 3119 | */ |
| 3120 | if (t->flags & SN_CONN_CLOSED) { |
| 3121 | delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0); |
| 3122 | txn->rsp.eoh += delta; |
| 3123 | cur_next += delta; |
| 3124 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 3125 | txn->hdr_idx.used--; |
| 3126 | cur_hdr->len = 0; |
| 3127 | } else { |
| 3128 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 3129 | delta = buffer_replace2(rep, cur_ptr + val, cur_end, |
| 3130 | "close", 5); |
| 3131 | cur_next += delta; |
| 3132 | cur_hdr->len += delta; |
| 3133 | txn->rsp.eoh += delta; |
| 3134 | } |
| 3135 | t->flags |= SN_CONN_CLOSED; |
| 3136 | } |
| 3137 | } |
| 3138 | old_idx = cur_idx; |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 3139 | } |
| 3140 | } |
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 | /* add response headers from the rule sets in the same order */ |
| 3143 | for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) { |
| 3144 | if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, |
| 3145 | rule_set->rsp_add[cur_idx])) < 0) |
| 3146 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 3147 | } |
| 3148 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3149 | /* check whether we're already working on the frontend */ |
| 3150 | if (cur_proxy == t->fe) |
| 3151 | break; |
| 3152 | cur_proxy = t->fe; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3153 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3154 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3155 | /* |
| 3156 | * 4: check for server cookie. |
| 3157 | */ |
| 3158 | if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name |
| 3159 | || (t->be->options & PR_O_CHK_CACHE)) |
| 3160 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3161 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3162 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3163 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3164 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3165 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3166 | if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0) |
| 3167 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3168 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3169 | /* |
| 3170 | * 6: add server cookie in the response if needed |
| 3171 | */ |
| 3172 | if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) && |
| 3173 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) { |
| 3174 | int len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3175 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3176 | /* the server is known, it's not the one the client requested, we have to |
| 3177 | * insert a set-cookie here, except if we want to insert only on POST |
| 3178 | * requests and this one isn't. Note that servers which don't have cookies |
| 3179 | * (eg: some backup servers) will return a full cookie removal request. |
| 3180 | */ |
| 3181 | len = sprintf(trash, "Set-Cookie: %s=%s; path=/", |
| 3182 | t->be->cookie_name, |
| 3183 | 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] | 3184 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3185 | if (t->be->cookie_domain) |
| 3186 | len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3187 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3188 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3189 | trash, len)) < 0) |
| 3190 | goto return_bad_resp; |
| 3191 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3192 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3193 | /* Here, we will tell an eventual cache on the client side that we don't |
| 3194 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 3195 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 3196 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 3197 | */ |
| 3198 | if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3199 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3200 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3201 | |
| 3202 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3203 | "Cache-control: private", 22)) < 0) |
| 3204 | goto return_bad_resp; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3205 | } |
| 3206 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3207 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3208 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3209 | /* |
| 3210 | * 7: check if result will be cacheable with a cookie. |
| 3211 | * We'll block the response if security checks have caught |
| 3212 | * nasty things such as a cacheable cookie. |
| 3213 | */ |
| 3214 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) == |
| 3215 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) && |
| 3216 | (t->be->options & PR_O_CHK_CACHE)) { |
| 3217 | |
| 3218 | /* we're in presence of a cacheable response containing |
| 3219 | * a set-cookie header. We'll block it as requested by |
| 3220 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3221 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3222 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3223 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3224 | t->srv->failed_secu++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3225 | //sess_change_server(t, NULL); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3226 | } |
| 3227 | t->be->denied_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3228 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3229 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 3230 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 3231 | send_log(t->be, LOG_ALERT, |
| 3232 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 3233 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 3234 | goto return_srv_prx_502; |
| 3235 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3236 | |
| 3237 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3238 | * 8: add "Connection: close" if needed and not yet set. |
| 3239 | * 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] | 3240 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3241 | if (!(t->flags & SN_CONN_CLOSED) && |
| 3242 | ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) { |
| 3243 | if ((unlikely(msg->sl.st.v_l != 8) || |
| 3244 | unlikely(req->data[msg->som + 7] != '0')) && |
| 3245 | unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3246 | "Connection: close", 17)) < 0) |
| 3247 | goto return_bad_resp; |
| 3248 | t->flags |= SN_CONN_CLOSED; |
| 3249 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3250 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3251 | /************************************************************* |
| 3252 | * OK, that's finished for the headers. We have done what we * |
| 3253 | * could. Let's switch to the DATA state. * |
| 3254 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3255 | |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 3256 | buffer_set_rlim(rep, BUFSIZE); /* no more rewrite needed */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3257 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3258 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3259 | #ifdef CONFIG_HAP_TCPSPLICE |
| 3260 | if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
| 3261 | /* TCP splicing supported by both FE and BE */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 3262 | tcp_splice_splicefd(rep->cons->fd, rep->prod->fd, 0); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3263 | } |
| 3264 | #endif |
| 3265 | /* if the user wants to log as soon as possible, without counting |
| 3266 | * bytes from the server, then this is the right moment. We have |
| 3267 | * to temporarily assign bytes_out to log what we currently have. |
| 3268 | */ |
| 3269 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 3270 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 3271 | t->logs.bytes_out = txn->rsp.eoh; |
| 3272 | if (t->fe->to_log & LW_REQ) |
| 3273 | http_sess_log(t); |
| 3274 | else |
| 3275 | tcp_sess_log(t); |
| 3276 | t->logs.bytes_out = 0; |
| 3277 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3278 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3279 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 3280 | * otherwise we would not let the client side wake up. |
| 3281 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3282 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3283 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3284 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3285 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3286 | /* Note: eventhough nobody should set an unknown flag, clearing them right now will |
| 3287 | * probably reduce one day's debugging session. |
| 3288 | */ |
| 3289 | #ifdef DEBUG_DEV |
| 3290 | if (rep->analysers & ~(AN_RTR_HTTP_HDR)) { |
| 3291 | fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n", |
| 3292 | __FILE__, __LINE__, rep->analysers); |
| 3293 | ABORT_NOW(); |
| 3294 | } |
| 3295 | #endif |
| 3296 | rep->analysers &= AN_RTR_HTTP_HDR; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3297 | return 0; |
| 3298 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3299 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 3300 | ///* |
| 3301 | // * Manages the client FSM and its socket. It normally returns zero, but may |
| 3302 | // * return 1 if it absolutely wants to be called again. |
| 3303 | // * |
| 3304 | // * Note: process_cli is the ONLY function allowed to set cli_state to anything |
| 3305 | // * but CL_STCLOSE. |
| 3306 | // */ |
| 3307 | //int process_cli(struct session *t) |
| 3308 | //{ |
| 3309 | // struct buffer *req = t->req; |
| 3310 | // struct buffer *rep = t->rep; |
| 3311 | // |
| 3312 | // 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", |
| 3313 | // now_ms, __FUNCTION__, |
| 3314 | // t->cli_fd, t->cli_fd >= 0 ? fdtab[t->cli_fd].state : 0, /* fd,state*/ |
| 3315 | // cli_stnames[t->cli_state], |
| 3316 | // t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_RD) : 0, |
| 3317 | // t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_WR) : 0, |
| 3318 | // req->rex, rep->wex, |
| 3319 | // req->flags, rep->flags, |
| 3320 | // req->l, rep->l); |
| 3321 | // |
| 3322 | // update_state: |
| 3323 | // /* FIXME: we still have to check for CL_STSHUTR because client_retnclose |
| 3324 | // * still set this state (and will do until unix sockets are converted). |
| 3325 | // */ |
| 3326 | // if (t->cli_state == CL_STDATA || t->cli_state == CL_STSHUTR) { |
| 3327 | // /* we can skip most of the tests at once if some conditions are not met */ |
| 3328 | // if (!((fdtab[t->cli_fd].state == FD_STERROR) || |
| 3329 | // (req->flags & (BF_READ_TIMEOUT|BF_READ_ERROR|BF_SHUTR_NOW)) || |
| 3330 | // (rep->flags & (BF_WRITE_TIMEOUT|BF_WRITE_ERROR|BF_SHUTW_NOW)) || |
| 3331 | // (!(req->flags & BF_SHUTR) && req->flags & (BF_READ_NULL|BF_SHUTW)) || |
| 3332 | // (!(rep->flags & BF_SHUTW) && |
| 3333 | // (rep->flags & (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR)) == (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR)))) |
| 3334 | // goto update_timeouts; |
| 3335 | // |
| 3336 | // /* read or write error */ |
| 3337 | // if (fdtab[t->cli_fd].state == FD_STERROR) { |
| 3338 | // buffer_shutr(req); |
| 3339 | // req->flags |= BF_READ_ERROR; |
| 3340 | // buffer_shutw(rep); |
| 3341 | // rep->flags |= BF_WRITE_ERROR; |
| 3342 | // fd_delete(t->cli_fd); |
| 3343 | // t->cli_state = CL_STCLOSE; |
| 3344 | // trace_term(t, TT_HTTP_CLI_1); |
| 3345 | // if (!req->analysers) { |
| 3346 | // if (!(t->flags & SN_ERR_MASK)) |
| 3347 | // t->flags |= SN_ERR_CLICL; |
| 3348 | // if (!(t->flags & SN_FINST_MASK)) { |
| 3349 | // if (req->cons->err_type <= SI_ET_QUEUE_ABRT) |
| 3350 | // t->flags |= SN_FINST_Q; |
| 3351 | // else if (req->cons->err_type <= SI_ET_CONN_OTHER) |
| 3352 | // t->flags |= SN_FINST_C; |
| 3353 | // else |
| 3354 | // t->flags |= SN_FINST_D; |
| 3355 | // } |
| 3356 | // } |
| 3357 | // goto update_state; |
| 3358 | // } |
| 3359 | // /* last read, or end of server write */ |
| 3360 | // else if (!(req->flags & BF_SHUTR) && /* not already done */ |
| 3361 | // req->flags & (BF_READ_NULL|BF_SHUTR_NOW|BF_SHUTW)) { |
| 3362 | // buffer_shutr(req); |
| 3363 | // if (!(rep->flags & BF_SHUTW)) { |
| 3364 | // EV_FD_CLR(t->cli_fd, DIR_RD); |
| 3365 | // trace_term(t, TT_HTTP_CLI_2); |
| 3366 | // } else { |
| 3367 | // /* output was already closed */ |
| 3368 | // fd_delete(t->cli_fd); |
| 3369 | // t->cli_state = CL_STCLOSE; |
| 3370 | // trace_term(t, TT_HTTP_CLI_3); |
| 3371 | // } |
| 3372 | // goto update_state; |
| 3373 | // } |
| 3374 | // /* last server read and buffer empty : we only check them when we're |
| 3375 | // * allowed to forward the data. |
| 3376 | // */ |
| 3377 | // else if (!(rep->flags & BF_SHUTW) && /* not already done */ |
| 3378 | // ((rep->flags & BF_SHUTW_NOW) || |
| 3379 | // (rep->flags & BF_EMPTY && rep->flags & BF_MAY_FORWARD && |
| 3380 | // rep->flags & BF_SHUTR && !(t->flags & SN_SELF_GEN)))) { |
| 3381 | // buffer_shutw(rep); |
| 3382 | // if (!(req->flags & BF_SHUTR)) { |
| 3383 | // EV_FD_CLR(t->cli_fd, DIR_WR); |
| 3384 | // shutdown(t->cli_fd, SHUT_WR); |
| 3385 | // trace_term(t, TT_HTTP_CLI_4); |
| 3386 | // } else { |
| 3387 | // fd_delete(t->cli_fd); |
| 3388 | // t->cli_state = CL_STCLOSE; |
| 3389 | // trace_term(t, TT_HTTP_CLI_5); |
| 3390 | // } |
| 3391 | // goto update_state; |
| 3392 | // } |
| 3393 | // /* read timeout */ |
| 3394 | // else if ((req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT) { |
| 3395 | // buffer_shutr(req); |
| 3396 | // if (!(rep->flags & BF_SHUTW)) { |
| 3397 | // EV_FD_CLR(t->cli_fd, DIR_RD); |
| 3398 | // trace_term(t, TT_HTTP_CLI_6); |
| 3399 | // } else { |
| 3400 | // /* output was already closed */ |
| 3401 | // fd_delete(t->cli_fd); |
| 3402 | // t->cli_state = CL_STCLOSE; |
| 3403 | // trace_term(t, TT_HTTP_CLI_7); |
| 3404 | // } |
| 3405 | // if (!req->analysers) { |
| 3406 | // if (!(t->flags & SN_ERR_MASK)) |
| 3407 | // t->flags |= SN_ERR_CLITO; |
| 3408 | // if (!(t->flags & SN_FINST_MASK)) { |
| 3409 | // if (req->cons->err_type <= SI_ET_QUEUE_ABRT) |
| 3410 | // t->flags |= SN_FINST_Q; |
| 3411 | // else if (req->cons->err_type <= SI_ET_CONN_OTHER) |
| 3412 | // t->flags |= SN_FINST_C; |
| 3413 | // else |
| 3414 | // t->flags |= SN_FINST_D; |
| 3415 | // } |
| 3416 | // } |
| 3417 | // goto update_state; |
| 3418 | // } |
| 3419 | // /* write timeout */ |
| 3420 | // else if ((rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT) { |
| 3421 | // buffer_shutw(rep); |
| 3422 | // if (!(req->flags & BF_SHUTR)) { |
| 3423 | // EV_FD_CLR(t->cli_fd, DIR_WR); |
| 3424 | // shutdown(t->cli_fd, SHUT_WR); |
| 3425 | // trace_term(t, TT_HTTP_CLI_8); |
| 3426 | // } else { |
| 3427 | // fd_delete(t->cli_fd); |
| 3428 | // t->cli_state = CL_STCLOSE; |
| 3429 | // trace_term(t, TT_HTTP_CLI_9); |
| 3430 | // } |
| 3431 | // if (!req->analysers) { |
| 3432 | // if (!(t->flags & SN_ERR_MASK)) |
| 3433 | // t->flags |= SN_ERR_CLITO; |
| 3434 | // if (!(t->flags & SN_FINST_MASK)) { |
| 3435 | // if (req->cons->err_type <= SI_ET_QUEUE_ABRT) |
| 3436 | // t->flags |= SN_FINST_Q; |
| 3437 | // else if (req->cons->err_type <= SI_ET_CONN_OTHER) |
| 3438 | // t->flags |= SN_FINST_C; |
| 3439 | // else |
| 3440 | // t->flags |= SN_FINST_D; |
| 3441 | // } |
| 3442 | // } |
| 3443 | // goto update_state; |
| 3444 | // } |
| 3445 | // |
| 3446 | // update_timeouts: |
| 3447 | // /* manage read timeout */ |
| 3448 | // if (!(req->flags & BF_SHUTR)) { |
| 3449 | // if (req->flags & BF_FULL) { |
| 3450 | // /* no room to read more data */ |
| 3451 | // if (EV_FD_COND_C(t->cli_fd, DIR_RD)) { |
| 3452 | // /* stop reading until we get some space */ |
| 3453 | // req->rex = TICK_ETERNITY; |
| 3454 | // } |
| 3455 | // } else { |
| 3456 | // EV_FD_COND_S(t->cli_fd, DIR_RD); |
| 3457 | // req->rex = tick_add_ifset(now_ms, t->fe->timeout.client); |
| 3458 | // } |
| 3459 | // } |
| 3460 | // |
| 3461 | // /* manage write timeout */ |
| 3462 | // if (!(rep->flags & BF_SHUTW)) { |
| 3463 | // /* first, we may have to produce data (eg: stats). |
| 3464 | // * right now, this is limited to the SHUTR state. |
| 3465 | // */ |
| 3466 | // if (req->flags & BF_SHUTR && t->flags & SN_SELF_GEN) { |
| 3467 | // produce_content(t); |
| 3468 | // if (rep->flags & BF_EMPTY) { |
| 3469 | // buffer_shutw(rep); |
| 3470 | // fd_delete(t->cli_fd); |
| 3471 | // t->cli_state = CL_STCLOSE; |
| 3472 | // trace_term(t, TT_HTTP_CLI_10); |
| 3473 | // goto update_state; |
| 3474 | // } |
| 3475 | // } |
| 3476 | // |
| 3477 | // /* we don't enable client write if the buffer is empty, nor if the server has to analyze it */ |
| 3478 | // if ((rep->flags & (BF_EMPTY|BF_MAY_FORWARD)) != BF_MAY_FORWARD) { |
| 3479 | // if (EV_FD_COND_C(t->cli_fd, DIR_WR)) { |
| 3480 | // /* stop writing */ |
| 3481 | // rep->wex = TICK_ETERNITY; |
| 3482 | // } |
| 3483 | // } else { |
| 3484 | // /* buffer not empty */ |
| 3485 | // EV_FD_COND_S(t->cli_fd, DIR_WR); |
| 3486 | // if (!tick_isset(rep->wex)) { |
| 3487 | // /* restart writing */ |
| 3488 | // rep->wex = tick_add_ifset(now_ms, t->fe->timeout.client); |
| 3489 | // if (!(req->flags & BF_SHUTR) && tick_isset(rep->wex) && tick_isset(req->rex)) { |
| 3490 | // /* FIXME: to prevent the client from expiring read timeouts during writes, |
| 3491 | // * we refresh it, except if it was already infinite. */ |
| 3492 | // req->rex = rep->wex; |
| 3493 | // } |
| 3494 | // } |
| 3495 | // } |
| 3496 | // } |
| 3497 | // return 0; /* other cases change nothing */ |
| 3498 | // } |
| 3499 | // else if (t->cli_state == CL_STCLOSE) { /* CL_STCLOSE: nothing to do */ |
| 3500 | // if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 3501 | // int len; |
| 3502 | // 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); |
| 3503 | // write(1, trash, len); |
| 3504 | // } |
| 3505 | // return 0; |
| 3506 | // } |
| 3507 | //#ifdef DEBUG_DEV |
| 3508 | // fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, t->cli_state); |
| 3509 | // ABORT_NOW(); |
| 3510 | //#endif |
| 3511 | // return 0; |
| 3512 | //} |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3513 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3514 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3515 | /* Return 1 if the pending connection has failed and should be retried, |
| 3516 | * otherwise zero. We may only come here in SI_ST_CON state, which means that |
| 3517 | * the socket's file descriptor is known. |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3518 | */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3519 | int tcp_connection_status(struct session *t) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3520 | { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3521 | struct buffer *req = t->req; |
| 3522 | struct buffer *rep = t->rep; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3523 | int conn_err = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3524 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3525 | DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n", |
| 3526 | now_ms, __FUNCTION__, |
| 3527 | cli_stnames[t->cli_state], |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3528 | rep->rex, req->wex, |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 3529 | req->flags, rep->flags, |
| 3530 | req->l, rep->l); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3531 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3532 | if ((req->flags & BF_SHUTW_NOW) || |
| 3533 | (rep->flags & BF_SHUTW) || |
| 3534 | ((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] | 3535 | ((req->flags & BF_EMPTY && !(req->flags & BF_WRITE_ACTIVITY)) || |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3536 | t->be->options & PR_O_ABRT_CLOSE))) { /* give up */ |
| 3537 | |
| 3538 | trace_term(t, TT_HTTP_SRV_5); |
| 3539 | req->wex = TICK_ETERNITY; |
| 3540 | fd_delete(req->cons->fd); |
| 3541 | if (t->srv) { |
| 3542 | t->srv->cur_sess--; |
| 3543 | sess_change_server(t, NULL); |
| 3544 | } |
| 3545 | /* note that this must not return any error because it would be able to |
| 3546 | * overwrite the client_retnclose() output. |
| 3547 | */ |
| 3548 | //srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL); |
| 3549 | |
| 3550 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3551 | buffer_shutw(req); |
| 3552 | buffer_shutr(rep); |
| 3553 | req->cons->state = SI_ST_CLO; |
| 3554 | if (!req->cons->err_type) |
| 3555 | req->cons->err_type = SI_ET_CONN_ABRT; |
| 3556 | req->cons->err_loc = t->srv; |
| 3557 | return 0; |
| 3558 | } |
| 3559 | |
| 3560 | /* check for timeouts and asynchronous connect errors */ |
| 3561 | if (fdtab[req->cons->fd].state == FD_STERROR) { |
| 3562 | conn_err = SI_ET_CONN_ERR; |
| 3563 | if (!req->cons->err_type) |
| 3564 | req->cons->err_type = SI_ET_CONN_ERR; |
| 3565 | } |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 3566 | else if (!(req->flags & BF_WRITE_ACTIVITY)) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3567 | /* nothing happened, maybe we timed out */ |
| 3568 | if (tick_is_expired(req->wex, now_ms)) { |
| 3569 | conn_err = SI_ET_CONN_TO; |
| 3570 | if (!req->cons->err_type) |
| 3571 | req->cons->err_type = SI_ET_CONN_TO; |
| 3572 | } |
| 3573 | else |
| 3574 | return 0; /* let's wait a bit more */ |
| 3575 | } |
| 3576 | |
| 3577 | if (conn_err) { |
| 3578 | fd_delete(req->cons->fd); |
| 3579 | req->cons->state = SI_ST_CLO; |
| 3580 | |
| 3581 | if (t->srv) { |
| 3582 | t->srv->cur_sess--; |
| 3583 | sess_change_server(t, NULL); |
| 3584 | req->cons->err_loc = t->srv; |
| 3585 | } |
| 3586 | |
| 3587 | /* ensure that we have enough retries left */ |
| 3588 | if (srv_count_retry_down(t, conn_err)) |
| 3589 | return 0; |
| 3590 | |
| 3591 | if (conn_err == SI_ET_CONN_ERR) { |
| 3592 | /* we encountered an immediate connection error, and we |
| 3593 | * will have to retry connecting to the same server, most |
| 3594 | * likely leading to the same result. To avoid this, we |
| 3595 | * fake a connection timeout to retry after a turn-around |
| 3596 | * time of 1 second. We will wait in the previous if block. |
| 3597 | */ |
| 3598 | req->cons->state = SI_ST_TAR; |
| 3599 | req->wex = tick_add(now_ms, MS_TO_TICKS(1000)); |
| 3600 | return 0; |
| 3601 | } |
| 3602 | |
| 3603 | if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) { |
| 3604 | /* We're on our last chance, and the REDISP option was specified. |
| 3605 | * We will ignore cookie and force to balance or use the dispatcher. |
| 3606 | */ |
| 3607 | /* let's try to offer this slot to anybody */ |
| 3608 | if (may_dequeue_tasks(t->srv, t->be)) |
| 3609 | process_srv_queue(t->srv); |
| 3610 | |
| 3611 | /* it's left to the dispatcher to choose a server */ |
| 3612 | t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
| 3613 | t->prev_srv = t->srv; |
| 3614 | } else { |
| 3615 | /* we just want to retry */ |
| 3616 | if (t->srv) |
| 3617 | t->srv->retries++; |
| 3618 | t->be->retries++; |
| 3619 | |
| 3620 | /* Now we will try to either reconnect to the same server or |
| 3621 | * connect to another server. If the connection gets queued |
| 3622 | * because all servers are saturated, then we will go back to |
| 3623 | * the idle state where the buffer's consumer is marked as |
| 3624 | * unknown. |
| 3625 | */ |
| 3626 | if (srv_retryable_connect(t)) { |
| 3627 | /* success or unrecoverable error */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3628 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3629 | return 0; |
| 3630 | } |
| 3631 | } |
| 3632 | |
| 3633 | /* We'll rely on the caller to try to get a connection again */ |
| 3634 | return 1; |
| 3635 | } |
| 3636 | else { |
| 3637 | /* no error and write OK : connection succeeded */ |
| 3638 | t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 3639 | req->cons->state = SI_ST_EST; |
| 3640 | req->cons->err_type = SI_ET_NONE; |
| 3641 | req->cons->err_loc = NULL; |
| 3642 | |
| 3643 | if (req->flags & BF_EMPTY) { |
| 3644 | EV_FD_CLR(req->cons->fd, DIR_WR); |
| 3645 | req->wex = TICK_ETERNITY; |
| 3646 | } else { |
| 3647 | EV_FD_SET(req->cons->fd, DIR_WR); |
| 3648 | req->wex = tick_add_ifset(now_ms, t->be->timeout.server); |
| 3649 | if (tick_isset(req->wex)) { |
| 3650 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 3651 | * we refresh it. */ |
| 3652 | rep->rex = req->wex; |
| 3653 | } |
| 3654 | } |
| 3655 | |
| 3656 | if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */ |
| 3657 | if (!(rep->flags & BF_HIJACK)) { |
| 3658 | EV_FD_SET(req->cons->fd, DIR_RD); |
| 3659 | rep->rex = tick_add_ifset(now_ms, t->be->timeout.server); |
| 3660 | } |
| 3661 | buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */ |
| 3662 | |
| 3663 | /* if the user wants to log as soon as possible, without counting |
| 3664 | bytes from the server, then this is the right moment. */ |
| 3665 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 3666 | t->logs.t_close = t->logs.t_connect; /* to get a valid end date */ |
| 3667 | tcp_sess_log(t); |
| 3668 | } |
| 3669 | #ifdef CONFIG_HAP_TCPSPLICE |
| 3670 | if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
| 3671 | /* TCP splicing supported by both FE and BE */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 3672 | tcp_splice_splicefd(req->prod->fd, req->cons->fd, 0); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3673 | } |
| 3674 | #endif |
| 3675 | } |
| 3676 | else { |
| 3677 | rep->analysers |= AN_RTR_HTTP_HDR; |
| 3678 | buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */ |
| 3679 | t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE; |
| 3680 | /* reset hdr_idx which was already initialized by the request. |
| 3681 | * right now, the http parser does it. |
| 3682 | * hdr_idx_init(&t->txn.hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3683 | */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3684 | } |
| 3685 | |
Willy Tarreau | 9a2d154 | 2008-08-30 12:31:07 +0200 | [diff] [blame^] | 3686 | rep->flags |= BF_READ_ATTACHED; /* producer is now attached */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3687 | req->wex = TICK_ETERNITY; |
| 3688 | return 0; |
| 3689 | } |
| 3690 | } |
| 3691 | |
| 3692 | |
| 3693 | /* |
| 3694 | * This function tries to assign a server to a stream_sock interface. |
| 3695 | * It may be called only for t->req->cons->state = one of { SI_ST_INI, |
| 3696 | * SI_ST_TAR, SI_ST_QUE }. It returns one of those states, SI_ST_ASS |
| 3697 | * in case of success, or SI_ST_CLO in case of failure. It returns 1 if |
| 3698 | * it returns SI_ST_ASS, otherwise zero. |
| 3699 | */ |
| 3700 | int stream_sock_assign_server(struct session *t) |
| 3701 | { |
| 3702 | DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n", |
| 3703 | now_ms, __FUNCTION__, |
| 3704 | cli_stnames[t->cli_state], |
| 3705 | t->rep->rex, t->req->wex, |
| 3706 | t->req->flags, t->rep->flags, |
| 3707 | t->req->l, t->rep->l); |
| 3708 | |
| 3709 | if (t->req->cons->state == SI_ST_TAR) { |
| 3710 | /* connection might be aborted */ |
| 3711 | if ((t->req->flags & BF_SHUTW_NOW) || |
| 3712 | (t->rep->flags & BF_SHUTW) || |
| 3713 | ((t->req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */ |
| 3714 | (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] | 3715 | |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3716 | trace_term(t, TT_HTTP_SRV_1); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3717 | t->req->wex = TICK_ETERNITY; |
| 3718 | |
| 3719 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3720 | buffer_shutr(t->rep); |
| 3721 | buffer_shutw(t->req); |
| 3722 | if (!t->req->cons->err_type) |
| 3723 | t->req->cons->err_type = SI_ET_CONN_ABRT; |
| 3724 | t->req->cons->state = SI_ST_CLO; |
| 3725 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3726 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3727 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3728 | if (!tick_is_expired(t->req->wex, now_ms)) |
| 3729 | return 0; /* still in turn-around */ |
| 3730 | |
| 3731 | t->req->cons->state = SI_ST_INI; |
| 3732 | } |
| 3733 | else if (t->req->cons->state == SI_ST_QUE) { |
| 3734 | if (t->pend_pos) { |
| 3735 | /* request still in queue... */ |
| 3736 | if (tick_is_expired(t->req->wex, now_ms)) { |
| 3737 | /* ... and timeout expired */ |
| 3738 | trace_term(t, TT_HTTP_SRV_3); |
| 3739 | t->req->wex = TICK_ETERNITY; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3740 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3741 | if (t->srv) |
| 3742 | t->srv->failed_conns++; |
| 3743 | t->be->failed_conns++; |
| 3744 | |
| 3745 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3746 | buffer_shutr(t->rep); |
| 3747 | buffer_shutw(t->req); |
| 3748 | t->req->flags |= BF_WRITE_TIMEOUT; |
| 3749 | if (!t->req->cons->err_type) |
| 3750 | t->req->cons->err_type = SI_ET_QUEUE_TO; |
| 3751 | t->req->cons->state = SI_ST_CLO; |
| 3752 | return 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3753 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3754 | /* connection remains in queue, check if we have to abort it */ |
| 3755 | if ((t->req->flags & BF_SHUTW_NOW) || |
| 3756 | (t->rep->flags & BF_SHUTW) || |
| 3757 | ((t->req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */ |
| 3758 | (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { |
| 3759 | /* give up */ |
| 3760 | trace_term(t, TT_HTTP_SRV_1); |
| 3761 | t->req->wex = TICK_ETERNITY; |
| 3762 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3763 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3764 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3765 | buffer_shutr(t->rep); |
| 3766 | buffer_shutw(t->req); |
| 3767 | if (!t->req->cons->err_type) |
| 3768 | t->req->cons->err_type = SI_ET_QUEUE_ABRT; |
| 3769 | t->req->cons->state = SI_ST_CLO; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3770 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3771 | return 0; |
| 3772 | } |
| 3773 | /* The connection is not in the queue anymore */ |
| 3774 | t->req->cons->state = SI_ST_INI; |
| 3775 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3776 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3777 | /* we may get here from above */ |
| 3778 | if (t->req->cons->state == SI_ST_INI) { |
| 3779 | /* no connection in progress, we have to get a new one */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3780 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3781 | /* first, check if the connection has been aborted */ |
| 3782 | if ((t->req->flags & BF_SHUTW_NOW) || |
| 3783 | (t->rep->flags & BF_SHUTW) || |
| 3784 | ((t->req->flags & BF_SHUTR) && |
| 3785 | (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] | 3786 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3787 | trace_term(t, TT_HTTP_SRV_1); |
| 3788 | t->req->wex = TICK_ETERNITY; |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3789 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3790 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3791 | buffer_shutr(t->rep); |
| 3792 | buffer_shutw(t->req); |
| 3793 | if (!t->req->cons->err_type) |
| 3794 | t->req->cons->err_type = SI_ET_CONN_ABRT; |
| 3795 | t->req->cons->state = SI_ST_CLO; |
| 3796 | return 0; |
| 3797 | } |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3798 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3799 | /* try to get a server assigned */ |
| 3800 | if (srv_redispatch_connect(t) != 0) { |
| 3801 | /* we did not get any server, let's check the cause */ |
| 3802 | if (t->req->cons->state == SI_ST_QUE) { |
| 3803 | /* the connection was queued, that's OK */ |
| 3804 | return 0; |
| 3805 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3806 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3807 | trace_term(t, TT_HTTP_SRV_2); |
| 3808 | t->req->wex = TICK_ETERNITY; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3809 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3810 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3811 | buffer_shutr(t->rep); |
| 3812 | buffer_shutw(t->req); |
| 3813 | t->req->flags |= BF_WRITE_ERROR; |
| 3814 | if (!t->req->cons->err_type) |
| 3815 | t->req->cons->err_type = SI_ET_CONN_OTHER; |
| 3816 | t->req->cons->state = SI_ST_CLO; |
| 3817 | return 0; |
| 3818 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3819 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3820 | t->req->cons->state = SI_ST_ASS; |
| 3821 | /* Once the server is assigned, we have to return because |
| 3822 | * the caller might be interested in checking several |
| 3823 | * things before connecting. |
| 3824 | */ |
| 3825 | return 1; |
| 3826 | } |
| 3827 | return 0; |
| 3828 | } |
Krzysztof Piotr Oledzki | efe3b6f | 2008-05-23 23:49:32 +0200 | [diff] [blame] | 3829 | |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3830 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3831 | /* |
| 3832 | * This function tries to establish a connection to an assigned server. It also |
| 3833 | * performs connection retries. It may only be called with t->req->cons->state |
| 3834 | * in { SI_ST_ASS, SI_ST_CON }. It may also set the state to SI_ST_INI, |
| 3835 | * SI_ST_EST, or SI_ST_CLO. |
| 3836 | */ |
| 3837 | int stream_sock_connect_server(struct session *t) |
| 3838 | { |
| 3839 | if (t->req->cons->state == SI_ST_ASS) { |
| 3840 | /* server assigned to request, we have to try to connect now */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3841 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3842 | if (!srv_retryable_connect(t)) { |
| 3843 | /* we need to redispatch */ |
| 3844 | t->req->cons->state = SI_ST_INI; |
| 3845 | return 0; |
| 3846 | } |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3847 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3848 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 3849 | if (t->req->cons->state != SI_ST_CON) { |
| 3850 | /* it was an error */ |
| 3851 | trace_term(t, TT_HTTP_SRV_4); |
| 3852 | t->req->wex = TICK_ETERNITY; |
| 3853 | |
| 3854 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3855 | buffer_shutr(t->rep); |
| 3856 | buffer_shutw(t->req); |
| 3857 | t->req->flags |= BF_WRITE_ERROR; |
| 3858 | if (!t->req->cons->err_type) |
| 3859 | t->req->cons->err_type = SI_ET_CONN_OTHER; |
| 3860 | t->req->cons->state = SI_ST_CLO; |
| 3861 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3862 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3863 | /* We have a socket and switched to SI_ST_CON */ |
| 3864 | } |
| 3865 | |
| 3866 | /* we may also get here from above */ |
| 3867 | if (t->req->cons->state == SI_ST_CON) { |
| 3868 | /* connection in progress or just completed */ |
| 3869 | if (!tcp_connection_status(t)) |
| 3870 | return 0; |
| 3871 | } |
| 3872 | return 0; |
| 3873 | } |
| 3874 | |
| 3875 | |
| 3876 | /* |
| 3877 | * Tries to establish a connection to the server and associate it to the |
| 3878 | * 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] | 3879 | * 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] | 3880 | * returns zero, but may return 1 if it absolutely wants to be called again. |
| 3881 | */ |
| 3882 | int process_srv_conn(struct session *t) |
| 3883 | { |
| 3884 | DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n", |
| 3885 | now_ms, __FUNCTION__, |
| 3886 | cli_stnames[t->cli_state], |
| 3887 | t->rep->rex, t->req->wex, |
| 3888 | t->req->flags, t->rep->flags, |
| 3889 | t->req->l, t->rep->l); |
| 3890 | |
| 3891 | do { |
| 3892 | if (t->req->cons->state == SI_ST_INI || |
| 3893 | t->req->cons->state == SI_ST_TAR || |
| 3894 | t->req->cons->state == SI_ST_QUE) { |
| 3895 | /* try to assign a server */ |
| 3896 | if (!stream_sock_assign_server(t)) |
| 3897 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3898 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3899 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3900 | if (t->req->cons->state == SI_ST_ASS && |
| 3901 | t->srv && t->srv->rdr_len && t->flags & SN_REDIRECTABLE) { |
| 3902 | /* Server supporting redirection and it is possible. |
| 3903 | * Invalid requests are reported as such. It concerns all |
| 3904 | * the largest ones. |
| 3905 | */ |
| 3906 | struct http_txn *txn = &t->txn; |
| 3907 | struct chunk rdr; |
| 3908 | char *path; |
| 3909 | int len; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3910 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3911 | /* 1: create the response header */ |
| 3912 | rdr.len = strlen(HTTP_302); |
| 3913 | rdr.str = trash; |
| 3914 | memcpy(rdr.str, HTTP_302, rdr.len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3915 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3916 | /* 2: add the server's prefix */ |
| 3917 | if (rdr.len + t->srv->rdr_len > sizeof(trash)) |
| 3918 | goto cancel_redir; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3919 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3920 | memcpy(rdr.str + rdr.len, t->srv->rdr_pfx, t->srv->rdr_len); |
| 3921 | rdr.len += t->srv->rdr_len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3922 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3923 | /* 3: add the request URI */ |
| 3924 | path = http_get_path(txn); |
| 3925 | if (!path) |
| 3926 | goto cancel_redir; |
| 3927 | len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path; |
| 3928 | if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */ |
| 3929 | goto cancel_redir; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3930 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3931 | memcpy(rdr.str + rdr.len, path, len); |
| 3932 | rdr.len += len; |
| 3933 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 3934 | rdr.len += 4; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3935 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3936 | srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr); |
| 3937 | trace_term(t, TT_HTTP_SRV_3); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3938 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3939 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
| 3940 | if (t->srv) |
| 3941 | t->srv->cum_sess++; |
| 3942 | |
| 3943 | t->req->cons->state = SI_ST_CLO; |
| 3944 | return 0; |
| 3945 | cancel_redir: |
| 3946 | //txn->status = 400; |
| 3947 | //t->fe->failed_req++; |
| 3948 | //srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, |
| 3949 | // 400, error_message(t, HTTP_ERR_400)); |
| 3950 | trace_term(t, TT_HTTP_SRV_4); |
| 3951 | |
| 3952 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3953 | buffer_shutw(t->req); |
| 3954 | buffer_shutr(t->rep); |
| 3955 | if (!t->req->cons->err_type) |
| 3956 | t->req->cons->err_type = SI_ET_CONN_OTHER; |
| 3957 | t->req->cons->state = SI_ST_CLO; |
| 3958 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3959 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3960 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3961 | if (t->req->cons->state == SI_ST_CON || |
| 3962 | t->req->cons->state == SI_ST_ASS) { |
| 3963 | stream_sock_connect_server(t); |
| 3964 | } |
| 3965 | } while (t->req->cons->state != SI_ST_CLO && |
| 3966 | t->req->cons->state != SI_ST_CON && |
| 3967 | t->req->cons->state != SI_ST_EST); |
| 3968 | return 0; |
| 3969 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3970 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3971 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3972 | /* |
| 3973 | * 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] | 3974 | * 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] | 3975 | * be produced. It stops by itself by unsetting the BF_HIJACK flag from the |
| 3976 | * 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] | 3977 | * 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] | 3978 | * when it wants to stop sending data, otherwise 0. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3979 | */ |
| 3980 | int produce_content(struct session *s) |
| 3981 | { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3982 | if (s->data_source == DATA_SRC_NONE) { |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 3983 | buffer_stop_hijack(s->rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3984 | return 1; |
| 3985 | } |
| 3986 | else if (s->data_source == DATA_SRC_STATS) { |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3987 | /* dump server statistics */ |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 3988 | int ret = stats_dump_http(s, s->be->uri_auth); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 3989 | if (ret >= 0) |
| 3990 | return ret; |
| 3991 | /* -1 indicates an error */ |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3992 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3993 | |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 3994 | /* unknown data source or internal error */ |
| 3995 | s->txn.status = 500; |
| 3996 | client_retnclose(s, error_message(s, HTTP_ERR_500)); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3997 | trace_term(s, TT_HTTP_CNT_1); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 3998 | if (!(s->flags & SN_ERR_MASK)) |
| 3999 | s->flags |= SN_ERR_PRXCOND; |
| 4000 | if (!(s->flags & SN_FINST_MASK)) |
| 4001 | s->flags |= SN_FINST_R; |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 4002 | buffer_stop_hijack(s->rep); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 4003 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4004 | } |
| 4005 | |
| 4006 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4007 | /* Iterate the same filter through all request headers. |
| 4008 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4009 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 4010 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4011 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4012 | 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] | 4013 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4014 | char term; |
| 4015 | char *cur_ptr, *cur_end, *cur_next; |
| 4016 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4017 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4018 | struct hdr_idx_elem *cur_hdr; |
| 4019 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 4020 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4021 | last_hdr = 0; |
| 4022 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4023 | 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] | 4024 | old_idx = 0; |
| 4025 | |
| 4026 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4027 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4028 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4029 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4030 | (exp->action == ACT_ALLOW || |
| 4031 | exp->action == ACT_DENY || |
| 4032 | exp->action == ACT_TARPIT)) |
| 4033 | return 0; |
| 4034 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4035 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4036 | if (!cur_idx) |
| 4037 | break; |
| 4038 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4039 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4040 | cur_ptr = cur_next; |
| 4041 | cur_end = cur_ptr + cur_hdr->len; |
| 4042 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4043 | |
| 4044 | /* Now we have one header between cur_ptr and cur_end, |
| 4045 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4046 | */ |
| 4047 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4048 | /* The annoying part is that pattern matching needs |
| 4049 | * that we modify the contents to null-terminate all |
| 4050 | * strings before testing them. |
| 4051 | */ |
| 4052 | |
| 4053 | term = *cur_end; |
| 4054 | *cur_end = '\0'; |
| 4055 | |
| 4056 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4057 | switch (exp->action) { |
| 4058 | case ACT_SETBE: |
| 4059 | /* It is not possible to jump a second time. |
| 4060 | * FIXME: should we return an HTTP/500 here so that |
| 4061 | * the admin knows there's a problem ? |
| 4062 | */ |
| 4063 | if (t->be != t->fe) |
| 4064 | break; |
| 4065 | |
| 4066 | /* Swithing Proxy */ |
| 4067 | t->be = (struct proxy *) exp->replace; |
| 4068 | |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4069 | /* right now, the backend switch is not overly complicated |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4070 | * because we have associated req_cap and rsp_cap to the |
| 4071 | * frontend, and the beconn will be updated later. |
| 4072 | */ |
| 4073 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 4074 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 4075 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 4076 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4077 | last_hdr = 1; |
| 4078 | break; |
| 4079 | |
| 4080 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4081 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4082 | last_hdr = 1; |
| 4083 | break; |
| 4084 | |
| 4085 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4086 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4087 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4088 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4089 | break; |
| 4090 | |
| 4091 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4092 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4093 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4094 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4095 | break; |
| 4096 | |
| 4097 | case ACT_REPLACE: |
| 4098 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4099 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 4100 | /* FIXME: if the user adds a newline in the replacement, the |
| 4101 | * index will not be recalculated for now, and the new line |
| 4102 | * will not be counted as a new header. |
| 4103 | */ |
| 4104 | |
| 4105 | cur_end += delta; |
| 4106 | cur_next += delta; |
| 4107 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4108 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4109 | break; |
| 4110 | |
| 4111 | case ACT_REMOVE: |
| 4112 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 4113 | cur_next += delta; |
| 4114 | |
| 4115 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4116 | txn->req.eoh += delta; |
| 4117 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4118 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4119 | cur_hdr->len = 0; |
| 4120 | cur_end = NULL; /* null-term has been rewritten */ |
| 4121 | break; |
| 4122 | |
| 4123 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4124 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4125 | if (cur_end) |
| 4126 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4127 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4128 | /* keep the link from this header to next one in case of later |
| 4129 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4130 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4131 | old_idx = cur_idx; |
| 4132 | } |
| 4133 | return 0; |
| 4134 | } |
| 4135 | |
| 4136 | |
| 4137 | /* Apply the filter to the request line. |
| 4138 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 4139 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4140 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 4141 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4142 | */ |
| 4143 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 4144 | { |
| 4145 | char term; |
| 4146 | char *cur_ptr, *cur_end; |
| 4147 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4148 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4149 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4150 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4151 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4152 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4153 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4154 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4155 | (exp->action == ACT_ALLOW || |
| 4156 | exp->action == ACT_DENY || |
| 4157 | exp->action == ACT_TARPIT)) |
| 4158 | return 0; |
| 4159 | else if (exp->action == ACT_REMOVE) |
| 4160 | return 0; |
| 4161 | |
| 4162 | done = 0; |
| 4163 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4164 | 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] | 4165 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4166 | |
| 4167 | /* Now we have the request line between cur_ptr and cur_end */ |
| 4168 | |
| 4169 | /* The annoying part is that pattern matching needs |
| 4170 | * that we modify the contents to null-terminate all |
| 4171 | * strings before testing them. |
| 4172 | */ |
| 4173 | |
| 4174 | term = *cur_end; |
| 4175 | *cur_end = '\0'; |
| 4176 | |
| 4177 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4178 | switch (exp->action) { |
| 4179 | case ACT_SETBE: |
| 4180 | /* It is not possible to jump a second time. |
| 4181 | * FIXME: should we return an HTTP/500 here so that |
| 4182 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4183 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4184 | if (t->be != t->fe) |
| 4185 | break; |
| 4186 | |
| 4187 | /* Swithing Proxy */ |
| 4188 | t->be = (struct proxy *) exp->replace; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4189 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4190 | /* right now, the backend switch is not too much complicated |
| 4191 | * because we have associated req_cap and rsp_cap to the |
| 4192 | * frontend, and the beconn will be updated later. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4193 | */ |
| 4194 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 4195 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 4196 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 4197 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4198 | done = 1; |
| 4199 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4200 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4201 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4202 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4203 | done = 1; |
| 4204 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4205 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4206 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4207 | txn->flags |= TX_CLDENY; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4208 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4209 | done = 1; |
| 4210 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4211 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4212 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4213 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4214 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4215 | done = 1; |
| 4216 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4217 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4218 | case ACT_REPLACE: |
| 4219 | *cur_end = term; /* restore the string terminator */ |
| 4220 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4221 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 4222 | /* FIXME: if the user adds a newline in the replacement, the |
| 4223 | * index will not be recalculated for now, and the new line |
| 4224 | * will not be counted as a new header. |
| 4225 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4226 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4227 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4228 | cur_end += delta; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4229 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4230 | 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] | 4231 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4232 | HTTP_MSG_RQMETH, |
| 4233 | cur_ptr, cur_end + 1, |
| 4234 | NULL, NULL); |
| 4235 | if (unlikely(!cur_end)) |
| 4236 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4237 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4238 | /* we have a full request and we know that we have either a CR |
| 4239 | * or an LF at <ptr>. |
| 4240 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4241 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 4242 | 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] | 4243 | /* there is no point trying this regex on headers */ |
| 4244 | return 1; |
| 4245 | } |
| 4246 | } |
| 4247 | *cur_end = term; /* restore the string terminator */ |
| 4248 | return done; |
| 4249 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 4250 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4251 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4252 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4253 | /* |
| 4254 | * Apply all the req filters <exp> to all headers in buffer <req> of session <t>. |
| 4255 | * 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] | 4256 | * unparsable request. Since it can manage the switch to another backend, it |
| 4257 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4258 | */ |
| 4259 | int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 4260 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4261 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4262 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4263 | while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4264 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4265 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4266 | /* |
| 4267 | * The interleaving of transformations and verdicts |
| 4268 | * makes it difficult to decide to continue or stop |
| 4269 | * the evaluation. |
| 4270 | */ |
| 4271 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4272 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4273 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 4274 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) { |
| 4275 | exp = exp->next; |
| 4276 | continue; |
| 4277 | } |
| 4278 | |
| 4279 | /* Apply the filter to the request line. */ |
| 4280 | ret = apply_filter_to_req_line(t, req, exp); |
| 4281 | if (unlikely(ret < 0)) |
| 4282 | return -1; |
| 4283 | |
| 4284 | if (likely(ret == 0)) { |
| 4285 | /* The filter did not match the request, it can be |
| 4286 | * iterated through all headers. |
| 4287 | */ |
| 4288 | apply_filter_to_req_headers(t, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4289 | } |
| 4290 | exp = exp->next; |
| 4291 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4292 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4293 | } |
| 4294 | |
| 4295 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4296 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4297 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 4298 | * Manage client-side cookie. It can impact performance by about 2% so it is |
| 4299 | * desirable to call it only when needed. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4300 | */ |
| 4301 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 4302 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4303 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4304 | char *p1, *p2, *p3, *p4; |
| 4305 | char *del_colon, *del_cookie, *colon; |
| 4306 | int app_cookies; |
| 4307 | |
| 4308 | appsess *asession_temp = NULL; |
| 4309 | appsess local_asession; |
| 4310 | |
| 4311 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4312 | int cur_idx, old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4313 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4314 | /* Iterate through the headers. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4315 | * we start with the start line. |
| 4316 | */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 4317 | old_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4318 | 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] | 4319 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4320 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4321 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4322 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4323 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4324 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4325 | cur_ptr = cur_next; |
| 4326 | cur_end = cur_ptr + cur_hdr->len; |
| 4327 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4328 | |
| 4329 | /* We have one full header between cur_ptr and cur_end, and the |
| 4330 | * next header starts at cur_next. We're only interested in |
| 4331 | * "Cookie:" headers. |
| 4332 | */ |
| 4333 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4334 | val = http_header_match2(cur_ptr, cur_end, "Cookie", 6); |
| 4335 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4336 | old_idx = cur_idx; |
| 4337 | continue; |
| 4338 | } |
| 4339 | |
| 4340 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 4341 | * attributes whose name begin with a '$', and associate them with |
| 4342 | * the right cookie, if we want to delete this cookie. |
| 4343 | * So there are 3 cases for each cookie read : |
| 4344 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 4345 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 4346 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 4347 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 4348 | * "special" cookie. |
| 4349 | * At the end of loop, if a "special" cookie remains, we may have to |
| 4350 | * remove it. If no application cookie persists in the header, we |
| 4351 | * *MUST* delete it |
| 4352 | */ |
| 4353 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4354 | colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4355 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4356 | /* del_cookie == NULL => nothing to be deleted */ |
| 4357 | del_colon = del_cookie = NULL; |
| 4358 | app_cookies = 0; |
| 4359 | |
| 4360 | while (p1 < cur_end) { |
| 4361 | /* skip spaces and colons, but keep an eye on these ones */ |
| 4362 | while (p1 < cur_end) { |
| 4363 | if (*p1 == ';' || *p1 == ',') |
| 4364 | colon = p1; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4365 | else if (!isspace((unsigned char)*p1)) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4366 | break; |
| 4367 | p1++; |
| 4368 | } |
| 4369 | |
| 4370 | if (p1 == cur_end) |
| 4371 | break; |
| 4372 | |
| 4373 | /* p1 is at the beginning of the cookie name */ |
| 4374 | p2 = p1; |
| 4375 | while (p2 < cur_end && *p2 != '=') |
| 4376 | p2++; |
| 4377 | |
| 4378 | if (p2 == cur_end) |
| 4379 | break; |
| 4380 | |
| 4381 | p3 = p2 + 1; /* skips the '=' sign */ |
| 4382 | if (p3 == cur_end) |
| 4383 | break; |
| 4384 | |
| 4385 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4386 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4387 | p4++; |
| 4388 | |
| 4389 | /* here, we have the cookie name between p1 and p2, |
| 4390 | * and its value between p3 and p4. |
| 4391 | * we can process it : |
| 4392 | * |
| 4393 | * Cookie: NAME=VALUE; |
| 4394 | * | || || | |
| 4395 | * | || || +--> p4 |
| 4396 | * | || |+-------> p3 |
| 4397 | * | || +--------> p2 |
| 4398 | * | |+------------> p1 |
| 4399 | * | +-------------> colon |
| 4400 | * +--------------------> cur_ptr |
| 4401 | */ |
| 4402 | |
| 4403 | if (*p1 == '$') { |
| 4404 | /* skip this one */ |
| 4405 | } |
| 4406 | else { |
| 4407 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4408 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4409 | txn->cli_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4410 | (p4 - p1 >= t->fe->capture_namelen) && |
| 4411 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4412 | int log_len = p4 - p1; |
| 4413 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 4414 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4415 | Alert("HTTP logging : out of memory.\n"); |
| 4416 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4417 | if (log_len > t->fe->capture_len) |
| 4418 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4419 | memcpy(txn->cli_cookie, p1, log_len); |
| 4420 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4421 | } |
| 4422 | } |
| 4423 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4424 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 4425 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4426 | /* Cool... it's the right one */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4427 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4428 | char *delim; |
| 4429 | |
| 4430 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 4431 | * have the server ID betweek p3 and delim, and the original cookie between |
| 4432 | * delim+1 and p4. Otherwise, delim==p4 : |
| 4433 | * |
| 4434 | * Cookie: NAME=SRV~VALUE; |
| 4435 | * | || || | | |
| 4436 | * | || || | +--> p4 |
| 4437 | * | || || +--------> delim |
| 4438 | * | || |+-----------> p3 |
| 4439 | * | || +------------> p2 |
| 4440 | * | |+----------------> p1 |
| 4441 | * | +-----------------> colon |
| 4442 | * +------------------------> cur_ptr |
| 4443 | */ |
| 4444 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4445 | if (t->be->options & PR_O_COOK_PFX) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4446 | for (delim = p3; delim < p4; delim++) |
| 4447 | if (*delim == COOKIE_DELIM) |
| 4448 | break; |
| 4449 | } |
| 4450 | else |
| 4451 | delim = p4; |
| 4452 | |
| 4453 | |
| 4454 | /* Here, we'll look for the first running server which supports the cookie. |
| 4455 | * This allows to share a same cookie between several servers, for example |
| 4456 | * to dedicate backup servers to specific servers only. |
| 4457 | * However, to prevent clients from sticking to cookie-less backup server |
| 4458 | * when they have incidentely learned an empty cookie, we simply ignore |
| 4459 | * empty cookies and mark them as invalid. |
| 4460 | */ |
| 4461 | if (delim == p3) |
| 4462 | srv = NULL; |
| 4463 | |
| 4464 | while (srv) { |
Willy Tarreau | 92f2ab1 | 2007-02-02 22:14:47 +0100 | [diff] [blame] | 4465 | if (srv->cookie && (srv->cklen == delim - p3) && |
| 4466 | !memcmp(p3, srv->cookie, delim - p3)) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4467 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4468 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4469 | txn->flags &= ~TX_CK_MASK; |
| 4470 | txn->flags |= TX_CK_VALID; |
| 4471 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4472 | t->srv = srv; |
| 4473 | break; |
| 4474 | } else { |
| 4475 | /* we found a server, but it's down */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4476 | txn->flags &= ~TX_CK_MASK; |
| 4477 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4478 | } |
| 4479 | } |
| 4480 | srv = srv->next; |
| 4481 | } |
| 4482 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4483 | if (!srv && !(txn->flags & TX_CK_DOWN)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4484 | /* no server matched this cookie */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4485 | txn->flags &= ~TX_CK_MASK; |
| 4486 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4487 | } |
| 4488 | |
| 4489 | /* depending on the cookie mode, we may have to either : |
| 4490 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 4491 | * the server never sees it ; |
| 4492 | * - remove the server id from the cookie value, and tag the cookie as an |
| 4493 | * application cookie so that it does not get accidentely removed later, |
| 4494 | * if we're in cookie prefix mode |
| 4495 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4496 | if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4497 | int delta; /* negative */ |
| 4498 | |
| 4499 | delta = buffer_replace2(req, p3, delim + 1, NULL, 0); |
| 4500 | p4 += delta; |
| 4501 | cur_end += delta; |
| 4502 | cur_next += delta; |
| 4503 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4504 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4505 | |
| 4506 | del_cookie = del_colon = NULL; |
| 4507 | app_cookies++; /* protect the header from deletion */ |
| 4508 | } |
| 4509 | else if (del_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4510 | (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] | 4511 | del_cookie = p1; |
| 4512 | del_colon = colon; |
| 4513 | } |
| 4514 | } else { |
| 4515 | /* now we know that we must keep this cookie since it's |
| 4516 | * not ours. But if we wanted to delete our cookie |
| 4517 | * earlier, we cannot remove the complete header, but we |
| 4518 | * can remove the previous block itself. |
| 4519 | */ |
| 4520 | app_cookies++; |
| 4521 | |
| 4522 | if (del_cookie != NULL) { |
| 4523 | int delta; /* negative */ |
| 4524 | |
| 4525 | delta = buffer_replace2(req, del_cookie, p1, NULL, 0); |
| 4526 | p4 += delta; |
| 4527 | cur_end += delta; |
| 4528 | cur_next += delta; |
| 4529 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4530 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4531 | del_cookie = del_colon = NULL; |
| 4532 | } |
| 4533 | } |
| 4534 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4535 | if ((t->be->appsession_name != NULL) && |
| 4536 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4537 | /* first, let's see if the cookie is our appcookie*/ |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4538 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4539 | /* Cool... it's the right one */ |
| 4540 | |
| 4541 | asession_temp = &local_asession; |
| 4542 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4543 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4544 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 4545 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 4546 | return; |
| 4547 | } |
| 4548 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4549 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 4550 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4551 | asession_temp->serverid = NULL; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4552 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4553 | /* only do insert, if lookup fails */ |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4554 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 4555 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4556 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4557 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4558 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4559 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 4560 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 4561 | return; |
| 4562 | } |
| 4563 | |
| 4564 | asession_temp->sessid = local_asession.sessid; |
| 4565 | asession_temp->serverid = local_asession.serverid; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4566 | asession_temp->request_count = 0; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4567 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4568 | } else { |
| 4569 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4570 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4571 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4572 | if (asession_temp->serverid == NULL) { |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4573 | /* TODO redispatch request */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4574 | Alert("Found Application Session without matching server.\n"); |
| 4575 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4576 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4577 | while (srv) { |
| 4578 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4579 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4580 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4581 | txn->flags &= ~TX_CK_MASK; |
| 4582 | txn->flags |= TX_CK_VALID; |
| 4583 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4584 | t->srv = srv; |
| 4585 | break; |
| 4586 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4587 | txn->flags &= ~TX_CK_MASK; |
| 4588 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4589 | } |
| 4590 | } |
| 4591 | srv = srv->next; |
| 4592 | }/* end while(srv) */ |
| 4593 | }/* end else if server == NULL */ |
| 4594 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 4595 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4596 | asession_temp->request_count++; |
| 4597 | #if defined(DEBUG_HASH) |
| 4598 | Alert("manage_client_side_cookies\n"); |
| 4599 | appsession_hash_dump(&(t->be->htbl_proxy)); |
| 4600 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4601 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 4602 | } |
| 4603 | |
| 4604 | /* we'll have to look for another cookie ... */ |
| 4605 | p1 = p4; |
| 4606 | } /* while (p1 < cur_end) */ |
| 4607 | |
| 4608 | /* There's no more cookie on this line. |
| 4609 | * We may have marked the last one(s) for deletion. |
| 4610 | * We must do this now in two ways : |
| 4611 | * - if there is no app cookie, we simply delete the header ; |
| 4612 | * - if there are app cookies, we must delete the end of the |
| 4613 | * string properly, including the colon/semi-colon before |
| 4614 | * the cookie name. |
| 4615 | */ |
| 4616 | if (del_cookie != NULL) { |
| 4617 | int delta; |
| 4618 | if (app_cookies) { |
| 4619 | delta = buffer_replace2(req, del_colon, cur_end, NULL, 0); |
| 4620 | cur_end = del_colon; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4621 | cur_hdr->len += delta; |
| 4622 | } else { |
| 4623 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4624 | |
| 4625 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4626 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4627 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4628 | cur_hdr->len = 0; |
| 4629 | } |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 4630 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4631 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4632 | } |
| 4633 | |
| 4634 | /* keep the link from this header to next one */ |
| 4635 | old_idx = cur_idx; |
| 4636 | } /* end of cookie processing on this header */ |
| 4637 | } |
| 4638 | |
| 4639 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4640 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 4641 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 4642 | */ |
| 4643 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4644 | { |
| 4645 | char term; |
| 4646 | char *cur_ptr, *cur_end, *cur_next; |
| 4647 | int cur_idx, old_idx, last_hdr; |
| 4648 | struct http_txn *txn = &t->txn; |
| 4649 | struct hdr_idx_elem *cur_hdr; |
| 4650 | int len, delta; |
| 4651 | |
| 4652 | last_hdr = 0; |
| 4653 | |
| 4654 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4655 | old_idx = 0; |
| 4656 | |
| 4657 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4658 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4659 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4660 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4661 | (exp->action == ACT_ALLOW || |
| 4662 | exp->action == ACT_DENY)) |
| 4663 | return 0; |
| 4664 | |
| 4665 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 4666 | if (!cur_idx) |
| 4667 | break; |
| 4668 | |
| 4669 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4670 | cur_ptr = cur_next; |
| 4671 | cur_end = cur_ptr + cur_hdr->len; |
| 4672 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4673 | |
| 4674 | /* Now we have one header between cur_ptr and cur_end, |
| 4675 | * and the next header starts at cur_next. |
| 4676 | */ |
| 4677 | |
| 4678 | /* The annoying part is that pattern matching needs |
| 4679 | * that we modify the contents to null-terminate all |
| 4680 | * strings before testing them. |
| 4681 | */ |
| 4682 | |
| 4683 | term = *cur_end; |
| 4684 | *cur_end = '\0'; |
| 4685 | |
| 4686 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4687 | switch (exp->action) { |
| 4688 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4689 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4690 | last_hdr = 1; |
| 4691 | break; |
| 4692 | |
| 4693 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4694 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4695 | last_hdr = 1; |
| 4696 | break; |
| 4697 | |
| 4698 | case ACT_REPLACE: |
| 4699 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4700 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 4701 | /* FIXME: if the user adds a newline in the replacement, the |
| 4702 | * index will not be recalculated for now, and the new line |
| 4703 | * will not be counted as a new header. |
| 4704 | */ |
| 4705 | |
| 4706 | cur_end += delta; |
| 4707 | cur_next += delta; |
| 4708 | cur_hdr->len += delta; |
| 4709 | txn->rsp.eoh += delta; |
| 4710 | break; |
| 4711 | |
| 4712 | case ACT_REMOVE: |
| 4713 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 4714 | cur_next += delta; |
| 4715 | |
| 4716 | /* FIXME: this should be a separate function */ |
| 4717 | txn->rsp.eoh += delta; |
| 4718 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4719 | txn->hdr_idx.used--; |
| 4720 | cur_hdr->len = 0; |
| 4721 | cur_end = NULL; /* null-term has been rewritten */ |
| 4722 | break; |
| 4723 | |
| 4724 | } |
| 4725 | } |
| 4726 | if (cur_end) |
| 4727 | *cur_end = term; /* restore the string terminator */ |
| 4728 | |
| 4729 | /* keep the link from this header to next one in case of later |
| 4730 | * removal of next header. |
| 4731 | */ |
| 4732 | old_idx = cur_idx; |
| 4733 | } |
| 4734 | return 0; |
| 4735 | } |
| 4736 | |
| 4737 | |
| 4738 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 4739 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 4740 | * or -1 if a replacement resulted in an invalid status line. |
| 4741 | */ |
| 4742 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4743 | { |
| 4744 | char term; |
| 4745 | char *cur_ptr, *cur_end; |
| 4746 | int done; |
| 4747 | struct http_txn *txn = &t->txn; |
| 4748 | int len, delta; |
| 4749 | |
| 4750 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4751 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4752 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4753 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4754 | (exp->action == ACT_ALLOW || |
| 4755 | exp->action == ACT_DENY)) |
| 4756 | return 0; |
| 4757 | else if (exp->action == ACT_REMOVE) |
| 4758 | return 0; |
| 4759 | |
| 4760 | done = 0; |
| 4761 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4762 | 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] | 4763 | cur_end = cur_ptr + txn->rsp.sl.rq.l; |
| 4764 | |
| 4765 | /* Now we have the status line between cur_ptr and cur_end */ |
| 4766 | |
| 4767 | /* The annoying part is that pattern matching needs |
| 4768 | * that we modify the contents to null-terminate all |
| 4769 | * strings before testing them. |
| 4770 | */ |
| 4771 | |
| 4772 | term = *cur_end; |
| 4773 | *cur_end = '\0'; |
| 4774 | |
| 4775 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4776 | switch (exp->action) { |
| 4777 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4778 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4779 | done = 1; |
| 4780 | break; |
| 4781 | |
| 4782 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4783 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4784 | done = 1; |
| 4785 | break; |
| 4786 | |
| 4787 | case ACT_REPLACE: |
| 4788 | *cur_end = term; /* restore the string terminator */ |
| 4789 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4790 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 4791 | /* FIXME: if the user adds a newline in the replacement, the |
| 4792 | * index will not be recalculated for now, and the new line |
| 4793 | * will not be counted as a new header. |
| 4794 | */ |
| 4795 | |
| 4796 | txn->rsp.eoh += delta; |
| 4797 | cur_end += delta; |
| 4798 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4799 | 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] | 4800 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 4801 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4802 | cur_ptr, cur_end + 1, |
| 4803 | NULL, NULL); |
| 4804 | if (unlikely(!cur_end)) |
| 4805 | return -1; |
| 4806 | |
| 4807 | /* we have a full respnse and we know that we have either a CR |
| 4808 | * or an LF at <ptr>. |
| 4809 | */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4810 | 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] | 4811 | hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r'); |
| 4812 | /* there is no point trying this regex on headers */ |
| 4813 | return 1; |
| 4814 | } |
| 4815 | } |
| 4816 | *cur_end = term; /* restore the string terminator */ |
| 4817 | return done; |
| 4818 | } |
| 4819 | |
| 4820 | |
| 4821 | |
| 4822 | /* |
| 4823 | * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>. |
| 4824 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 4825 | * unparsable response. |
| 4826 | */ |
| 4827 | int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4828 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4829 | struct http_txn *txn = &t->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4830 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4831 | while (exp && !(txn->flags & TX_SVDENY)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4832 | int ret; |
| 4833 | |
| 4834 | /* |
| 4835 | * The interleaving of transformations and verdicts |
| 4836 | * makes it difficult to decide to continue or stop |
| 4837 | * the evaluation. |
| 4838 | */ |
| 4839 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4840 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4841 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 4842 | exp->action == ACT_PASS)) { |
| 4843 | exp = exp->next; |
| 4844 | continue; |
| 4845 | } |
| 4846 | |
| 4847 | /* Apply the filter to the status line. */ |
| 4848 | ret = apply_filter_to_sts_line(t, rtr, exp); |
| 4849 | if (unlikely(ret < 0)) |
| 4850 | return -1; |
| 4851 | |
| 4852 | if (likely(ret == 0)) { |
| 4853 | /* The filter did not match the response, it can be |
| 4854 | * iterated through all headers. |
| 4855 | */ |
| 4856 | apply_filter_to_resp_headers(t, rtr, exp); |
| 4857 | } |
| 4858 | exp = exp->next; |
| 4859 | } |
| 4860 | return 0; |
| 4861 | } |
| 4862 | |
| 4863 | |
| 4864 | |
| 4865 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 4866 | * Manage server-side cookies. It can impact performance by about 2% so it is |
| 4867 | * desirable to call it only when needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4868 | */ |
| 4869 | void manage_server_side_cookies(struct session *t, struct buffer *rtr) |
| 4870 | { |
| 4871 | struct http_txn *txn = &t->txn; |
| 4872 | char *p1, *p2, *p3, *p4; |
| 4873 | |
| 4874 | appsess *asession_temp = NULL; |
| 4875 | appsess local_asession; |
| 4876 | |
| 4877 | char *cur_ptr, *cur_end, *cur_next; |
| 4878 | int cur_idx, old_idx, delta; |
| 4879 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4880 | /* Iterate through the headers. |
| 4881 | * we start with the start line. |
| 4882 | */ |
| 4883 | old_idx = 0; |
| 4884 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4885 | |
| 4886 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 4887 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4888 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4889 | |
| 4890 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4891 | cur_ptr = cur_next; |
| 4892 | cur_end = cur_ptr + cur_hdr->len; |
| 4893 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4894 | |
| 4895 | /* We have one full header between cur_ptr and cur_end, and the |
| 4896 | * next header starts at cur_next. We're only interested in |
| 4897 | * "Cookie:" headers. |
| 4898 | */ |
| 4899 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4900 | val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10); |
| 4901 | if (!val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4902 | old_idx = cur_idx; |
| 4903 | continue; |
| 4904 | } |
| 4905 | |
| 4906 | /* 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] | 4907 | txn->flags |= TX_SCK_ANY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4908 | |
| 4909 | |
| 4910 | /* maybe we only wanted to see if there was a set-cookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4911 | if (t->be->cookie_name == NULL && |
| 4912 | t->be->appsession_name == NULL && |
| 4913 | t->be->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4914 | return; |
| 4915 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4916 | p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4917 | |
| 4918 | 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] | 4919 | if (p1 == cur_end || *p1 == ';') /* end of cookie */ |
| 4920 | break; |
| 4921 | |
| 4922 | /* p1 is at the beginning of the cookie name */ |
| 4923 | p2 = p1; |
| 4924 | |
| 4925 | while (p2 < cur_end && *p2 != '=' && *p2 != ';') |
| 4926 | p2++; |
| 4927 | |
| 4928 | if (p2 == cur_end || *p2 == ';') /* next cookie */ |
| 4929 | break; |
| 4930 | |
| 4931 | p3 = p2 + 1; /* skip the '=' sign */ |
| 4932 | if (p3 == cur_end) |
| 4933 | break; |
| 4934 | |
| 4935 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4936 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';') |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4937 | p4++; |
| 4938 | |
| 4939 | /* here, we have the cookie name between p1 and p2, |
| 4940 | * and its value between p3 and p4. |
| 4941 | * we can process it. |
| 4942 | */ |
| 4943 | |
| 4944 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4945 | if (t->be->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4946 | txn->srv_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4947 | (p4 - p1 >= t->be->capture_namelen) && |
| 4948 | memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4949 | int log_len = p4 - p1; |
| 4950 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 4951 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4952 | Alert("HTTP logging : out of memory.\n"); |
| 4953 | } |
| 4954 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4955 | if (log_len > t->be->capture_len) |
| 4956 | log_len = t->be->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4957 | memcpy(txn->srv_cookie, p1, log_len); |
| 4958 | txn->srv_cookie[log_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4959 | } |
| 4960 | |
| 4961 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4962 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 4963 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4964 | /* Cool... it's the right one */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4965 | txn->flags |= TX_SCK_SEEN; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4966 | |
| 4967 | /* If the cookie is in insert mode on a known server, we'll delete |
| 4968 | * this occurrence because we'll insert another one later. |
| 4969 | * We'll delete it too if the "indirect" option is set and we're in |
| 4970 | * a direct access. */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4971 | if (((t->srv) && (t->be->options & PR_O_COOK_INS)) || |
| 4972 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4973 | /* this header must be deleted */ |
| 4974 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 4975 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4976 | txn->hdr_idx.used--; |
| 4977 | cur_hdr->len = 0; |
| 4978 | cur_next += delta; |
| 4979 | txn->rsp.eoh += delta; |
| 4980 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4981 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4982 | } |
| 4983 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4984 | (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4985 | /* replace bytes p3->p4 with the cookie name associated |
| 4986 | * with this server since we know it. |
| 4987 | */ |
| 4988 | delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen); |
| 4989 | cur_hdr->len += delta; |
| 4990 | cur_next += delta; |
| 4991 | txn->rsp.eoh += delta; |
| 4992 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4993 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4994 | } |
| 4995 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4996 | (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4997 | /* insert the cookie name associated with this server |
| 4998 | * before existing cookie, and insert a delimitor between them.. |
| 4999 | */ |
| 5000 | delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1); |
| 5001 | cur_hdr->len += delta; |
| 5002 | cur_next += delta; |
| 5003 | txn->rsp.eoh += delta; |
| 5004 | |
| 5005 | p3[t->srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5006 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5007 | } |
| 5008 | } |
| 5009 | /* next, let's see if the cookie is our appcookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5010 | else if ((t->be->appsession_name != NULL) && |
| 5011 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5012 | |
| 5013 | /* Cool... it's the right one */ |
| 5014 | |
| 5015 | size_t server_id_len = strlen(t->srv->id) + 1; |
| 5016 | asession_temp = &local_asession; |
| 5017 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5018 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5019 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5020 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5021 | return; |
| 5022 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5023 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 5024 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5025 | asession_temp->serverid = NULL; |
| 5026 | |
| 5027 | /* only do insert, if lookup fails */ |
Ryan Warnick | 6d0b1fa | 2008-02-17 11:24:35 +0100 | [diff] [blame] | 5028 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 5029 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5030 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5031 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 5032 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 5033 | return; |
| 5034 | } |
| 5035 | asession_temp->sessid = local_asession.sessid; |
| 5036 | asession_temp->serverid = local_asession.serverid; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5037 | asession_temp->request_count = 0; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5038 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
| 5039 | } else { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5040 | /* free wasted memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5041 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5042 | } |
| 5043 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5044 | if (asession_temp->serverid == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5045 | if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5046 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5047 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5048 | return; |
| 5049 | } |
| 5050 | asession_temp->serverid[0] = '\0'; |
| 5051 | } |
| 5052 | |
| 5053 | if (asession_temp->serverid[0] == '\0') |
| 5054 | memcpy(asession_temp->serverid, t->srv->id, server_id_len); |
| 5055 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 5056 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5057 | asession_temp->request_count++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5058 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5059 | Alert("manage_server_side_cookies\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5060 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5061 | #endif |
| 5062 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 5063 | break; /* we don't want to loop again since there cannot be another cookie on the same line */ |
| 5064 | } /* we're now at the end of the cookie value */ |
| 5065 | |
| 5066 | /* keep the link from this header to next one */ |
| 5067 | old_idx = cur_idx; |
| 5068 | } /* end of cookie processing on this header */ |
| 5069 | } |
| 5070 | |
| 5071 | |
| 5072 | |
| 5073 | /* |
| 5074 | * Check if response is cacheable or not. Updates t->flags. |
| 5075 | */ |
| 5076 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 5077 | { |
| 5078 | struct http_txn *txn = &t->txn; |
| 5079 | char *p1, *p2; |
| 5080 | |
| 5081 | char *cur_ptr, *cur_end, *cur_next; |
| 5082 | int cur_idx; |
| 5083 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 5084 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5085 | return; |
| 5086 | |
| 5087 | /* Iterate through the headers. |
| 5088 | * we start with the start line. |
| 5089 | */ |
| 5090 | cur_idx = 0; |
| 5091 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 5092 | |
| 5093 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 5094 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5095 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5096 | |
| 5097 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 5098 | cur_ptr = cur_next; |
| 5099 | cur_end = cur_ptr + cur_hdr->len; |
| 5100 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5101 | |
| 5102 | /* We have one full header between cur_ptr and cur_end, and the |
| 5103 | * next header starts at cur_next. We're only interested in |
| 5104 | * "Cookie:" headers. |
| 5105 | */ |
| 5106 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5107 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 5108 | if (val) { |
| 5109 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 5110 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 5111 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 5112 | return; |
| 5113 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5114 | } |
| 5115 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5116 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 5117 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5118 | continue; |
| 5119 | |
| 5120 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 5121 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5122 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5123 | |
| 5124 | if (p1 >= cur_end) /* no more info */ |
| 5125 | continue; |
| 5126 | |
| 5127 | /* p1 is at the beginning of the value */ |
| 5128 | p2 = p1; |
| 5129 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 5130 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5131 | p2++; |
| 5132 | |
| 5133 | /* we have a complete value between p1 and p2 */ |
| 5134 | if (p2 < cur_end && *p2 == '=') { |
| 5135 | /* we have something of the form no-cache="set-cookie" */ |
| 5136 | if ((cur_end - p1 >= 21) && |
| 5137 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 5138 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5139 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5140 | continue; |
| 5141 | } |
| 5142 | |
| 5143 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 5144 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 5145 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 5146 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 5147 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5148 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5149 | return; |
| 5150 | } |
| 5151 | |
| 5152 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5153 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5154 | continue; |
| 5155 | } |
| 5156 | } |
| 5157 | } |
| 5158 | |
| 5159 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5160 | /* |
| 5161 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 5162 | * If the server is found, it's assigned to the session. |
| 5163 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5164 | 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] | 5165 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5166 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5167 | appsess *asession_temp = NULL; |
| 5168 | appsess local_asession; |
| 5169 | char *request_line; |
| 5170 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5171 | if (t->be->appsession_name == NULL || |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 5172 | (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) || |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5173 | (request_line = memchr(begin, ';', len)) == NULL || |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5174 | ((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] | 5175 | return; |
| 5176 | |
| 5177 | /* skip ';' */ |
| 5178 | request_line++; |
| 5179 | |
| 5180 | /* look if we have a jsessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5181 | 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] | 5182 | return; |
| 5183 | |
| 5184 | /* skip jsessionid= */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5185 | request_line += t->be->appsession_name_len + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5186 | |
| 5187 | /* First try if we already have an appsession */ |
| 5188 | asession_temp = &local_asession; |
| 5189 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5190 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5191 | Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n"); |
| 5192 | send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n"); |
| 5193 | return; |
| 5194 | } |
| 5195 | |
| 5196 | /* Copy the sessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5197 | memcpy(asession_temp->sessid, request_line, t->be->appsession_len); |
| 5198 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5199 | asession_temp->serverid = NULL; |
| 5200 | |
| 5201 | /* only do insert, if lookup fails */ |
Ryan Warnick | 6d0b1fa | 2008-02-17 11:24:35 +0100 | [diff] [blame] | 5202 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 5203 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5204 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5205 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5206 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5207 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 5208 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 5209 | return; |
| 5210 | } |
| 5211 | asession_temp->sessid = local_asession.sessid; |
| 5212 | asession_temp->serverid = local_asession.serverid; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5213 | asession_temp->request_count=0; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5214 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5215 | } |
| 5216 | else { |
| 5217 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5218 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5219 | } |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5220 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 5221 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5222 | asession_temp->request_count++; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5223 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5224 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5225 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5226 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5227 | #endif |
| 5228 | if (asession_temp->serverid == NULL) { |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5229 | /* TODO redispatch request */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5230 | Alert("Found Application Session without matching server.\n"); |
| 5231 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5232 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5233 | while (srv) { |
| 5234 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5235 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5236 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5237 | txn->flags &= ~TX_CK_MASK; |
| 5238 | txn->flags |= TX_CK_VALID; |
| 5239 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5240 | t->srv = srv; |
| 5241 | break; |
| 5242 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5243 | txn->flags &= ~TX_CK_MASK; |
| 5244 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5245 | } |
| 5246 | } |
| 5247 | srv = srv->next; |
| 5248 | } |
| 5249 | } |
| 5250 | } |
| 5251 | |
| 5252 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5253 | /* |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5254 | * In a GET or HEAD request, check if the requested URI matches the stats uri |
| 5255 | * 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] | 5256 | * |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5257 | * 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] | 5258 | * 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] | 5259 | * produce_content() can be called to start sending data. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5260 | * |
| 5261 | * Returns 1 if the session's state changes, otherwise 0. |
| 5262 | */ |
| 5263 | int stats_check_uri_auth(struct session *t, struct proxy *backend) |
| 5264 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5265 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5266 | struct uri_auth *uri_auth = backend->uri_auth; |
| 5267 | struct user_auth *user; |
| 5268 | int authenticated, cur_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5269 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5270 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5271 | memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats)); |
| 5272 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5273 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5274 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5275 | return 0; |
| 5276 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5277 | h = t->req->data + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5278 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5279 | /* the URI is in h */ |
| 5280 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5281 | return 0; |
| 5282 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 5283 | h += uri_auth->uri_len; |
| 5284 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) { |
| 5285 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5286 | t->data_ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 5287 | break; |
| 5288 | } |
| 5289 | h++; |
| 5290 | } |
| 5291 | |
| 5292 | if (uri_auth->refresh) { |
| 5293 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 5294 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) { |
| 5295 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5296 | t->data_ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 5297 | break; |
| 5298 | } |
| 5299 | h++; |
| 5300 | } |
| 5301 | } |
| 5302 | |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 5303 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 5304 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) { |
| 5305 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5306 | t->data_ctx.stats.flags |= STAT_FMT_CSV; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 5307 | break; |
| 5308 | } |
| 5309 | h++; |
| 5310 | } |
| 5311 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5312 | t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO; |
| 5313 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5314 | /* we are in front of a interceptable URI. Let's check |
| 5315 | * if there's an authentication and if it's valid. |
| 5316 | */ |
| 5317 | user = uri_auth->users; |
| 5318 | if (!user) { |
| 5319 | /* no user auth required, it's OK */ |
| 5320 | authenticated = 1; |
| 5321 | } else { |
| 5322 | authenticated = 0; |
| 5323 | |
| 5324 | /* a user list is defined, we have to check. |
| 5325 | * skip 21 chars for "Authorization: Basic ". |
| 5326 | */ |
| 5327 | |
| 5328 | /* FIXME: this should move to an earlier place */ |
| 5329 | cur_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5330 | h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 5331 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 5332 | int len = txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5333 | if (len > 14 && |
| 5334 | !strncasecmp("Authorization:", h, 14)) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5335 | txn->auth_hdr.str = h; |
| 5336 | txn->auth_hdr.len = len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5337 | break; |
| 5338 | } |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5339 | h += len + txn->hdr_idx.v[cur_idx].cr + 1; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5340 | } |
| 5341 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5342 | if (txn->auth_hdr.len < 21 || |
| 5343 | memcmp(txn->auth_hdr.str + 14, " Basic ", 7)) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5344 | user = NULL; |
| 5345 | |
| 5346 | while (user) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5347 | if ((txn->auth_hdr.len == user->user_len + 14 + 7) |
| 5348 | && !memcmp(txn->auth_hdr.str + 14 + 7, |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5349 | user->user_pwd, user->user_len)) { |
| 5350 | authenticated = 1; |
| 5351 | break; |
| 5352 | } |
| 5353 | user = user->next; |
| 5354 | } |
| 5355 | } |
| 5356 | |
| 5357 | if (!authenticated) { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5358 | struct chunk msg; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5359 | |
| 5360 | /* no need to go further */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5361 | msg.str = trash; |
| 5362 | msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm); |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5363 | txn->status = 401; |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5364 | client_retnclose(t, &msg); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 5365 | trace_term(t, TT_HTTP_URI_1); |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 5366 | t->req->analysers = 0; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5367 | if (!(t->flags & SN_ERR_MASK)) |
| 5368 | t->flags |= SN_ERR_PRXCOND; |
| 5369 | if (!(t->flags & SN_FINST_MASK)) |
| 5370 | t->flags |= SN_FINST_R; |
| 5371 | return 1; |
| 5372 | } |
| 5373 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5374 | /* The request is valid, the user is authenticated. Let's start sending |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5375 | * data. |
| 5376 | */ |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 5377 | buffer_shutw_now(t->req); |
| 5378 | buffer_shutr_now(t->rep); |
| 5379 | buffer_start_hijack(t->rep); |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 5380 | t->logs.tv_request = now; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5381 | t->data_source = DATA_SRC_STATS; |
| 5382 | t->data_state = DATA_ST_INIT; |
Willy Tarreau | 91e9993 | 2008-06-30 07:51:00 +0200 | [diff] [blame] | 5383 | t->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5384 | produce_content(t); |
| 5385 | return 1; |
| 5386 | } |
| 5387 | |
| 5388 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5389 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5390 | * Print a debug line with a header |
| 5391 | */ |
| 5392 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 5393 | { |
| 5394 | int len, max; |
| 5395 | 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] | 5396 | 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] | 5397 | max = end - start; |
| 5398 | UBOUND(max, sizeof(trash) - len - 1); |
| 5399 | len += strlcpy2(trash + len, start, max + 1); |
| 5400 | trash[len++] = '\n'; |
| 5401 | write(1, trash, len); |
| 5402 | } |
| 5403 | |
| 5404 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5405 | /************************************************************************/ |
| 5406 | /* The code below is dedicated to ACL parsing and matching */ |
| 5407 | /************************************************************************/ |
| 5408 | |
| 5409 | |
| 5410 | |
| 5411 | |
| 5412 | /* 1. Check on METHOD |
| 5413 | * We use the pre-parsed method if it is known, and store its number as an |
| 5414 | * integer. If it is unknown, we use the pointer and the length. |
| 5415 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5416 | 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] | 5417 | { |
| 5418 | int len, meth; |
| 5419 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5420 | len = strlen(*text); |
| 5421 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5422 | |
| 5423 | pattern->val.i = meth; |
| 5424 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5425 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5426 | if (!pattern->ptr.str) |
| 5427 | return 0; |
| 5428 | pattern->len = len; |
| 5429 | } |
| 5430 | return 1; |
| 5431 | } |
| 5432 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5433 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5434 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5435 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5436 | { |
| 5437 | int meth; |
| 5438 | struct http_txn *txn = l7; |
| 5439 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5440 | if (!txn) |
| 5441 | return 0; |
| 5442 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5443 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5444 | return 0; |
| 5445 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5446 | meth = txn->meth; |
| 5447 | test->i = meth; |
| 5448 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5449 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5450 | /* ensure the indexes are not affected */ |
| 5451 | return 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5452 | test->len = txn->req.sl.rq.m_l; |
| 5453 | test->ptr = txn->req.sol; |
| 5454 | } |
| 5455 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5456 | return 1; |
| 5457 | } |
| 5458 | |
| 5459 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 5460 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 5461 | int icase; |
| 5462 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5463 | if (test->i != pattern->val.i) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5464 | return ACL_PAT_FAIL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5465 | |
| 5466 | if (test->i != HTTP_METH_OTHER) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5467 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5468 | |
| 5469 | /* Other method, we must compare the strings */ |
| 5470 | if (pattern->len != test->len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5471 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 5472 | |
| 5473 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 5474 | if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) || |
| 5475 | (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5476 | return ACL_PAT_FAIL; |
| 5477 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5478 | } |
| 5479 | |
| 5480 | /* 2. Check on Request/Status Version |
| 5481 | * We simply compare strings here. |
| 5482 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5483 | 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] | 5484 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5485 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5486 | if (!pattern->ptr.str) |
| 5487 | return 0; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5488 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5489 | return 1; |
| 5490 | } |
| 5491 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5492 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5493 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5494 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5495 | { |
| 5496 | struct http_txn *txn = l7; |
| 5497 | char *ptr; |
| 5498 | int len; |
| 5499 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5500 | if (!txn) |
| 5501 | return 0; |
| 5502 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5503 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5504 | return 0; |
| 5505 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5506 | len = txn->req.sl.rq.v_l; |
| 5507 | ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som; |
| 5508 | |
| 5509 | while ((len-- > 0) && (*ptr++ != '/')); |
| 5510 | if (len <= 0) |
| 5511 | return 0; |
| 5512 | |
| 5513 | test->ptr = ptr; |
| 5514 | test->len = len; |
| 5515 | |
| 5516 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5517 | return 1; |
| 5518 | } |
| 5519 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5520 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5521 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5522 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5523 | { |
| 5524 | struct http_txn *txn = l7; |
| 5525 | char *ptr; |
| 5526 | int len; |
| 5527 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5528 | if (!txn) |
| 5529 | return 0; |
| 5530 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5531 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5532 | return 0; |
| 5533 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5534 | len = txn->rsp.sl.st.v_l; |
| 5535 | ptr = txn->rsp.sol; |
| 5536 | |
| 5537 | while ((len-- > 0) && (*ptr++ != '/')); |
| 5538 | if (len <= 0) |
| 5539 | return 0; |
| 5540 | |
| 5541 | test->ptr = ptr; |
| 5542 | test->len = len; |
| 5543 | |
| 5544 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5545 | return 1; |
| 5546 | } |
| 5547 | |
| 5548 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5549 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5550 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5551 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5552 | { |
| 5553 | struct http_txn *txn = l7; |
| 5554 | char *ptr; |
| 5555 | int len; |
| 5556 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5557 | if (!txn) |
| 5558 | return 0; |
| 5559 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5560 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5561 | return 0; |
| 5562 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5563 | len = txn->rsp.sl.st.c_l; |
| 5564 | ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som; |
| 5565 | |
| 5566 | test->i = __strl2ui(ptr, len); |
| 5567 | test->flags = ACL_TEST_F_VOL_1ST; |
| 5568 | return 1; |
| 5569 | } |
| 5570 | |
| 5571 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5572 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5573 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5574 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5575 | { |
| 5576 | struct http_txn *txn = l7; |
| 5577 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5578 | if (!txn) |
| 5579 | return 0; |
| 5580 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5581 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5582 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5583 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5584 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5585 | /* ensure the indexes are not affected */ |
| 5586 | return 0; |
| 5587 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5588 | test->len = txn->req.sl.rq.u_l; |
| 5589 | test->ptr = txn->req.sol + txn->req.sl.rq.u; |
| 5590 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 5591 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 5592 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5593 | return 1; |
| 5594 | } |
| 5595 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5596 | static int |
| 5597 | acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5598 | struct acl_expr *expr, struct acl_test *test) |
| 5599 | { |
| 5600 | struct http_txn *txn = l7; |
| 5601 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5602 | if (!txn) |
| 5603 | return 0; |
| 5604 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5605 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5606 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5607 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5608 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5609 | /* ensure the indexes are not affected */ |
| 5610 | return 0; |
| 5611 | |
| 5612 | /* Parse HTTP request */ |
| 5613 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
| 5614 | test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr; |
| 5615 | test->i = AF_INET; |
| 5616 | |
| 5617 | /* |
| 5618 | * If we are parsing url in frontend space, we prepare backend stage |
| 5619 | * to not parse again the same url ! optimization lazyness... |
| 5620 | */ |
| 5621 | if (px->options & PR_O_HTTP_PROXY) |
| 5622 | l4->flags |= SN_ADDR_SET; |
| 5623 | |
| 5624 | test->flags = ACL_TEST_F_READ_ONLY; |
| 5625 | return 1; |
| 5626 | } |
| 5627 | |
| 5628 | static int |
| 5629 | acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5630 | struct acl_expr *expr, struct acl_test *test) |
| 5631 | { |
| 5632 | struct http_txn *txn = l7; |
| 5633 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5634 | if (!txn) |
| 5635 | return 0; |
| 5636 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5637 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5638 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5639 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5640 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5641 | /* ensure the indexes are not affected */ |
| 5642 | return 0; |
| 5643 | |
| 5644 | /* Same optimization as url_ip */ |
| 5645 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
| 5646 | test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port); |
| 5647 | |
| 5648 | if (px->options & PR_O_HTTP_PROXY) |
| 5649 | l4->flags |= SN_ADDR_SET; |
| 5650 | |
| 5651 | test->flags = ACL_TEST_F_READ_ONLY; |
| 5652 | return 1; |
| 5653 | } |
| 5654 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5655 | /* 5. Check on HTTP header. A pointer to the beginning of the value is returned. |
| 5656 | * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr(). |
| 5657 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5658 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5659 | 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] | 5660 | struct acl_expr *expr, struct acl_test *test) |
| 5661 | { |
| 5662 | struct http_txn *txn = l7; |
| 5663 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5664 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5665 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5666 | if (!txn) |
| 5667 | return 0; |
| 5668 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5669 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 5670 | /* search for header from the beginning */ |
| 5671 | ctx->idx = 0; |
| 5672 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5673 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 5674 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 5675 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5676 | test->len = ctx->vlen; |
| 5677 | test->ptr = (char *)ctx->line + ctx->val; |
| 5678 | return 1; |
| 5679 | } |
| 5680 | |
| 5681 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 5682 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5683 | return 0; |
| 5684 | } |
| 5685 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5686 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5687 | acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5688 | struct acl_expr *expr, struct acl_test *test) |
| 5689 | { |
| 5690 | struct http_txn *txn = l7; |
| 5691 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5692 | if (!txn) |
| 5693 | return 0; |
| 5694 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5695 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5696 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5697 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5698 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5699 | /* ensure the indexes are not affected */ |
| 5700 | return 0; |
| 5701 | |
| 5702 | return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test); |
| 5703 | } |
| 5704 | |
| 5705 | static int |
| 5706 | acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5707 | struct acl_expr *expr, struct acl_test *test) |
| 5708 | { |
| 5709 | struct http_txn *txn = l7; |
| 5710 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5711 | if (!txn) |
| 5712 | return 0; |
| 5713 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5714 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5715 | return 0; |
| 5716 | |
| 5717 | return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test); |
| 5718 | } |
| 5719 | |
| 5720 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 5721 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 5722 | */ |
| 5723 | static int |
| 5724 | 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] | 5725 | struct acl_expr *expr, struct acl_test *test) |
| 5726 | { |
| 5727 | struct http_txn *txn = l7; |
| 5728 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5729 | struct hdr_ctx ctx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5730 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5731 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5732 | if (!txn) |
| 5733 | return 0; |
| 5734 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5735 | ctx.idx = 0; |
| 5736 | cnt = 0; |
| 5737 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx)) |
| 5738 | cnt++; |
| 5739 | |
| 5740 | test->i = cnt; |
| 5741 | test->flags = ACL_TEST_F_VOL_HDR; |
| 5742 | return 1; |
| 5743 | } |
| 5744 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5745 | static int |
| 5746 | acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5747 | struct acl_expr *expr, struct acl_test *test) |
| 5748 | { |
| 5749 | struct http_txn *txn = l7; |
| 5750 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5751 | if (!txn) |
| 5752 | return 0; |
| 5753 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5754 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5755 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5756 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5757 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5758 | /* ensure the indexes are not affected */ |
| 5759 | return 0; |
| 5760 | |
| 5761 | return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test); |
| 5762 | } |
| 5763 | |
| 5764 | static int |
| 5765 | acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5766 | struct acl_expr *expr, struct acl_test *test) |
| 5767 | { |
| 5768 | struct http_txn *txn = l7; |
| 5769 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5770 | if (!txn) |
| 5771 | return 0; |
| 5772 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5773 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5774 | return 0; |
| 5775 | |
| 5776 | return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test); |
| 5777 | } |
| 5778 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5779 | /* 7. Check on HTTP header's integer value. The integer value is returned. |
| 5780 | * FIXME: the type is 'int', it may not be appropriate for everything. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5781 | * 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] | 5782 | */ |
| 5783 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5784 | 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] | 5785 | struct acl_expr *expr, struct acl_test *test) |
| 5786 | { |
| 5787 | struct http_txn *txn = l7; |
| 5788 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5789 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5790 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5791 | if (!txn) |
| 5792 | return 0; |
| 5793 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5794 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 5795 | /* search for header from the beginning */ |
| 5796 | ctx->idx = 0; |
| 5797 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5798 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 5799 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 5800 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5801 | test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen); |
| 5802 | return 1; |
| 5803 | } |
| 5804 | |
| 5805 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 5806 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5807 | return 0; |
| 5808 | } |
| 5809 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5810 | static int |
| 5811 | acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5812 | struct acl_expr *expr, struct acl_test *test) |
| 5813 | { |
| 5814 | struct http_txn *txn = l7; |
| 5815 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5816 | if (!txn) |
| 5817 | return 0; |
| 5818 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5819 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5820 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5821 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5822 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5823 | /* ensure the indexes are not affected */ |
| 5824 | return 0; |
| 5825 | |
| 5826 | return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test); |
| 5827 | } |
| 5828 | |
| 5829 | static int |
| 5830 | acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5831 | struct acl_expr *expr, struct acl_test *test) |
| 5832 | { |
| 5833 | struct http_txn *txn = l7; |
| 5834 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5835 | if (!txn) |
| 5836 | return 0; |
| 5837 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5838 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5839 | return 0; |
| 5840 | |
| 5841 | return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test); |
| 5842 | } |
| 5843 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5844 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 5845 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 5846 | */ |
| 5847 | static int |
| 5848 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5849 | struct acl_expr *expr, struct acl_test *test) |
| 5850 | { |
| 5851 | struct http_txn *txn = l7; |
| 5852 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5853 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5854 | if (!txn) |
| 5855 | return 0; |
| 5856 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5857 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5858 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5859 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5860 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5861 | /* ensure the indexes are not affected */ |
| 5862 | return 0; |
| 5863 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5864 | end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 5865 | ptr = http_get_path(txn); |
| 5866 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5867 | return 0; |
| 5868 | |
| 5869 | /* OK, we got the '/' ! */ |
| 5870 | test->ptr = ptr; |
| 5871 | |
| 5872 | while (ptr < end && *ptr != '?') |
| 5873 | ptr++; |
| 5874 | |
| 5875 | test->len = ptr - test->ptr; |
| 5876 | |
| 5877 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 5878 | test->flags = ACL_TEST_F_VOL_1ST; |
| 5879 | return 1; |
| 5880 | } |
| 5881 | |
| 5882 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5883 | |
| 5884 | /************************************************************************/ |
| 5885 | /* All supported keywords must be declared here. */ |
| 5886 | /************************************************************************/ |
| 5887 | |
| 5888 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 5889 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5890 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT }, |
| 5891 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5892 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 5893 | { "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] | 5894 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5895 | { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5896 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 5897 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 5898 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 5899 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 5900 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 5901 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 5902 | { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE }, |
| 5903 | { "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] | 5904 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5905 | /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */ |
| 5906 | { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5907 | { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 5908 | { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 5909 | { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 5910 | { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 5911 | { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 5912 | { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 5913 | { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
| 5914 | { "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] | 5915 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5916 | { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 5917 | { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE }, |
| 5918 | { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE }, |
| 5919 | { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE }, |
| 5920 | { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE }, |
| 5921 | { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE }, |
| 5922 | { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE }, |
| 5923 | { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
| 5924 | { "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] | 5925 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5926 | { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5927 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 5928 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 5929 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 5930 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 5931 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 5932 | { "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] | 5933 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 5934 | { NULL, NULL, NULL, NULL }, |
| 5935 | |
| 5936 | #if 0 |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5937 | { "line", acl_parse_str, acl_fetch_line, acl_match_str }, |
| 5938 | { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg }, |
| 5939 | { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg }, |
| 5940 | { "line_end", acl_parse_str, acl_fetch_line, acl_match_end }, |
| 5941 | { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub }, |
| 5942 | { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir }, |
| 5943 | { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom }, |
| 5944 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5945 | { "cook", acl_parse_str, acl_fetch_cook, acl_match_str }, |
| 5946 | { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg }, |
| 5947 | { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg }, |
| 5948 | { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end }, |
| 5949 | { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub }, |
| 5950 | { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir }, |
| 5951 | { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom }, |
| 5952 | { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst }, |
| 5953 | |
| 5954 | { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str }, |
| 5955 | { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg }, |
| 5956 | { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str }, |
| 5957 | { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 }, |
| 5958 | { NULL, NULL, NULL, NULL }, |
| 5959 | #endif |
| 5960 | }}; |
| 5961 | |
| 5962 | |
| 5963 | __attribute__((constructor)) |
| 5964 | static void __http_protocol_init(void) |
| 5965 | { |
| 5966 | acl_register_keywords(&acl_kws); |
| 5967 | } |
| 5968 | |
| 5969 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5970 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5971 | * Local variables: |
| 5972 | * c-indent-level: 8 |
| 5973 | * c-basic-offset: 8 |
| 5974 | * End: |
| 5975 | */ |