Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP protocol analyzer |
| 3 | * |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 4 | * Copyright 2000-2008 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <ctype.h> |
| 14 | #include <errno.h> |
| 15 | #include <fcntl.h> |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | #include <syslog.h> |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 20 | #include <time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | |
| 22 | #include <sys/socket.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 26 | #include <common/appsession.h> |
| 27 | #include <common/compat.h> |
| 28 | #include <common/config.h> |
Willy Tarreau | a4cd1f5 | 2006-12-16 19:57:26 +0100 | [diff] [blame] | 29 | #include <common/debug.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 30 | #include <common/memory.h> |
| 31 | #include <common/mini-clist.h> |
| 32 | #include <common/standard.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 33 | #include <common/ticks.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 34 | #include <common/time.h> |
| 35 | #include <common/uri_auth.h> |
| 36 | #include <common/version.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 37 | |
| 38 | #include <types/capture.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 39 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 40 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 41 | #include <proto/acl.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 42 | #include <proto/backend.h> |
| 43 | #include <proto/buffers.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 44 | #include <proto/dumpstats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 45 | #include <proto/fd.h> |
| 46 | #include <proto/log.h> |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 47 | #include <proto/hdr_idx.h> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 48 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 49 | #include <proto/proto_http.h> |
| 50 | #include <proto/queue.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 51 | #include <proto/senddata.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 52 | #include <proto/session.h> |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 53 | #include <proto/stream_sock.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 54 | #include <proto/task.h> |
| 55 | |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 56 | #ifdef CONFIG_HAP_TCPSPLICE |
| 57 | #include <libtcpsplice.h> |
| 58 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 59 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 60 | #define DEBUG_PARSE_NO_SPEEDUP |
| 61 | #undef DEBUG_PARSE_NO_SPEEDUP |
| 62 | |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 63 | /* This is used to perform a quick jump as an alternative to a break/continue |
| 64 | * instruction. The first argument is the label for normal operation, and the |
| 65 | * second one is the break/continue instruction in the no_speedup mode. |
| 66 | */ |
| 67 | |
| 68 | #ifdef DEBUG_PARSE_NO_SPEEDUP |
| 69 | #define QUICK_JUMP(x,y) y |
| 70 | #else |
| 71 | #define QUICK_JUMP(x,y) goto x |
| 72 | #endif |
| 73 | |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 74 | /* This is used by remote monitoring */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 75 | const char HTTP_200[] = |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 76 | "HTTP/1.0 200 OK\r\n" |
| 77 | "Cache-Control: no-cache\r\n" |
| 78 | "Connection: close\r\n" |
| 79 | "Content-Type: text/html\r\n" |
| 80 | "\r\n" |
| 81 | "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n"; |
| 82 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 83 | const struct chunk http_200_chunk = { |
| 84 | .str = (char *)&HTTP_200, |
| 85 | .len = sizeof(HTTP_200)-1 |
| 86 | }; |
| 87 | |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 88 | const char *HTTP_301 = |
| 89 | "HTTP/1.0 301 Moved Permantenly\r\n" |
| 90 | "Cache-Control: no-cache\r\n" |
| 91 | "Connection: close\r\n" |
| 92 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 93 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 94 | const char *HTTP_302 = |
| 95 | "HTTP/1.0 302 Found\r\n" |
| 96 | "Cache-Control: no-cache\r\n" |
| 97 | "Connection: close\r\n" |
| 98 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 99 | |
| 100 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 101 | const char *HTTP_303 = |
| 102 | "HTTP/1.0 303 See Other\r\n" |
| 103 | "Cache-Control: no-cache\r\n" |
| 104 | "Connection: close\r\n" |
| 105 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 106 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 107 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 108 | const char *HTTP_401_fmt = |
| 109 | "HTTP/1.0 401 Unauthorized\r\n" |
| 110 | "Cache-Control: no-cache\r\n" |
| 111 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 112 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 113 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 114 | "\r\n" |
| 115 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 116 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 117 | |
| 118 | const int http_err_codes[HTTP_ERR_SIZE] = { |
| 119 | [HTTP_ERR_400] = 400, |
| 120 | [HTTP_ERR_403] = 403, |
| 121 | [HTTP_ERR_408] = 408, |
| 122 | [HTTP_ERR_500] = 500, |
| 123 | [HTTP_ERR_502] = 502, |
| 124 | [HTTP_ERR_503] = 503, |
| 125 | [HTTP_ERR_504] = 504, |
| 126 | }; |
| 127 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 128 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 129 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 130 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 131 | "Cache-Control: no-cache\r\n" |
| 132 | "Connection: close\r\n" |
| 133 | "Content-Type: text/html\r\n" |
| 134 | "\r\n" |
| 135 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 136 | |
| 137 | [HTTP_ERR_403] = |
| 138 | "HTTP/1.0 403 Forbidden\r\n" |
| 139 | "Cache-Control: no-cache\r\n" |
| 140 | "Connection: close\r\n" |
| 141 | "Content-Type: text/html\r\n" |
| 142 | "\r\n" |
| 143 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 144 | |
| 145 | [HTTP_ERR_408] = |
| 146 | "HTTP/1.0 408 Request Time-out\r\n" |
| 147 | "Cache-Control: no-cache\r\n" |
| 148 | "Connection: close\r\n" |
| 149 | "Content-Type: text/html\r\n" |
| 150 | "\r\n" |
| 151 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 152 | |
| 153 | [HTTP_ERR_500] = |
| 154 | "HTTP/1.0 500 Server Error\r\n" |
| 155 | "Cache-Control: no-cache\r\n" |
| 156 | "Connection: close\r\n" |
| 157 | "Content-Type: text/html\r\n" |
| 158 | "\r\n" |
| 159 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 160 | |
| 161 | [HTTP_ERR_502] = |
| 162 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 163 | "Cache-Control: no-cache\r\n" |
| 164 | "Connection: close\r\n" |
| 165 | "Content-Type: text/html\r\n" |
| 166 | "\r\n" |
| 167 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 168 | |
| 169 | [HTTP_ERR_503] = |
| 170 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 171 | "Cache-Control: no-cache\r\n" |
| 172 | "Connection: close\r\n" |
| 173 | "Content-Type: text/html\r\n" |
| 174 | "\r\n" |
| 175 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 176 | |
| 177 | [HTTP_ERR_504] = |
| 178 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 179 | "Cache-Control: no-cache\r\n" |
| 180 | "Connection: close\r\n" |
| 181 | "Content-Type: text/html\r\n" |
| 182 | "\r\n" |
| 183 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 184 | |
| 185 | }; |
| 186 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 187 | /* We must put the messages here since GCC cannot initialize consts depending |
| 188 | * on strlen(). |
| 189 | */ |
| 190 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 191 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 192 | #define FD_SETS_ARE_BITFIELDS |
| 193 | #ifdef FD_SETS_ARE_BITFIELDS |
| 194 | /* |
| 195 | * This map is used with all the FD_* macros to check whether a particular bit |
| 196 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 197 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 198 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 199 | * exclusively to the macros. |
| 200 | */ |
| 201 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 202 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 203 | |
| 204 | #else |
| 205 | #error "Check if your OS uses bitfields for fd_sets" |
| 206 | #endif |
| 207 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 208 | void init_proto_http() |
| 209 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 210 | int i; |
| 211 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 212 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 213 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 214 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 215 | if (!http_err_msgs[msg]) { |
| 216 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 217 | abort(); |
| 218 | } |
| 219 | |
| 220 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 221 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 222 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 223 | |
| 224 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 225 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 226 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 227 | * printable characters above. |
| 228 | */ |
| 229 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 230 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 231 | for (i = 0; i < 32; i++) { |
| 232 | FD_SET(i, hdr_encode_map); |
| 233 | FD_SET(i, url_encode_map); |
| 234 | } |
| 235 | for (i = 127; i < 256; i++) { |
| 236 | FD_SET(i, hdr_encode_map); |
| 237 | FD_SET(i, url_encode_map); |
| 238 | } |
| 239 | |
| 240 | tmp = "\"#{|}"; |
| 241 | while (*tmp) { |
| 242 | FD_SET(*tmp, hdr_encode_map); |
| 243 | tmp++; |
| 244 | } |
| 245 | |
| 246 | tmp = "\"#"; |
| 247 | while (*tmp) { |
| 248 | FD_SET(*tmp, url_encode_map); |
| 249 | tmp++; |
| 250 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 251 | |
| 252 | /* memory allocations */ |
| 253 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 254 | pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 255 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 256 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 257 | /* |
| 258 | * We have 26 list of methods (1 per first letter), each of which can have |
| 259 | * up to 3 entries (2 valid, 1 null). |
| 260 | */ |
| 261 | struct http_method_desc { |
| 262 | http_meth_t meth; |
| 263 | int len; |
| 264 | const char text[8]; |
| 265 | }; |
| 266 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 267 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 268 | ['C' - 'A'] = { |
| 269 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 270 | }, |
| 271 | ['D' - 'A'] = { |
| 272 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 273 | }, |
| 274 | ['G' - 'A'] = { |
| 275 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 276 | }, |
| 277 | ['H' - 'A'] = { |
| 278 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 279 | }, |
| 280 | ['P' - 'A'] = { |
| 281 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 282 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 283 | }, |
| 284 | ['T' - 'A'] = { |
| 285 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 286 | }, |
| 287 | /* rest is empty like this : |
| 288 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 289 | */ |
| 290 | }; |
| 291 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 292 | /* It is about twice as fast on recent architectures to lookup a byte in a |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 293 | * table than to perform a boolean AND or OR between two tests. Refer to |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 294 | * RFC2616 for those chars. |
| 295 | */ |
| 296 | |
| 297 | const char http_is_spht[256] = { |
| 298 | [' '] = 1, ['\t'] = 1, |
| 299 | }; |
| 300 | |
| 301 | const char http_is_crlf[256] = { |
| 302 | ['\r'] = 1, ['\n'] = 1, |
| 303 | }; |
| 304 | |
| 305 | const char http_is_lws[256] = { |
| 306 | [' '] = 1, ['\t'] = 1, |
| 307 | ['\r'] = 1, ['\n'] = 1, |
| 308 | }; |
| 309 | |
| 310 | const char http_is_sep[256] = { |
| 311 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 312 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 313 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 314 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 315 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 316 | }; |
| 317 | |
| 318 | const char http_is_ctl[256] = { |
| 319 | [0 ... 31] = 1, |
| 320 | [127] = 1, |
| 321 | }; |
| 322 | |
| 323 | /* |
| 324 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 325 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 326 | * them correctly. Instead, define every non-CTL char's status. |
| 327 | */ |
| 328 | const char http_is_token[256] = { |
| 329 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 330 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 331 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 332 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 333 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 334 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 335 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 336 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 337 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 338 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 339 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 340 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 341 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 342 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 343 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 344 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 345 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 346 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 347 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 348 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 349 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 350 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 351 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 352 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 353 | }; |
| 354 | |
| 355 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 356 | /* |
| 357 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 358 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 359 | */ |
| 360 | const char http_is_ver_token[256] = { |
| 361 | ['.'] = 1, ['/'] = 1, |
| 362 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 363 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| 364 | ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| 365 | }; |
| 366 | |
| 367 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 368 | #ifdef DEBUG_FULL |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 369 | static char *cli_stnames[4] = { "DAT", "SHR", "SHW", "CLS" }; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 370 | #endif |
| 371 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 372 | static void http_sess_log(struct session *s); |
| 373 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 374 | /* |
| 375 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 376 | * CRLF. Text length is measured first, so it cannot be NULL. |
| 377 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 378 | * of headers is automatically adjusted. The number of bytes added is returned |
| 379 | * on success, otherwise <0 is returned indicating an error. |
| 380 | */ |
| 381 | int http_header_add_tail(struct buffer *b, struct http_msg *msg, |
| 382 | struct hdr_idx *hdr_idx, const char *text) |
| 383 | { |
| 384 | int bytes, len; |
| 385 | |
| 386 | len = strlen(text); |
| 387 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 388 | if (!bytes) |
| 389 | return -1; |
| 390 | msg->eoh += bytes; |
| 391 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 392 | } |
| 393 | |
| 394 | /* |
| 395 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 396 | * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then |
| 397 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 398 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 399 | * of headers is automatically adjusted. The number of bytes added is returned |
| 400 | * on success, otherwise <0 is returned indicating an error. |
| 401 | */ |
| 402 | int http_header_add_tail2(struct buffer *b, struct http_msg *msg, |
| 403 | struct hdr_idx *hdr_idx, const char *text, int len) |
| 404 | { |
| 405 | int bytes; |
| 406 | |
| 407 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 408 | if (!bytes) |
| 409 | return -1; |
| 410 | msg->eoh += bytes; |
| 411 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 412 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 413 | |
| 414 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 415 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 416 | * If so, returns the position of the first non-space character relative to |
| 417 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 418 | * to return a pointer to the place after the first space. Returns 0 if the |
| 419 | * header name does not match. Checks are case-insensitive. |
| 420 | */ |
| 421 | int http_header_match2(const char *hdr, const char *end, |
| 422 | const char *name, int len) |
| 423 | { |
| 424 | const char *val; |
| 425 | |
| 426 | if (hdr + len >= end) |
| 427 | return 0; |
| 428 | if (hdr[len] != ':') |
| 429 | return 0; |
| 430 | if (strncasecmp(hdr, name, len) != 0) |
| 431 | return 0; |
| 432 | val = hdr + len + 1; |
| 433 | while (val < end && HTTP_IS_SPHT(*val)) |
| 434 | val++; |
| 435 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 436 | return len + 2; /* we may replace starting from second space */ |
| 437 | return val - hdr; |
| 438 | } |
| 439 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 440 | /* Find the end of the header value contained between <s> and <e>. |
| 441 | * See RFC2616, par 2.2 for more information. Note that it requires |
| 442 | * a valid header to return a valid result. |
| 443 | */ |
| 444 | const char *find_hdr_value_end(const char *s, const char *e) |
| 445 | { |
| 446 | int quoted, qdpair; |
| 447 | |
| 448 | quoted = qdpair = 0; |
| 449 | for (; s < e; s++) { |
| 450 | if (qdpair) qdpair = 0; |
| 451 | else if (quoted && *s == '\\') qdpair = 1; |
| 452 | else if (quoted && *s == '"') quoted = 0; |
| 453 | else if (*s == '"') quoted = 1; |
| 454 | else if (*s == ',') return s; |
| 455 | } |
| 456 | return s; |
| 457 | } |
| 458 | |
| 459 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 460 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 461 | * structure holds everything necessary to use the header and find next |
| 462 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 463 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 464 | * 1 when it finds a value, and 0 when there is no more. |
| 465 | */ |
| 466 | int http_find_header2(const char *name, int len, |
| 467 | const char *sol, struct hdr_idx *idx, |
| 468 | struct hdr_ctx *ctx) |
| 469 | { |
| 470 | __label__ return_hdr, next_hdr; |
| 471 | const char *eol, *sov; |
| 472 | int cur_idx; |
| 473 | |
| 474 | if (ctx->idx) { |
| 475 | /* We have previously returned a value, let's search |
| 476 | * another one on the same line. |
| 477 | */ |
| 478 | cur_idx = ctx->idx; |
| 479 | sol = ctx->line; |
| 480 | sov = sol + ctx->val + ctx->vlen; |
| 481 | eol = sol + idx->v[cur_idx].len; |
| 482 | |
| 483 | if (sov >= eol) |
| 484 | /* no more values in this header */ |
| 485 | goto next_hdr; |
| 486 | |
| 487 | /* values remaining for this header, skip the comma */ |
| 488 | sov++; |
| 489 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 490 | sov++; |
| 491 | |
| 492 | goto return_hdr; |
| 493 | } |
| 494 | |
| 495 | /* first request for this header */ |
| 496 | sol += hdr_idx_first_pos(idx); |
| 497 | cur_idx = hdr_idx_first_idx(idx); |
| 498 | |
| 499 | while (cur_idx) { |
| 500 | eol = sol + idx->v[cur_idx].len; |
| 501 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 502 | if (len == 0) { |
| 503 | /* No argument was passed, we want any header. |
| 504 | * To achieve this, we simply build a fake request. */ |
| 505 | while (sol + len < eol && sol[len] != ':') |
| 506 | len++; |
| 507 | name = sol; |
| 508 | } |
| 509 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 510 | if ((len < eol - sol) && |
| 511 | (sol[len] == ':') && |
| 512 | (strncasecmp(sol, name, len) == 0)) { |
| 513 | |
| 514 | sov = sol + len + 1; |
| 515 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 516 | sov++; |
| 517 | return_hdr: |
| 518 | ctx->line = sol; |
| 519 | ctx->idx = cur_idx; |
| 520 | ctx->val = sov - sol; |
| 521 | |
| 522 | eol = find_hdr_value_end(sov, eol); |
| 523 | ctx->vlen = eol - sov; |
| 524 | return 1; |
| 525 | } |
| 526 | next_hdr: |
| 527 | sol = eol + idx->v[cur_idx].cr + 1; |
| 528 | cur_idx = idx->v[cur_idx].next; |
| 529 | } |
| 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | int http_find_header(const char *name, |
| 534 | const char *sol, struct hdr_idx *idx, |
| 535 | struct hdr_ctx *ctx) |
| 536 | { |
| 537 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 538 | } |
| 539 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 540 | /* This function shuts down the buffers on the server side, and sets indicators |
| 541 | * accordingly. The server's fd is supposed to already be closed. Note that if |
| 542 | * <status> is 0, or if the message pointer is NULL, then no message is returned. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 543 | */ |
| 544 | void srv_close_with_err(struct session *t, int err, int finst, |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 545 | int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 546 | { |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 547 | buffer_write_ena(t->rep); |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 548 | buffer_shutw(t->req); |
| 549 | buffer_shutr(t->rep); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 550 | if (status > 0 && msg) { |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 551 | t->txn.status = status; |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 552 | if (t->fe->mode == PR_MODE_HTTP) |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 553 | client_return(t, msg); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 554 | } |
| 555 | if (!(t->flags & SN_ERR_MASK)) |
| 556 | t->flags |= err; |
| 557 | if (!(t->flags & SN_FINST_MASK)) |
| 558 | t->flags |= finst; |
| 559 | } |
| 560 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 561 | /* This function returns the appropriate error location for the given session |
| 562 | * and message. |
| 563 | */ |
| 564 | |
| 565 | struct chunk *error_message(struct session *s, int msgnum) |
| 566 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 567 | if (s->be->errmsg[msgnum].str) |
| 568 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 569 | else if (s->fe->errmsg[msgnum].str) |
| 570 | return &s->fe->errmsg[msgnum]; |
| 571 | else |
| 572 | return &http_err_chunks[msgnum]; |
| 573 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 574 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 575 | /* |
| 576 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 577 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 578 | */ |
| 579 | static http_meth_t find_http_meth(const char *str, const int len) |
| 580 | { |
| 581 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 582 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 583 | |
| 584 | m = ((unsigned)*str - 'A'); |
| 585 | |
| 586 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 587 | for (h = http_methods[m]; h->len > 0; h++) { |
| 588 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 589 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 590 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 591 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 592 | }; |
| 593 | return HTTP_METH_OTHER; |
| 594 | } |
| 595 | return HTTP_METH_NONE; |
| 596 | |
| 597 | } |
| 598 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 599 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 600 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 601 | * It is returned otherwise. |
| 602 | */ |
| 603 | static char * |
| 604 | http_get_path(struct http_txn *txn) |
| 605 | { |
| 606 | char *ptr, *end; |
| 607 | |
| 608 | ptr = txn->req.sol + txn->req.sl.rq.u; |
| 609 | end = ptr + txn->req.sl.rq.u_l; |
| 610 | |
| 611 | if (ptr >= end) |
| 612 | return NULL; |
| 613 | |
| 614 | /* RFC2616, par. 5.1.2 : |
| 615 | * Request-URI = "*" | absuri | abspath | authority |
| 616 | */ |
| 617 | |
| 618 | if (*ptr == '*') |
| 619 | return NULL; |
| 620 | |
| 621 | if (isalpha((unsigned char)*ptr)) { |
| 622 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 623 | ptr++; |
| 624 | while (ptr < end && |
| 625 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 626 | ptr++; |
| 627 | /* skip '://' */ |
| 628 | if (ptr == end || *ptr++ != ':') |
| 629 | return NULL; |
| 630 | if (ptr == end || *ptr++ != '/') |
| 631 | return NULL; |
| 632 | if (ptr == end || *ptr++ != '/') |
| 633 | return NULL; |
| 634 | } |
| 635 | /* skip [user[:passwd]@]host[:[port]] */ |
| 636 | |
| 637 | while (ptr < end && *ptr != '/') |
| 638 | ptr++; |
| 639 | |
| 640 | if (ptr == end) |
| 641 | return NULL; |
| 642 | |
| 643 | /* OK, we got the '/' ! */ |
| 644 | return ptr; |
| 645 | } |
| 646 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 647 | /* Processes the client, server, request and response jobs of a session task, |
| 648 | * then puts it back to the wait queue in a clean state, or cleans up its |
| 649 | * resources if it must be deleted. Returns in <next> the date the task wants |
| 650 | * to be woken up, or TICK_ETERNITY. In order not to call all functions for |
| 651 | * nothing too many times, the request and response buffers flags are monitored |
| 652 | * and each function is called only if at least another function has changed at |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 653 | * least one flag it is interested in. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 654 | */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 655 | void process_session(struct task *t, int *next) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 656 | { |
| 657 | struct session *s = t->context; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 658 | int resync; |
| 659 | unsigned int rqf_cli, rpf_cli; |
| 660 | unsigned int rqf_srv, rpf_srv; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 661 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 662 | /* Check timeouts only during data phase for now */ |
| 663 | if (unlikely(t->state & TASK_WOKEN_TIMER)) { |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame^] | 664 | if (s->rep->cons->state == SI_ST_EST) { |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 665 | stream_sock_data_check_timeouts(s->rep->cons->fd); |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame^] | 666 | if (tick_is_expired(s->rep->analyse_exp, now_ms)) |
| 667 | s->rep->flags |= BF_ANA_TIMEOUT; |
| 668 | } |
| 669 | if (s->req->cons->state == SI_ST_EST) { |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 670 | stream_sock_data_check_timeouts(s->req->cons->fd); |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame^] | 671 | if (tick_is_expired(s->req->analyse_exp, now_ms)) |
| 672 | s->req->flags |= BF_ANA_TIMEOUT; |
| 673 | } |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 674 | } |
| 675 | |
Willy Tarreau | 48adac5 | 2008-08-30 04:58:38 +0200 | [diff] [blame] | 676 | /* Check if we need to close the write side. This can only happen |
| 677 | * when either SHUTR or EMPTY appears, because WRITE_ENA cannot appear |
| 678 | * from low level, and neither HIJACK nor SHUTW can disappear from low |
| 679 | * level. |
| 680 | */ |
| 681 | if (unlikely((s->req->flags & (BF_SHUTW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) == (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR))) { |
| 682 | buffer_shutw(s->req); |
| 683 | s->req->cons->shutw(s->req->cons); |
| 684 | } |
| 685 | |
| 686 | if (unlikely((s->rep->flags & (BF_SHUTW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) == (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR))) { |
| 687 | buffer_shutw(s->rep); |
| 688 | s->rep->cons->shutw(s->rep->cons); |
| 689 | } |
| 690 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 691 | /* When a server-side connection is released, we have to |
| 692 | * count it and check for pending connections on this server. |
| 693 | */ |
| 694 | if (unlikely(s->req->cons->state == SI_ST_CLO && |
| 695 | s->req->cons->prev_state == SI_ST_EST)) { |
| 696 | /* Count server-side errors (but not timeouts). */ |
| 697 | if (s->req->flags & BF_WRITE_ERROR) { |
| 698 | s->be->failed_resp++; |
| 699 | if (s->srv) |
| 700 | s->srv->failed_resp++; |
| 701 | } |
| 702 | |
| 703 | if (s->srv) { |
| 704 | s->srv->cur_sess--; |
| 705 | sess_change_server(s, NULL); |
| 706 | if (may_dequeue_tasks(s->srv, s->be)) |
| 707 | process_srv_queue(s->srv); |
| 708 | } |
| 709 | |
| 710 | if (unlikely((s->req->cons->state == SI_ST_CLO) && |
| 711 | (global.mode & MODE_DEBUG) && |
| 712 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
| 713 | int len; |
| 714 | len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n", |
| 715 | s->uniq_id, s->be->id, (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd); |
| 716 | write(1, trash, len); |
| 717 | } |
| 718 | } |
| 719 | |
Willy Tarreau | 48adac5 | 2008-08-30 04:58:38 +0200 | [diff] [blame] | 720 | /* This is needed when debugging is enabled, to indicate client-side close */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 721 | if (unlikely(s->rep->cons->state == SI_ST_CLO && |
| 722 | s->rep->cons->prev_state == SI_ST_EST)) { |
| 723 | if (unlikely((s->rep->cons->state == SI_ST_CLO) && |
| 724 | (global.mode & MODE_DEBUG) && |
| 725 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
| 726 | int len; |
| 727 | len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", |
| 728 | s->uniq_id, s->be->id, (unsigned short)s->rep->prod->fd, (unsigned short)s->req->cons->fd); |
| 729 | write(1, trash, len); |
| 730 | } |
| 731 | } |
| 732 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 733 | /* Dirty trick: force one first pass everywhere */ |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame^] | 734 | rqf_cli = rqf_srv = ~s->req->flags; |
| 735 | rpf_cli = rpf_srv = ~s->rep->flags; |
Willy Tarreau | 507385d | 2008-08-17 13:04:25 +0200 | [diff] [blame] | 736 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 737 | /* well, the ST_CONN state is already handled properly */ |
| 738 | if (s->req->prod->state == SI_ST_EST) { |
| 739 | rqf_cli = s->req->flags; |
| 740 | rpf_cli = s->rep->flags; |
| 741 | } |
| 742 | |
| 743 | if (s->req->cons->state == SI_ST_EST) { |
| 744 | rqf_srv = s->req->flags; |
| 745 | rpf_srv = s->rep->flags; |
| 746 | } |
| 747 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 748 | do { |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 749 | 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", |
| 750 | now_ms, __FUNCTION__, |
| 751 | t, |
| 752 | s->req, s->rep, |
| 753 | s->req->rex, s->rep->wex, |
| 754 | s->req->flags, s->rep->flags, |
| 755 | s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state); |
| 756 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 757 | resync = 0; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 758 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 759 | /* Maybe resync client FD state */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 760 | if (s->rep->cons->state != SI_ST_CLO) { |
| 761 | if (((rqf_cli ^ s->req->flags) & BF_MASK_INTERFACE_I) || |
| 762 | ((rpf_cli ^ s->rep->flags) & BF_MASK_INTERFACE_O)) { |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 763 | stream_sock_data_update(s->rep->cons->fd); |
| 764 | rqf_cli = s->req->flags; |
| 765 | rpf_cli = s->rep->flags; |
| 766 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 767 | if (unlikely((s->rep->cons->state == SI_ST_CLO) && |
| 768 | (global.mode & MODE_DEBUG) && |
| 769 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
| 770 | int len; |
| 771 | len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 772 | 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] | 773 | write(1, trash, len); |
| 774 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 775 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 776 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 777 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 778 | /* Maybe resync server FD state */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 779 | if (s->req->cons->state != SI_ST_CLO) { |
| 780 | if (((rpf_srv ^ s->rep->flags) & BF_MASK_INTERFACE_I) || |
| 781 | ((rqf_srv ^ s->req->flags) & BF_MASK_INTERFACE_O)) { |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame^] | 782 | 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] | 783 | process_srv_conn(s); |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame^] | 784 | resync = 1; /* we might have to resync */ |
| 785 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 786 | |
| 787 | if (s->req->cons->state == SI_ST_EST) { |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 788 | 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] | 789 | s->be->options & PR_O_FORCE_CLO && |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 790 | s->rep->flags & BF_READ_ACTIVITY) { |
Willy Tarreau | 376580a | 2008-08-27 18:52:22 +0200 | [diff] [blame] | 791 | /* We want to force the connection to the server to close, |
| 792 | * and the server has begun to respond. That's the right |
| 793 | * time. |
| 794 | */ |
| 795 | buffer_shutw_now(s->req); |
| 796 | } |
| 797 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 798 | stream_sock_data_update(s->req->cons->fd); |
Willy Tarreau | 376580a | 2008-08-27 18:52:22 +0200 | [diff] [blame] | 799 | |
| 800 | /* When a server-side connection is released, we have to |
| 801 | * count it and check for pending connections on this server. |
| 802 | */ |
| 803 | if (s->req->cons->state == SI_ST_CLO) { |
| 804 | if (s->srv) { |
| 805 | s->srv->cur_sess--; |
| 806 | sess_change_server(s, NULL); |
| 807 | if (may_dequeue_tasks(s->srv, s->be)) |
| 808 | process_srv_queue(s->srv); |
| 809 | } |
| 810 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 811 | } |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 812 | rqf_srv = s->req->flags; |
| 813 | rpf_srv = s->rep->flags; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 814 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 815 | if (unlikely((s->req->cons->state == SI_ST_CLO) && |
| 816 | (global.mode & MODE_DEBUG) && |
| 817 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
| 818 | int len; |
| 819 | len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n", |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 820 | 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] | 821 | write(1, trash, len); |
| 822 | } |
| 823 | } |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame^] | 824 | } |
| 825 | |
| 826 | /* we may have to resync because of pending connections */ |
| 827 | if (resync) |
| 828 | continue; |
| 829 | |
| 830 | /**** Process layer 7 below ****/ |
| 831 | |
| 832 | /* Analyse request */ |
| 833 | if (s->req->flags & BF_MASK_ANALYSER) { |
| 834 | unsigned int flags = s->req->flags; |
| 835 | |
| 836 | if (s->req->prod->state >= SI_ST_EST) { |
| 837 | /* it's up to the analysers to reset write_ena */ |
| 838 | buffer_write_ena(s->req); |
| 839 | if (s->req->analysers) |
| 840 | process_request(s); |
| 841 | } |
| 842 | s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 843 | flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 844 | if (s->req->flags != flags) |
| 845 | resync = 1; |
| 846 | } |
| 847 | |
| 848 | /* Analyse response */ |
| 849 | if (unlikely(s->rep->flags & BF_HIJACK)) { |
| 850 | /* In inject mode, we wake up everytime something has |
| 851 | * happened on the write side of the buffer. |
| 852 | */ |
| 853 | unsigned int flags = s->rep->flags; |
| 854 | |
| 855 | if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) && |
| 856 | !(s->rep->flags & BF_FULL)) { |
| 857 | produce_content(s); |
| 858 | } |
| 859 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 860 | flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 861 | if (s->rep->flags != flags) |
| 862 | resync = 1; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 863 | } |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame^] | 864 | else if (s->rep->flags & BF_MASK_ANALYSER) { |
| 865 | unsigned int flags = s->rep->flags; |
| 866 | |
| 867 | if (s->rep->prod->state >= SI_ST_EST) { |
| 868 | /* it's up to the analysers to reset write_ena */ |
| 869 | buffer_write_ena(s->rep); |
| 870 | if (s->rep->analysers) |
| 871 | process_response(s); |
| 872 | } |
| 873 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 874 | flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 875 | if (s->rep->flags != flags) |
| 876 | resync = 1; |
| 877 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 878 | |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame^] | 879 | /* For the moment, we need to clean the client and server flags that |
| 880 | * have vanished. This is just a temporary measure though. |
| 881 | */ |
| 882 | rqf_cli &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 883 | rqf_srv &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 884 | rpf_cli &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 885 | rpf_srv &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 886 | } while (resync); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 887 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 888 | if (likely((s->rep->cons->state != SI_ST_CLO) || |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 889 | (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] | 890 | |
| 891 | if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED)) |
| 892 | session_process_counters(s); |
| 893 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 894 | if (s->rep->cons->state == SI_ST_EST) |
| 895 | stream_sock_data_finish(s->rep->cons->fd); |
| 896 | |
| 897 | if (s->req->cons->state == SI_ST_EST) |
| 898 | stream_sock_data_finish(s->req->cons->fd); |
| 899 | |
Willy Tarreau | 9a2d154 | 2008-08-30 12:31:07 +0200 | [diff] [blame] | 900 | s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 901 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 902 | s->si[0].prev_state = s->si[0].state; |
| 903 | s->si[1].prev_state = s->si[1].state; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 904 | |
Willy Tarreau | 7f875f6 | 2008-08-11 17:35:01 +0200 | [diff] [blame] | 905 | /* Trick: if a request is being waiting for the server to respond, |
| 906 | * and if we know the server can timeout, we don't want the timeout |
| 907 | * to expire on the client side first, but we're still interested |
| 908 | * in passing data from the client to the server (eg: POST). Thus, |
| 909 | * we can cancel the client's request timeout if the server's |
| 910 | * request timeout is set and the server has not yet sent a response. |
| 911 | */ |
| 912 | |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 913 | if ((s->rep->flags & (BF_WRITE_ENA|BF_SHUTR)) == 0 && |
Willy Tarreau | 26ed74d | 2008-08-17 12:11:14 +0200 | [diff] [blame] | 914 | (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) |
Willy Tarreau | 7f875f6 | 2008-08-11 17:35:01 +0200 | [diff] [blame] | 915 | s->req->rex = TICK_ETERNITY; |
| 916 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 917 | t->expire = tick_first(tick_first(s->req->rex, s->req->wex), |
| 918 | tick_first(s->rep->rex, s->rep->wex)); |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 919 | if (s->req->analysers) |
| 920 | t->expire = tick_first(t->expire, s->req->analyse_exp); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 921 | |
Willy Tarreau | cb65125 | 2008-08-29 13:57:30 +0200 | [diff] [blame] | 922 | #ifdef DEBUG_FULL |
| 923 | fprintf(stderr, "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u rep->rex=%u rep->wex=%u\n", |
| 924 | now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp, s->rep->rex, s->rep->wex); |
| 925 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 926 | /* restore t to its place in the task list */ |
| 927 | task_queue(t); |
| 928 | |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 929 | #ifdef DEBUG_DEV |
| 930 | /* this may only happen when no timeout is set or in case of an FSM bug */ |
| 931 | if (!t->expire) |
| 932 | ABORT_NOW(); |
| 933 | #endif |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 934 | *next = t->expire; |
| 935 | return; /* nothing more to do */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 936 | } |
| 937 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 938 | s->fe->feconn--; |
| 939 | if (s->flags & SN_BE_ASSIGNED) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 940 | s->be->beconn--; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 941 | actconn--; |
| 942 | |
Willy Tarreau | f41d4b1 | 2007-04-28 23:26:14 +0200 | [diff] [blame] | 943 | if (unlikely((global.mode & MODE_DEBUG) && |
| 944 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 945 | int len; |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 946 | 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] | 947 | s->uniq_id, s->be->id, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 948 | (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd, |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 949 | s->term_trace); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 950 | write(1, trash, len); |
| 951 | } |
| 952 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 953 | 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] | 954 | session_process_counters(s); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 955 | |
| 956 | /* let's do a final log if we need it */ |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 957 | if (s->logs.logwait && |
| 958 | !(s->flags & SN_MONITOR) && |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 959 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 960 | if (s->fe->to_log & LW_REQ) |
| 961 | http_sess_log(s); |
| 962 | else |
| 963 | tcp_sess_log(s); |
| 964 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 965 | |
| 966 | /* the task MUST not be in the run queue anymore */ |
| 967 | task_delete(t); |
| 968 | session_free(s); |
| 969 | task_free(t); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 970 | *next = TICK_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 974 | extern const char sess_term_cond[8]; |
| 975 | extern const char sess_fin_state[8]; |
| 976 | extern const char *monthname[12]; |
| 977 | const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */ |
| 978 | const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown, |
| 979 | Set-cookie seen and left unchanged (passive), Set-cookie Deleted, |
| 980 | unknown, Set-cookie Rewritten */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 981 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 982 | struct pool_head *pool2_capture; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 983 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 984 | /* |
| 985 | * send a log for the session when we have enough info about it. |
| 986 | * Will not log if the frontend has no log defined. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 987 | */ |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 988 | static void http_sess_log(struct session *s) |
| 989 | { |
| 990 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 991 | struct proxy *fe = s->fe; |
| 992 | struct proxy *be = s->be; |
| 993 | struct proxy *prx_log; |
| 994 | struct http_txn *txn = &s->txn; |
| 995 | int tolog; |
| 996 | char *uri, *h; |
| 997 | char *svid; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 998 | struct tm tm; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 999 | static char tmpline[MAX_SYSLOG_LEN]; |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1000 | int t_request; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1001 | int hdr; |
| 1002 | |
| 1003 | if (fe->logfac1 < 0 && fe->logfac2 < 0) |
| 1004 | return; |
| 1005 | prx_log = fe; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1006 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1007 | if (s->cli_addr.ss_family == AF_INET) |
| 1008 | inet_ntop(AF_INET, |
| 1009 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 1010 | pn, sizeof(pn)); |
| 1011 | else |
| 1012 | inet_ntop(AF_INET6, |
| 1013 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 1014 | pn, sizeof(pn)); |
| 1015 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 1016 | get_localtime(s->logs.accept_date.tv_sec, &tm); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1017 | |
| 1018 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 1019 | tolog = fe->to_log; |
| 1020 | |
| 1021 | h = tmpline; |
| 1022 | if (fe->to_log & LW_REQHDR && |
| 1023 | txn->req.cap && |
| 1024 | (h < tmpline + sizeof(tmpline) - 10)) { |
| 1025 | *(h++) = ' '; |
| 1026 | *(h++) = '{'; |
| 1027 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 1028 | if (hdr) |
| 1029 | *(h++) = '|'; |
| 1030 | if (txn->req.cap[hdr] != NULL) |
| 1031 | h = encode_string(h, tmpline + sizeof(tmpline) - 7, |
| 1032 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 1033 | } |
| 1034 | *(h++) = '}'; |
| 1035 | } |
| 1036 | |
| 1037 | if (fe->to_log & LW_RSPHDR && |
| 1038 | txn->rsp.cap && |
| 1039 | (h < tmpline + sizeof(tmpline) - 7)) { |
| 1040 | *(h++) = ' '; |
| 1041 | *(h++) = '{'; |
| 1042 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 1043 | if (hdr) |
| 1044 | *(h++) = '|'; |
| 1045 | if (txn->rsp.cap[hdr] != NULL) |
| 1046 | h = encode_string(h, tmpline + sizeof(tmpline) - 4, |
| 1047 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 1048 | } |
| 1049 | *(h++) = '}'; |
| 1050 | } |
| 1051 | |
| 1052 | if (h < tmpline + sizeof(tmpline) - 4) { |
| 1053 | *(h++) = ' '; |
| 1054 | *(h++) = '"'; |
| 1055 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 1056 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 1057 | '#', url_encode_map, uri); |
| 1058 | *(h++) = '"'; |
| 1059 | } |
| 1060 | *h = '\0'; |
| 1061 | |
| 1062 | svid = (tolog & LW_SVID) ? |
| 1063 | (s->data_source != DATA_SRC_STATS) ? |
| 1064 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 1065 | |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1066 | t_request = -1; |
| 1067 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 1068 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 1069 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1070 | send_log(prx_log, LOG_INFO, |
| 1071 | "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]" |
| 1072 | " %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] | 1073 | " %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] | 1074 | pn, |
| 1075 | (s->cli_addr.ss_family == AF_INET) ? |
| 1076 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 1077 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1078 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 1079 | 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] | 1080 | fe->id, be->id, svid, |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1081 | t_request, |
| 1082 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1083 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 1084 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 1085 | (tolog & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 1086 | txn->status, |
Willy Tarreau | 8b3977f | 2008-01-18 11:16:32 +0100 | [diff] [blame] | 1087 | (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1088 | txn->cli_cookie ? txn->cli_cookie : "-", |
| 1089 | txn->srv_cookie ? txn->srv_cookie : "-", |
| 1090 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 1091 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 1092 | (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 1093 | (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-', |
| 1094 | 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] | 1095 | (s->flags & SN_REDISP)?"+":"", |
| 1096 | (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] | 1097 | s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline); |
| 1098 | |
| 1099 | s->logs.logwait = 0; |
| 1100 | } |
| 1101 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1102 | |
| 1103 | /* |
| 1104 | * Capture headers from message starting at <som> according to header list |
| 1105 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 1106 | */ |
| 1107 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1108 | char **cap, struct cap_hdr *cap_hdr) |
| 1109 | { |
| 1110 | char *eol, *sol, *col, *sov; |
| 1111 | int cur_idx; |
| 1112 | struct cap_hdr *h; |
| 1113 | int len; |
| 1114 | |
| 1115 | sol = som + hdr_idx_first_pos(idx); |
| 1116 | cur_idx = hdr_idx_first_idx(idx); |
| 1117 | |
| 1118 | while (cur_idx) { |
| 1119 | eol = sol + idx->v[cur_idx].len; |
| 1120 | |
| 1121 | col = sol; |
| 1122 | while (col < eol && *col != ':') |
| 1123 | col++; |
| 1124 | |
| 1125 | sov = col + 1; |
| 1126 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 1127 | sov++; |
| 1128 | |
| 1129 | for (h = cap_hdr; h; h = h->next) { |
| 1130 | if ((h->namelen == col - sol) && |
| 1131 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1132 | if (cap[h->index] == NULL) |
| 1133 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1134 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1135 | |
| 1136 | if (cap[h->index] == NULL) { |
| 1137 | Alert("HTTP capture : out of memory.\n"); |
| 1138 | continue; |
| 1139 | } |
| 1140 | |
| 1141 | len = eol - sov; |
| 1142 | if (len > h->len) |
| 1143 | len = h->len; |
| 1144 | |
| 1145 | memcpy(cap[h->index], sov, len); |
| 1146 | cap[h->index][len]=0; |
| 1147 | } |
| 1148 | } |
| 1149 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1150 | cur_idx = idx->v[cur_idx].next; |
| 1151 | } |
| 1152 | } |
| 1153 | |
| 1154 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1155 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1156 | */ |
| 1157 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1158 | |
| 1159 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1160 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1161 | */ |
| 1162 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1163 | ptr++; \ |
| 1164 | if (likely(ptr < end)) \ |
| 1165 | goto good; \ |
| 1166 | else { \ |
| 1167 | state = (st); \ |
| 1168 | goto http_msg_ood; \ |
| 1169 | } \ |
| 1170 | } while (0) |
| 1171 | |
| 1172 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1173 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1174 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1175 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1176 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1177 | * will give undefined results. |
| 1178 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1179 | * and that msg->sol points to the beginning of the response. |
| 1180 | * If a complete line is found (which implies that at least one CR or LF is |
| 1181 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1182 | * returned indicating an incomplete line (which does not mean that parts have |
| 1183 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1184 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1185 | * upon next call. |
| 1186 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1187 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1188 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1189 | * 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] | 1190 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1191 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1192 | const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, |
| 1193 | unsigned int state, const char *ptr, const char *end, |
| 1194 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1195 | { |
| 1196 | __label__ |
| 1197 | http_msg_rpver, |
| 1198 | http_msg_rpver_sp, |
| 1199 | http_msg_rpcode, |
| 1200 | http_msg_rpcode_sp, |
| 1201 | http_msg_rpreason, |
| 1202 | http_msg_rpline_eol, |
| 1203 | http_msg_ood, /* out of data */ |
| 1204 | http_msg_invalid; |
| 1205 | |
| 1206 | switch (state) { |
| 1207 | http_msg_rpver: |
| 1208 | case HTTP_MSG_RPVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1209 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1210 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1211 | |
| 1212 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1213 | msg->sl.st.v_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1214 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1215 | } |
| 1216 | goto http_msg_invalid; |
| 1217 | |
| 1218 | http_msg_rpver_sp: |
| 1219 | case HTTP_MSG_RPVER_SP: |
| 1220 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1221 | msg->sl.st.c = ptr - msg_buf; |
| 1222 | goto http_msg_rpcode; |
| 1223 | } |
| 1224 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1225 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1226 | /* so it's a CR/LF, this is invalid */ |
| 1227 | goto http_msg_invalid; |
| 1228 | |
| 1229 | http_msg_rpcode: |
| 1230 | case HTTP_MSG_RPCODE: |
| 1231 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1232 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1233 | |
| 1234 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1235 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 1236 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1237 | } |
| 1238 | |
| 1239 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1240 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 1241 | http_msg_rsp_reason: |
| 1242 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
| 1243 | msg->sl.st.r = ptr - msg_buf; |
| 1244 | msg->sl.st.r_l = 0; |
| 1245 | goto http_msg_rpline_eol; |
| 1246 | |
| 1247 | http_msg_rpcode_sp: |
| 1248 | case HTTP_MSG_RPCODE_SP: |
| 1249 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1250 | msg->sl.st.r = ptr - msg_buf; |
| 1251 | goto http_msg_rpreason; |
| 1252 | } |
| 1253 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1254 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1255 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1256 | goto http_msg_rsp_reason; |
| 1257 | |
| 1258 | http_msg_rpreason: |
| 1259 | case HTTP_MSG_RPREASON: |
| 1260 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1261 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
| 1262 | msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r; |
| 1263 | http_msg_rpline_eol: |
| 1264 | /* We have seen the end of line. Note that we do not |
| 1265 | * necessarily have the \n yet, but at least we know that we |
| 1266 | * have EITHER \r OR \n, otherwise the response would not be |
| 1267 | * complete. We can then record the response length and return |
| 1268 | * to the caller which will be able to register it. |
| 1269 | */ |
| 1270 | msg->sl.st.l = ptr - msg->sol; |
| 1271 | return ptr; |
| 1272 | |
| 1273 | #ifdef DEBUG_FULL |
| 1274 | default: |
| 1275 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1276 | exit(1); |
| 1277 | #endif |
| 1278 | } |
| 1279 | |
| 1280 | http_msg_ood: |
| 1281 | /* out of data */ |
| 1282 | if (ret_state) |
| 1283 | *ret_state = state; |
| 1284 | if (ret_ptr) |
| 1285 | *ret_ptr = (char *)ptr; |
| 1286 | return NULL; |
| 1287 | |
| 1288 | http_msg_invalid: |
| 1289 | /* invalid message */ |
| 1290 | if (ret_state) |
| 1291 | *ret_state = HTTP_MSG_ERROR; |
| 1292 | return NULL; |
| 1293 | } |
| 1294 | |
| 1295 | |
| 1296 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1297 | * This function parses a request line between <ptr> and <end>, starting with |
| 1298 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1299 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1300 | * will give undefined results. |
| 1301 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1302 | * and that msg->sol points to the beginning of the request. |
| 1303 | * If a complete line is found (which implies that at least one CR or LF is |
| 1304 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1305 | * returned indicating an incomplete line (which does not mean that parts have |
| 1306 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1307 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1308 | * upon next call. |
| 1309 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1310 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1311 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1312 | * 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] | 1313 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1314 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1315 | const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, |
| 1316 | unsigned int state, const char *ptr, const char *end, |
| 1317 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1318 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1319 | __label__ |
| 1320 | http_msg_rqmeth, |
| 1321 | http_msg_rqmeth_sp, |
| 1322 | http_msg_rquri, |
| 1323 | http_msg_rquri_sp, |
| 1324 | http_msg_rqver, |
| 1325 | http_msg_rqline_eol, |
| 1326 | http_msg_ood, /* out of data */ |
| 1327 | http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1328 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1329 | switch (state) { |
| 1330 | http_msg_rqmeth: |
| 1331 | case HTTP_MSG_RQMETH: |
| 1332 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1333 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1334 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1335 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1336 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1337 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1338 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1339 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1340 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1341 | /* HTTP 0.9 request */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1342 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1343 | http_msg_req09_uri: |
| 1344 | msg->sl.rq.u = ptr - msg_buf; |
| 1345 | http_msg_req09_uri_e: |
| 1346 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1347 | http_msg_req09_ver: |
| 1348 | msg->sl.rq.v = ptr - msg_buf; |
| 1349 | msg->sl.rq.v_l = 0; |
| 1350 | goto http_msg_rqline_eol; |
| 1351 | } |
| 1352 | goto http_msg_invalid; |
| 1353 | |
| 1354 | http_msg_rqmeth_sp: |
| 1355 | case HTTP_MSG_RQMETH_SP: |
| 1356 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1357 | msg->sl.rq.u = ptr - msg_buf; |
| 1358 | goto http_msg_rquri; |
| 1359 | } |
| 1360 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1361 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1362 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1363 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1364 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1365 | http_msg_rquri: |
| 1366 | case HTTP_MSG_RQURI: |
| 1367 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1368 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1369 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1370 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1371 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1372 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1373 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1374 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1375 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1376 | goto http_msg_req09_uri_e; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1377 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1378 | http_msg_rquri_sp: |
| 1379 | case HTTP_MSG_RQURI_SP: |
| 1380 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1381 | msg->sl.rq.v = ptr - msg_buf; |
| 1382 | goto http_msg_rqver; |
| 1383 | } |
| 1384 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1385 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1386 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1387 | goto http_msg_req09_ver; |
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_rqver: |
| 1390 | case HTTP_MSG_RQVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1391 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1392 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1393 | |
| 1394 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1395 | msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v; |
| 1396 | http_msg_rqline_eol: |
| 1397 | /* We have seen the end of line. Note that we do not |
| 1398 | * necessarily have the \n yet, but at least we know that we |
| 1399 | * have EITHER \r OR \n, otherwise the request would not be |
| 1400 | * complete. We can then record the request length and return |
| 1401 | * to the caller which will be able to register it. |
| 1402 | */ |
| 1403 | msg->sl.rq.l = ptr - msg->sol; |
| 1404 | return ptr; |
| 1405 | } |
| 1406 | |
| 1407 | /* neither an HTTP_VER token nor a CRLF */ |
| 1408 | goto http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1409 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1410 | #ifdef DEBUG_FULL |
| 1411 | default: |
| 1412 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1413 | exit(1); |
| 1414 | #endif |
| 1415 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1416 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1417 | http_msg_ood: |
| 1418 | /* out of data */ |
| 1419 | if (ret_state) |
| 1420 | *ret_state = state; |
| 1421 | if (ret_ptr) |
| 1422 | *ret_ptr = (char *)ptr; |
| 1423 | return NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1424 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1425 | http_msg_invalid: |
| 1426 | /* invalid message */ |
| 1427 | if (ret_state) |
| 1428 | *ret_state = HTTP_MSG_ERROR; |
| 1429 | return NULL; |
| 1430 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1431 | |
| 1432 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1433 | /* |
| 1434 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1435 | * depending on the initial msg->msg_state. It can be preempted everywhere |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1436 | * when data are missing and recalled at the exact same location with no |
| 1437 | * information loss. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1438 | * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other |
| 1439 | * fields. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1440 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1441 | void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx) |
| 1442 | { |
| 1443 | __label__ |
| 1444 | http_msg_rqbefore, |
| 1445 | http_msg_rqbefore_cr, |
| 1446 | http_msg_rqmeth, |
| 1447 | http_msg_rqline_end, |
| 1448 | http_msg_hdr_first, |
| 1449 | http_msg_hdr_name, |
| 1450 | http_msg_hdr_l1_sp, |
| 1451 | http_msg_hdr_l1_lf, |
| 1452 | http_msg_hdr_l1_lws, |
| 1453 | http_msg_hdr_val, |
| 1454 | http_msg_hdr_l2_lf, |
| 1455 | http_msg_hdr_l2_lws, |
| 1456 | http_msg_complete_header, |
| 1457 | http_msg_last_lf, |
| 1458 | http_msg_ood, /* out of data */ |
| 1459 | http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1460 | |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1461 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1462 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1463 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1464 | state = msg->msg_state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1465 | ptr = buf->lr; |
| 1466 | end = buf->r; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1467 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1468 | if (unlikely(ptr >= end)) |
| 1469 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1470 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1471 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1472 | /* |
| 1473 | * First, states that are specific to the response only. |
| 1474 | * We check them first so that request and headers are |
| 1475 | * closer to each other (accessed more often). |
| 1476 | */ |
| 1477 | http_msg_rpbefore: |
| 1478 | case HTTP_MSG_RPBEFORE: |
| 1479 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1480 | if (likely(ptr == buf->data)) { |
| 1481 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1482 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1483 | } else { |
| 1484 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1485 | /* only skip empty leading lines, don't remove them */ |
| 1486 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1487 | msg->som = ptr - buf->data; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1488 | #else |
| 1489 | /* Remove empty leading lines, as recommended by |
| 1490 | * RFC2616. This takes a lot of time because we |
| 1491 | * must move all the buffer backwards, but this |
| 1492 | * is rarely needed. The method above will be |
| 1493 | * cleaner when we'll be able to start sending |
| 1494 | * the request from any place in the buffer. |
| 1495 | */ |
| 1496 | buf->lr = ptr; |
| 1497 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1498 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1499 | msg->sol = buf->data; |
| 1500 | ptr = buf->data; |
| 1501 | end = buf->r; |
| 1502 | #endif |
| 1503 | } |
| 1504 | hdr_idx_init(idx); |
| 1505 | state = HTTP_MSG_RPVER; |
| 1506 | goto http_msg_rpver; |
| 1507 | } |
| 1508 | |
| 1509 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1510 | goto http_msg_invalid; |
| 1511 | |
| 1512 | if (unlikely(*ptr == '\n')) |
| 1513 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1514 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1515 | /* stop here */ |
| 1516 | |
| 1517 | http_msg_rpbefore_cr: |
| 1518 | case HTTP_MSG_RPBEFORE_CR: |
| 1519 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1520 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1521 | /* stop here */ |
| 1522 | |
| 1523 | http_msg_rpver: |
| 1524 | case HTTP_MSG_RPVER: |
| 1525 | case HTTP_MSG_RPVER_SP: |
| 1526 | case HTTP_MSG_RPCODE: |
| 1527 | case HTTP_MSG_RPCODE_SP: |
| 1528 | case HTTP_MSG_RPREASON: |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1529 | ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1530 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1531 | if (unlikely(!ptr)) |
| 1532 | return; |
| 1533 | |
| 1534 | /* we have a full response and we know that we have either a CR |
| 1535 | * or an LF at <ptr>. |
| 1536 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1537 | //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] | 1538 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1539 | |
| 1540 | msg->sol = ptr; |
| 1541 | if (likely(*ptr == '\r')) |
| 1542 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1543 | goto http_msg_rpline_end; |
| 1544 | |
| 1545 | http_msg_rpline_end: |
| 1546 | case HTTP_MSG_RPLINE_END: |
| 1547 | /* msg->sol must point to the first of CR or LF. */ |
| 1548 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1549 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1550 | /* stop here */ |
| 1551 | |
| 1552 | /* |
| 1553 | * Second, states that are specific to the request only |
| 1554 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1555 | http_msg_rqbefore: |
| 1556 | case HTTP_MSG_RQBEFORE: |
| 1557 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1558 | if (likely(ptr == buf->data)) { |
| 1559 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1560 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1561 | } else { |
| 1562 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1563 | /* only skip empty leading lines, don't remove them */ |
| 1564 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1565 | msg->som = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1566 | #else |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1567 | /* Remove empty leading lines, as recommended by |
| 1568 | * RFC2616. This takes a lot of time because we |
| 1569 | * must move all the buffer backwards, but this |
| 1570 | * is rarely needed. The method above will be |
| 1571 | * cleaner when we'll be able to start sending |
| 1572 | * the request from any place in the buffer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1573 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1574 | buf->lr = ptr; |
| 1575 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1576 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1577 | msg->sol = buf->data; |
| 1578 | ptr = buf->data; |
| 1579 | end = buf->r; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1580 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1581 | } |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1582 | /* we will need this when keep-alive will be supported |
| 1583 | hdr_idx_init(idx); |
| 1584 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1585 | state = HTTP_MSG_RQMETH; |
| 1586 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1587 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1588 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1589 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1590 | goto http_msg_invalid; |
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 | if (unlikely(*ptr == '\n')) |
| 1593 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1594 | 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] | 1595 | /* stop here */ |
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_rqbefore_cr: |
| 1598 | case HTTP_MSG_RQBEFORE_CR: |
| 1599 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1600 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1601 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1602 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1603 | http_msg_rqmeth: |
| 1604 | case HTTP_MSG_RQMETH: |
| 1605 | case HTTP_MSG_RQMETH_SP: |
| 1606 | case HTTP_MSG_RQURI: |
| 1607 | case HTTP_MSG_RQURI_SP: |
| 1608 | case HTTP_MSG_RQVER: |
| 1609 | ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1610 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1611 | if (unlikely(!ptr)) |
| 1612 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1613 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1614 | /* we have a full request and we know that we have either a CR |
| 1615 | * or an LF at <ptr>. |
| 1616 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1617 | //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] | 1618 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1619 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1620 | msg->sol = ptr; |
| 1621 | if (likely(*ptr == '\r')) |
| 1622 | 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] | 1623 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1624 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1625 | http_msg_rqline_end: |
| 1626 | case HTTP_MSG_RQLINE_END: |
| 1627 | /* check for HTTP/0.9 request : no version information available. |
| 1628 | * msg->sol must point to the first of CR or LF. |
| 1629 | */ |
| 1630 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1631 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1632 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1633 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1634 | 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] | 1635 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1636 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1637 | /* |
| 1638 | * Common states below |
| 1639 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1640 | http_msg_hdr_first: |
| 1641 | case HTTP_MSG_HDR_FIRST: |
| 1642 | msg->sol = ptr; |
| 1643 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1644 | goto http_msg_hdr_name; |
| 1645 | } |
| 1646 | |
| 1647 | if (likely(*ptr == '\r')) |
| 1648 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1649 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1650 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1651 | http_msg_hdr_name: |
| 1652 | case HTTP_MSG_HDR_NAME: |
| 1653 | /* assumes msg->sol points to the first char */ |
| 1654 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1655 | 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] | 1656 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1657 | if (likely(*ptr == ':')) { |
| 1658 | msg->col = ptr - buf->data; |
| 1659 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| 1660 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1661 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1662 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1663 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1664 | http_msg_hdr_l1_sp: |
| 1665 | case HTTP_MSG_HDR_L1_SP: |
| 1666 | /* assumes msg->sol points to the first char and msg->col to the colon */ |
| 1667 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1668 | 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] | 1669 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1670 | /* header value can be basically anything except CR/LF */ |
| 1671 | msg->sov = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1672 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1673 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1674 | goto http_msg_hdr_val; |
| 1675 | } |
| 1676 | |
| 1677 | if (likely(*ptr == '\r')) |
| 1678 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1679 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1680 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1681 | http_msg_hdr_l1_lf: |
| 1682 | case HTTP_MSG_HDR_L1_LF: |
| 1683 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1684 | 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] | 1685 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1686 | http_msg_hdr_l1_lws: |
| 1687 | case HTTP_MSG_HDR_L1_LWS: |
| 1688 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1689 | /* replace HT,CR,LF with spaces */ |
| 1690 | for (; buf->data+msg->sov < ptr; msg->sov++) |
| 1691 | buf->data[msg->sov] = ' '; |
| 1692 | goto http_msg_hdr_l1_sp; |
| 1693 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1694 | /* we had a header consisting only in spaces ! */ |
| 1695 | msg->eol = buf->data + msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1696 | goto http_msg_complete_header; |
| 1697 | |
| 1698 | http_msg_hdr_val: |
| 1699 | case HTTP_MSG_HDR_VAL: |
| 1700 | /* assumes msg->sol points to the first char, msg->col to the |
| 1701 | * colon, and msg->sov points to the first character of the |
| 1702 | * value. |
| 1703 | */ |
| 1704 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1705 | 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] | 1706 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1707 | msg->eol = ptr; |
| 1708 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1709 | * real header end in case it ends with lots of LWS, but is this |
| 1710 | * really needed ? |
| 1711 | */ |
| 1712 | if (likely(*ptr == '\r')) |
| 1713 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1714 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1715 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1716 | http_msg_hdr_l2_lf: |
| 1717 | case HTTP_MSG_HDR_L2_LF: |
| 1718 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1719 | 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] | 1720 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1721 | http_msg_hdr_l2_lws: |
| 1722 | case HTTP_MSG_HDR_L2_LWS: |
| 1723 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1724 | /* LWS: replace HT,CR,LF with spaces */ |
| 1725 | for (; msg->eol < ptr; msg->eol++) |
| 1726 | *msg->eol = ' '; |
| 1727 | goto http_msg_hdr_val; |
| 1728 | } |
| 1729 | http_msg_complete_header: |
| 1730 | /* |
| 1731 | * It was a new header, so the last one is finished. |
| 1732 | * Assumes msg->sol points to the first char, msg->col to the |
| 1733 | * colon, msg->sov points to the first character of the value |
| 1734 | * and msg->eol to the first CR or LF so we know how the line |
| 1735 | * ends. We insert last header into the index. |
| 1736 | */ |
| 1737 | /* |
| 1738 | fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol); |
| 1739 | write(2, msg->sol, msg->eol-msg->sol); |
| 1740 | fprintf(stderr,"\n"); |
| 1741 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1742 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1743 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r', |
| 1744 | idx, idx->tail) < 0)) |
| 1745 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1746 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1747 | msg->sol = ptr; |
| 1748 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1749 | goto http_msg_hdr_name; |
| 1750 | } |
| 1751 | |
| 1752 | if (likely(*ptr == '\r')) |
| 1753 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1754 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1755 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1756 | http_msg_last_lf: |
| 1757 | case HTTP_MSG_LAST_LF: |
| 1758 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1759 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1760 | ptr++; |
| 1761 | buf->lr = ptr; |
| 1762 | msg->eoh = msg->sol - buf->data; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1763 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1764 | return; |
| 1765 | #ifdef DEBUG_FULL |
| 1766 | default: |
| 1767 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1768 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1769 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1770 | } |
| 1771 | http_msg_ood: |
| 1772 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1773 | msg->msg_state = state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1774 | buf->lr = ptr; |
| 1775 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1776 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1777 | http_msg_invalid: |
| 1778 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1779 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1780 | return; |
| 1781 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1782 | |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1783 | /* This function performs all the processing enabled for the current request. |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1784 | * It normally returns zero, but may return 1 if it absolutely needs to be |
| 1785 | * called again after other functions. It relies on buffers flags, and updates |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1786 | * 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] | 1787 | * functions. Its behaviour is rather simple : |
| 1788 | * - all enabled analysers are called in turn from the lower to the higher |
| 1789 | * bit. |
| 1790 | * - 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] | 1791 | * 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] | 1792 | * that unprocessed data will not be forwarded. But that probably depends on |
| 1793 | * the protocol. Generally it is not reset in case of errors. |
| 1794 | * - 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] | 1795 | * analyser without touching BF_WRITE_ENA (it is enabled prior to |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 1796 | * analysis). |
| 1797 | * - if an analyser thinks it has no added value anymore staying here, it must |
| 1798 | * reset its bit from the analysers flags in order not to be called anymore. |
| 1799 | * |
| 1800 | * In the future, analysers should be able to indicate that they want to be |
| 1801 | * called after XXX bytes have been received (or transfered), and the min of |
| 1802 | * all's wishes will be used to ring back (unless a special condition occurs). |
| 1803 | * |
| 1804 | * |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1805 | */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1806 | int process_request(struct session *t) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1807 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1808 | struct buffer *req = t->req; |
| 1809 | struct buffer *rep = t->rep; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1810 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 1811 | 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] | 1812 | now_ms, __FUNCTION__, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 1813 | t, |
| 1814 | req, |
| 1815 | req->rex, req->wex, |
| 1816 | req->flags, |
| 1817 | req->l, |
| 1818 | req->analysers); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1819 | |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 1820 | /* The tcp-inspect analyser is always called alone */ |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1821 | if (req->analysers & AN_REQ_INSPECT) { |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1822 | struct tcp_rule *rule; |
| 1823 | int partial; |
| 1824 | |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 1825 | /* We will abort if we encounter a read error. In theory, we |
| 1826 | * should not abort if we get a close, it might be valid, |
| 1827 | * although very unlikely. FIXME: we'll abort for now, this |
| 1828 | * will be easier to change later. |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1829 | */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1830 | if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1831 | req->analysers = 0; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 1832 | //t->fe->failed_req++; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1833 | if (!(t->flags & SN_ERR_MASK)) |
| 1834 | t->flags |= SN_ERR_CLICL; |
| 1835 | if (!(t->flags & SN_FINST_MASK)) |
| 1836 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1837 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1838 | } |
| 1839 | |
| 1840 | /* Abort if client read timeout has expired */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1841 | else if (req->flags & BF_READ_TIMEOUT) { |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1842 | req->analysers = 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1843 | t->fe->failed_req++; |
| 1844 | if (!(t->flags & SN_ERR_MASK)) |
| 1845 | t->flags |= SN_ERR_CLITO; |
| 1846 | if (!(t->flags & SN_FINST_MASK)) |
| 1847 | t->flags |= SN_FINST_R; |
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 | } |
| 1850 | |
| 1851 | /* We don't know whether we have enough data, so must proceed |
| 1852 | * this way : |
| 1853 | * - iterate through all rules in their declaration order |
| 1854 | * - if one rule returns MISS, it means the inspect delay is |
| 1855 | * not over yet, then return immediately, otherwise consider |
| 1856 | * it as a non-match. |
| 1857 | * - if one rule returns OK, then return OK |
| 1858 | * - if one rule returns KO, then return KO |
| 1859 | */ |
| 1860 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 1861 | 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] | 1862 | partial = 0; |
| 1863 | else |
| 1864 | partial = ACL_PARTIAL; |
| 1865 | |
| 1866 | list_for_each_entry(rule, &t->fe->tcp_req.inspect_rules, list) { |
| 1867 | int ret = ACL_PAT_PASS; |
| 1868 | |
| 1869 | if (rule->cond) { |
| 1870 | ret = acl_exec_cond(rule->cond, t->fe, t, NULL, ACL_DIR_REQ | partial); |
| 1871 | if (ret == ACL_PAT_MISS) { |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 1872 | buffer_write_dis(req); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1873 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 1874 | if (!tick_isset(req->analyse_exp)) |
| 1875 | 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] | 1876 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1877 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1878 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1879 | ret = acl_pass(ret); |
| 1880 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 1881 | ret = !ret; |
| 1882 | } |
| 1883 | |
| 1884 | if (ret) { |
| 1885 | /* we have a matching rule. */ |
| 1886 | if (rule->action == TCP_ACT_REJECT) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1887 | buffer_abort(req); |
| 1888 | buffer_abort(rep); |
| 1889 | //FIXME: this delete this |
| 1890 | //fd_delete(t->cli_fd); |
| 1891 | //t->cli_state = CL_STCLOSE; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1892 | req->analysers = 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1893 | t->fe->failed_req++; |
| 1894 | if (!(t->flags & SN_ERR_MASK)) |
| 1895 | t->flags |= SN_ERR_PRXCOND; |
| 1896 | if (!(t->flags & SN_FINST_MASK)) |
| 1897 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1898 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1899 | } |
| 1900 | /* otherwise accept */ |
| 1901 | break; |
| 1902 | } |
| 1903 | } |
| 1904 | |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1905 | /* if we get there, it means we have no rule which matches, or |
| 1906 | * we have an explicit accept, so we apply the default accept. |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1907 | */ |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1908 | req->analysers &= ~AN_REQ_INSPECT; |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 1909 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1910 | } |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1911 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1912 | if (req->analysers & AN_REQ_HTTP_HDR) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1913 | /* |
| 1914 | * Now parse the partial (or complete) lines. |
| 1915 | * We will check the request syntax, and also join multi-line |
| 1916 | * headers. An index of all the lines will be elaborated while |
| 1917 | * parsing. |
| 1918 | * |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1919 | * For the parsing, we use a 28 states FSM. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1920 | * |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1921 | * Here is the information we currently have : |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1922 | * req->data + req->som = beginning of request |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1923 | * req->data + req->eoh = end of processed headers / start of current one |
| 1924 | * req->data + req->eol = end of current header or line (LF or CRLF) |
| 1925 | * req->lr = first non-visited byte |
| 1926 | * req->r = end of data |
| 1927 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1928 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1929 | int cur_idx; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1930 | struct http_txn *txn = &t->txn; |
| 1931 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1932 | struct proxy *cur_proxy; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1933 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1934 | if (likely(req->lr < req->r)) |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1935 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1936 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1937 | /* 1: we might have to print this header in debug mode */ |
| 1938 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1939 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1940 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1941 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1942 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1943 | sol = req->data + msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1944 | eol = sol + msg->sl.rq.l; |
| 1945 | debug_hdr("clireq", t, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 1946 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1947 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 1948 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1949 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1950 | while (cur_idx) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1951 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1952 | debug_hdr("clihdr", t, sol, eol); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1953 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 1954 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1955 | } |
| 1956 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1957 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1958 | |
| 1959 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1960 | * Now we quickly check if we have found a full valid request. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1961 | * If not so, we check the FD and buffer states before leaving. |
| 1962 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1963 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 1964 | * requests are checked first. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1965 | * |
| 1966 | */ |
| 1967 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1968 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1969 | /* |
| 1970 | * First, let's catch bad requests. |
| 1971 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1972 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1973 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1974 | |
| 1975 | /* 1: Since we are in header mode, if there's no space |
| 1976 | * left for headers, we won't be able to free more |
| 1977 | * later, so the session will never terminate. We |
| 1978 | * must terminate it now. |
| 1979 | */ |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 1980 | if (unlikely(req->flags & BF_FULL)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1981 | /* FIXME: check if URI is set and return Status |
| 1982 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1983 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1984 | goto return_bad_req; |
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 | /* 2: have we encountered a read error ? */ |
| 1988 | else if (req->flags & BF_READ_ERROR) { |
| 1989 | /* we cannot return any message on error */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1990 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1991 | req->analysers = 0; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 1992 | //t->fe->failed_req++; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1993 | if (!(t->flags & SN_ERR_MASK)) |
| 1994 | t->flags |= SN_ERR_CLICL; |
| 1995 | if (!(t->flags & SN_FINST_MASK)) |
| 1996 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1997 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1998 | } |
| 1999 | |
| 2000 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 2001 | 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] | 2002 | /* read timeout : give up with an error message. */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2003 | txn->status = 408; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2004 | client_retnclose(t, error_message(t, HTTP_ERR_408)); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2005 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2006 | req->analysers = 0; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 2007 | t->fe->failed_req++; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2008 | if (!(t->flags & SN_ERR_MASK)) |
| 2009 | t->flags |= SN_ERR_CLITO; |
| 2010 | if (!(t->flags & SN_FINST_MASK)) |
| 2011 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2012 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2013 | } |
| 2014 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2015 | /* 4: have we encountered a close ? */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 2016 | else if (req->flags & BF_SHUTR) { |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2017 | txn->status = 400; |
| 2018 | client_retnclose(t, error_message(t, HTTP_ERR_400)); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2019 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2020 | req->analysers = 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2021 | t->fe->failed_req++; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2022 | |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2023 | if (!(t->flags & SN_ERR_MASK)) |
| 2024 | t->flags |= SN_ERR_CLICL; |
| 2025 | if (!(t->flags & SN_FINST_MASK)) |
| 2026 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2027 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2028 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2029 | |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 2030 | buffer_write_dis(req); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2031 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 2032 | if (!tick_isset(req->analyse_exp)) |
| 2033 | req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2034 | |
| 2035 | /* we're not ready yet */ |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2036 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2037 | } |
| 2038 | |
| 2039 | |
| 2040 | /**************************************************************** |
| 2041 | * More interesting part now : we know that we have a complete * |
| 2042 | * request which at least looks like HTTP. We have an indicator * |
| 2043 | * of each header's length, so we can parse them quickly. * |
| 2044 | ****************************************************************/ |
| 2045 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2046 | req->analysers &= ~AN_REQ_HTTP_HDR; |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 2047 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 2048 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2049 | /* ensure we keep this pointer to the beginning of the message */ |
| 2050 | msg->sol = req->data + msg->som; |
| 2051 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2052 | /* |
| 2053 | * 1: identify the method |
| 2054 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2055 | 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] | 2056 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2057 | /* we can make use of server redirect on GET and HEAD */ |
| 2058 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2059 | t->flags |= SN_REDIRECTABLE; |
| 2060 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2061 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2062 | * 2: check if the URI matches the monitor_uri. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2063 | * We have to do this for every request which gets in, because |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2064 | * the monitor-uri is defined by the frontend. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2065 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2066 | if (unlikely((t->fe->monitor_uri_len != 0) && |
| 2067 | (t->fe->monitor_uri_len == msg->sl.rq.u_l) && |
| 2068 | !memcmp(&req->data[msg->sl.rq.u], |
| 2069 | t->fe->monitor_uri, |
| 2070 | t->fe->monitor_uri_len))) { |
| 2071 | /* |
| 2072 | * We have found the monitor URI |
| 2073 | */ |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2074 | struct acl_cond *cond; |
| 2075 | cur_proxy = t->fe; |
| 2076 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2077 | t->flags |= SN_MONITOR; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2078 | |
| 2079 | /* Check if we want to fail this monitor request or not */ |
| 2080 | list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) { |
| 2081 | 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] | 2082 | |
| 2083 | ret = acl_pass(ret); |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2084 | if (cond->pol == ACL_COND_UNLESS) |
| 2085 | ret = !ret; |
| 2086 | |
| 2087 | if (ret) { |
| 2088 | /* we fail this request, let's return 503 service unavail */ |
| 2089 | txn->status = 503; |
| 2090 | client_retnclose(t, error_message(t, HTTP_ERR_503)); |
| 2091 | goto return_prx_cond; |
| 2092 | } |
| 2093 | } |
| 2094 | |
| 2095 | /* nothing to fail, let's reply normaly */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2096 | txn->status = 200; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2097 | client_retnclose(t, &http_200_chunk); |
| 2098 | goto return_prx_cond; |
| 2099 | } |
| 2100 | |
| 2101 | /* |
| 2102 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2103 | * Note: we cannot log anymore if the request has been |
| 2104 | * classified as invalid. |
| 2105 | */ |
| 2106 | if (unlikely(t->logs.logwait & LW_REQ)) { |
| 2107 | /* we have a complete HTTP request that we must log */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 2108 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2109 | int urilen = msg->sl.rq.l; |
| 2110 | |
| 2111 | if (urilen >= REQURI_LEN) |
| 2112 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2113 | memcpy(txn->uri, &req->data[msg->som], urilen); |
| 2114 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2115 | |
| 2116 | if (!(t->logs.logwait &= ~LW_REQ)) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 2117 | http_sess_log(t); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2118 | } else { |
| 2119 | Alert("HTTP logging : out of memory.\n"); |
| 2120 | } |
| 2121 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2122 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2123 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2124 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 2125 | if (unlikely(msg->sl.rq.v_l == 0)) { |
| 2126 | int delta; |
| 2127 | char *cur_end; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2128 | msg->sol = req->data + msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2129 | cur_end = msg->sol + msg->sl.rq.l; |
| 2130 | delta = 0; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2131 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2132 | if (msg->sl.rq.u_l == 0) { |
| 2133 | /* if no URI was set, add "/" */ |
| 2134 | delta = buffer_replace2(req, cur_end, cur_end, " /", 2); |
| 2135 | cur_end += delta; |
| 2136 | msg->eoh += delta; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2137 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2138 | /* add HTTP version */ |
| 2139 | delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11); |
| 2140 | msg->eoh += delta; |
| 2141 | cur_end += delta; |
| 2142 | cur_end = (char *)http_parse_reqline(msg, req->data, |
| 2143 | HTTP_MSG_RQMETH, |
| 2144 | msg->sol, cur_end + 1, |
| 2145 | NULL, NULL); |
| 2146 | if (unlikely(!cur_end)) |
| 2147 | goto return_bad_req; |
| 2148 | |
| 2149 | /* we have a full HTTP/1.0 request now and we know that |
| 2150 | * we have either a CR or an LF at <ptr>. |
| 2151 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2152 | 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] | 2153 | } |
| 2154 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2155 | |
| 2156 | /* 5: we may need to capture headers */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2157 | if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap)) |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 2158 | capture_headers(req->data + msg->som, &txn->hdr_idx, |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2159 | txn->req.cap, t->fe->req_cap); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2160 | |
| 2161 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2162 | * 6: we will have to evaluate the filters. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2163 | * As opposed to version 1.2, now they will be evaluated in the |
| 2164 | * filters order and not in the header order. This means that |
| 2165 | * each filter has to be validated among all headers. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2166 | * |
| 2167 | * We can now check whether we want to switch to another |
| 2168 | * backend, in which case we will re-check the backend's |
| 2169 | * filters and various options. In order to support 3-level |
| 2170 | * switching, here's how we should proceed : |
| 2171 | * |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2172 | * a) run be. |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2173 | * if (switch) then switch ->be to the new backend. |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2174 | * b) run be if (be != fe). |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2175 | * There cannot be any switch from there, so ->be cannot be |
| 2176 | * changed anymore. |
| 2177 | * |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2178 | * => filters always apply to ->be, then ->be may change. |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2179 | * |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2180 | * The response path will be able to apply either ->be, or |
| 2181 | * ->be then ->fe filters in order to match the reverse of |
| 2182 | * the forward sequence. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2183 | */ |
| 2184 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2185 | do { |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2186 | struct acl_cond *cond; |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2187 | struct redirect_rule *rule; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2188 | struct proxy *rule_set = t->be; |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2189 | cur_proxy = t->be; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2190 | |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2191 | /* first check whether we have some ACLs set to redirect this request */ |
| 2192 | list_for_each_entry(rule, &cur_proxy->redirect_rules, list) { |
| 2193 | 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] | 2194 | |
| 2195 | ret = acl_pass(ret); |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2196 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2197 | ret = !ret; |
| 2198 | |
| 2199 | if (ret) { |
| 2200 | struct chunk rdr = { trash, 0 }; |
| 2201 | const char *msg_fmt; |
| 2202 | |
| 2203 | /* build redirect message */ |
| 2204 | switch(rule->code) { |
| 2205 | case 303: |
| 2206 | rdr.len = strlen(HTTP_303); |
| 2207 | msg_fmt = HTTP_303; |
| 2208 | break; |
| 2209 | case 301: |
| 2210 | rdr.len = strlen(HTTP_301); |
| 2211 | msg_fmt = HTTP_301; |
| 2212 | break; |
| 2213 | case 302: |
| 2214 | default: |
| 2215 | rdr.len = strlen(HTTP_302); |
| 2216 | msg_fmt = HTTP_302; |
| 2217 | break; |
| 2218 | } |
| 2219 | |
| 2220 | if (unlikely(rdr.len > sizeof(trash))) |
| 2221 | goto return_bad_req; |
| 2222 | memcpy(rdr.str, msg_fmt, rdr.len); |
| 2223 | |
| 2224 | switch(rule->type) { |
| 2225 | case REDIRECT_TYPE_PREFIX: { |
| 2226 | const char *path; |
| 2227 | int pathlen; |
| 2228 | |
| 2229 | path = http_get_path(txn); |
| 2230 | /* build message using path */ |
| 2231 | if (path) { |
| 2232 | pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path; |
| 2233 | } else { |
| 2234 | path = "/"; |
| 2235 | pathlen = 1; |
| 2236 | } |
| 2237 | |
| 2238 | if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4) |
| 2239 | goto return_bad_req; |
| 2240 | |
| 2241 | /* add prefix */ |
| 2242 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 2243 | rdr.len += rule->rdr_len; |
| 2244 | |
| 2245 | /* add path */ |
| 2246 | memcpy(rdr.str + rdr.len, path, pathlen); |
| 2247 | rdr.len += pathlen; |
| 2248 | break; |
| 2249 | } |
| 2250 | case REDIRECT_TYPE_LOCATION: |
| 2251 | default: |
| 2252 | if (rdr.len + rule->rdr_len > sizeof(trash) - 4) |
| 2253 | goto return_bad_req; |
| 2254 | |
| 2255 | /* add location */ |
| 2256 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 2257 | rdr.len += rule->rdr_len; |
| 2258 | break; |
| 2259 | } |
| 2260 | |
| 2261 | /* add end of headers */ |
| 2262 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 2263 | rdr.len += 4; |
| 2264 | |
| 2265 | txn->status = rule->code; |
| 2266 | /* let's log the request time */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2267 | t->logs.tv_request = now; |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2268 | client_retnclose(t, &rdr); |
| 2269 | goto return_prx_cond; |
| 2270 | } |
| 2271 | } |
| 2272 | |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2273 | /* first check whether we have some ACLs set to block this request */ |
| 2274 | list_for_each_entry(cond, &cur_proxy->block_cond, list) { |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 2275 | 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] | 2276 | |
| 2277 | ret = acl_pass(ret); |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2278 | if (cond->pol == ACL_COND_UNLESS) |
| 2279 | ret = !ret; |
| 2280 | |
| 2281 | if (ret) { |
| 2282 | txn->status = 403; |
| 2283 | /* let's log the request time */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2284 | t->logs.tv_request = now; |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2285 | client_retnclose(t, error_message(t, HTTP_ERR_403)); |
| 2286 | goto return_prx_cond; |
| 2287 | } |
| 2288 | } |
| 2289 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2290 | /* try headers filters */ |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2291 | if (rule_set->req_exp != NULL) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2292 | if (apply_filters_to_request(t, req, rule_set->req_exp) < 0) |
| 2293 | goto return_bad_req; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2294 | } |
| 2295 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2296 | if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) { |
| 2297 | /* to ensure correct connection accounting on |
| 2298 | * the backend, we count the connection for the |
| 2299 | * one managing the queue. |
| 2300 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2301 | t->be->beconn++; |
| 2302 | if (t->be->beconn > t->be->beconn_max) |
| 2303 | t->be->beconn_max = t->be->beconn; |
| 2304 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2305 | t->flags |= SN_BE_ASSIGNED; |
| 2306 | } |
| 2307 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2308 | /* has the request been denied ? */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2309 | if (txn->flags & TX_CLDENY) { |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2310 | /* no need to go further */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2311 | txn->status = 403; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2312 | /* let's log the request time */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2313 | t->logs.tv_request = now; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2314 | client_retnclose(t, error_message(t, HTTP_ERR_403)); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2315 | goto return_prx_cond; |
| 2316 | } |
| 2317 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2318 | /* We might have to check for "Connection:" */ |
Krzysztof Oledzki | 336d475 | 2007-12-25 02:40:22 +0100 | [diff] [blame] | 2319 | 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] | 2320 | !(t->flags & SN_CONN_CLOSED)) { |
| 2321 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2322 | int cur_idx, old_idx, delta, val; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2323 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2324 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2325 | 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] | 2326 | old_idx = 0; |
| 2327 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2328 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 2329 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2330 | cur_ptr = cur_next; |
| 2331 | cur_end = cur_ptr + cur_hdr->len; |
| 2332 | cur_next = cur_end + cur_hdr->cr + 1; |
| 2333 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2334 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 2335 | if (val) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2336 | /* 3 possibilities : |
| 2337 | * - we have already set Connection: close, |
| 2338 | * so we remove this line. |
| 2339 | * - we have not yet set Connection: close, |
| 2340 | * but this line indicates close. We leave |
| 2341 | * it untouched and set the flag. |
| 2342 | * - we have not yet set Connection: close, |
| 2343 | * and this line indicates non-close. We |
| 2344 | * replace it. |
| 2345 | */ |
| 2346 | if (t->flags & SN_CONN_CLOSED) { |
| 2347 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2348 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2349 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2350 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 2351 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2352 | cur_hdr->len = 0; |
| 2353 | } else { |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2354 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 2355 | delta = buffer_replace2(req, cur_ptr + val, cur_end, |
| 2356 | "close", 5); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2357 | cur_next += delta; |
| 2358 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2359 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2360 | } |
| 2361 | t->flags |= SN_CONN_CLOSED; |
| 2362 | } |
| 2363 | } |
| 2364 | old_idx = cur_idx; |
| 2365 | } |
Willy Tarreau | f2f0ee8 | 2007-03-30 12:02:43 +0200 | [diff] [blame] | 2366 | } |
| 2367 | /* add request headers from the rule sets in the same order */ |
| 2368 | for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) { |
| 2369 | if (unlikely(http_header_add_tail(req, |
| 2370 | &txn->req, |
| 2371 | &txn->hdr_idx, |
| 2372 | rule_set->req_add[cur_idx])) < 0) |
| 2373 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2374 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2375 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2376 | /* check if stats URI was requested, and if an auth is needed */ |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 2377 | if (rule_set->uri_auth != NULL && |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2378 | (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) { |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2379 | /* we have to check the URI and auth for this request. |
| 2380 | * FIXME!!! that one is rather dangerous, we want to |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2381 | * make it follow standard rules (eg: clear req->analysers). |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2382 | */ |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2383 | if (stats_check_uri_auth(t, rule_set)) |
| 2384 | return 1; |
| 2385 | } |
| 2386 | |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2387 | /* now check whether we have some switching rules for this request */ |
| 2388 | if (!(t->flags & SN_BE_ASSIGNED)) { |
| 2389 | struct switching_rule *rule; |
| 2390 | |
| 2391 | list_for_each_entry(rule, &cur_proxy->switching_rules, list) { |
| 2392 | int ret; |
| 2393 | |
| 2394 | 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] | 2395 | |
| 2396 | ret = acl_pass(ret); |
Willy Tarreau | a8cfa34 | 2008-07-09 11:23:31 +0200 | [diff] [blame] | 2397 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2398 | ret = !ret; |
| 2399 | |
| 2400 | if (ret) { |
| 2401 | t->be = rule->be.backend; |
| 2402 | t->be->beconn++; |
| 2403 | if (t->be->beconn > t->be->beconn_max) |
| 2404 | t->be->beconn_max = t->be->beconn; |
| 2405 | t->be->cum_beconn++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2406 | |
| 2407 | /* assign new parameters to the session from the new backend */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2408 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 2409 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2410 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2411 | t->flags |= SN_BE_ASSIGNED; |
| 2412 | break; |
| 2413 | } |
| 2414 | } |
| 2415 | } |
| 2416 | |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 2417 | if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) { |
| 2418 | /* No backend was set, but there was a default |
| 2419 | * backend set in the frontend, so we use it and |
| 2420 | * loop again. |
| 2421 | */ |
| 2422 | t->be = cur_proxy->defbe.be; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2423 | t->be->beconn++; |
| 2424 | if (t->be->beconn > t->be->beconn_max) |
| 2425 | t->be->beconn_max = t->be->beconn; |
| 2426 | t->be->cum_beconn++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2427 | |
| 2428 | /* assign new parameters to the session from the new backend */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2429 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 2430 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2431 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 2432 | t->flags |= SN_BE_ASSIGNED; |
| 2433 | } |
| 2434 | } 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] | 2435 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2436 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2437 | if (!(t->flags & SN_BE_ASSIGNED)) { |
| 2438 | /* To ensure correct connection accounting on |
| 2439 | * the backend, we count the connection for the |
| 2440 | * one managing the queue. |
| 2441 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2442 | t->be->beconn++; |
| 2443 | if (t->be->beconn > t->be->beconn_max) |
| 2444 | t->be->beconn_max = t->be->beconn; |
| 2445 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2446 | t->flags |= SN_BE_ASSIGNED; |
| 2447 | } |
| 2448 | |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2449 | /* |
| 2450 | * Right now, we know that we have processed the entire headers |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2451 | * and that unwanted requests have been filtered out. We can do |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2452 | * whatever we want with the remaining request. Also, now we |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2453 | * may have separate values for ->fe, ->be. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2454 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2455 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 2456 | /* |
| 2457 | * If HTTP PROXY is set we simply get remote server address |
| 2458 | * parsing incoming request. |
| 2459 | */ |
| 2460 | if ((t->be->options & PR_O_HTTP_PROXY) && !(t->flags & SN_ADDR_SET)) { |
| 2461 | url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &t->srv_addr); |
| 2462 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2463 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2464 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2465 | * 7: the appsession cookie was looked up very early in 1.2, |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2466 | * so let's do the same now. |
| 2467 | */ |
| 2468 | |
| 2469 | /* It needs to look into the URI */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2470 | if (t->be->appsession_name) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2471 | 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] | 2472 | } |
| 2473 | |
| 2474 | |
| 2475 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2476 | * 8: Now we can work with the cookies. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2477 | * Note that doing so might move headers in the request, but |
| 2478 | * the fields will stay coherent and the URI will not move. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2479 | * This should only be performed in the backend. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2480 | */ |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 2481 | if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name) |
| 2482 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2483 | manage_client_side_cookies(t, req); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2484 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2485 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2486 | /* |
Willy Tarreau | bb046ac | 2007-03-03 19:17:03 +0100 | [diff] [blame] | 2487 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 2488 | * asks for it. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2489 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2490 | if ((t->fe->options | t->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2491 | if (t->cli_addr.ss_family == AF_INET) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2492 | /* Add an X-Forwarded-For header unless the source IP is |
| 2493 | * in the 'except' network range. |
| 2494 | */ |
| 2495 | if ((!t->fe->except_mask.s_addr || |
| 2496 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr) |
| 2497 | != t->fe->except_net.s_addr) && |
| 2498 | (!t->be->except_mask.s_addr || |
| 2499 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr) |
| 2500 | != t->be->except_net.s_addr)) { |
| 2501 | int len; |
| 2502 | unsigned char *pn; |
| 2503 | pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr; |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2504 | |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2505 | /* Note: we rely on the backend to get the header name to be used for |
| 2506 | * x-forwarded-for, because the header is really meant for the backends. |
| 2507 | * However, if the backend did not specify any option, we have to rely |
| 2508 | * on the frontend's header name. |
| 2509 | */ |
| 2510 | if (t->be->fwdfor_hdr_len) { |
| 2511 | len = t->be->fwdfor_hdr_len; |
| 2512 | memcpy(trash, t->be->fwdfor_hdr_name, len); |
| 2513 | } else { |
| 2514 | len = t->fe->fwdfor_hdr_len; |
| 2515 | memcpy(trash, t->fe->fwdfor_hdr_name, len); |
| 2516 | } |
| 2517 | 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] | 2518 | |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2519 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2520 | &txn->hdr_idx, trash, len)) < 0) |
| 2521 | goto return_bad_req; |
| 2522 | } |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2523 | } |
| 2524 | else if (t->cli_addr.ss_family == AF_INET6) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2525 | /* FIXME: for the sake of completeness, we should also support |
| 2526 | * 'except' here, although it is mostly useless in this case. |
| 2527 | */ |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2528 | int len; |
| 2529 | char pn[INET6_ADDRSTRLEN]; |
| 2530 | inet_ntop(AF_INET6, |
| 2531 | (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr, |
| 2532 | pn, sizeof(pn)); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2533 | |
| 2534 | /* Note: we rely on the backend to get the header name to be used for |
| 2535 | * x-forwarded-for, because the header is really meant for the backends. |
| 2536 | * However, if the backend did not specify any option, we have to rely |
| 2537 | * on the frontend's header name. |
| 2538 | */ |
| 2539 | if (t->be->fwdfor_hdr_len) { |
| 2540 | len = t->be->fwdfor_hdr_len; |
| 2541 | memcpy(trash, t->be->fwdfor_hdr_name, len); |
| 2542 | } else { |
| 2543 | len = t->fe->fwdfor_hdr_len; |
| 2544 | memcpy(trash, t->fe->fwdfor_hdr_name, len); |
| 2545 | } |
| 2546 | len += sprintf(trash + len, ": %s", pn); |
| 2547 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2548 | if (unlikely(http_header_add_tail2(req, &txn->req, |
| 2549 | &txn->hdr_idx, trash, len)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2550 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2551 | } |
| 2552 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2553 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2554 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2555 | * 10: add "Connection: close" if needed and not yet set. |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2556 | * 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] | 2557 | */ |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2558 | if (!(t->flags & SN_CONN_CLOSED) && |
Krzysztof Oledzki | 336d475 | 2007-12-25 02:40:22 +0100 | [diff] [blame] | 2559 | ((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] | 2560 | if ((unlikely(msg->sl.rq.v_l != 8) || |
| 2561 | unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) && |
| 2562 | unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx, |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2563 | "Connection: close", 17)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2564 | goto return_bad_req; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2565 | t->flags |= SN_CONN_CLOSED; |
Willy Tarreau | e15d913 | 2006-12-14 22:26:42 +0100 | [diff] [blame] | 2566 | } |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2567 | /* Before we switch to data, was assignment set in manage_client_side_cookie? |
| 2568 | * If not assigned, perhaps we are balancing on url_param, but this is a |
| 2569 | * POST; and the parameters are in the body, maybe scan there to find our server. |
| 2570 | * (unless headers overflowed the buffer?) |
| 2571 | */ |
| 2572 | if (!(t->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 2573 | t->txn.meth == HTTP_METH_POST && t->be->url_param_name != NULL && |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2574 | 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] | 2575 | memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) { |
| 2576 | /* are there enough bytes here? total == l || r || rlim ? |
| 2577 | * len is unsigned, but eoh is int, |
| 2578 | * how many bytes of body have we received? |
| 2579 | * eoh is the first empty line of the header |
| 2580 | */ |
| 2581 | /* 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] | 2582 | 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] | 2583 | |
| 2584 | /* If we have HTTP/1.1 and Expect: 100-continue, then abort. |
| 2585 | * We can't assume responsibility for the server's decision, |
| 2586 | * on this URI and header set. See rfc2616: 14.20, 8.2.3, |
| 2587 | * We also can't change our mind later, about which server to choose, so round robin. |
| 2588 | */ |
| 2589 | if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) { |
| 2590 | struct hdr_ctx ctx; |
| 2591 | ctx.idx = 0; |
| 2592 | /* Expect is allowed in 1.1, look for it */ |
| 2593 | http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx); |
| 2594 | if (ctx.idx != 0 && |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2595 | 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] | 2596 | /* We can't reliablly stall and wait for data, because of |
| 2597 | * .NET clients that don't conform to rfc2616; so, no need for |
| 2598 | * the next block to check length expectations. |
| 2599 | * We could send 100 status back to the client, but then we need to |
| 2600 | * re-write headers, and send the message. And this isn't the right |
| 2601 | * place for that action. |
| 2602 | * TODO: support Expect elsewhere and delete this block. |
| 2603 | */ |
| 2604 | goto end_check_maybe_wait_for_body; |
| 2605 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2606 | |
| 2607 | if (likely(len > t->be->url_param_post_limit)) { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2608 | /* nothing to do, we got enough */ |
| 2609 | } else { |
| 2610 | /* limit implies we are supposed to need this many bytes |
| 2611 | * to find the parameter. Let's see how many bytes we can wait for. |
| 2612 | */ |
| 2613 | long long hint = len; |
| 2614 | struct hdr_ctx ctx; |
| 2615 | ctx.idx = 0; |
| 2616 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx); |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2617 | 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] | 2618 | buffer_write_dis(req); |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2619 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2620 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2621 | else { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2622 | ctx.idx = 0; |
| 2623 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx); |
| 2624 | /* now if we have a length, we'll take the hint */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2625 | if (ctx.idx) { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2626 | /* We have Content-Length */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2627 | if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint)) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2628 | hint = 0; /* parse failure, untrusted client */ |
| 2629 | else { |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2630 | if (hint > 0) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2631 | msg->hdr_content_len = hint; |
| 2632 | else |
| 2633 | hint = 0; /* bad client, sent negative length */ |
| 2634 | } |
| 2635 | } |
| 2636 | /* 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] | 2637 | if (t->be->url_param_post_limit < hint) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2638 | hint = t->be->url_param_post_limit; |
| 2639 | /* now do we really need to buffer more data? */ |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2640 | if (len < hint) { |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 2641 | buffer_write_dis(req); |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2642 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2643 | } |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2644 | /* else... There are no body bytes to wait for */ |
| 2645 | } |
| 2646 | } |
| 2647 | } |
| 2648 | end_check_maybe_wait_for_body: |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2649 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2650 | /************************************************************* |
| 2651 | * OK, that's finished for the headers. We have done what we * |
| 2652 | * could. Let's switch to the DATA state. * |
| 2653 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2654 | |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2655 | buffer_set_rlim(req, BUFSIZE); /* no more rewrite needed */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2656 | t->logs.tv_request = now; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2657 | |
Willy Tarreau | 1fa3126 | 2007-12-03 00:36:16 +0100 | [diff] [blame] | 2658 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 2659 | * which may be the same as the connect timeout if unspecified. |
| 2660 | * 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] | 2661 | * eventually expire. We build the tarpit as an analyser. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2662 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2663 | if (txn->flags & TX_CLTARPIT) { |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2664 | buffer_flush(t->req); |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2665 | /* flush the request so that we can drop the connection early |
| 2666 | * if the client closes first. |
| 2667 | */ |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 2668 | buffer_write_dis(req); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2669 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 2670 | req->analyse_exp = tick_add_ifset(now_ms, t->be->timeout.tarpit); |
| 2671 | if (!req->analyse_exp) |
| 2672 | req->analyse_exp = now_ms; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2673 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2674 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2675 | /* OK let's go on with the BODY now */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2676 | goto end_of_headers; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2677 | |
| 2678 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2679 | txn->req.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2680 | txn->status = 400; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2681 | req->analysers = 0; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2682 | client_retnclose(t, error_message(t, HTTP_ERR_400)); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 2683 | t->fe->failed_req++; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2684 | return_prx_cond: |
| 2685 | if (!(t->flags & SN_ERR_MASK)) |
| 2686 | t->flags |= SN_ERR_PRXCOND; |
| 2687 | if (!(t->flags & SN_FINST_MASK)) |
| 2688 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2689 | return 0; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2690 | end_of_headers: |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2691 | ; // to keep gcc happy |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2692 | } |
| 2693 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2694 | if (req->analysers & AN_REQ_HTTP_TARPIT) { |
| 2695 | struct http_txn *txn = &t->txn; |
| 2696 | |
| 2697 | /* This connection is being tarpitted. The CLIENT side has |
| 2698 | * already set the connect expiration date to the right |
| 2699 | * timeout. We just have to check that the client is still |
| 2700 | * there and that the timeout has not expired. |
| 2701 | */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 2702 | if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 && |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2703 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 2704 | return 0; |
| 2705 | |
| 2706 | /* We will set the queue timer to the time spent, just for |
| 2707 | * logging purposes. We fake a 500 server error, so that the |
| 2708 | * attacker will not suspect his connection has been tarpitted. |
| 2709 | * It will not cause trouble to the logs because we can exclude |
| 2710 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 2711 | */ |
| 2712 | trace_term(t, TT_HTTP_SRV_2); |
| 2713 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 2714 | |
| 2715 | txn->status = 500; |
| 2716 | if (req->flags != BF_READ_ERROR) |
| 2717 | client_retnclose(t, error_message(t, HTTP_ERR_500)); |
| 2718 | |
| 2719 | req->analysers = 0; |
| 2720 | req->analyse_exp = TICK_ETERNITY; |
| 2721 | |
| 2722 | t->fe->failed_req++; |
| 2723 | if (!(t->flags & SN_ERR_MASK)) |
| 2724 | t->flags |= SN_ERR_PRXCOND; |
| 2725 | if (!(t->flags & SN_FINST_MASK)) |
| 2726 | t->flags |= SN_FINST_T; |
| 2727 | return 0; |
| 2728 | } |
| 2729 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2730 | if (req->analysers & AN_REQ_HTTP_BODY) { |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2731 | /* We have to parse the HTTP request body to find any required data. |
| 2732 | * "balance url_param check_post" should have been the only way to get |
| 2733 | * into this. We were brought here after HTTP header analysis, so all |
| 2734 | * related structures are ready. |
| 2735 | */ |
| 2736 | struct http_msg *msg = &t->txn.req; |
| 2737 | unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1; |
| 2738 | long long limit = t->be->url_param_post_limit; |
| 2739 | struct hdr_ctx ctx; |
| 2740 | |
| 2741 | ctx.idx = 0; |
| 2742 | |
| 2743 | /* now if we have a length, we'll take the hint */ |
| 2744 | http_find_header2("Transfer-Encoding", 17, msg->sol, &t->txn.hdr_idx, &ctx); |
| 2745 | if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) { |
| 2746 | unsigned int chunk = 0; |
| 2747 | while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) { |
| 2748 | char c = msg->sol[body]; |
| 2749 | if (ishex(c)) { |
| 2750 | unsigned int hex = toupper(c) - '0'; |
| 2751 | if (hex > 9) |
| 2752 | hex -= 'A' - '9' - 1; |
| 2753 | chunk = (chunk << 4) | hex; |
| 2754 | } else |
| 2755 | break; |
| 2756 | body++; |
| 2757 | } |
| 2758 | if (body + 2 >= req->l) /* we want CRLF too */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2759 | goto http_body_end; /* end of buffer? data missing! */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2760 | |
| 2761 | if (memcmp(msg->sol+body, "\r\n", 2) != 0) |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2762 | 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] | 2763 | |
| 2764 | body += 2; // skip CRLF |
| 2765 | |
| 2766 | /* if we support more then one chunk here, we have to do it again when assigning server |
| 2767 | * 1. how much entity data do we have? new var |
| 2768 | * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here |
| 2769 | * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found) |
| 2770 | */ |
| 2771 | |
| 2772 | if (chunk < limit) |
| 2773 | limit = chunk; /* only reading one chunk */ |
| 2774 | } else { |
| 2775 | if (msg->hdr_content_len < limit) |
| 2776 | limit = msg->hdr_content_len; |
| 2777 | } |
| 2778 | |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2779 | http_body_end: |
| 2780 | /* we leave once we know we have nothing left to do. This means that we have |
| 2781 | * enough bytes, or that we know we'll not get any more (buffer full, read |
| 2782 | * buffer closed). |
| 2783 | */ |
| 2784 | if (req->l - body >= limit || /* enough bytes! */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 2785 | req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR | BF_READ_TIMEOUT) || |
Willy Tarreau | c52164a | 2008-08-17 19:17:57 +0200 | [diff] [blame] | 2786 | tick_is_expired(req->analyse_exp, now_ms)) { |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2787 | /* 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] | 2788 | 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] | 2789 | req->analysers &= ~AN_REQ_HTTP_BODY; |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 2790 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2791 | } |
Willy Tarreau | c52164a | 2008-08-17 19:17:57 +0200 | [diff] [blame] | 2792 | else { |
| 2793 | /* Not enough data. We'll re-use the http-request |
| 2794 | * timeout here. Ideally, we should set the timeout |
| 2795 | * relative to the accept() date. We just set the |
| 2796 | * request timeout once at the beginning of the |
| 2797 | * request. |
| 2798 | */ |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 2799 | buffer_write_dis(req); |
Willy Tarreau | c52164a | 2008-08-17 19:17:57 +0200 | [diff] [blame] | 2800 | if (!tick_isset(req->analyse_exp)) |
| 2801 | req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq); |
| 2802 | return 0; |
| 2803 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2804 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2805 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2806 | /* Note: eventhough nobody should set an unknown flag, clearing them right now will |
| 2807 | * probably reduce one day's debugging session. |
| 2808 | */ |
| 2809 | #ifdef DEBUG_DEV |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2810 | 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] | 2811 | fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n", |
| 2812 | __FILE__, __LINE__, req->analysers); |
| 2813 | ABORT_NOW(); |
| 2814 | } |
| 2815 | #endif |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2816 | 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] | 2817 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2818 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2819 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2820 | /* This function performs all the processing enabled for the current response. |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2821 | * It normally returns zero, but may return 1 if it absolutely needs to be |
| 2822 | * called again after other functions. It relies on buffers flags, and updates |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2823 | * 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] | 2824 | * functions. It works like process_request (see indications above). |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2825 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2826 | int process_response(struct session *t) |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2827 | { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2828 | struct http_txn *txn = &t->txn; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2829 | struct buffer *req = t->req; |
| 2830 | struct buffer *rep = t->rep; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2831 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 2832 | 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] | 2833 | now_ms, __FUNCTION__, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 2834 | t, |
| 2835 | rep, |
| 2836 | rep->rex, rep->wex, |
| 2837 | rep->flags, |
| 2838 | rep->l, |
| 2839 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 2840 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2841 | if (rep->analysers & AN_RTR_HTTP_HDR) { /* receiving server headers */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2842 | /* |
| 2843 | * Now parse the partial (or complete) lines. |
| 2844 | * We will check the response syntax, and also join multi-line |
| 2845 | * headers. An index of all the lines will be elaborated while |
| 2846 | * parsing. |
| 2847 | * |
| 2848 | * For the parsing, we use a 28 states FSM. |
| 2849 | * |
| 2850 | * Here is the information we currently have : |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2851 | * rep->data + rep->som = beginning of response |
| 2852 | * rep->data + rep->eoh = end of processed headers / start of current one |
| 2853 | * 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] | 2854 | * rep->lr = first non-visited byte |
| 2855 | * rep->r = end of data |
| 2856 | */ |
Willy Tarreau | 7f875f6 | 2008-08-11 17:35:01 +0200 | [diff] [blame] | 2857 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2858 | int cur_idx; |
| 2859 | struct http_msg *msg = &txn->rsp; |
| 2860 | struct proxy *cur_proxy; |
| 2861 | |
| 2862 | if (likely(rep->lr < rep->r)) |
| 2863 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
| 2864 | |
| 2865 | /* 1: we might have to print this header in debug mode */ |
| 2866 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2867 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
| 2868 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
| 2869 | char *eol, *sol; |
| 2870 | |
| 2871 | sol = rep->data + msg->som; |
| 2872 | eol = sol + msg->sl.rq.l; |
| 2873 | debug_hdr("srvrep", t, sol, eol); |
| 2874 | |
| 2875 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2876 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
| 2877 | |
| 2878 | while (cur_idx) { |
| 2879 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2880 | debug_hdr("srvhdr", t, sol, eol); |
| 2881 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2882 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2883 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2884 | } |
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 | /* |
| 2887 | * Now we quickly check if we have found a full valid response. |
| 2888 | * If not so, we check the FD and buffer states before leaving. |
| 2889 | * A full response is indicated by the fact that we have seen |
| 2890 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 2891 | * responses are checked first. |
| 2892 | * |
| 2893 | * Depending on whether the client is still there or not, we |
| 2894 | * may send an error response back or not. Note that normally |
| 2895 | * we should only check for HTTP status there, and check I/O |
| 2896 | * errors somewhere else. |
| 2897 | */ |
| 2898 | |
| 2899 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2900 | /* Invalid response */ |
| 2901 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 2902 | hdr_response_bad: |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2903 | //buffer_shutr(rep); |
| 2904 | //buffer_shutw(req); |
| 2905 | //fd_delete(req->cons->fd); |
| 2906 | //req->cons->state = SI_ST_CLO; |
| 2907 | buffer_shutr_now(rep); |
| 2908 | buffer_shutw_now(req); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2909 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2910 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2911 | t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2912 | //sess_change_server(t, NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2913 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2914 | t->be->failed_resp++; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2915 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2916 | txn->status = 502; |
| 2917 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 2918 | if (!(t->flags & SN_ERR_MASK)) |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2919 | t->flags |= SN_ERR_PRXCOND; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2920 | if (!(t->flags & SN_FINST_MASK)) |
| 2921 | t->flags |= SN_FINST_H; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 2922 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2923 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 2924 | // process_srv_queue(t->srv); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2925 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2926 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2927 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2928 | /* too large response does not fit in buffer. */ |
| 2929 | else if (rep->flags & BF_FULL) { |
| 2930 | goto hdr_response_bad; |
| 2931 | } |
| 2932 | /* read error */ |
| 2933 | else if (rep->flags & BF_READ_ERROR) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2934 | buffer_shutr_now(rep); |
| 2935 | buffer_shutw_now(req); |
| 2936 | //fd_delete(req->cons->fd); |
| 2937 | //req->cons->state = SI_ST_CLO; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2938 | //if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2939 | //t->srv->cur_sess--; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2940 | //t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2941 | //sess_change_server(t, NULL); |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2942 | //} |
| 2943 | //t->be->failed_resp++; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2944 | rep->analysers = 0; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2945 | txn->status = 502; |
| 2946 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 2947 | if (!(t->flags & SN_ERR_MASK)) |
| 2948 | t->flags |= SN_ERR_SRVCL; |
| 2949 | if (!(t->flags & SN_FINST_MASK)) |
| 2950 | t->flags |= SN_FINST_H; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 2951 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2952 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 2953 | // process_srv_queue(t->srv); |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2954 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2955 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2956 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2957 | /* read timeout : return a 504 to the client. */ |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2958 | else if (rep->flags & BF_READ_TIMEOUT) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2959 | buffer_shutr_now(rep); |
| 2960 | buffer_shutw_now(req); |
| 2961 | //fd_delete(req->cons->fd); |
| 2962 | //req->cons->state = SI_ST_CLO; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2963 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2964 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2965 | t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2966 | //sess_change_server(t, NULL); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2967 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2968 | t->be->failed_resp++; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2969 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2970 | txn->status = 504; |
| 2971 | client_return(t, error_message(t, HTTP_ERR_504)); |
| 2972 | if (!(t->flags & SN_ERR_MASK)) |
| 2973 | t->flags |= SN_ERR_SRVTO; |
| 2974 | if (!(t->flags & SN_FINST_MASK)) |
| 2975 | t->flags |= SN_FINST_H; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 2976 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2977 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 2978 | // process_srv_queue(t->srv); |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2979 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2980 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2981 | /* write error to client, or close from server */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 2982 | else if (rep->flags & (BF_WRITE_ERROR|BF_SHUTR)) { |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2983 | buffer_shutr_now(rep); |
| 2984 | buffer_shutw_now(req); |
| 2985 | //fd_delete(req->cons->fd); |
| 2986 | //req->cons->state = SI_ST_CLO; |
| 2987 | if (t->srv) { |
| 2988 | //t->srv->cur_sess--; |
| 2989 | t->srv->failed_resp++; |
| 2990 | //sess_change_server(t, NULL); |
| 2991 | } |
| 2992 | t->be->failed_resp++; |
| 2993 | rep->analysers = 0; |
| 2994 | txn->status = 502; |
| 2995 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 2996 | if (!(t->flags & SN_ERR_MASK)) |
| 2997 | t->flags |= SN_ERR_SRVCL; |
| 2998 | if (!(t->flags & SN_FINST_MASK)) |
| 2999 | t->flags |= SN_FINST_H; |
| 3000 | |
| 3001 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 3002 | // process_srv_queue(t->srv); |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 3003 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 3004 | return 0; |
| 3005 | } |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 3006 | buffer_write_dis(rep); |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 3007 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3008 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3009 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3010 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3011 | /***************************************************************** |
| 3012 | * More interesting part now : we know that we have a complete * |
| 3013 | * response which at least looks like HTTP. We have an indicator * |
| 3014 | * of each header's length, so we can parse them quickly. * |
| 3015 | ****************************************************************/ |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3016 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3017 | rep->analysers &= ~AN_RTR_HTTP_HDR; |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 3018 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3019 | /* ensure we keep this pointer to the beginning of the message */ |
| 3020 | msg->sol = rep->data + msg->som; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3021 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3022 | /* |
| 3023 | * 1: get the status code and check for cacheability. |
| 3024 | */ |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3025 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3026 | t->logs.logwait &= ~LW_RESP; |
| 3027 | 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] | 3028 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3029 | switch (txn->status) { |
| 3030 | case 200: |
| 3031 | case 203: |
| 3032 | case 206: |
| 3033 | case 300: |
| 3034 | case 301: |
| 3035 | case 410: |
| 3036 | /* RFC2616 @13.4: |
| 3037 | * "A response received with a status code of |
| 3038 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 3039 | * by a cache (...) unless a cache-control |
| 3040 | * directive prohibits caching." |
| 3041 | * |
| 3042 | * RFC2616 @9.5: POST method : |
| 3043 | * "Responses to this method are not cacheable, |
| 3044 | * unless the response includes appropriate |
| 3045 | * Cache-Control or Expires header fields." |
| 3046 | */ |
| 3047 | if (likely(txn->meth != HTTP_METH_POST) && |
| 3048 | (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
| 3049 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 3050 | break; |
| 3051 | default: |
| 3052 | break; |
| 3053 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3054 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3055 | /* |
| 3056 | * 2: we may need to capture headers |
| 3057 | */ |
| 3058 | if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap)) |
| 3059 | capture_headers(rep->data + msg->som, &txn->hdr_idx, |
| 3060 | txn->rsp.cap, t->fe->rsp_cap); |
| 3061 | |
| 3062 | /* |
| 3063 | * 3: we will have to evaluate the filters. |
| 3064 | * As opposed to version 1.2, now they will be evaluated in the |
| 3065 | * filters order and not in the header order. This means that |
| 3066 | * each filter has to be validated among all headers. |
| 3067 | * |
| 3068 | * Filters are tried with ->be first, then with ->fe if it is |
| 3069 | * different from ->be. |
| 3070 | */ |
| 3071 | |
| 3072 | t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */ |
| 3073 | |
| 3074 | cur_proxy = t->be; |
| 3075 | while (1) { |
| 3076 | struct proxy *rule_set = cur_proxy; |
| 3077 | |
| 3078 | /* try headers filters */ |
| 3079 | if (rule_set->rsp_exp != NULL) { |
| 3080 | if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) { |
| 3081 | return_bad_resp: |
| 3082 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3083 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3084 | t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3085 | //sess_change_server(t, NULL); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3086 | } |
| 3087 | cur_proxy->failed_resp++; |
| 3088 | return_srv_prx_502: |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3089 | buffer_shutr_now(rep); |
| 3090 | buffer_shutw_now(req); |
| 3091 | //fd_delete(req->cons->fd); |
| 3092 | //req->cons->state = SI_ST_CLO; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3093 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3094 | txn->status = 502; |
| 3095 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 3096 | if (!(t->flags & SN_ERR_MASK)) |
| 3097 | t->flags |= SN_ERR_PRXCOND; |
| 3098 | if (!(t->flags & SN_FINST_MASK)) |
| 3099 | t->flags |= SN_FINST_H; |
| 3100 | /* We used to have a free connection slot. Since we'll never use it, |
| 3101 | * we have to inform the server that it may be used by another session. |
| 3102 | */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3103 | //if (t->srv && may_dequeue_tasks(t->srv, cur_proxy)) |
| 3104 | // process_srv_queue(t->srv); |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3105 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3106 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3107 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3108 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3109 | /* has the response been denied ? */ |
| 3110 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | f899b94 | 2008-03-28 18:09:38 +0100 | [diff] [blame] | 3111 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3112 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3113 | t->srv->failed_secu++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3114 | //sess_change_server(t, NULL); |
Willy Tarreau | f899b94 | 2008-03-28 18:09:38 +0100 | [diff] [blame] | 3115 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3116 | cur_proxy->denied_resp++; |
| 3117 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 3118 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3119 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3120 | /* We might have to check for "Connection:" */ |
| 3121 | if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) && |
| 3122 | !(t->flags & SN_CONN_CLOSED)) { |
| 3123 | char *cur_ptr, *cur_end, *cur_next; |
| 3124 | int cur_idx, old_idx, delta, val; |
| 3125 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3126 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3127 | cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 3128 | old_idx = 0; |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 3129 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3130 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 3131 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 3132 | cur_ptr = cur_next; |
| 3133 | cur_end = cur_ptr + cur_hdr->len; |
| 3134 | cur_next = cur_end + cur_hdr->cr + 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3135 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3136 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 3137 | if (val) { |
| 3138 | /* 3 possibilities : |
| 3139 | * - we have already set Connection: close, |
| 3140 | * so we remove this line. |
| 3141 | * - we have not yet set Connection: close, |
| 3142 | * but this line indicates close. We leave |
| 3143 | * it untouched and set the flag. |
| 3144 | * - we have not yet set Connection: close, |
| 3145 | * and this line indicates non-close. We |
| 3146 | * replace it. |
| 3147 | */ |
| 3148 | if (t->flags & SN_CONN_CLOSED) { |
| 3149 | delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0); |
| 3150 | txn->rsp.eoh += delta; |
| 3151 | cur_next += delta; |
| 3152 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 3153 | txn->hdr_idx.used--; |
| 3154 | cur_hdr->len = 0; |
| 3155 | } else { |
| 3156 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 3157 | delta = buffer_replace2(rep, cur_ptr + val, cur_end, |
| 3158 | "close", 5); |
| 3159 | cur_next += delta; |
| 3160 | cur_hdr->len += delta; |
| 3161 | txn->rsp.eoh += delta; |
| 3162 | } |
| 3163 | t->flags |= SN_CONN_CLOSED; |
| 3164 | } |
| 3165 | } |
| 3166 | old_idx = cur_idx; |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 3167 | } |
| 3168 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3169 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3170 | /* add response headers from the rule sets in the same order */ |
| 3171 | for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) { |
| 3172 | if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, |
| 3173 | rule_set->rsp_add[cur_idx])) < 0) |
| 3174 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 3175 | } |
| 3176 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3177 | /* check whether we're already working on the frontend */ |
| 3178 | if (cur_proxy == t->fe) |
| 3179 | break; |
| 3180 | cur_proxy = t->fe; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3181 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3182 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3183 | /* |
| 3184 | * 4: check for server cookie. |
| 3185 | */ |
| 3186 | if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name |
| 3187 | || (t->be->options & PR_O_CHK_CACHE)) |
| 3188 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3189 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3190 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3191 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3192 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3193 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3194 | if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0) |
| 3195 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3196 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3197 | /* |
| 3198 | * 6: add server cookie in the response if needed |
| 3199 | */ |
| 3200 | if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) && |
| 3201 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) { |
| 3202 | int len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3203 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3204 | /* the server is known, it's not the one the client requested, we have to |
| 3205 | * insert a set-cookie here, except if we want to insert only on POST |
| 3206 | * requests and this one isn't. Note that servers which don't have cookies |
| 3207 | * (eg: some backup servers) will return a full cookie removal request. |
| 3208 | */ |
| 3209 | len = sprintf(trash, "Set-Cookie: %s=%s; path=/", |
| 3210 | t->be->cookie_name, |
| 3211 | 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] | 3212 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3213 | if (t->be->cookie_domain) |
| 3214 | len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3215 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3216 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3217 | trash, len)) < 0) |
| 3218 | goto return_bad_resp; |
| 3219 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3220 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3221 | /* Here, we will tell an eventual cache on the client side that we don't |
| 3222 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 3223 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 3224 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 3225 | */ |
| 3226 | if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3227 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3228 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3229 | |
| 3230 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3231 | "Cache-control: private", 22)) < 0) |
| 3232 | goto return_bad_resp; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3233 | } |
| 3234 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3235 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3236 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3237 | /* |
| 3238 | * 7: check if result will be cacheable with a cookie. |
| 3239 | * We'll block the response if security checks have caught |
| 3240 | * nasty things such as a cacheable cookie. |
| 3241 | */ |
| 3242 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) == |
| 3243 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) && |
| 3244 | (t->be->options & PR_O_CHK_CACHE)) { |
| 3245 | |
| 3246 | /* we're in presence of a cacheable response containing |
| 3247 | * a set-cookie header. We'll block it as requested by |
| 3248 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3249 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3250 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3251 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3252 | t->srv->failed_secu++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3253 | //sess_change_server(t, NULL); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3254 | } |
| 3255 | t->be->denied_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3256 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3257 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 3258 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 3259 | send_log(t->be, LOG_ALERT, |
| 3260 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 3261 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 3262 | goto return_srv_prx_502; |
| 3263 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3264 | |
| 3265 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3266 | * 8: add "Connection: close" if needed and not yet set. |
| 3267 | * 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] | 3268 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3269 | if (!(t->flags & SN_CONN_CLOSED) && |
| 3270 | ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) { |
| 3271 | if ((unlikely(msg->sl.st.v_l != 8) || |
| 3272 | unlikely(req->data[msg->som + 7] != '0')) && |
| 3273 | unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3274 | "Connection: close", 17)) < 0) |
| 3275 | goto return_bad_resp; |
| 3276 | t->flags |= SN_CONN_CLOSED; |
| 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 | /************************************************************* |
| 3280 | * OK, that's finished for the headers. We have done what we * |
| 3281 | * could. Let's switch to the DATA state. * |
| 3282 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3283 | |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 3284 | buffer_set_rlim(rep, BUFSIZE); /* no more rewrite needed */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3285 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3286 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3287 | #ifdef CONFIG_HAP_TCPSPLICE |
| 3288 | if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
| 3289 | /* TCP splicing supported by both FE and BE */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 3290 | tcp_splice_splicefd(rep->cons->fd, rep->prod->fd, 0); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3291 | } |
| 3292 | #endif |
| 3293 | /* if the user wants to log as soon as possible, without counting |
| 3294 | * bytes from the server, then this is the right moment. We have |
| 3295 | * to temporarily assign bytes_out to log what we currently have. |
| 3296 | */ |
| 3297 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 3298 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 3299 | t->logs.bytes_out = txn->rsp.eoh; |
| 3300 | if (t->fe->to_log & LW_REQ) |
| 3301 | http_sess_log(t); |
| 3302 | else |
| 3303 | tcp_sess_log(t); |
| 3304 | t->logs.bytes_out = 0; |
| 3305 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3306 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3307 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 3308 | * otherwise we would not let the client side wake up. |
| 3309 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3310 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3311 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3312 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3313 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3314 | /* Note: eventhough nobody should set an unknown flag, clearing them right now will |
| 3315 | * probably reduce one day's debugging session. |
| 3316 | */ |
| 3317 | #ifdef DEBUG_DEV |
| 3318 | if (rep->analysers & ~(AN_RTR_HTTP_HDR)) { |
| 3319 | fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n", |
| 3320 | __FILE__, __LINE__, rep->analysers); |
| 3321 | ABORT_NOW(); |
| 3322 | } |
| 3323 | #endif |
| 3324 | rep->analysers &= AN_RTR_HTTP_HDR; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3325 | return 0; |
| 3326 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3327 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 3328 | ///* |
| 3329 | // * Manages the client FSM and its socket. It normally returns zero, but may |
| 3330 | // * return 1 if it absolutely wants to be called again. |
| 3331 | // * |
| 3332 | // * Note: process_cli is the ONLY function allowed to set cli_state to anything |
| 3333 | // * but CL_STCLOSE. |
| 3334 | // */ |
| 3335 | //int process_cli(struct session *t) |
| 3336 | //{ |
| 3337 | // struct buffer *req = t->req; |
| 3338 | // struct buffer *rep = t->rep; |
| 3339 | // |
| 3340 | // 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", |
| 3341 | // now_ms, __FUNCTION__, |
| 3342 | // t->cli_fd, t->cli_fd >= 0 ? fdtab[t->cli_fd].state : 0, /* fd,state*/ |
| 3343 | // cli_stnames[t->cli_state], |
| 3344 | // t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_RD) : 0, |
| 3345 | // t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_WR) : 0, |
| 3346 | // req->rex, rep->wex, |
| 3347 | // req->flags, rep->flags, |
| 3348 | // req->l, rep->l); |
| 3349 | // |
| 3350 | // update_state: |
| 3351 | // /* FIXME: we still have to check for CL_STSHUTR because client_retnclose |
| 3352 | // * still set this state (and will do until unix sockets are converted). |
| 3353 | // */ |
| 3354 | // if (t->cli_state == CL_STDATA || t->cli_state == CL_STSHUTR) { |
| 3355 | // /* we can skip most of the tests at once if some conditions are not met */ |
| 3356 | // if (!((fdtab[t->cli_fd].state == FD_STERROR) || |
| 3357 | // (req->flags & (BF_READ_TIMEOUT|BF_READ_ERROR|BF_SHUTR_NOW)) || |
| 3358 | // (rep->flags & (BF_WRITE_TIMEOUT|BF_WRITE_ERROR|BF_SHUTW_NOW)) || |
| 3359 | // (!(req->flags & BF_SHUTR) && req->flags & (BF_READ_NULL|BF_SHUTW)) || |
| 3360 | // (!(rep->flags & BF_SHUTW) && |
| 3361 | // (rep->flags & (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR)) == (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR)))) |
| 3362 | // goto update_timeouts; |
| 3363 | // |
| 3364 | // /* read or write error */ |
| 3365 | // if (fdtab[t->cli_fd].state == FD_STERROR) { |
| 3366 | // buffer_shutr(req); |
| 3367 | // req->flags |= BF_READ_ERROR; |
| 3368 | // buffer_shutw(rep); |
| 3369 | // rep->flags |= BF_WRITE_ERROR; |
| 3370 | // fd_delete(t->cli_fd); |
| 3371 | // t->cli_state = CL_STCLOSE; |
| 3372 | // trace_term(t, TT_HTTP_CLI_1); |
| 3373 | // if (!req->analysers) { |
| 3374 | // if (!(t->flags & SN_ERR_MASK)) |
| 3375 | // t->flags |= SN_ERR_CLICL; |
| 3376 | // if (!(t->flags & SN_FINST_MASK)) { |
| 3377 | // if (req->cons->err_type <= SI_ET_QUEUE_ABRT) |
| 3378 | // t->flags |= SN_FINST_Q; |
| 3379 | // else if (req->cons->err_type <= SI_ET_CONN_OTHER) |
| 3380 | // t->flags |= SN_FINST_C; |
| 3381 | // else |
| 3382 | // t->flags |= SN_FINST_D; |
| 3383 | // } |
| 3384 | // } |
| 3385 | // goto update_state; |
| 3386 | // } |
| 3387 | // /* last read, or end of server write */ |
| 3388 | // else if (!(req->flags & BF_SHUTR) && /* not already done */ |
| 3389 | // req->flags & (BF_READ_NULL|BF_SHUTR_NOW|BF_SHUTW)) { |
| 3390 | // buffer_shutr(req); |
| 3391 | // if (!(rep->flags & BF_SHUTW)) { |
| 3392 | // EV_FD_CLR(t->cli_fd, DIR_RD); |
| 3393 | // trace_term(t, TT_HTTP_CLI_2); |
| 3394 | // } else { |
| 3395 | // /* output was already closed */ |
| 3396 | // fd_delete(t->cli_fd); |
| 3397 | // t->cli_state = CL_STCLOSE; |
| 3398 | // trace_term(t, TT_HTTP_CLI_3); |
| 3399 | // } |
| 3400 | // goto update_state; |
| 3401 | // } |
| 3402 | // /* last server read and buffer empty : we only check them when we're |
| 3403 | // * allowed to forward the data. |
| 3404 | // */ |
| 3405 | // else if (!(rep->flags & BF_SHUTW) && /* not already done */ |
| 3406 | // ((rep->flags & BF_SHUTW_NOW) || |
| 3407 | // (rep->flags & BF_EMPTY && rep->flags & BF_MAY_FORWARD && |
| 3408 | // rep->flags & BF_SHUTR && !(t->flags & SN_SELF_GEN)))) { |
| 3409 | // buffer_shutw(rep); |
| 3410 | // if (!(req->flags & BF_SHUTR)) { |
| 3411 | // EV_FD_CLR(t->cli_fd, DIR_WR); |
| 3412 | // shutdown(t->cli_fd, SHUT_WR); |
| 3413 | // trace_term(t, TT_HTTP_CLI_4); |
| 3414 | // } else { |
| 3415 | // fd_delete(t->cli_fd); |
| 3416 | // t->cli_state = CL_STCLOSE; |
| 3417 | // trace_term(t, TT_HTTP_CLI_5); |
| 3418 | // } |
| 3419 | // goto update_state; |
| 3420 | // } |
| 3421 | // /* read timeout */ |
| 3422 | // else if ((req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT) { |
| 3423 | // buffer_shutr(req); |
| 3424 | // if (!(rep->flags & BF_SHUTW)) { |
| 3425 | // EV_FD_CLR(t->cli_fd, DIR_RD); |
| 3426 | // trace_term(t, TT_HTTP_CLI_6); |
| 3427 | // } else { |
| 3428 | // /* output was already closed */ |
| 3429 | // fd_delete(t->cli_fd); |
| 3430 | // t->cli_state = CL_STCLOSE; |
| 3431 | // trace_term(t, TT_HTTP_CLI_7); |
| 3432 | // } |
| 3433 | // if (!req->analysers) { |
| 3434 | // if (!(t->flags & SN_ERR_MASK)) |
| 3435 | // t->flags |= SN_ERR_CLITO; |
| 3436 | // if (!(t->flags & SN_FINST_MASK)) { |
| 3437 | // if (req->cons->err_type <= SI_ET_QUEUE_ABRT) |
| 3438 | // t->flags |= SN_FINST_Q; |
| 3439 | // else if (req->cons->err_type <= SI_ET_CONN_OTHER) |
| 3440 | // t->flags |= SN_FINST_C; |
| 3441 | // else |
| 3442 | // t->flags |= SN_FINST_D; |
| 3443 | // } |
| 3444 | // } |
| 3445 | // goto update_state; |
| 3446 | // } |
| 3447 | // /* write timeout */ |
| 3448 | // else if ((rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT) { |
| 3449 | // buffer_shutw(rep); |
| 3450 | // if (!(req->flags & BF_SHUTR)) { |
| 3451 | // EV_FD_CLR(t->cli_fd, DIR_WR); |
| 3452 | // shutdown(t->cli_fd, SHUT_WR); |
| 3453 | // trace_term(t, TT_HTTP_CLI_8); |
| 3454 | // } else { |
| 3455 | // fd_delete(t->cli_fd); |
| 3456 | // t->cli_state = CL_STCLOSE; |
| 3457 | // trace_term(t, TT_HTTP_CLI_9); |
| 3458 | // } |
| 3459 | // if (!req->analysers) { |
| 3460 | // if (!(t->flags & SN_ERR_MASK)) |
| 3461 | // t->flags |= SN_ERR_CLITO; |
| 3462 | // if (!(t->flags & SN_FINST_MASK)) { |
| 3463 | // if (req->cons->err_type <= SI_ET_QUEUE_ABRT) |
| 3464 | // t->flags |= SN_FINST_Q; |
| 3465 | // else if (req->cons->err_type <= SI_ET_CONN_OTHER) |
| 3466 | // t->flags |= SN_FINST_C; |
| 3467 | // else |
| 3468 | // t->flags |= SN_FINST_D; |
| 3469 | // } |
| 3470 | // } |
| 3471 | // goto update_state; |
| 3472 | // } |
| 3473 | // |
| 3474 | // update_timeouts: |
| 3475 | // /* manage read timeout */ |
| 3476 | // if (!(req->flags & BF_SHUTR)) { |
| 3477 | // if (req->flags & BF_FULL) { |
| 3478 | // /* no room to read more data */ |
| 3479 | // if (EV_FD_COND_C(t->cli_fd, DIR_RD)) { |
| 3480 | // /* stop reading until we get some space */ |
| 3481 | // req->rex = TICK_ETERNITY; |
| 3482 | // } |
| 3483 | // } else { |
| 3484 | // EV_FD_COND_S(t->cli_fd, DIR_RD); |
| 3485 | // req->rex = tick_add_ifset(now_ms, t->fe->timeout.client); |
| 3486 | // } |
| 3487 | // } |
| 3488 | // |
| 3489 | // /* manage write timeout */ |
| 3490 | // if (!(rep->flags & BF_SHUTW)) { |
| 3491 | // /* first, we may have to produce data (eg: stats). |
| 3492 | // * right now, this is limited to the SHUTR state. |
| 3493 | // */ |
| 3494 | // if (req->flags & BF_SHUTR && t->flags & SN_SELF_GEN) { |
| 3495 | // produce_content(t); |
| 3496 | // if (rep->flags & BF_EMPTY) { |
| 3497 | // buffer_shutw(rep); |
| 3498 | // fd_delete(t->cli_fd); |
| 3499 | // t->cli_state = CL_STCLOSE; |
| 3500 | // trace_term(t, TT_HTTP_CLI_10); |
| 3501 | // goto update_state; |
| 3502 | // } |
| 3503 | // } |
| 3504 | // |
| 3505 | // /* we don't enable client write if the buffer is empty, nor if the server has to analyze it */ |
| 3506 | // if ((rep->flags & (BF_EMPTY|BF_MAY_FORWARD)) != BF_MAY_FORWARD) { |
| 3507 | // if (EV_FD_COND_C(t->cli_fd, DIR_WR)) { |
| 3508 | // /* stop writing */ |
| 3509 | // rep->wex = TICK_ETERNITY; |
| 3510 | // } |
| 3511 | // } else { |
| 3512 | // /* buffer not empty */ |
| 3513 | // EV_FD_COND_S(t->cli_fd, DIR_WR); |
| 3514 | // if (!tick_isset(rep->wex)) { |
| 3515 | // /* restart writing */ |
| 3516 | // rep->wex = tick_add_ifset(now_ms, t->fe->timeout.client); |
| 3517 | // if (!(req->flags & BF_SHUTR) && tick_isset(rep->wex) && tick_isset(req->rex)) { |
| 3518 | // /* FIXME: to prevent the client from expiring read timeouts during writes, |
| 3519 | // * we refresh it, except if it was already infinite. */ |
| 3520 | // req->rex = rep->wex; |
| 3521 | // } |
| 3522 | // } |
| 3523 | // } |
| 3524 | // } |
| 3525 | // return 0; /* other cases change nothing */ |
| 3526 | // } |
| 3527 | // else if (t->cli_state == CL_STCLOSE) { /* CL_STCLOSE: nothing to do */ |
| 3528 | // if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 3529 | // int len; |
| 3530 | // 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); |
| 3531 | // write(1, trash, len); |
| 3532 | // } |
| 3533 | // return 0; |
| 3534 | // } |
| 3535 | //#ifdef DEBUG_DEV |
| 3536 | // fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, t->cli_state); |
| 3537 | // ABORT_NOW(); |
| 3538 | //#endif |
| 3539 | // return 0; |
| 3540 | //} |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3541 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3542 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3543 | /* Return 1 if the pending connection has failed and should be retried, |
| 3544 | * otherwise zero. We may only come here in SI_ST_CON state, which means that |
| 3545 | * the socket's file descriptor is known. |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3546 | */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3547 | int tcp_connection_status(struct session *t) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3548 | { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3549 | struct buffer *req = t->req; |
| 3550 | struct buffer *rep = t->rep; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3551 | int conn_err = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3552 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3553 | DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n", |
| 3554 | now_ms, __FUNCTION__, |
| 3555 | cli_stnames[t->cli_state], |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3556 | rep->rex, req->wex, |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 3557 | req->flags, rep->flags, |
| 3558 | req->l, rep->l); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3559 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3560 | if ((req->flags & BF_SHUTW_NOW) || |
| 3561 | (rep->flags & BF_SHUTW) || |
| 3562 | ((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] | 3563 | ((req->flags & BF_EMPTY && !(req->flags & BF_WRITE_ACTIVITY)) || |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3564 | t->be->options & PR_O_ABRT_CLOSE))) { /* give up */ |
| 3565 | |
| 3566 | trace_term(t, TT_HTTP_SRV_5); |
| 3567 | req->wex = TICK_ETERNITY; |
| 3568 | fd_delete(req->cons->fd); |
| 3569 | if (t->srv) { |
| 3570 | t->srv->cur_sess--; |
| 3571 | sess_change_server(t, NULL); |
| 3572 | } |
| 3573 | /* note that this must not return any error because it would be able to |
| 3574 | * overwrite the client_retnclose() output. |
| 3575 | */ |
| 3576 | //srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL); |
| 3577 | |
| 3578 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3579 | buffer_shutw(req); |
| 3580 | buffer_shutr(rep); |
| 3581 | req->cons->state = SI_ST_CLO; |
| 3582 | if (!req->cons->err_type) |
| 3583 | req->cons->err_type = SI_ET_CONN_ABRT; |
| 3584 | req->cons->err_loc = t->srv; |
| 3585 | return 0; |
| 3586 | } |
| 3587 | |
| 3588 | /* check for timeouts and asynchronous connect errors */ |
| 3589 | if (fdtab[req->cons->fd].state == FD_STERROR) { |
| 3590 | conn_err = SI_ET_CONN_ERR; |
| 3591 | if (!req->cons->err_type) |
| 3592 | req->cons->err_type = SI_ET_CONN_ERR; |
| 3593 | } |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 3594 | else if (!(req->flags & BF_WRITE_ACTIVITY)) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3595 | /* nothing happened, maybe we timed out */ |
| 3596 | if (tick_is_expired(req->wex, now_ms)) { |
| 3597 | conn_err = SI_ET_CONN_TO; |
| 3598 | if (!req->cons->err_type) |
| 3599 | req->cons->err_type = SI_ET_CONN_TO; |
| 3600 | } |
| 3601 | else |
| 3602 | return 0; /* let's wait a bit more */ |
| 3603 | } |
| 3604 | |
| 3605 | if (conn_err) { |
| 3606 | fd_delete(req->cons->fd); |
| 3607 | req->cons->state = SI_ST_CLO; |
| 3608 | |
| 3609 | if (t->srv) { |
| 3610 | t->srv->cur_sess--; |
| 3611 | sess_change_server(t, NULL); |
| 3612 | req->cons->err_loc = t->srv; |
| 3613 | } |
| 3614 | |
| 3615 | /* ensure that we have enough retries left */ |
| 3616 | if (srv_count_retry_down(t, conn_err)) |
| 3617 | return 0; |
| 3618 | |
| 3619 | if (conn_err == SI_ET_CONN_ERR) { |
| 3620 | /* we encountered an immediate connection error, and we |
| 3621 | * will have to retry connecting to the same server, most |
| 3622 | * likely leading to the same result. To avoid this, we |
| 3623 | * fake a connection timeout to retry after a turn-around |
| 3624 | * time of 1 second. We will wait in the previous if block. |
| 3625 | */ |
| 3626 | req->cons->state = SI_ST_TAR; |
| 3627 | req->wex = tick_add(now_ms, MS_TO_TICKS(1000)); |
| 3628 | return 0; |
| 3629 | } |
| 3630 | |
| 3631 | if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) { |
| 3632 | /* We're on our last chance, and the REDISP option was specified. |
| 3633 | * We will ignore cookie and force to balance or use the dispatcher. |
| 3634 | */ |
| 3635 | /* let's try to offer this slot to anybody */ |
| 3636 | if (may_dequeue_tasks(t->srv, t->be)) |
| 3637 | process_srv_queue(t->srv); |
| 3638 | |
| 3639 | /* it's left to the dispatcher to choose a server */ |
| 3640 | t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
| 3641 | t->prev_srv = t->srv; |
| 3642 | } else { |
| 3643 | /* we just want to retry */ |
| 3644 | if (t->srv) |
| 3645 | t->srv->retries++; |
| 3646 | t->be->retries++; |
| 3647 | |
| 3648 | /* Now we will try to either reconnect to the same server or |
| 3649 | * connect to another server. If the connection gets queued |
| 3650 | * because all servers are saturated, then we will go back to |
| 3651 | * the idle state where the buffer's consumer is marked as |
| 3652 | * unknown. |
| 3653 | */ |
| 3654 | if (srv_retryable_connect(t)) { |
| 3655 | /* success or unrecoverable error */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3656 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3657 | return 0; |
| 3658 | } |
| 3659 | } |
| 3660 | |
| 3661 | /* We'll rely on the caller to try to get a connection again */ |
| 3662 | return 1; |
| 3663 | } |
| 3664 | else { |
| 3665 | /* no error and write OK : connection succeeded */ |
| 3666 | t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 3667 | req->cons->state = SI_ST_EST; |
| 3668 | req->cons->err_type = SI_ET_NONE; |
| 3669 | req->cons->err_loc = NULL; |
| 3670 | |
| 3671 | if (req->flags & BF_EMPTY) { |
| 3672 | EV_FD_CLR(req->cons->fd, DIR_WR); |
| 3673 | req->wex = TICK_ETERNITY; |
| 3674 | } else { |
| 3675 | EV_FD_SET(req->cons->fd, DIR_WR); |
| 3676 | req->wex = tick_add_ifset(now_ms, t->be->timeout.server); |
| 3677 | if (tick_isset(req->wex)) { |
| 3678 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 3679 | * we refresh it. */ |
| 3680 | rep->rex = req->wex; |
| 3681 | } |
| 3682 | } |
| 3683 | |
| 3684 | if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */ |
| 3685 | if (!(rep->flags & BF_HIJACK)) { |
| 3686 | EV_FD_SET(req->cons->fd, DIR_RD); |
| 3687 | rep->rex = tick_add_ifset(now_ms, t->be->timeout.server); |
| 3688 | } |
| 3689 | buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */ |
| 3690 | |
| 3691 | /* if the user wants to log as soon as possible, without counting |
| 3692 | bytes from the server, then this is the right moment. */ |
| 3693 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 3694 | t->logs.t_close = t->logs.t_connect; /* to get a valid end date */ |
| 3695 | tcp_sess_log(t); |
| 3696 | } |
| 3697 | #ifdef CONFIG_HAP_TCPSPLICE |
| 3698 | if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
| 3699 | /* TCP splicing supported by both FE and BE */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 3700 | tcp_splice_splicefd(req->prod->fd, req->cons->fd, 0); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3701 | } |
| 3702 | #endif |
| 3703 | } |
| 3704 | else { |
| 3705 | rep->analysers |= AN_RTR_HTTP_HDR; |
| 3706 | buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */ |
| 3707 | t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE; |
| 3708 | /* reset hdr_idx which was already initialized by the request. |
| 3709 | * right now, the http parser does it. |
| 3710 | * hdr_idx_init(&t->txn.hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3711 | */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3712 | } |
| 3713 | |
Willy Tarreau | 9a2d154 | 2008-08-30 12:31:07 +0200 | [diff] [blame] | 3714 | rep->flags |= BF_READ_ATTACHED; /* producer is now attached */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3715 | req->wex = TICK_ETERNITY; |
| 3716 | return 0; |
| 3717 | } |
| 3718 | } |
| 3719 | |
| 3720 | |
| 3721 | /* |
| 3722 | * This function tries to assign a server to a stream_sock interface. |
| 3723 | * It may be called only for t->req->cons->state = one of { SI_ST_INI, |
| 3724 | * SI_ST_TAR, SI_ST_QUE }. It returns one of those states, SI_ST_ASS |
| 3725 | * in case of success, or SI_ST_CLO in case of failure. It returns 1 if |
| 3726 | * it returns SI_ST_ASS, otherwise zero. |
| 3727 | */ |
| 3728 | int stream_sock_assign_server(struct session *t) |
| 3729 | { |
| 3730 | DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n", |
| 3731 | now_ms, __FUNCTION__, |
| 3732 | cli_stnames[t->cli_state], |
| 3733 | t->rep->rex, t->req->wex, |
| 3734 | t->req->flags, t->rep->flags, |
| 3735 | t->req->l, t->rep->l); |
| 3736 | |
| 3737 | if (t->req->cons->state == SI_ST_TAR) { |
| 3738 | /* connection might be aborted */ |
| 3739 | if ((t->req->flags & BF_SHUTW_NOW) || |
| 3740 | (t->rep->flags & BF_SHUTW) || |
| 3741 | ((t->req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */ |
| 3742 | (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] | 3743 | |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3744 | trace_term(t, TT_HTTP_SRV_1); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3745 | t->req->wex = TICK_ETERNITY; |
| 3746 | |
| 3747 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3748 | buffer_shutr(t->rep); |
| 3749 | buffer_shutw(t->req); |
| 3750 | if (!t->req->cons->err_type) |
| 3751 | t->req->cons->err_type = SI_ET_CONN_ABRT; |
| 3752 | t->req->cons->state = SI_ST_CLO; |
| 3753 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3754 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3755 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3756 | if (!tick_is_expired(t->req->wex, now_ms)) |
| 3757 | return 0; /* still in turn-around */ |
| 3758 | |
| 3759 | t->req->cons->state = SI_ST_INI; |
| 3760 | } |
| 3761 | else if (t->req->cons->state == SI_ST_QUE) { |
| 3762 | if (t->pend_pos) { |
| 3763 | /* request still in queue... */ |
| 3764 | if (tick_is_expired(t->req->wex, now_ms)) { |
| 3765 | /* ... and timeout expired */ |
| 3766 | trace_term(t, TT_HTTP_SRV_3); |
| 3767 | t->req->wex = TICK_ETERNITY; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3768 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3769 | if (t->srv) |
| 3770 | t->srv->failed_conns++; |
| 3771 | t->be->failed_conns++; |
| 3772 | |
| 3773 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3774 | buffer_shutr(t->rep); |
| 3775 | buffer_shutw(t->req); |
| 3776 | t->req->flags |= BF_WRITE_TIMEOUT; |
| 3777 | if (!t->req->cons->err_type) |
| 3778 | t->req->cons->err_type = SI_ET_QUEUE_TO; |
| 3779 | t->req->cons->state = SI_ST_CLO; |
| 3780 | return 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3781 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3782 | /* connection remains in queue, check if we have to abort it */ |
| 3783 | if ((t->req->flags & BF_SHUTW_NOW) || |
| 3784 | (t->rep->flags & BF_SHUTW) || |
| 3785 | ((t->req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */ |
| 3786 | (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { |
| 3787 | /* give up */ |
| 3788 | trace_term(t, TT_HTTP_SRV_1); |
| 3789 | t->req->wex = TICK_ETERNITY; |
| 3790 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3791 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3792 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3793 | buffer_shutr(t->rep); |
| 3794 | buffer_shutw(t->req); |
| 3795 | if (!t->req->cons->err_type) |
| 3796 | t->req->cons->err_type = SI_ET_QUEUE_ABRT; |
| 3797 | t->req->cons->state = SI_ST_CLO; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3798 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3799 | return 0; |
| 3800 | } |
| 3801 | /* The connection is not in the queue anymore */ |
| 3802 | t->req->cons->state = SI_ST_INI; |
| 3803 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3804 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3805 | /* we may get here from above */ |
| 3806 | if (t->req->cons->state == SI_ST_INI) { |
| 3807 | /* no connection in progress, we have to get a new one */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3808 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3809 | /* first, check if the connection has been aborted */ |
| 3810 | if ((t->req->flags & BF_SHUTW_NOW) || |
| 3811 | (t->rep->flags & BF_SHUTW) || |
| 3812 | ((t->req->flags & BF_SHUTR) && |
| 3813 | (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] | 3814 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3815 | trace_term(t, TT_HTTP_SRV_1); |
| 3816 | t->req->wex = TICK_ETERNITY; |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3817 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3818 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3819 | buffer_shutr(t->rep); |
| 3820 | buffer_shutw(t->req); |
| 3821 | if (!t->req->cons->err_type) |
| 3822 | t->req->cons->err_type = SI_ET_CONN_ABRT; |
| 3823 | t->req->cons->state = SI_ST_CLO; |
| 3824 | return 0; |
| 3825 | } |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3826 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3827 | /* try to get a server assigned */ |
| 3828 | if (srv_redispatch_connect(t) != 0) { |
| 3829 | /* we did not get any server, let's check the cause */ |
| 3830 | if (t->req->cons->state == SI_ST_QUE) { |
| 3831 | /* the connection was queued, that's OK */ |
| 3832 | return 0; |
| 3833 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3834 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3835 | trace_term(t, TT_HTTP_SRV_2); |
| 3836 | t->req->wex = TICK_ETERNITY; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3837 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3838 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3839 | buffer_shutr(t->rep); |
| 3840 | buffer_shutw(t->req); |
| 3841 | t->req->flags |= BF_WRITE_ERROR; |
| 3842 | if (!t->req->cons->err_type) |
| 3843 | t->req->cons->err_type = SI_ET_CONN_OTHER; |
| 3844 | t->req->cons->state = SI_ST_CLO; |
| 3845 | return 0; |
| 3846 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3847 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3848 | t->req->cons->state = SI_ST_ASS; |
| 3849 | /* Once the server is assigned, we have to return because |
| 3850 | * the caller might be interested in checking several |
| 3851 | * things before connecting. |
| 3852 | */ |
| 3853 | return 1; |
| 3854 | } |
| 3855 | return 0; |
| 3856 | } |
Krzysztof Piotr Oledzki | efe3b6f | 2008-05-23 23:49:32 +0200 | [diff] [blame] | 3857 | |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3858 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3859 | /* |
| 3860 | * This function tries to establish a connection to an assigned server. It also |
| 3861 | * performs connection retries. It may only be called with t->req->cons->state |
| 3862 | * in { SI_ST_ASS, SI_ST_CON }. It may also set the state to SI_ST_INI, |
| 3863 | * SI_ST_EST, or SI_ST_CLO. |
| 3864 | */ |
| 3865 | int stream_sock_connect_server(struct session *t) |
| 3866 | { |
| 3867 | if (t->req->cons->state == SI_ST_ASS) { |
| 3868 | /* server assigned to request, we have to try to connect now */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3869 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3870 | if (!srv_retryable_connect(t)) { |
| 3871 | /* we need to redispatch */ |
| 3872 | t->req->cons->state = SI_ST_INI; |
| 3873 | return 0; |
| 3874 | } |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3875 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3876 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 3877 | if (t->req->cons->state != SI_ST_CON) { |
| 3878 | /* it was an error */ |
| 3879 | trace_term(t, TT_HTTP_SRV_4); |
| 3880 | t->req->wex = TICK_ETERNITY; |
| 3881 | |
| 3882 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3883 | buffer_shutr(t->rep); |
| 3884 | buffer_shutw(t->req); |
| 3885 | t->req->flags |= BF_WRITE_ERROR; |
| 3886 | if (!t->req->cons->err_type) |
| 3887 | t->req->cons->err_type = SI_ET_CONN_OTHER; |
| 3888 | t->req->cons->state = SI_ST_CLO; |
| 3889 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3890 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3891 | /* We have a socket and switched to SI_ST_CON */ |
| 3892 | } |
| 3893 | |
| 3894 | /* we may also get here from above */ |
| 3895 | if (t->req->cons->state == SI_ST_CON) { |
| 3896 | /* connection in progress or just completed */ |
| 3897 | if (!tcp_connection_status(t)) |
| 3898 | return 0; |
| 3899 | } |
| 3900 | return 0; |
| 3901 | } |
| 3902 | |
| 3903 | |
| 3904 | /* |
| 3905 | * Tries to establish a connection to the server and associate it to the |
| 3906 | * 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] | 3907 | * 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] | 3908 | * returns zero, but may return 1 if it absolutely wants to be called again. |
| 3909 | */ |
| 3910 | int process_srv_conn(struct session *t) |
| 3911 | { |
| 3912 | DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n", |
| 3913 | now_ms, __FUNCTION__, |
| 3914 | cli_stnames[t->cli_state], |
| 3915 | t->rep->rex, t->req->wex, |
| 3916 | t->req->flags, t->rep->flags, |
| 3917 | t->req->l, t->rep->l); |
| 3918 | |
| 3919 | do { |
| 3920 | if (t->req->cons->state == SI_ST_INI || |
| 3921 | t->req->cons->state == SI_ST_TAR || |
| 3922 | t->req->cons->state == SI_ST_QUE) { |
| 3923 | /* try to assign a server */ |
| 3924 | if (!stream_sock_assign_server(t)) |
| 3925 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3926 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3927 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3928 | if (t->req->cons->state == SI_ST_ASS && |
| 3929 | t->srv && t->srv->rdr_len && t->flags & SN_REDIRECTABLE) { |
| 3930 | /* Server supporting redirection and it is possible. |
| 3931 | * Invalid requests are reported as such. It concerns all |
| 3932 | * the largest ones. |
| 3933 | */ |
| 3934 | struct http_txn *txn = &t->txn; |
| 3935 | struct chunk rdr; |
| 3936 | char *path; |
| 3937 | int len; |
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 | /* 1: create the response header */ |
| 3940 | rdr.len = strlen(HTTP_302); |
| 3941 | rdr.str = trash; |
| 3942 | memcpy(rdr.str, HTTP_302, rdr.len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3943 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3944 | /* 2: add the server's prefix */ |
| 3945 | if (rdr.len + t->srv->rdr_len > sizeof(trash)) |
| 3946 | goto cancel_redir; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3947 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3948 | memcpy(rdr.str + rdr.len, t->srv->rdr_pfx, t->srv->rdr_len); |
| 3949 | rdr.len += t->srv->rdr_len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3950 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3951 | /* 3: add the request URI */ |
| 3952 | path = http_get_path(txn); |
| 3953 | if (!path) |
| 3954 | goto cancel_redir; |
| 3955 | len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path; |
| 3956 | if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */ |
| 3957 | goto cancel_redir; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3958 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3959 | memcpy(rdr.str + rdr.len, path, len); |
| 3960 | rdr.len += len; |
| 3961 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 3962 | rdr.len += 4; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3963 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3964 | srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr); |
| 3965 | trace_term(t, TT_HTTP_SRV_3); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3966 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3967 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
| 3968 | if (t->srv) |
| 3969 | t->srv->cum_sess++; |
| 3970 | |
| 3971 | t->req->cons->state = SI_ST_CLO; |
| 3972 | return 0; |
| 3973 | cancel_redir: |
| 3974 | //txn->status = 400; |
| 3975 | //t->fe->failed_req++; |
| 3976 | //srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, |
| 3977 | // 400, error_message(t, HTTP_ERR_400)); |
| 3978 | trace_term(t, TT_HTTP_SRV_4); |
| 3979 | |
| 3980 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3981 | buffer_shutw(t->req); |
| 3982 | buffer_shutr(t->rep); |
| 3983 | if (!t->req->cons->err_type) |
| 3984 | t->req->cons->err_type = SI_ET_CONN_OTHER; |
| 3985 | t->req->cons->state = SI_ST_CLO; |
| 3986 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3987 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3988 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3989 | if (t->req->cons->state == SI_ST_CON || |
| 3990 | t->req->cons->state == SI_ST_ASS) { |
| 3991 | stream_sock_connect_server(t); |
| 3992 | } |
| 3993 | } while (t->req->cons->state != SI_ST_CLO && |
| 3994 | t->req->cons->state != SI_ST_CON && |
| 3995 | t->req->cons->state != SI_ST_EST); |
| 3996 | return 0; |
| 3997 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3998 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3999 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4000 | /* |
| 4001 | * 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] | 4002 | * 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] | 4003 | * be produced. It stops by itself by unsetting the BF_HIJACK flag from the |
| 4004 | * 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] | 4005 | * 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] | 4006 | * when it wants to stop sending data, otherwise 0. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4007 | */ |
| 4008 | int produce_content(struct session *s) |
| 4009 | { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4010 | if (s->data_source == DATA_SRC_NONE) { |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 4011 | buffer_stop_hijack(s->rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4012 | return 1; |
| 4013 | } |
| 4014 | else if (s->data_source == DATA_SRC_STATS) { |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 4015 | /* dump server statistics */ |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 4016 | int ret = stats_dump_http(s, s->be->uri_auth); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 4017 | if (ret >= 0) |
| 4018 | return ret; |
| 4019 | /* -1 indicates an error */ |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 4020 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4021 | |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 4022 | /* unknown data source or internal error */ |
| 4023 | s->txn.status = 500; |
| 4024 | client_retnclose(s, error_message(s, HTTP_ERR_500)); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 4025 | trace_term(s, TT_HTTP_CNT_1); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 4026 | if (!(s->flags & SN_ERR_MASK)) |
| 4027 | s->flags |= SN_ERR_PRXCOND; |
| 4028 | if (!(s->flags & SN_FINST_MASK)) |
| 4029 | s->flags |= SN_FINST_R; |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 4030 | buffer_stop_hijack(s->rep); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 4031 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4032 | } |
| 4033 | |
| 4034 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4035 | /* Iterate the same filter through all request headers. |
| 4036 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4037 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 4038 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4039 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4040 | 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] | 4041 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4042 | char term; |
| 4043 | char *cur_ptr, *cur_end, *cur_next; |
| 4044 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4045 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4046 | struct hdr_idx_elem *cur_hdr; |
| 4047 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 4048 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4049 | last_hdr = 0; |
| 4050 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4051 | 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] | 4052 | old_idx = 0; |
| 4053 | |
| 4054 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4055 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4056 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4057 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4058 | (exp->action == ACT_ALLOW || |
| 4059 | exp->action == ACT_DENY || |
| 4060 | exp->action == ACT_TARPIT)) |
| 4061 | return 0; |
| 4062 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4063 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4064 | if (!cur_idx) |
| 4065 | break; |
| 4066 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4067 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4068 | cur_ptr = cur_next; |
| 4069 | cur_end = cur_ptr + cur_hdr->len; |
| 4070 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4071 | |
| 4072 | /* Now we have one header between cur_ptr and cur_end, |
| 4073 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4074 | */ |
| 4075 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4076 | /* The annoying part is that pattern matching needs |
| 4077 | * that we modify the contents to null-terminate all |
| 4078 | * strings before testing them. |
| 4079 | */ |
| 4080 | |
| 4081 | term = *cur_end; |
| 4082 | *cur_end = '\0'; |
| 4083 | |
| 4084 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4085 | switch (exp->action) { |
| 4086 | case ACT_SETBE: |
| 4087 | /* It is not possible to jump a second time. |
| 4088 | * FIXME: should we return an HTTP/500 here so that |
| 4089 | * the admin knows there's a problem ? |
| 4090 | */ |
| 4091 | if (t->be != t->fe) |
| 4092 | break; |
| 4093 | |
| 4094 | /* Swithing Proxy */ |
| 4095 | t->be = (struct proxy *) exp->replace; |
| 4096 | |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4097 | /* right now, the backend switch is not overly complicated |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4098 | * because we have associated req_cap and rsp_cap to the |
| 4099 | * frontend, and the beconn will be updated later. |
| 4100 | */ |
| 4101 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 4102 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 4103 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 4104 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4105 | last_hdr = 1; |
| 4106 | break; |
| 4107 | |
| 4108 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4109 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4110 | last_hdr = 1; |
| 4111 | break; |
| 4112 | |
| 4113 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4114 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4115 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4116 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4117 | break; |
| 4118 | |
| 4119 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4120 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4121 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4122 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4123 | break; |
| 4124 | |
| 4125 | case ACT_REPLACE: |
| 4126 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4127 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 4128 | /* FIXME: if the user adds a newline in the replacement, the |
| 4129 | * index will not be recalculated for now, and the new line |
| 4130 | * will not be counted as a new header. |
| 4131 | */ |
| 4132 | |
| 4133 | cur_end += delta; |
| 4134 | cur_next += delta; |
| 4135 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4136 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4137 | break; |
| 4138 | |
| 4139 | case ACT_REMOVE: |
| 4140 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 4141 | cur_next += delta; |
| 4142 | |
| 4143 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4144 | txn->req.eoh += delta; |
| 4145 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4146 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4147 | cur_hdr->len = 0; |
| 4148 | cur_end = NULL; /* null-term has been rewritten */ |
| 4149 | break; |
| 4150 | |
| 4151 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4152 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4153 | if (cur_end) |
| 4154 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4155 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4156 | /* keep the link from this header to next one in case of later |
| 4157 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4158 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4159 | old_idx = cur_idx; |
| 4160 | } |
| 4161 | return 0; |
| 4162 | } |
| 4163 | |
| 4164 | |
| 4165 | /* Apply the filter to the request line. |
| 4166 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 4167 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4168 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 4169 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4170 | */ |
| 4171 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 4172 | { |
| 4173 | char term; |
| 4174 | char *cur_ptr, *cur_end; |
| 4175 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4176 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4177 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4178 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4179 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4180 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4181 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4182 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4183 | (exp->action == ACT_ALLOW || |
| 4184 | exp->action == ACT_DENY || |
| 4185 | exp->action == ACT_TARPIT)) |
| 4186 | return 0; |
| 4187 | else if (exp->action == ACT_REMOVE) |
| 4188 | return 0; |
| 4189 | |
| 4190 | done = 0; |
| 4191 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4192 | 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] | 4193 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4194 | |
| 4195 | /* Now we have the request line between cur_ptr and cur_end */ |
| 4196 | |
| 4197 | /* The annoying part is that pattern matching needs |
| 4198 | * that we modify the contents to null-terminate all |
| 4199 | * strings before testing them. |
| 4200 | */ |
| 4201 | |
| 4202 | term = *cur_end; |
| 4203 | *cur_end = '\0'; |
| 4204 | |
| 4205 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4206 | switch (exp->action) { |
| 4207 | case ACT_SETBE: |
| 4208 | /* It is not possible to jump a second time. |
| 4209 | * FIXME: should we return an HTTP/500 here so that |
| 4210 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4211 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4212 | if (t->be != t->fe) |
| 4213 | break; |
| 4214 | |
| 4215 | /* Swithing Proxy */ |
| 4216 | t->be = (struct proxy *) exp->replace; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4217 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4218 | /* right now, the backend switch is not too much complicated |
| 4219 | * because we have associated req_cap and rsp_cap to the |
| 4220 | * frontend, and the beconn will be updated later. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4221 | */ |
| 4222 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 4223 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 4224 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 4225 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4226 | done = 1; |
| 4227 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4228 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4229 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4230 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4231 | done = 1; |
| 4232 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4233 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4234 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4235 | txn->flags |= TX_CLDENY; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4236 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4237 | done = 1; |
| 4238 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4239 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4240 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4241 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4242 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4243 | done = 1; |
| 4244 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4245 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4246 | case ACT_REPLACE: |
| 4247 | *cur_end = term; /* restore the string terminator */ |
| 4248 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4249 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 4250 | /* FIXME: if the user adds a newline in the replacement, the |
| 4251 | * index will not be recalculated for now, and the new line |
| 4252 | * will not be counted as a new header. |
| 4253 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4254 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4255 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4256 | cur_end += delta; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4257 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4258 | 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] | 4259 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4260 | HTTP_MSG_RQMETH, |
| 4261 | cur_ptr, cur_end + 1, |
| 4262 | NULL, NULL); |
| 4263 | if (unlikely(!cur_end)) |
| 4264 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4265 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4266 | /* we have a full request and we know that we have either a CR |
| 4267 | * or an LF at <ptr>. |
| 4268 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4269 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 4270 | 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] | 4271 | /* there is no point trying this regex on headers */ |
| 4272 | return 1; |
| 4273 | } |
| 4274 | } |
| 4275 | *cur_end = term; /* restore the string terminator */ |
| 4276 | return done; |
| 4277 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 4278 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4279 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4280 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4281 | /* |
| 4282 | * Apply all the req filters <exp> to all headers in buffer <req> of session <t>. |
| 4283 | * 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] | 4284 | * unparsable request. Since it can manage the switch to another backend, it |
| 4285 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4286 | */ |
| 4287 | int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 4288 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4289 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4290 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4291 | while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4292 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4293 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4294 | /* |
| 4295 | * The interleaving of transformations and verdicts |
| 4296 | * makes it difficult to decide to continue or stop |
| 4297 | * the evaluation. |
| 4298 | */ |
| 4299 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4300 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4301 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 4302 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) { |
| 4303 | exp = exp->next; |
| 4304 | continue; |
| 4305 | } |
| 4306 | |
| 4307 | /* Apply the filter to the request line. */ |
| 4308 | ret = apply_filter_to_req_line(t, req, exp); |
| 4309 | if (unlikely(ret < 0)) |
| 4310 | return -1; |
| 4311 | |
| 4312 | if (likely(ret == 0)) { |
| 4313 | /* The filter did not match the request, it can be |
| 4314 | * iterated through all headers. |
| 4315 | */ |
| 4316 | apply_filter_to_req_headers(t, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4317 | } |
| 4318 | exp = exp->next; |
| 4319 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4320 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4321 | } |
| 4322 | |
| 4323 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4324 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4325 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 4326 | * Manage client-side cookie. It can impact performance by about 2% so it is |
| 4327 | * desirable to call it only when needed. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4328 | */ |
| 4329 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 4330 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4331 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4332 | char *p1, *p2, *p3, *p4; |
| 4333 | char *del_colon, *del_cookie, *colon; |
| 4334 | int app_cookies; |
| 4335 | |
| 4336 | appsess *asession_temp = NULL; |
| 4337 | appsess local_asession; |
| 4338 | |
| 4339 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4340 | int cur_idx, old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4341 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4342 | /* Iterate through the headers. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4343 | * we start with the start line. |
| 4344 | */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 4345 | old_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4346 | 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] | 4347 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4348 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4349 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4350 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4351 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4352 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4353 | cur_ptr = cur_next; |
| 4354 | cur_end = cur_ptr + cur_hdr->len; |
| 4355 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4356 | |
| 4357 | /* We have one full header between cur_ptr and cur_end, and the |
| 4358 | * next header starts at cur_next. We're only interested in |
| 4359 | * "Cookie:" headers. |
| 4360 | */ |
| 4361 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4362 | val = http_header_match2(cur_ptr, cur_end, "Cookie", 6); |
| 4363 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4364 | old_idx = cur_idx; |
| 4365 | continue; |
| 4366 | } |
| 4367 | |
| 4368 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 4369 | * attributes whose name begin with a '$', and associate them with |
| 4370 | * the right cookie, if we want to delete this cookie. |
| 4371 | * So there are 3 cases for each cookie read : |
| 4372 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 4373 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 4374 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 4375 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 4376 | * "special" cookie. |
| 4377 | * At the end of loop, if a "special" cookie remains, we may have to |
| 4378 | * remove it. If no application cookie persists in the header, we |
| 4379 | * *MUST* delete it |
| 4380 | */ |
| 4381 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4382 | colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4383 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4384 | /* del_cookie == NULL => nothing to be deleted */ |
| 4385 | del_colon = del_cookie = NULL; |
| 4386 | app_cookies = 0; |
| 4387 | |
| 4388 | while (p1 < cur_end) { |
| 4389 | /* skip spaces and colons, but keep an eye on these ones */ |
| 4390 | while (p1 < cur_end) { |
| 4391 | if (*p1 == ';' || *p1 == ',') |
| 4392 | colon = p1; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4393 | else if (!isspace((unsigned char)*p1)) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4394 | break; |
| 4395 | p1++; |
| 4396 | } |
| 4397 | |
| 4398 | if (p1 == cur_end) |
| 4399 | break; |
| 4400 | |
| 4401 | /* p1 is at the beginning of the cookie name */ |
| 4402 | p2 = p1; |
| 4403 | while (p2 < cur_end && *p2 != '=') |
| 4404 | p2++; |
| 4405 | |
| 4406 | if (p2 == cur_end) |
| 4407 | break; |
| 4408 | |
| 4409 | p3 = p2 + 1; /* skips the '=' sign */ |
| 4410 | if (p3 == cur_end) |
| 4411 | break; |
| 4412 | |
| 4413 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4414 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4415 | p4++; |
| 4416 | |
| 4417 | /* here, we have the cookie name between p1 and p2, |
| 4418 | * and its value between p3 and p4. |
| 4419 | * we can process it : |
| 4420 | * |
| 4421 | * Cookie: NAME=VALUE; |
| 4422 | * | || || | |
| 4423 | * | || || +--> p4 |
| 4424 | * | || |+-------> p3 |
| 4425 | * | || +--------> p2 |
| 4426 | * | |+------------> p1 |
| 4427 | * | +-------------> colon |
| 4428 | * +--------------------> cur_ptr |
| 4429 | */ |
| 4430 | |
| 4431 | if (*p1 == '$') { |
| 4432 | /* skip this one */ |
| 4433 | } |
| 4434 | else { |
| 4435 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4436 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4437 | txn->cli_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4438 | (p4 - p1 >= t->fe->capture_namelen) && |
| 4439 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4440 | int log_len = p4 - p1; |
| 4441 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 4442 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4443 | Alert("HTTP logging : out of memory.\n"); |
| 4444 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4445 | if (log_len > t->fe->capture_len) |
| 4446 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4447 | memcpy(txn->cli_cookie, p1, log_len); |
| 4448 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4449 | } |
| 4450 | } |
| 4451 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4452 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 4453 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4454 | /* Cool... it's the right one */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4455 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4456 | char *delim; |
| 4457 | |
| 4458 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 4459 | * have the server ID betweek p3 and delim, and the original cookie between |
| 4460 | * delim+1 and p4. Otherwise, delim==p4 : |
| 4461 | * |
| 4462 | * Cookie: NAME=SRV~VALUE; |
| 4463 | * | || || | | |
| 4464 | * | || || | +--> p4 |
| 4465 | * | || || +--------> delim |
| 4466 | * | || |+-----------> p3 |
| 4467 | * | || +------------> p2 |
| 4468 | * | |+----------------> p1 |
| 4469 | * | +-----------------> colon |
| 4470 | * +------------------------> cur_ptr |
| 4471 | */ |
| 4472 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4473 | if (t->be->options & PR_O_COOK_PFX) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4474 | for (delim = p3; delim < p4; delim++) |
| 4475 | if (*delim == COOKIE_DELIM) |
| 4476 | break; |
| 4477 | } |
| 4478 | else |
| 4479 | delim = p4; |
| 4480 | |
| 4481 | |
| 4482 | /* Here, we'll look for the first running server which supports the cookie. |
| 4483 | * This allows to share a same cookie between several servers, for example |
| 4484 | * to dedicate backup servers to specific servers only. |
| 4485 | * However, to prevent clients from sticking to cookie-less backup server |
| 4486 | * when they have incidentely learned an empty cookie, we simply ignore |
| 4487 | * empty cookies and mark them as invalid. |
| 4488 | */ |
| 4489 | if (delim == p3) |
| 4490 | srv = NULL; |
| 4491 | |
| 4492 | while (srv) { |
Willy Tarreau | 92f2ab1 | 2007-02-02 22:14:47 +0100 | [diff] [blame] | 4493 | if (srv->cookie && (srv->cklen == delim - p3) && |
| 4494 | !memcmp(p3, srv->cookie, delim - p3)) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4495 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4496 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4497 | txn->flags &= ~TX_CK_MASK; |
| 4498 | txn->flags |= TX_CK_VALID; |
| 4499 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4500 | t->srv = srv; |
| 4501 | break; |
| 4502 | } else { |
| 4503 | /* we found a server, but it's down */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4504 | txn->flags &= ~TX_CK_MASK; |
| 4505 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4506 | } |
| 4507 | } |
| 4508 | srv = srv->next; |
| 4509 | } |
| 4510 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4511 | if (!srv && !(txn->flags & TX_CK_DOWN)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4512 | /* no server matched this cookie */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4513 | txn->flags &= ~TX_CK_MASK; |
| 4514 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4515 | } |
| 4516 | |
| 4517 | /* depending on the cookie mode, we may have to either : |
| 4518 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 4519 | * the server never sees it ; |
| 4520 | * - remove the server id from the cookie value, and tag the cookie as an |
| 4521 | * application cookie so that it does not get accidentely removed later, |
| 4522 | * if we're in cookie prefix mode |
| 4523 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4524 | if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4525 | int delta; /* negative */ |
| 4526 | |
| 4527 | delta = buffer_replace2(req, p3, delim + 1, NULL, 0); |
| 4528 | p4 += delta; |
| 4529 | cur_end += delta; |
| 4530 | cur_next += delta; |
| 4531 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4532 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4533 | |
| 4534 | del_cookie = del_colon = NULL; |
| 4535 | app_cookies++; /* protect the header from deletion */ |
| 4536 | } |
| 4537 | else if (del_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4538 | (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] | 4539 | del_cookie = p1; |
| 4540 | del_colon = colon; |
| 4541 | } |
| 4542 | } else { |
| 4543 | /* now we know that we must keep this cookie since it's |
| 4544 | * not ours. But if we wanted to delete our cookie |
| 4545 | * earlier, we cannot remove the complete header, but we |
| 4546 | * can remove the previous block itself. |
| 4547 | */ |
| 4548 | app_cookies++; |
| 4549 | |
| 4550 | if (del_cookie != NULL) { |
| 4551 | int delta; /* negative */ |
| 4552 | |
| 4553 | delta = buffer_replace2(req, del_cookie, p1, NULL, 0); |
| 4554 | p4 += delta; |
| 4555 | cur_end += delta; |
| 4556 | cur_next += delta; |
| 4557 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4558 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4559 | del_cookie = del_colon = NULL; |
| 4560 | } |
| 4561 | } |
| 4562 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4563 | if ((t->be->appsession_name != NULL) && |
| 4564 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4565 | /* first, let's see if the cookie is our appcookie*/ |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4566 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4567 | /* Cool... it's the right one */ |
| 4568 | |
| 4569 | asession_temp = &local_asession; |
| 4570 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4571 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4572 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 4573 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 4574 | return; |
| 4575 | } |
| 4576 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4577 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 4578 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4579 | asession_temp->serverid = NULL; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4580 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4581 | /* only do insert, if lookup fails */ |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4582 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 4583 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4584 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4585 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4586 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4587 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 4588 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 4589 | return; |
| 4590 | } |
| 4591 | |
| 4592 | asession_temp->sessid = local_asession.sessid; |
| 4593 | asession_temp->serverid = local_asession.serverid; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4594 | asession_temp->request_count = 0; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4595 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4596 | } else { |
| 4597 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4598 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4599 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4600 | if (asession_temp->serverid == NULL) { |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4601 | /* TODO redispatch request */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4602 | Alert("Found Application Session without matching server.\n"); |
| 4603 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4604 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4605 | while (srv) { |
| 4606 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4607 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4608 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4609 | txn->flags &= ~TX_CK_MASK; |
| 4610 | txn->flags |= TX_CK_VALID; |
| 4611 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4612 | t->srv = srv; |
| 4613 | break; |
| 4614 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4615 | txn->flags &= ~TX_CK_MASK; |
| 4616 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4617 | } |
| 4618 | } |
| 4619 | srv = srv->next; |
| 4620 | }/* end while(srv) */ |
| 4621 | }/* end else if server == NULL */ |
| 4622 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 4623 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4624 | asession_temp->request_count++; |
| 4625 | #if defined(DEBUG_HASH) |
| 4626 | Alert("manage_client_side_cookies\n"); |
| 4627 | appsession_hash_dump(&(t->be->htbl_proxy)); |
| 4628 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4629 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 4630 | } |
| 4631 | |
| 4632 | /* we'll have to look for another cookie ... */ |
| 4633 | p1 = p4; |
| 4634 | } /* while (p1 < cur_end) */ |
| 4635 | |
| 4636 | /* There's no more cookie on this line. |
| 4637 | * We may have marked the last one(s) for deletion. |
| 4638 | * We must do this now in two ways : |
| 4639 | * - if there is no app cookie, we simply delete the header ; |
| 4640 | * - if there are app cookies, we must delete the end of the |
| 4641 | * string properly, including the colon/semi-colon before |
| 4642 | * the cookie name. |
| 4643 | */ |
| 4644 | if (del_cookie != NULL) { |
| 4645 | int delta; |
| 4646 | if (app_cookies) { |
| 4647 | delta = buffer_replace2(req, del_colon, cur_end, NULL, 0); |
| 4648 | cur_end = del_colon; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4649 | cur_hdr->len += delta; |
| 4650 | } else { |
| 4651 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4652 | |
| 4653 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4654 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4655 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4656 | cur_hdr->len = 0; |
| 4657 | } |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 4658 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4659 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4660 | } |
| 4661 | |
| 4662 | /* keep the link from this header to next one */ |
| 4663 | old_idx = cur_idx; |
| 4664 | } /* end of cookie processing on this header */ |
| 4665 | } |
| 4666 | |
| 4667 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4668 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 4669 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 4670 | */ |
| 4671 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4672 | { |
| 4673 | char term; |
| 4674 | char *cur_ptr, *cur_end, *cur_next; |
| 4675 | int cur_idx, old_idx, last_hdr; |
| 4676 | struct http_txn *txn = &t->txn; |
| 4677 | struct hdr_idx_elem *cur_hdr; |
| 4678 | int len, delta; |
| 4679 | |
| 4680 | last_hdr = 0; |
| 4681 | |
| 4682 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4683 | old_idx = 0; |
| 4684 | |
| 4685 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4686 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4687 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4688 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4689 | (exp->action == ACT_ALLOW || |
| 4690 | exp->action == ACT_DENY)) |
| 4691 | return 0; |
| 4692 | |
| 4693 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 4694 | if (!cur_idx) |
| 4695 | break; |
| 4696 | |
| 4697 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4698 | cur_ptr = cur_next; |
| 4699 | cur_end = cur_ptr + cur_hdr->len; |
| 4700 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4701 | |
| 4702 | /* Now we have one header between cur_ptr and cur_end, |
| 4703 | * and the next header starts at cur_next. |
| 4704 | */ |
| 4705 | |
| 4706 | /* The annoying part is that pattern matching needs |
| 4707 | * that we modify the contents to null-terminate all |
| 4708 | * strings before testing them. |
| 4709 | */ |
| 4710 | |
| 4711 | term = *cur_end; |
| 4712 | *cur_end = '\0'; |
| 4713 | |
| 4714 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4715 | switch (exp->action) { |
| 4716 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4717 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4718 | last_hdr = 1; |
| 4719 | break; |
| 4720 | |
| 4721 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4722 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4723 | last_hdr = 1; |
| 4724 | break; |
| 4725 | |
| 4726 | case ACT_REPLACE: |
| 4727 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4728 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 4729 | /* FIXME: if the user adds a newline in the replacement, the |
| 4730 | * index will not be recalculated for now, and the new line |
| 4731 | * will not be counted as a new header. |
| 4732 | */ |
| 4733 | |
| 4734 | cur_end += delta; |
| 4735 | cur_next += delta; |
| 4736 | cur_hdr->len += delta; |
| 4737 | txn->rsp.eoh += delta; |
| 4738 | break; |
| 4739 | |
| 4740 | case ACT_REMOVE: |
| 4741 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 4742 | cur_next += delta; |
| 4743 | |
| 4744 | /* FIXME: this should be a separate function */ |
| 4745 | txn->rsp.eoh += delta; |
| 4746 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4747 | txn->hdr_idx.used--; |
| 4748 | cur_hdr->len = 0; |
| 4749 | cur_end = NULL; /* null-term has been rewritten */ |
| 4750 | break; |
| 4751 | |
| 4752 | } |
| 4753 | } |
| 4754 | if (cur_end) |
| 4755 | *cur_end = term; /* restore the string terminator */ |
| 4756 | |
| 4757 | /* keep the link from this header to next one in case of later |
| 4758 | * removal of next header. |
| 4759 | */ |
| 4760 | old_idx = cur_idx; |
| 4761 | } |
| 4762 | return 0; |
| 4763 | } |
| 4764 | |
| 4765 | |
| 4766 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 4767 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 4768 | * or -1 if a replacement resulted in an invalid status line. |
| 4769 | */ |
| 4770 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4771 | { |
| 4772 | char term; |
| 4773 | char *cur_ptr, *cur_end; |
| 4774 | int done; |
| 4775 | struct http_txn *txn = &t->txn; |
| 4776 | int len, delta; |
| 4777 | |
| 4778 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4779 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4780 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4781 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4782 | (exp->action == ACT_ALLOW || |
| 4783 | exp->action == ACT_DENY)) |
| 4784 | return 0; |
| 4785 | else if (exp->action == ACT_REMOVE) |
| 4786 | return 0; |
| 4787 | |
| 4788 | done = 0; |
| 4789 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4790 | 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] | 4791 | cur_end = cur_ptr + txn->rsp.sl.rq.l; |
| 4792 | |
| 4793 | /* Now we have the status line between cur_ptr and cur_end */ |
| 4794 | |
| 4795 | /* The annoying part is that pattern matching needs |
| 4796 | * that we modify the contents to null-terminate all |
| 4797 | * strings before testing them. |
| 4798 | */ |
| 4799 | |
| 4800 | term = *cur_end; |
| 4801 | *cur_end = '\0'; |
| 4802 | |
| 4803 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4804 | switch (exp->action) { |
| 4805 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4806 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4807 | done = 1; |
| 4808 | break; |
| 4809 | |
| 4810 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4811 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4812 | done = 1; |
| 4813 | break; |
| 4814 | |
| 4815 | case ACT_REPLACE: |
| 4816 | *cur_end = term; /* restore the string terminator */ |
| 4817 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4818 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 4819 | /* FIXME: if the user adds a newline in the replacement, the |
| 4820 | * index will not be recalculated for now, and the new line |
| 4821 | * will not be counted as a new header. |
| 4822 | */ |
| 4823 | |
| 4824 | txn->rsp.eoh += delta; |
| 4825 | cur_end += delta; |
| 4826 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4827 | 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] | 4828 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 4829 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4830 | cur_ptr, cur_end + 1, |
| 4831 | NULL, NULL); |
| 4832 | if (unlikely(!cur_end)) |
| 4833 | return -1; |
| 4834 | |
| 4835 | /* we have a full respnse and we know that we have either a CR |
| 4836 | * or an LF at <ptr>. |
| 4837 | */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4838 | 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] | 4839 | hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r'); |
| 4840 | /* there is no point trying this regex on headers */ |
| 4841 | return 1; |
| 4842 | } |
| 4843 | } |
| 4844 | *cur_end = term; /* restore the string terminator */ |
| 4845 | return done; |
| 4846 | } |
| 4847 | |
| 4848 | |
| 4849 | |
| 4850 | /* |
| 4851 | * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>. |
| 4852 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 4853 | * unparsable response. |
| 4854 | */ |
| 4855 | int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4856 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4857 | struct http_txn *txn = &t->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4858 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4859 | while (exp && !(txn->flags & TX_SVDENY)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4860 | int ret; |
| 4861 | |
| 4862 | /* |
| 4863 | * The interleaving of transformations and verdicts |
| 4864 | * makes it difficult to decide to continue or stop |
| 4865 | * the evaluation. |
| 4866 | */ |
| 4867 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4868 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4869 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 4870 | exp->action == ACT_PASS)) { |
| 4871 | exp = exp->next; |
| 4872 | continue; |
| 4873 | } |
| 4874 | |
| 4875 | /* Apply the filter to the status line. */ |
| 4876 | ret = apply_filter_to_sts_line(t, rtr, exp); |
| 4877 | if (unlikely(ret < 0)) |
| 4878 | return -1; |
| 4879 | |
| 4880 | if (likely(ret == 0)) { |
| 4881 | /* The filter did not match the response, it can be |
| 4882 | * iterated through all headers. |
| 4883 | */ |
| 4884 | apply_filter_to_resp_headers(t, rtr, exp); |
| 4885 | } |
| 4886 | exp = exp->next; |
| 4887 | } |
| 4888 | return 0; |
| 4889 | } |
| 4890 | |
| 4891 | |
| 4892 | |
| 4893 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 4894 | * Manage server-side cookies. It can impact performance by about 2% so it is |
| 4895 | * desirable to call it only when needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4896 | */ |
| 4897 | void manage_server_side_cookies(struct session *t, struct buffer *rtr) |
| 4898 | { |
| 4899 | struct http_txn *txn = &t->txn; |
| 4900 | char *p1, *p2, *p3, *p4; |
| 4901 | |
| 4902 | appsess *asession_temp = NULL; |
| 4903 | appsess local_asession; |
| 4904 | |
| 4905 | char *cur_ptr, *cur_end, *cur_next; |
| 4906 | int cur_idx, old_idx, delta; |
| 4907 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4908 | /* Iterate through the headers. |
| 4909 | * we start with the start line. |
| 4910 | */ |
| 4911 | old_idx = 0; |
| 4912 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4913 | |
| 4914 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 4915 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4916 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4917 | |
| 4918 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4919 | cur_ptr = cur_next; |
| 4920 | cur_end = cur_ptr + cur_hdr->len; |
| 4921 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4922 | |
| 4923 | /* We have one full header between cur_ptr and cur_end, and the |
| 4924 | * next header starts at cur_next. We're only interested in |
| 4925 | * "Cookie:" headers. |
| 4926 | */ |
| 4927 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4928 | val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10); |
| 4929 | if (!val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4930 | old_idx = cur_idx; |
| 4931 | continue; |
| 4932 | } |
| 4933 | |
| 4934 | /* 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] | 4935 | txn->flags |= TX_SCK_ANY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4936 | |
| 4937 | |
| 4938 | /* maybe we only wanted to see if there was a set-cookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4939 | if (t->be->cookie_name == NULL && |
| 4940 | t->be->appsession_name == NULL && |
| 4941 | t->be->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4942 | return; |
| 4943 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4944 | p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4945 | |
| 4946 | 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] | 4947 | if (p1 == cur_end || *p1 == ';') /* end of cookie */ |
| 4948 | break; |
| 4949 | |
| 4950 | /* p1 is at the beginning of the cookie name */ |
| 4951 | p2 = p1; |
| 4952 | |
| 4953 | while (p2 < cur_end && *p2 != '=' && *p2 != ';') |
| 4954 | p2++; |
| 4955 | |
| 4956 | if (p2 == cur_end || *p2 == ';') /* next cookie */ |
| 4957 | break; |
| 4958 | |
| 4959 | p3 = p2 + 1; /* skip the '=' sign */ |
| 4960 | if (p3 == cur_end) |
| 4961 | break; |
| 4962 | |
| 4963 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4964 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';') |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4965 | p4++; |
| 4966 | |
| 4967 | /* here, we have the cookie name between p1 and p2, |
| 4968 | * and its value between p3 and p4. |
| 4969 | * we can process it. |
| 4970 | */ |
| 4971 | |
| 4972 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4973 | if (t->be->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4974 | txn->srv_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4975 | (p4 - p1 >= t->be->capture_namelen) && |
| 4976 | memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4977 | int log_len = p4 - p1; |
| 4978 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 4979 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4980 | Alert("HTTP logging : out of memory.\n"); |
| 4981 | } |
| 4982 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4983 | if (log_len > t->be->capture_len) |
| 4984 | log_len = t->be->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4985 | memcpy(txn->srv_cookie, p1, log_len); |
| 4986 | txn->srv_cookie[log_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4987 | } |
| 4988 | |
| 4989 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4990 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 4991 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4992 | /* Cool... it's the right one */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4993 | txn->flags |= TX_SCK_SEEN; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4994 | |
| 4995 | /* If the cookie is in insert mode on a known server, we'll delete |
| 4996 | * this occurrence because we'll insert another one later. |
| 4997 | * We'll delete it too if the "indirect" option is set and we're in |
| 4998 | * a direct access. */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4999 | if (((t->srv) && (t->be->options & PR_O_COOK_INS)) || |
| 5000 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5001 | /* this header must be deleted */ |
| 5002 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 5003 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5004 | txn->hdr_idx.used--; |
| 5005 | cur_hdr->len = 0; |
| 5006 | cur_next += delta; |
| 5007 | txn->rsp.eoh += delta; |
| 5008 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5009 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5010 | } |
| 5011 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5012 | (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5013 | /* replace bytes p3->p4 with the cookie name associated |
| 5014 | * with this server since we know it. |
| 5015 | */ |
| 5016 | delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen); |
| 5017 | cur_hdr->len += delta; |
| 5018 | cur_next += delta; |
| 5019 | txn->rsp.eoh += delta; |
| 5020 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5021 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5022 | } |
| 5023 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5024 | (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5025 | /* insert the cookie name associated with this server |
| 5026 | * before existing cookie, and insert a delimitor between them.. |
| 5027 | */ |
| 5028 | delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1); |
| 5029 | cur_hdr->len += delta; |
| 5030 | cur_next += delta; |
| 5031 | txn->rsp.eoh += delta; |
| 5032 | |
| 5033 | p3[t->srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5034 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5035 | } |
| 5036 | } |
| 5037 | /* next, let's see if the cookie is our appcookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5038 | else if ((t->be->appsession_name != NULL) && |
| 5039 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5040 | |
| 5041 | /* Cool... it's the right one */ |
| 5042 | |
| 5043 | size_t server_id_len = strlen(t->srv->id) + 1; |
| 5044 | asession_temp = &local_asession; |
| 5045 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5046 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5047 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5048 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5049 | return; |
| 5050 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5051 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 5052 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5053 | asession_temp->serverid = NULL; |
| 5054 | |
| 5055 | /* only do insert, if lookup fails */ |
Ryan Warnick | 6d0b1fa | 2008-02-17 11:24:35 +0100 | [diff] [blame] | 5056 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 5057 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5058 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5059 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 5060 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 5061 | return; |
| 5062 | } |
| 5063 | asession_temp->sessid = local_asession.sessid; |
| 5064 | asession_temp->serverid = local_asession.serverid; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5065 | asession_temp->request_count = 0; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5066 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
| 5067 | } else { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5068 | /* free wasted memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5069 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5070 | } |
| 5071 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5072 | if (asession_temp->serverid == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5073 | if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5074 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5075 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5076 | return; |
| 5077 | } |
| 5078 | asession_temp->serverid[0] = '\0'; |
| 5079 | } |
| 5080 | |
| 5081 | if (asession_temp->serverid[0] == '\0') |
| 5082 | memcpy(asession_temp->serverid, t->srv->id, server_id_len); |
| 5083 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 5084 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5085 | asession_temp->request_count++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5086 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5087 | Alert("manage_server_side_cookies\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5088 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5089 | #endif |
| 5090 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 5091 | break; /* we don't want to loop again since there cannot be another cookie on the same line */ |
| 5092 | } /* we're now at the end of the cookie value */ |
| 5093 | |
| 5094 | /* keep the link from this header to next one */ |
| 5095 | old_idx = cur_idx; |
| 5096 | } /* end of cookie processing on this header */ |
| 5097 | } |
| 5098 | |
| 5099 | |
| 5100 | |
| 5101 | /* |
| 5102 | * Check if response is cacheable or not. Updates t->flags. |
| 5103 | */ |
| 5104 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 5105 | { |
| 5106 | struct http_txn *txn = &t->txn; |
| 5107 | char *p1, *p2; |
| 5108 | |
| 5109 | char *cur_ptr, *cur_end, *cur_next; |
| 5110 | int cur_idx; |
| 5111 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 5112 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5113 | return; |
| 5114 | |
| 5115 | /* Iterate through the headers. |
| 5116 | * we start with the start line. |
| 5117 | */ |
| 5118 | cur_idx = 0; |
| 5119 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 5120 | |
| 5121 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 5122 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5123 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5124 | |
| 5125 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 5126 | cur_ptr = cur_next; |
| 5127 | cur_end = cur_ptr + cur_hdr->len; |
| 5128 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5129 | |
| 5130 | /* We have one full header between cur_ptr and cur_end, and the |
| 5131 | * next header starts at cur_next. We're only interested in |
| 5132 | * "Cookie:" headers. |
| 5133 | */ |
| 5134 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5135 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 5136 | if (val) { |
| 5137 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 5138 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 5139 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 5140 | return; |
| 5141 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5142 | } |
| 5143 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5144 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 5145 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5146 | continue; |
| 5147 | |
| 5148 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 5149 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5150 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5151 | |
| 5152 | if (p1 >= cur_end) /* no more info */ |
| 5153 | continue; |
| 5154 | |
| 5155 | /* p1 is at the beginning of the value */ |
| 5156 | p2 = p1; |
| 5157 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 5158 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5159 | p2++; |
| 5160 | |
| 5161 | /* we have a complete value between p1 and p2 */ |
| 5162 | if (p2 < cur_end && *p2 == '=') { |
| 5163 | /* we have something of the form no-cache="set-cookie" */ |
| 5164 | if ((cur_end - p1 >= 21) && |
| 5165 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 5166 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5167 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5168 | continue; |
| 5169 | } |
| 5170 | |
| 5171 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 5172 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 5173 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 5174 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 5175 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5176 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5177 | return; |
| 5178 | } |
| 5179 | |
| 5180 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5181 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5182 | continue; |
| 5183 | } |
| 5184 | } |
| 5185 | } |
| 5186 | |
| 5187 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5188 | /* |
| 5189 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 5190 | * If the server is found, it's assigned to the session. |
| 5191 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5192 | 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] | 5193 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5194 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5195 | appsess *asession_temp = NULL; |
| 5196 | appsess local_asession; |
| 5197 | char *request_line; |
| 5198 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5199 | if (t->be->appsession_name == NULL || |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 5200 | (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) || |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5201 | (request_line = memchr(begin, ';', len)) == NULL || |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5202 | ((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] | 5203 | return; |
| 5204 | |
| 5205 | /* skip ';' */ |
| 5206 | request_line++; |
| 5207 | |
| 5208 | /* look if we have a jsessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5209 | 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] | 5210 | return; |
| 5211 | |
| 5212 | /* skip jsessionid= */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5213 | request_line += t->be->appsession_name_len + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5214 | |
| 5215 | /* First try if we already have an appsession */ |
| 5216 | asession_temp = &local_asession; |
| 5217 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5218 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5219 | Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n"); |
| 5220 | send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n"); |
| 5221 | return; |
| 5222 | } |
| 5223 | |
| 5224 | /* Copy the sessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5225 | memcpy(asession_temp->sessid, request_line, t->be->appsession_len); |
| 5226 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5227 | asession_temp->serverid = NULL; |
| 5228 | |
| 5229 | /* only do insert, if lookup fails */ |
Ryan Warnick | 6d0b1fa | 2008-02-17 11:24:35 +0100 | [diff] [blame] | 5230 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 5231 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5232 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5233 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5234 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5235 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 5236 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 5237 | return; |
| 5238 | } |
| 5239 | asession_temp->sessid = local_asession.sessid; |
| 5240 | asession_temp->serverid = local_asession.serverid; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5241 | asession_temp->request_count=0; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5242 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5243 | } |
| 5244 | else { |
| 5245 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5246 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5247 | } |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5248 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 5249 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5250 | asession_temp->request_count++; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5251 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5252 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5253 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5254 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5255 | #endif |
| 5256 | if (asession_temp->serverid == NULL) { |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5257 | /* TODO redispatch request */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5258 | Alert("Found Application Session without matching server.\n"); |
| 5259 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5260 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5261 | while (srv) { |
| 5262 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5263 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5264 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5265 | txn->flags &= ~TX_CK_MASK; |
| 5266 | txn->flags |= TX_CK_VALID; |
| 5267 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5268 | t->srv = srv; |
| 5269 | break; |
| 5270 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5271 | txn->flags &= ~TX_CK_MASK; |
| 5272 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5273 | } |
| 5274 | } |
| 5275 | srv = srv->next; |
| 5276 | } |
| 5277 | } |
| 5278 | } |
| 5279 | |
| 5280 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5281 | /* |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5282 | * In a GET or HEAD request, check if the requested URI matches the stats uri |
| 5283 | * 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] | 5284 | * |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5285 | * 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] | 5286 | * 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] | 5287 | * produce_content() can be called to start sending data. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5288 | * |
| 5289 | * Returns 1 if the session's state changes, otherwise 0. |
| 5290 | */ |
| 5291 | int stats_check_uri_auth(struct session *t, struct proxy *backend) |
| 5292 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5293 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5294 | struct uri_auth *uri_auth = backend->uri_auth; |
| 5295 | struct user_auth *user; |
| 5296 | int authenticated, cur_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5297 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5298 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5299 | memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats)); |
| 5300 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5301 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5302 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5303 | return 0; |
| 5304 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5305 | h = t->req->data + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5306 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5307 | /* the URI is in h */ |
| 5308 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5309 | return 0; |
| 5310 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 5311 | h += uri_auth->uri_len; |
| 5312 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) { |
| 5313 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5314 | t->data_ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 5315 | break; |
| 5316 | } |
| 5317 | h++; |
| 5318 | } |
| 5319 | |
| 5320 | if (uri_auth->refresh) { |
| 5321 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 5322 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) { |
| 5323 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5324 | t->data_ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 5325 | break; |
| 5326 | } |
| 5327 | h++; |
| 5328 | } |
| 5329 | } |
| 5330 | |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 5331 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 5332 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) { |
| 5333 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5334 | t->data_ctx.stats.flags |= STAT_FMT_CSV; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 5335 | break; |
| 5336 | } |
| 5337 | h++; |
| 5338 | } |
| 5339 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5340 | t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO; |
| 5341 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5342 | /* we are in front of a interceptable URI. Let's check |
| 5343 | * if there's an authentication and if it's valid. |
| 5344 | */ |
| 5345 | user = uri_auth->users; |
| 5346 | if (!user) { |
| 5347 | /* no user auth required, it's OK */ |
| 5348 | authenticated = 1; |
| 5349 | } else { |
| 5350 | authenticated = 0; |
| 5351 | |
| 5352 | /* a user list is defined, we have to check. |
| 5353 | * skip 21 chars for "Authorization: Basic ". |
| 5354 | */ |
| 5355 | |
| 5356 | /* FIXME: this should move to an earlier place */ |
| 5357 | cur_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5358 | h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 5359 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 5360 | int len = txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5361 | if (len > 14 && |
| 5362 | !strncasecmp("Authorization:", h, 14)) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5363 | txn->auth_hdr.str = h; |
| 5364 | txn->auth_hdr.len = len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5365 | break; |
| 5366 | } |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5367 | h += len + txn->hdr_idx.v[cur_idx].cr + 1; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5368 | } |
| 5369 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5370 | if (txn->auth_hdr.len < 21 || |
| 5371 | memcmp(txn->auth_hdr.str + 14, " Basic ", 7)) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5372 | user = NULL; |
| 5373 | |
| 5374 | while (user) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5375 | if ((txn->auth_hdr.len == user->user_len + 14 + 7) |
| 5376 | && !memcmp(txn->auth_hdr.str + 14 + 7, |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5377 | user->user_pwd, user->user_len)) { |
| 5378 | authenticated = 1; |
| 5379 | break; |
| 5380 | } |
| 5381 | user = user->next; |
| 5382 | } |
| 5383 | } |
| 5384 | |
| 5385 | if (!authenticated) { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5386 | struct chunk msg; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5387 | |
| 5388 | /* no need to go further */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5389 | msg.str = trash; |
| 5390 | msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm); |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5391 | txn->status = 401; |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5392 | client_retnclose(t, &msg); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 5393 | trace_term(t, TT_HTTP_URI_1); |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 5394 | t->req->analysers = 0; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5395 | if (!(t->flags & SN_ERR_MASK)) |
| 5396 | t->flags |= SN_ERR_PRXCOND; |
| 5397 | if (!(t->flags & SN_FINST_MASK)) |
| 5398 | t->flags |= SN_FINST_R; |
| 5399 | return 1; |
| 5400 | } |
| 5401 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5402 | /* The request is valid, the user is authenticated. Let's start sending |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5403 | * data. |
| 5404 | */ |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 5405 | buffer_shutw_now(t->req); |
| 5406 | buffer_shutr_now(t->rep); |
| 5407 | buffer_start_hijack(t->rep); |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 5408 | t->logs.tv_request = now; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5409 | t->data_source = DATA_SRC_STATS; |
| 5410 | t->data_state = DATA_ST_INIT; |
Willy Tarreau | 91e9993 | 2008-06-30 07:51:00 +0200 | [diff] [blame] | 5411 | t->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5412 | produce_content(t); |
| 5413 | return 1; |
| 5414 | } |
| 5415 | |
| 5416 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5417 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5418 | * Print a debug line with a header |
| 5419 | */ |
| 5420 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 5421 | { |
| 5422 | int len, max; |
| 5423 | 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] | 5424 | 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] | 5425 | max = end - start; |
| 5426 | UBOUND(max, sizeof(trash) - len - 1); |
| 5427 | len += strlcpy2(trash + len, start, max + 1); |
| 5428 | trash[len++] = '\n'; |
| 5429 | write(1, trash, len); |
| 5430 | } |
| 5431 | |
| 5432 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5433 | /************************************************************************/ |
| 5434 | /* The code below is dedicated to ACL parsing and matching */ |
| 5435 | /************************************************************************/ |
| 5436 | |
| 5437 | |
| 5438 | |
| 5439 | |
| 5440 | /* 1. Check on METHOD |
| 5441 | * We use the pre-parsed method if it is known, and store its number as an |
| 5442 | * integer. If it is unknown, we use the pointer and the length. |
| 5443 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5444 | 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] | 5445 | { |
| 5446 | int len, meth; |
| 5447 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5448 | len = strlen(*text); |
| 5449 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5450 | |
| 5451 | pattern->val.i = meth; |
| 5452 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5453 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5454 | if (!pattern->ptr.str) |
| 5455 | return 0; |
| 5456 | pattern->len = len; |
| 5457 | } |
| 5458 | return 1; |
| 5459 | } |
| 5460 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5461 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5462 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5463 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5464 | { |
| 5465 | int meth; |
| 5466 | struct http_txn *txn = l7; |
| 5467 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5468 | if (!txn) |
| 5469 | return 0; |
| 5470 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5471 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5472 | return 0; |
| 5473 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5474 | meth = txn->meth; |
| 5475 | test->i = meth; |
| 5476 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5477 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5478 | /* ensure the indexes are not affected */ |
| 5479 | return 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5480 | test->len = txn->req.sl.rq.m_l; |
| 5481 | test->ptr = txn->req.sol; |
| 5482 | } |
| 5483 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5484 | return 1; |
| 5485 | } |
| 5486 | |
| 5487 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 5488 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 5489 | int icase; |
| 5490 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5491 | if (test->i != pattern->val.i) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5492 | return ACL_PAT_FAIL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5493 | |
| 5494 | if (test->i != HTTP_METH_OTHER) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5495 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5496 | |
| 5497 | /* Other method, we must compare the strings */ |
| 5498 | if (pattern->len != test->len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5499 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 5500 | |
| 5501 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 5502 | if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) || |
| 5503 | (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5504 | return ACL_PAT_FAIL; |
| 5505 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5506 | } |
| 5507 | |
| 5508 | /* 2. Check on Request/Status Version |
| 5509 | * We simply compare strings here. |
| 5510 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5511 | 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] | 5512 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5513 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5514 | if (!pattern->ptr.str) |
| 5515 | return 0; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5516 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 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_rqver(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->req.msg_state != HTTP_MSG_BODY) |
| 5532 | return 0; |
| 5533 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5534 | len = txn->req.sl.rq.v_l; |
| 5535 | ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som; |
| 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 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5548 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5549 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5550 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5551 | { |
| 5552 | struct http_txn *txn = l7; |
| 5553 | char *ptr; |
| 5554 | int len; |
| 5555 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5556 | if (!txn) |
| 5557 | return 0; |
| 5558 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5559 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5560 | return 0; |
| 5561 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5562 | len = txn->rsp.sl.st.v_l; |
| 5563 | ptr = txn->rsp.sol; |
| 5564 | |
| 5565 | while ((len-- > 0) && (*ptr++ != '/')); |
| 5566 | if (len <= 0) |
| 5567 | return 0; |
| 5568 | |
| 5569 | test->ptr = ptr; |
| 5570 | test->len = len; |
| 5571 | |
| 5572 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5573 | return 1; |
| 5574 | } |
| 5575 | |
| 5576 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5577 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5578 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5579 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5580 | { |
| 5581 | struct http_txn *txn = l7; |
| 5582 | char *ptr; |
| 5583 | int len; |
| 5584 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5585 | if (!txn) |
| 5586 | return 0; |
| 5587 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5588 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5589 | return 0; |
| 5590 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5591 | len = txn->rsp.sl.st.c_l; |
| 5592 | ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som; |
| 5593 | |
| 5594 | test->i = __strl2ui(ptr, len); |
| 5595 | test->flags = ACL_TEST_F_VOL_1ST; |
| 5596 | return 1; |
| 5597 | } |
| 5598 | |
| 5599 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5600 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5601 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5602 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5603 | { |
| 5604 | struct http_txn *txn = l7; |
| 5605 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5606 | if (!txn) |
| 5607 | return 0; |
| 5608 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5609 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5610 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5611 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5612 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5613 | /* ensure the indexes are not affected */ |
| 5614 | return 0; |
| 5615 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5616 | test->len = txn->req.sl.rq.u_l; |
| 5617 | test->ptr = txn->req.sol + txn->req.sl.rq.u; |
| 5618 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 5619 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 5620 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5621 | return 1; |
| 5622 | } |
| 5623 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5624 | static int |
| 5625 | acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5626 | struct acl_expr *expr, struct acl_test *test) |
| 5627 | { |
| 5628 | struct http_txn *txn = l7; |
| 5629 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5630 | if (!txn) |
| 5631 | return 0; |
| 5632 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5633 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5634 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5635 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5636 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5637 | /* ensure the indexes are not affected */ |
| 5638 | return 0; |
| 5639 | |
| 5640 | /* Parse HTTP request */ |
| 5641 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
| 5642 | test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr; |
| 5643 | test->i = AF_INET; |
| 5644 | |
| 5645 | /* |
| 5646 | * If we are parsing url in frontend space, we prepare backend stage |
| 5647 | * to not parse again the same url ! optimization lazyness... |
| 5648 | */ |
| 5649 | if (px->options & PR_O_HTTP_PROXY) |
| 5650 | l4->flags |= SN_ADDR_SET; |
| 5651 | |
| 5652 | test->flags = ACL_TEST_F_READ_ONLY; |
| 5653 | return 1; |
| 5654 | } |
| 5655 | |
| 5656 | static int |
| 5657 | acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5658 | struct acl_expr *expr, struct acl_test *test) |
| 5659 | { |
| 5660 | struct http_txn *txn = l7; |
| 5661 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5662 | if (!txn) |
| 5663 | return 0; |
| 5664 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5665 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5666 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5667 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5668 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5669 | /* ensure the indexes are not affected */ |
| 5670 | return 0; |
| 5671 | |
| 5672 | /* Same optimization as url_ip */ |
| 5673 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
| 5674 | test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port); |
| 5675 | |
| 5676 | if (px->options & PR_O_HTTP_PROXY) |
| 5677 | l4->flags |= SN_ADDR_SET; |
| 5678 | |
| 5679 | test->flags = ACL_TEST_F_READ_ONLY; |
| 5680 | return 1; |
| 5681 | } |
| 5682 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5683 | /* 5. Check on HTTP header. A pointer to the beginning of the value is returned. |
| 5684 | * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr(). |
| 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_hdr(struct proxy *px, struct session *l4, void *l7, char *sol, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5688 | struct acl_expr *expr, struct acl_test *test) |
| 5689 | { |
| 5690 | struct http_txn *txn = l7; |
| 5691 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5692 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5693 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5694 | if (!txn) |
| 5695 | return 0; |
| 5696 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5697 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 5698 | /* search for header from the beginning */ |
| 5699 | ctx->idx = 0; |
| 5700 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5701 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 5702 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 5703 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5704 | test->len = ctx->vlen; |
| 5705 | test->ptr = (char *)ctx->line + ctx->val; |
| 5706 | return 1; |
| 5707 | } |
| 5708 | |
| 5709 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 5710 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5711 | return 0; |
| 5712 | } |
| 5713 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5714 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5715 | acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5716 | struct acl_expr *expr, struct acl_test *test) |
| 5717 | { |
| 5718 | struct http_txn *txn = l7; |
| 5719 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5720 | if (!txn) |
| 5721 | return 0; |
| 5722 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5723 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5724 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5725 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5726 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5727 | /* ensure the indexes are not affected */ |
| 5728 | return 0; |
| 5729 | |
| 5730 | return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test); |
| 5731 | } |
| 5732 | |
| 5733 | static int |
| 5734 | acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5735 | struct acl_expr *expr, struct acl_test *test) |
| 5736 | { |
| 5737 | struct http_txn *txn = l7; |
| 5738 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5739 | if (!txn) |
| 5740 | return 0; |
| 5741 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5742 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5743 | return 0; |
| 5744 | |
| 5745 | return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test); |
| 5746 | } |
| 5747 | |
| 5748 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 5749 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 5750 | */ |
| 5751 | static int |
| 5752 | 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] | 5753 | struct acl_expr *expr, struct acl_test *test) |
| 5754 | { |
| 5755 | struct http_txn *txn = l7; |
| 5756 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5757 | struct hdr_ctx ctx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5758 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5759 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5760 | if (!txn) |
| 5761 | return 0; |
| 5762 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5763 | ctx.idx = 0; |
| 5764 | cnt = 0; |
| 5765 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx)) |
| 5766 | cnt++; |
| 5767 | |
| 5768 | test->i = cnt; |
| 5769 | test->flags = ACL_TEST_F_VOL_HDR; |
| 5770 | return 1; |
| 5771 | } |
| 5772 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5773 | static int |
| 5774 | acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5775 | struct acl_expr *expr, struct acl_test *test) |
| 5776 | { |
| 5777 | struct http_txn *txn = l7; |
| 5778 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5779 | if (!txn) |
| 5780 | return 0; |
| 5781 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5782 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5783 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5784 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5785 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5786 | /* ensure the indexes are not affected */ |
| 5787 | return 0; |
| 5788 | |
| 5789 | return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test); |
| 5790 | } |
| 5791 | |
| 5792 | static int |
| 5793 | acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5794 | struct acl_expr *expr, struct acl_test *test) |
| 5795 | { |
| 5796 | struct http_txn *txn = l7; |
| 5797 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5798 | if (!txn) |
| 5799 | return 0; |
| 5800 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5801 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5802 | return 0; |
| 5803 | |
| 5804 | return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test); |
| 5805 | } |
| 5806 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5807 | /* 7. Check on HTTP header's integer value. The integer value is returned. |
| 5808 | * FIXME: the type is 'int', it may not be appropriate for everything. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5809 | * 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] | 5810 | */ |
| 5811 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5812 | 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] | 5813 | struct acl_expr *expr, struct acl_test *test) |
| 5814 | { |
| 5815 | struct http_txn *txn = l7; |
| 5816 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5817 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5818 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5819 | if (!txn) |
| 5820 | return 0; |
| 5821 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5822 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 5823 | /* search for header from the beginning */ |
| 5824 | ctx->idx = 0; |
| 5825 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5826 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 5827 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 5828 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5829 | test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen); |
| 5830 | return 1; |
| 5831 | } |
| 5832 | |
| 5833 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 5834 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5835 | return 0; |
| 5836 | } |
| 5837 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5838 | static int |
| 5839 | acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5840 | struct acl_expr *expr, struct acl_test *test) |
| 5841 | { |
| 5842 | struct http_txn *txn = l7; |
| 5843 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5844 | if (!txn) |
| 5845 | return 0; |
| 5846 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5847 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5848 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5849 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5850 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5851 | /* ensure the indexes are not affected */ |
| 5852 | return 0; |
| 5853 | |
| 5854 | return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test); |
| 5855 | } |
| 5856 | |
| 5857 | static int |
| 5858 | acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5859 | struct acl_expr *expr, struct acl_test *test) |
| 5860 | { |
| 5861 | struct http_txn *txn = l7; |
| 5862 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5863 | if (!txn) |
| 5864 | return 0; |
| 5865 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5866 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5867 | return 0; |
| 5868 | |
| 5869 | return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test); |
| 5870 | } |
| 5871 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5872 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 5873 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 5874 | */ |
| 5875 | static int |
| 5876 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5877 | struct acl_expr *expr, struct acl_test *test) |
| 5878 | { |
| 5879 | struct http_txn *txn = l7; |
| 5880 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5881 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5882 | if (!txn) |
| 5883 | return 0; |
| 5884 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5885 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5886 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5887 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5888 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5889 | /* ensure the indexes are not affected */ |
| 5890 | return 0; |
| 5891 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5892 | end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 5893 | ptr = http_get_path(txn); |
| 5894 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5895 | return 0; |
| 5896 | |
| 5897 | /* OK, we got the '/' ! */ |
| 5898 | test->ptr = ptr; |
| 5899 | |
| 5900 | while (ptr < end && *ptr != '?') |
| 5901 | ptr++; |
| 5902 | |
| 5903 | test->len = ptr - test->ptr; |
| 5904 | |
| 5905 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 5906 | test->flags = ACL_TEST_F_VOL_1ST; |
| 5907 | return 1; |
| 5908 | } |
| 5909 | |
| 5910 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5911 | |
| 5912 | /************************************************************************/ |
| 5913 | /* All supported keywords must be declared here. */ |
| 5914 | /************************************************************************/ |
| 5915 | |
| 5916 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 5917 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5918 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT }, |
| 5919 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5920 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 5921 | { "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] | 5922 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5923 | { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5924 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 5925 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 5926 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 5927 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 5928 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 5929 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 5930 | { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE }, |
| 5931 | { "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] | 5932 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5933 | /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */ |
| 5934 | { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5935 | { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 5936 | { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 5937 | { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 5938 | { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 5939 | { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 5940 | { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 5941 | { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
| 5942 | { "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] | 5943 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5944 | { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 5945 | { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE }, |
| 5946 | { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE }, |
| 5947 | { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE }, |
| 5948 | { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE }, |
| 5949 | { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE }, |
| 5950 | { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE }, |
| 5951 | { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
| 5952 | { "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] | 5953 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5954 | { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5955 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 5956 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 5957 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 5958 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 5959 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 5960 | { "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] | 5961 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 5962 | { NULL, NULL, NULL, NULL }, |
| 5963 | |
| 5964 | #if 0 |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5965 | { "line", acl_parse_str, acl_fetch_line, acl_match_str }, |
| 5966 | { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg }, |
| 5967 | { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg }, |
| 5968 | { "line_end", acl_parse_str, acl_fetch_line, acl_match_end }, |
| 5969 | { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub }, |
| 5970 | { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir }, |
| 5971 | { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom }, |
| 5972 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5973 | { "cook", acl_parse_str, acl_fetch_cook, acl_match_str }, |
| 5974 | { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg }, |
| 5975 | { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg }, |
| 5976 | { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end }, |
| 5977 | { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub }, |
| 5978 | { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir }, |
| 5979 | { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom }, |
| 5980 | { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst }, |
| 5981 | |
| 5982 | { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str }, |
| 5983 | { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg }, |
| 5984 | { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str }, |
| 5985 | { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 }, |
| 5986 | { NULL, NULL, NULL, NULL }, |
| 5987 | #endif |
| 5988 | }}; |
| 5989 | |
| 5990 | |
| 5991 | __attribute__((constructor)) |
| 5992 | static void __http_protocol_init(void) |
| 5993 | { |
| 5994 | acl_register_keywords(&acl_kws); |
| 5995 | } |
| 5996 | |
| 5997 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5998 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5999 | * Local variables: |
| 6000 | * c-indent-level: 8 |
| 6001 | * c-basic-offset: 8 |
| 6002 | * End: |
| 6003 | */ |