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 | 3c6ab2e | 2008-09-04 11:19:41 +0200 | [diff] [blame^] | 664 | buffer_check_timeouts(s->req); |
| 665 | buffer_check_timeouts(s->rep); |
| 666 | |
| 667 | if (unlikely(s->req->flags & (BF_READ_TIMEOUT|BF_WRITE_TIMEOUT))) { |
| 668 | if (s->req->flags & BF_READ_TIMEOUT) { |
| 669 | buffer_shutw(s->req); |
| 670 | s->req->cons->shutr(s->req->prod); |
| 671 | } |
| 672 | if (s->req->flags & BF_WRITE_TIMEOUT) { |
| 673 | buffer_shutw(s->req); |
| 674 | s->req->cons->shutw(s->req->cons); |
| 675 | } |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame] | 676 | } |
Willy Tarreau | 3c6ab2e | 2008-09-04 11:19:41 +0200 | [diff] [blame^] | 677 | |
| 678 | if (unlikely(s->rep->flags & (BF_READ_TIMEOUT|BF_WRITE_TIMEOUT))) { |
| 679 | if (s->rep->flags & BF_READ_TIMEOUT) { |
| 680 | buffer_shutw(s->rep); |
| 681 | s->rep->cons->shutr(s->rep->prod); |
| 682 | } |
| 683 | if (s->rep->flags & BF_WRITE_TIMEOUT) { |
| 684 | buffer_shutw(s->rep); |
| 685 | s->rep->cons->shutw(s->rep->cons); |
| 686 | } |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame] | 687 | } |
Willy Tarreau | 3537467 | 2008-09-03 18:11:02 +0200 | [diff] [blame] | 688 | /* Note that we don't check nor indicate if we wake up because |
| 689 | * of a timeout on a stream interface. |
| 690 | */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 691 | } |
| 692 | |
Willy Tarreau | 48adac5 | 2008-08-30 04:58:38 +0200 | [diff] [blame] | 693 | /* Check if we need to close the write side. This can only happen |
| 694 | * when either SHUTR or EMPTY appears, because WRITE_ENA cannot appear |
| 695 | * from low level, and neither HIJACK nor SHUTW can disappear from low |
| 696 | * level. |
| 697 | */ |
| 698 | if (unlikely((s->req->flags & (BF_SHUTW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) == (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR))) { |
| 699 | buffer_shutw(s->req); |
| 700 | s->req->cons->shutw(s->req->cons); |
| 701 | } |
| 702 | |
| 703 | if (unlikely((s->rep->flags & (BF_SHUTW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) == (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR))) { |
| 704 | buffer_shutw(s->rep); |
| 705 | s->rep->cons->shutw(s->rep->cons); |
| 706 | } |
| 707 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 708 | /* When a server-side connection is released, we have to |
| 709 | * count it and check for pending connections on this server. |
| 710 | */ |
| 711 | if (unlikely(s->req->cons->state == SI_ST_CLO && |
| 712 | s->req->cons->prev_state == SI_ST_EST)) { |
| 713 | /* Count server-side errors (but not timeouts). */ |
| 714 | if (s->req->flags & BF_WRITE_ERROR) { |
| 715 | s->be->failed_resp++; |
| 716 | if (s->srv) |
| 717 | s->srv->failed_resp++; |
| 718 | } |
| 719 | |
| 720 | if (s->srv) { |
| 721 | s->srv->cur_sess--; |
| 722 | sess_change_server(s, NULL); |
| 723 | if (may_dequeue_tasks(s->srv, s->be)) |
| 724 | process_srv_queue(s->srv); |
| 725 | } |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 726 | } |
| 727 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 728 | /* Dirty trick: force one first pass everywhere */ |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame] | 729 | rqf_cli = rqf_srv = ~s->req->flags; |
| 730 | rpf_cli = rpf_srv = ~s->rep->flags; |
Willy Tarreau | 507385d | 2008-08-17 13:04:25 +0200 | [diff] [blame] | 731 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 732 | /* well, the ST_CONN state is already handled properly */ |
| 733 | if (s->req->prod->state == SI_ST_EST) { |
| 734 | rqf_cli = s->req->flags; |
| 735 | rpf_cli = s->rep->flags; |
| 736 | } |
| 737 | |
| 738 | if (s->req->cons->state == SI_ST_EST) { |
| 739 | rqf_srv = s->req->flags; |
| 740 | rpf_srv = s->rep->flags; |
| 741 | } |
| 742 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 743 | do { |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 744 | 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", |
| 745 | now_ms, __FUNCTION__, |
| 746 | t, |
| 747 | s->req, s->rep, |
| 748 | s->req->rex, s->rep->wex, |
| 749 | s->req->flags, s->rep->flags, |
| 750 | s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state); |
| 751 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 752 | resync = 0; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 753 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 754 | /* Maybe resync client FD state */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 755 | if (s->rep->cons->state != SI_ST_CLO) { |
| 756 | if (((rqf_cli ^ s->req->flags) & BF_MASK_INTERFACE_I) || |
| 757 | ((rpf_cli ^ s->rep->flags) & BF_MASK_INTERFACE_O)) { |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 758 | stream_sock_data_update(s->rep->cons->fd); |
| 759 | rqf_cli = s->req->flags; |
| 760 | rpf_cli = s->rep->flags; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 761 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 762 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 763 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 764 | /* Maybe resync server FD state */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 765 | if (s->req->cons->state != SI_ST_CLO) { |
| 766 | if (((rpf_srv ^ s->rep->flags) & BF_MASK_INTERFACE_I) || |
| 767 | ((rqf_srv ^ s->req->flags) & BF_MASK_INTERFACE_O)) { |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame] | 768 | 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] | 769 | process_srv_conn(s); |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame] | 770 | resync = 1; /* we might have to resync */ |
| 771 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 772 | |
| 773 | if (s->req->cons->state == SI_ST_EST) { |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 774 | 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] | 775 | s->be->options & PR_O_FORCE_CLO && |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 776 | s->rep->flags & BF_READ_ACTIVITY) { |
Willy Tarreau | 376580a | 2008-08-27 18:52:22 +0200 | [diff] [blame] | 777 | /* We want to force the connection to the server to close, |
| 778 | * and the server has begun to respond. That's the right |
| 779 | * time. |
| 780 | */ |
| 781 | buffer_shutw_now(s->req); |
| 782 | } |
| 783 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 784 | stream_sock_data_update(s->req->cons->fd); |
Willy Tarreau | 376580a | 2008-08-27 18:52:22 +0200 | [diff] [blame] | 785 | |
| 786 | /* When a server-side connection is released, we have to |
| 787 | * count it and check for pending connections on this server. |
| 788 | */ |
| 789 | if (s->req->cons->state == SI_ST_CLO) { |
| 790 | if (s->srv) { |
| 791 | s->srv->cur_sess--; |
| 792 | sess_change_server(s, NULL); |
| 793 | if (may_dequeue_tasks(s->srv, s->be)) |
| 794 | process_srv_queue(s->srv); |
| 795 | } |
| 796 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 797 | } |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 798 | rqf_srv = s->req->flags; |
| 799 | rpf_srv = s->rep->flags; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 800 | } |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | /* we may have to resync because of pending connections */ |
| 804 | if (resync) |
| 805 | continue; |
| 806 | |
| 807 | /**** Process layer 7 below ****/ |
| 808 | |
| 809 | /* Analyse request */ |
| 810 | if (s->req->flags & BF_MASK_ANALYSER) { |
| 811 | unsigned int flags = s->req->flags; |
| 812 | |
| 813 | if (s->req->prod->state >= SI_ST_EST) { |
| 814 | /* it's up to the analysers to reset write_ena */ |
| 815 | buffer_write_ena(s->req); |
| 816 | if (s->req->analysers) |
| 817 | process_request(s); |
| 818 | } |
| 819 | s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 820 | flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 821 | if (s->req->flags != flags) |
| 822 | resync = 1; |
| 823 | } |
| 824 | |
| 825 | /* Analyse response */ |
| 826 | if (unlikely(s->rep->flags & BF_HIJACK)) { |
| 827 | /* In inject mode, we wake up everytime something has |
| 828 | * happened on the write side of the buffer. |
| 829 | */ |
| 830 | unsigned int flags = s->rep->flags; |
| 831 | |
| 832 | if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) && |
| 833 | !(s->rep->flags & BF_FULL)) { |
| 834 | produce_content(s); |
| 835 | } |
| 836 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 837 | flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 838 | if (s->rep->flags != flags) |
| 839 | resync = 1; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 840 | } |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame] | 841 | else if (s->rep->flags & BF_MASK_ANALYSER) { |
| 842 | unsigned int flags = s->rep->flags; |
| 843 | |
| 844 | if (s->rep->prod->state >= SI_ST_EST) { |
| 845 | /* it's up to the analysers to reset write_ena */ |
| 846 | buffer_write_ena(s->rep); |
| 847 | if (s->rep->analysers) |
| 848 | process_response(s); |
| 849 | } |
| 850 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 851 | flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 852 | if (s->rep->flags != flags) |
| 853 | resync = 1; |
| 854 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 855 | |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame] | 856 | /* For the moment, we need to clean the client and server flags that |
| 857 | * have vanished. This is just a temporary measure though. |
| 858 | */ |
| 859 | rqf_cli &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 860 | rqf_srv &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 861 | rpf_cli &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 862 | rpf_srv &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 863 | } while (resync); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 864 | |
Willy Tarreau | a37095b | 2008-09-03 11:37:47 +0200 | [diff] [blame] | 865 | /* This is needed only when debugging is enabled, to indicate |
| 866 | * client-side or server-side close. Please note that in the unlikely |
| 867 | * event where both sides would close at once, the sequence is reported |
| 868 | * on the server side first. |
| 869 | */ |
| 870 | if (unlikely((global.mode & MODE_DEBUG) && |
| 871 | (!(global.mode & MODE_QUIET) || |
| 872 | (global.mode & MODE_VERBOSE)))) { |
| 873 | int len; |
| 874 | |
| 875 | if (s->si[1].state == SI_ST_CLO && |
| 876 | s->si[1].prev_state == SI_ST_EST) { |
| 877 | len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n", |
| 878 | s->uniq_id, s->be->id, |
| 879 | (unsigned short)s->si[0].fd, |
| 880 | (unsigned short)s->si[1].fd); |
| 881 | write(1, trash, len); |
| 882 | } |
| 883 | |
| 884 | if (s->si[0].state == SI_ST_CLO && |
| 885 | s->si[0].prev_state == SI_ST_EST) { |
| 886 | len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", |
| 887 | s->uniq_id, s->be->id, |
| 888 | (unsigned short)s->si[0].fd, |
| 889 | (unsigned short)s->si[1].fd); |
| 890 | write(1, trash, len); |
| 891 | } |
| 892 | } |
| 893 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 894 | if (likely((s->rep->cons->state != SI_ST_CLO) || |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 895 | (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] | 896 | |
| 897 | if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED)) |
| 898 | session_process_counters(s); |
| 899 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 900 | if (s->rep->cons->state == SI_ST_EST) |
| 901 | stream_sock_data_finish(s->rep->cons->fd); |
| 902 | |
| 903 | if (s->req->cons->state == SI_ST_EST) |
| 904 | stream_sock_data_finish(s->req->cons->fd); |
| 905 | |
Willy Tarreau | 9a2d154 | 2008-08-30 12:31:07 +0200 | [diff] [blame] | 906 | s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 907 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 908 | s->si[0].prev_state = s->si[0].state; |
| 909 | s->si[1].prev_state = s->si[1].state; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 910 | |
Willy Tarreau | 7f875f6 | 2008-08-11 17:35:01 +0200 | [diff] [blame] | 911 | /* Trick: if a request is being waiting for the server to respond, |
| 912 | * and if we know the server can timeout, we don't want the timeout |
| 913 | * to expire on the client side first, but we're still interested |
| 914 | * in passing data from the client to the server (eg: POST). Thus, |
| 915 | * we can cancel the client's request timeout if the server's |
| 916 | * request timeout is set and the server has not yet sent a response. |
| 917 | */ |
| 918 | |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 919 | if ((s->rep->flags & (BF_WRITE_ENA|BF_SHUTR)) == 0 && |
Willy Tarreau | 26ed74d | 2008-08-17 12:11:14 +0200 | [diff] [blame] | 920 | (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) |
Willy Tarreau | 7f875f6 | 2008-08-11 17:35:01 +0200 | [diff] [blame] | 921 | s->req->rex = TICK_ETERNITY; |
| 922 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 923 | t->expire = tick_first(tick_first(s->req->rex, s->req->wex), |
| 924 | tick_first(s->rep->rex, s->rep->wex)); |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 925 | if (s->req->analysers) |
| 926 | t->expire = tick_first(t->expire, s->req->analyse_exp); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 927 | |
Willy Tarreau | 3537467 | 2008-09-03 18:11:02 +0200 | [diff] [blame] | 928 | if (s->si[0].exp) |
| 929 | t->expire = tick_first(t->expire, s->si[0].exp); |
| 930 | |
| 931 | if (s->si[1].exp) |
| 932 | t->expire = tick_first(t->expire, s->si[1].exp); |
| 933 | |
Willy Tarreau | cb65125 | 2008-08-29 13:57:30 +0200 | [diff] [blame] | 934 | #ifdef DEBUG_FULL |
| 935 | fprintf(stderr, "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u rep->rex=%u rep->wex=%u\n", |
| 936 | now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp, s->rep->rex, s->rep->wex); |
| 937 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 938 | /* restore t to its place in the task list */ |
| 939 | task_queue(t); |
| 940 | |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 941 | #ifdef DEBUG_DEV |
| 942 | /* this may only happen when no timeout is set or in case of an FSM bug */ |
| 943 | if (!t->expire) |
| 944 | ABORT_NOW(); |
| 945 | #endif |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 946 | *next = t->expire; |
| 947 | return; /* nothing more to do */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 948 | } |
| 949 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 950 | s->fe->feconn--; |
| 951 | if (s->flags & SN_BE_ASSIGNED) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 952 | s->be->beconn--; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 953 | actconn--; |
| 954 | |
Willy Tarreau | f41d4b1 | 2007-04-28 23:26:14 +0200 | [diff] [blame] | 955 | if (unlikely((global.mode & MODE_DEBUG) && |
| 956 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 957 | int len; |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 958 | 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] | 959 | s->uniq_id, s->be->id, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 960 | (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd, |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 961 | s->term_trace); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 962 | write(1, trash, len); |
| 963 | } |
| 964 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 965 | 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] | 966 | session_process_counters(s); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 967 | |
| 968 | /* let's do a final log if we need it */ |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 969 | if (s->logs.logwait && |
| 970 | !(s->flags & SN_MONITOR) && |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 971 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 972 | if (s->fe->to_log & LW_REQ) |
| 973 | http_sess_log(s); |
| 974 | else |
| 975 | tcp_sess_log(s); |
| 976 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 977 | |
| 978 | /* the task MUST not be in the run queue anymore */ |
| 979 | task_delete(t); |
| 980 | session_free(s); |
| 981 | task_free(t); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 982 | *next = TICK_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 986 | extern const char sess_term_cond[8]; |
| 987 | extern const char sess_fin_state[8]; |
| 988 | extern const char *monthname[12]; |
| 989 | const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */ |
| 990 | const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown, |
| 991 | Set-cookie seen and left unchanged (passive), Set-cookie Deleted, |
| 992 | unknown, Set-cookie Rewritten */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 993 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 994 | struct pool_head *pool2_capture; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 995 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 996 | /* |
| 997 | * send a log for the session when we have enough info about it. |
| 998 | * Will not log if the frontend has no log defined. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 999 | */ |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1000 | static void http_sess_log(struct session *s) |
| 1001 | { |
| 1002 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 1003 | struct proxy *fe = s->fe; |
| 1004 | struct proxy *be = s->be; |
| 1005 | struct proxy *prx_log; |
| 1006 | struct http_txn *txn = &s->txn; |
| 1007 | int tolog; |
| 1008 | char *uri, *h; |
| 1009 | char *svid; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1010 | struct tm tm; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1011 | static char tmpline[MAX_SYSLOG_LEN]; |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1012 | int t_request; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1013 | int hdr; |
| 1014 | |
| 1015 | if (fe->logfac1 < 0 && fe->logfac2 < 0) |
| 1016 | return; |
| 1017 | prx_log = fe; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1018 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1019 | if (s->cli_addr.ss_family == AF_INET) |
| 1020 | inet_ntop(AF_INET, |
| 1021 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 1022 | pn, sizeof(pn)); |
| 1023 | else |
| 1024 | inet_ntop(AF_INET6, |
| 1025 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 1026 | pn, sizeof(pn)); |
| 1027 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 1028 | get_localtime(s->logs.accept_date.tv_sec, &tm); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1029 | |
| 1030 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 1031 | tolog = fe->to_log; |
| 1032 | |
| 1033 | h = tmpline; |
| 1034 | if (fe->to_log & LW_REQHDR && |
| 1035 | txn->req.cap && |
| 1036 | (h < tmpline + sizeof(tmpline) - 10)) { |
| 1037 | *(h++) = ' '; |
| 1038 | *(h++) = '{'; |
| 1039 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 1040 | if (hdr) |
| 1041 | *(h++) = '|'; |
| 1042 | if (txn->req.cap[hdr] != NULL) |
| 1043 | h = encode_string(h, tmpline + sizeof(tmpline) - 7, |
| 1044 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 1045 | } |
| 1046 | *(h++) = '}'; |
| 1047 | } |
| 1048 | |
| 1049 | if (fe->to_log & LW_RSPHDR && |
| 1050 | txn->rsp.cap && |
| 1051 | (h < tmpline + sizeof(tmpline) - 7)) { |
| 1052 | *(h++) = ' '; |
| 1053 | *(h++) = '{'; |
| 1054 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 1055 | if (hdr) |
| 1056 | *(h++) = '|'; |
| 1057 | if (txn->rsp.cap[hdr] != NULL) |
| 1058 | h = encode_string(h, tmpline + sizeof(tmpline) - 4, |
| 1059 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 1060 | } |
| 1061 | *(h++) = '}'; |
| 1062 | } |
| 1063 | |
| 1064 | if (h < tmpline + sizeof(tmpline) - 4) { |
| 1065 | *(h++) = ' '; |
| 1066 | *(h++) = '"'; |
| 1067 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 1068 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 1069 | '#', url_encode_map, uri); |
| 1070 | *(h++) = '"'; |
| 1071 | } |
| 1072 | *h = '\0'; |
| 1073 | |
| 1074 | svid = (tolog & LW_SVID) ? |
| 1075 | (s->data_source != DATA_SRC_STATS) ? |
| 1076 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 1077 | |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1078 | t_request = -1; |
| 1079 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 1080 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 1081 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1082 | send_log(prx_log, LOG_INFO, |
| 1083 | "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]" |
| 1084 | " %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] | 1085 | " %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] | 1086 | pn, |
| 1087 | (s->cli_addr.ss_family == AF_INET) ? |
| 1088 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 1089 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1090 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 1091 | 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] | 1092 | fe->id, be->id, svid, |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1093 | t_request, |
| 1094 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1095 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 1096 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 1097 | (tolog & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 1098 | txn->status, |
Willy Tarreau | 8b3977f | 2008-01-18 11:16:32 +0100 | [diff] [blame] | 1099 | (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1100 | txn->cli_cookie ? txn->cli_cookie : "-", |
| 1101 | txn->srv_cookie ? txn->srv_cookie : "-", |
| 1102 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 1103 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 1104 | (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 1105 | (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-', |
| 1106 | 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] | 1107 | (s->flags & SN_REDISP)?"+":"", |
| 1108 | (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] | 1109 | s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline); |
| 1110 | |
| 1111 | s->logs.logwait = 0; |
| 1112 | } |
| 1113 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1114 | |
| 1115 | /* |
| 1116 | * Capture headers from message starting at <som> according to header list |
| 1117 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 1118 | */ |
| 1119 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1120 | char **cap, struct cap_hdr *cap_hdr) |
| 1121 | { |
| 1122 | char *eol, *sol, *col, *sov; |
| 1123 | int cur_idx; |
| 1124 | struct cap_hdr *h; |
| 1125 | int len; |
| 1126 | |
| 1127 | sol = som + hdr_idx_first_pos(idx); |
| 1128 | cur_idx = hdr_idx_first_idx(idx); |
| 1129 | |
| 1130 | while (cur_idx) { |
| 1131 | eol = sol + idx->v[cur_idx].len; |
| 1132 | |
| 1133 | col = sol; |
| 1134 | while (col < eol && *col != ':') |
| 1135 | col++; |
| 1136 | |
| 1137 | sov = col + 1; |
| 1138 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 1139 | sov++; |
| 1140 | |
| 1141 | for (h = cap_hdr; h; h = h->next) { |
| 1142 | if ((h->namelen == col - sol) && |
| 1143 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1144 | if (cap[h->index] == NULL) |
| 1145 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1146 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1147 | |
| 1148 | if (cap[h->index] == NULL) { |
| 1149 | Alert("HTTP capture : out of memory.\n"); |
| 1150 | continue; |
| 1151 | } |
| 1152 | |
| 1153 | len = eol - sov; |
| 1154 | if (len > h->len) |
| 1155 | len = h->len; |
| 1156 | |
| 1157 | memcpy(cap[h->index], sov, len); |
| 1158 | cap[h->index][len]=0; |
| 1159 | } |
| 1160 | } |
| 1161 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1162 | cur_idx = idx->v[cur_idx].next; |
| 1163 | } |
| 1164 | } |
| 1165 | |
| 1166 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1167 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1168 | */ |
| 1169 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1170 | |
| 1171 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1172 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1173 | */ |
| 1174 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1175 | ptr++; \ |
| 1176 | if (likely(ptr < end)) \ |
| 1177 | goto good; \ |
| 1178 | else { \ |
| 1179 | state = (st); \ |
| 1180 | goto http_msg_ood; \ |
| 1181 | } \ |
| 1182 | } while (0) |
| 1183 | |
| 1184 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1185 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1186 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1187 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1188 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1189 | * will give undefined results. |
| 1190 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1191 | * and that msg->sol points to the beginning of the response. |
| 1192 | * If a complete line is found (which implies that at least one CR or LF is |
| 1193 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1194 | * returned indicating an incomplete line (which does not mean that parts have |
| 1195 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1196 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1197 | * upon next call. |
| 1198 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1199 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1200 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1201 | * 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] | 1202 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1203 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1204 | const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, |
| 1205 | unsigned int state, const char *ptr, const char *end, |
| 1206 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1207 | { |
| 1208 | __label__ |
| 1209 | http_msg_rpver, |
| 1210 | http_msg_rpver_sp, |
| 1211 | http_msg_rpcode, |
| 1212 | http_msg_rpcode_sp, |
| 1213 | http_msg_rpreason, |
| 1214 | http_msg_rpline_eol, |
| 1215 | http_msg_ood, /* out of data */ |
| 1216 | http_msg_invalid; |
| 1217 | |
| 1218 | switch (state) { |
| 1219 | http_msg_rpver: |
| 1220 | case HTTP_MSG_RPVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1221 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1222 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1223 | |
| 1224 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1225 | msg->sl.st.v_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1226 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1227 | } |
| 1228 | goto http_msg_invalid; |
| 1229 | |
| 1230 | http_msg_rpver_sp: |
| 1231 | case HTTP_MSG_RPVER_SP: |
| 1232 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1233 | msg->sl.st.c = ptr - msg_buf; |
| 1234 | goto http_msg_rpcode; |
| 1235 | } |
| 1236 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1237 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1238 | /* so it's a CR/LF, this is invalid */ |
| 1239 | goto http_msg_invalid; |
| 1240 | |
| 1241 | http_msg_rpcode: |
| 1242 | case HTTP_MSG_RPCODE: |
| 1243 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1244 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1245 | |
| 1246 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1247 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 1248 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1249 | } |
| 1250 | |
| 1251 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1252 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 1253 | http_msg_rsp_reason: |
| 1254 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
| 1255 | msg->sl.st.r = ptr - msg_buf; |
| 1256 | msg->sl.st.r_l = 0; |
| 1257 | goto http_msg_rpline_eol; |
| 1258 | |
| 1259 | http_msg_rpcode_sp: |
| 1260 | case HTTP_MSG_RPCODE_SP: |
| 1261 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1262 | msg->sl.st.r = ptr - msg_buf; |
| 1263 | goto http_msg_rpreason; |
| 1264 | } |
| 1265 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1266 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1267 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1268 | goto http_msg_rsp_reason; |
| 1269 | |
| 1270 | http_msg_rpreason: |
| 1271 | case HTTP_MSG_RPREASON: |
| 1272 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1273 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
| 1274 | msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r; |
| 1275 | http_msg_rpline_eol: |
| 1276 | /* We have seen the end of line. Note that we do not |
| 1277 | * necessarily have the \n yet, but at least we know that we |
| 1278 | * have EITHER \r OR \n, otherwise the response would not be |
| 1279 | * complete. We can then record the response length and return |
| 1280 | * to the caller which will be able to register it. |
| 1281 | */ |
| 1282 | msg->sl.st.l = ptr - msg->sol; |
| 1283 | return ptr; |
| 1284 | |
| 1285 | #ifdef DEBUG_FULL |
| 1286 | default: |
| 1287 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1288 | exit(1); |
| 1289 | #endif |
| 1290 | } |
| 1291 | |
| 1292 | http_msg_ood: |
| 1293 | /* out of data */ |
| 1294 | if (ret_state) |
| 1295 | *ret_state = state; |
| 1296 | if (ret_ptr) |
| 1297 | *ret_ptr = (char *)ptr; |
| 1298 | return NULL; |
| 1299 | |
| 1300 | http_msg_invalid: |
| 1301 | /* invalid message */ |
| 1302 | if (ret_state) |
| 1303 | *ret_state = HTTP_MSG_ERROR; |
| 1304 | return NULL; |
| 1305 | } |
| 1306 | |
| 1307 | |
| 1308 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1309 | * This function parses a request line between <ptr> and <end>, starting with |
| 1310 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1311 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1312 | * will give undefined results. |
| 1313 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1314 | * and that msg->sol points to the beginning of the request. |
| 1315 | * If a complete line is found (which implies that at least one CR or LF is |
| 1316 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1317 | * returned indicating an incomplete line (which does not mean that parts have |
| 1318 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1319 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1320 | * upon next call. |
| 1321 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1322 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1323 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1324 | * 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] | 1325 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1326 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1327 | const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, |
| 1328 | unsigned int state, const char *ptr, const char *end, |
| 1329 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1330 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1331 | __label__ |
| 1332 | http_msg_rqmeth, |
| 1333 | http_msg_rqmeth_sp, |
| 1334 | http_msg_rquri, |
| 1335 | http_msg_rquri_sp, |
| 1336 | http_msg_rqver, |
| 1337 | http_msg_rqline_eol, |
| 1338 | http_msg_ood, /* out of data */ |
| 1339 | http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1340 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1341 | switch (state) { |
| 1342 | http_msg_rqmeth: |
| 1343 | case HTTP_MSG_RQMETH: |
| 1344 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1345 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1346 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1347 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1348 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1349 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1350 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1351 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1352 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1353 | /* HTTP 0.9 request */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1354 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1355 | http_msg_req09_uri: |
| 1356 | msg->sl.rq.u = ptr - msg_buf; |
| 1357 | http_msg_req09_uri_e: |
| 1358 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1359 | http_msg_req09_ver: |
| 1360 | msg->sl.rq.v = ptr - msg_buf; |
| 1361 | msg->sl.rq.v_l = 0; |
| 1362 | goto http_msg_rqline_eol; |
| 1363 | } |
| 1364 | goto http_msg_invalid; |
| 1365 | |
| 1366 | http_msg_rqmeth_sp: |
| 1367 | case HTTP_MSG_RQMETH_SP: |
| 1368 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1369 | msg->sl.rq.u = ptr - msg_buf; |
| 1370 | goto http_msg_rquri; |
| 1371 | } |
| 1372 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1373 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1374 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1375 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1376 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1377 | http_msg_rquri: |
| 1378 | case HTTP_MSG_RQURI: |
| 1379 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1380 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1381 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1382 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1383 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1384 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1385 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1386 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1387 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1388 | goto http_msg_req09_uri_e; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1389 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1390 | http_msg_rquri_sp: |
| 1391 | case HTTP_MSG_RQURI_SP: |
| 1392 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1393 | msg->sl.rq.v = ptr - msg_buf; |
| 1394 | goto http_msg_rqver; |
| 1395 | } |
| 1396 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1397 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1398 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1399 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1400 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1401 | http_msg_rqver: |
| 1402 | case HTTP_MSG_RQVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1403 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1404 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1405 | |
| 1406 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1407 | msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v; |
| 1408 | http_msg_rqline_eol: |
| 1409 | /* We have seen the end of line. Note that we do not |
| 1410 | * necessarily have the \n yet, but at least we know that we |
| 1411 | * have EITHER \r OR \n, otherwise the request would not be |
| 1412 | * complete. We can then record the request length and return |
| 1413 | * to the caller which will be able to register it. |
| 1414 | */ |
| 1415 | msg->sl.rq.l = ptr - msg->sol; |
| 1416 | return ptr; |
| 1417 | } |
| 1418 | |
| 1419 | /* neither an HTTP_VER token nor a CRLF */ |
| 1420 | goto http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1421 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1422 | #ifdef DEBUG_FULL |
| 1423 | default: |
| 1424 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1425 | exit(1); |
| 1426 | #endif |
| 1427 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1428 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1429 | http_msg_ood: |
| 1430 | /* out of data */ |
| 1431 | if (ret_state) |
| 1432 | *ret_state = state; |
| 1433 | if (ret_ptr) |
| 1434 | *ret_ptr = (char *)ptr; |
| 1435 | return NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1436 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1437 | http_msg_invalid: |
| 1438 | /* invalid message */ |
| 1439 | if (ret_state) |
| 1440 | *ret_state = HTTP_MSG_ERROR; |
| 1441 | return NULL; |
| 1442 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1443 | |
| 1444 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1445 | /* |
| 1446 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1447 | * depending on the initial msg->msg_state. It can be preempted everywhere |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1448 | * when data are missing and recalled at the exact same location with no |
| 1449 | * information loss. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1450 | * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other |
| 1451 | * fields. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1452 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1453 | void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx) |
| 1454 | { |
| 1455 | __label__ |
| 1456 | http_msg_rqbefore, |
| 1457 | http_msg_rqbefore_cr, |
| 1458 | http_msg_rqmeth, |
| 1459 | http_msg_rqline_end, |
| 1460 | http_msg_hdr_first, |
| 1461 | http_msg_hdr_name, |
| 1462 | http_msg_hdr_l1_sp, |
| 1463 | http_msg_hdr_l1_lf, |
| 1464 | http_msg_hdr_l1_lws, |
| 1465 | http_msg_hdr_val, |
| 1466 | http_msg_hdr_l2_lf, |
| 1467 | http_msg_hdr_l2_lws, |
| 1468 | http_msg_complete_header, |
| 1469 | http_msg_last_lf, |
| 1470 | http_msg_ood, /* out of data */ |
| 1471 | http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1472 | |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1473 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1474 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1475 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1476 | state = msg->msg_state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1477 | ptr = buf->lr; |
| 1478 | end = buf->r; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1479 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1480 | if (unlikely(ptr >= end)) |
| 1481 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1482 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1483 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1484 | /* |
| 1485 | * First, states that are specific to the response only. |
| 1486 | * We check them first so that request and headers are |
| 1487 | * closer to each other (accessed more often). |
| 1488 | */ |
| 1489 | http_msg_rpbefore: |
| 1490 | case HTTP_MSG_RPBEFORE: |
| 1491 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1492 | if (likely(ptr == buf->data)) { |
| 1493 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1494 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1495 | } else { |
| 1496 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1497 | /* only skip empty leading lines, don't remove them */ |
| 1498 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1499 | msg->som = ptr - buf->data; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1500 | #else |
| 1501 | /* Remove empty leading lines, as recommended by |
| 1502 | * RFC2616. This takes a lot of time because we |
| 1503 | * must move all the buffer backwards, but this |
| 1504 | * is rarely needed. The method above will be |
| 1505 | * cleaner when we'll be able to start sending |
| 1506 | * the request from any place in the buffer. |
| 1507 | */ |
| 1508 | buf->lr = ptr; |
| 1509 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1510 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1511 | msg->sol = buf->data; |
| 1512 | ptr = buf->data; |
| 1513 | end = buf->r; |
| 1514 | #endif |
| 1515 | } |
| 1516 | hdr_idx_init(idx); |
| 1517 | state = HTTP_MSG_RPVER; |
| 1518 | goto http_msg_rpver; |
| 1519 | } |
| 1520 | |
| 1521 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1522 | goto http_msg_invalid; |
| 1523 | |
| 1524 | if (unlikely(*ptr == '\n')) |
| 1525 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1526 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1527 | /* stop here */ |
| 1528 | |
| 1529 | http_msg_rpbefore_cr: |
| 1530 | case HTTP_MSG_RPBEFORE_CR: |
| 1531 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1532 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1533 | /* stop here */ |
| 1534 | |
| 1535 | http_msg_rpver: |
| 1536 | case HTTP_MSG_RPVER: |
| 1537 | case HTTP_MSG_RPVER_SP: |
| 1538 | case HTTP_MSG_RPCODE: |
| 1539 | case HTTP_MSG_RPCODE_SP: |
| 1540 | case HTTP_MSG_RPREASON: |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1541 | ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1542 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1543 | if (unlikely(!ptr)) |
| 1544 | return; |
| 1545 | |
| 1546 | /* we have a full response and we know that we have either a CR |
| 1547 | * or an LF at <ptr>. |
| 1548 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1549 | //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] | 1550 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1551 | |
| 1552 | msg->sol = ptr; |
| 1553 | if (likely(*ptr == '\r')) |
| 1554 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1555 | goto http_msg_rpline_end; |
| 1556 | |
| 1557 | http_msg_rpline_end: |
| 1558 | case HTTP_MSG_RPLINE_END: |
| 1559 | /* msg->sol must point to the first of CR or LF. */ |
| 1560 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1561 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1562 | /* stop here */ |
| 1563 | |
| 1564 | /* |
| 1565 | * Second, states that are specific to the request only |
| 1566 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1567 | http_msg_rqbefore: |
| 1568 | case HTTP_MSG_RQBEFORE: |
| 1569 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1570 | if (likely(ptr == buf->data)) { |
| 1571 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1572 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1573 | } else { |
| 1574 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1575 | /* only skip empty leading lines, don't remove them */ |
| 1576 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1577 | msg->som = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1578 | #else |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1579 | /* Remove empty leading lines, as recommended by |
| 1580 | * RFC2616. This takes a lot of time because we |
| 1581 | * must move all the buffer backwards, but this |
| 1582 | * is rarely needed. The method above will be |
| 1583 | * cleaner when we'll be able to start sending |
| 1584 | * the request from any place in the buffer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1585 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1586 | buf->lr = ptr; |
| 1587 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1588 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1589 | msg->sol = buf->data; |
| 1590 | ptr = buf->data; |
| 1591 | end = buf->r; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1592 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1593 | } |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1594 | /* we will need this when keep-alive will be supported |
| 1595 | hdr_idx_init(idx); |
| 1596 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1597 | state = HTTP_MSG_RQMETH; |
| 1598 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1599 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1600 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1601 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1602 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1603 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1604 | if (unlikely(*ptr == '\n')) |
| 1605 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1606 | 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] | 1607 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1608 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1609 | http_msg_rqbefore_cr: |
| 1610 | case HTTP_MSG_RQBEFORE_CR: |
| 1611 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1612 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1613 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1614 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1615 | http_msg_rqmeth: |
| 1616 | case HTTP_MSG_RQMETH: |
| 1617 | case HTTP_MSG_RQMETH_SP: |
| 1618 | case HTTP_MSG_RQURI: |
| 1619 | case HTTP_MSG_RQURI_SP: |
| 1620 | case HTTP_MSG_RQVER: |
| 1621 | ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1622 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1623 | if (unlikely(!ptr)) |
| 1624 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1625 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1626 | /* we have a full request and we know that we have either a CR |
| 1627 | * or an LF at <ptr>. |
| 1628 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1629 | //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] | 1630 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1631 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1632 | msg->sol = ptr; |
| 1633 | if (likely(*ptr == '\r')) |
| 1634 | 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] | 1635 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1636 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1637 | http_msg_rqline_end: |
| 1638 | case HTTP_MSG_RQLINE_END: |
| 1639 | /* check for HTTP/0.9 request : no version information available. |
| 1640 | * msg->sol must point to the first of CR or LF. |
| 1641 | */ |
| 1642 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1643 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1644 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1645 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1646 | 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] | 1647 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1648 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1649 | /* |
| 1650 | * Common states below |
| 1651 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1652 | http_msg_hdr_first: |
| 1653 | case HTTP_MSG_HDR_FIRST: |
| 1654 | msg->sol = ptr; |
| 1655 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1656 | goto http_msg_hdr_name; |
| 1657 | } |
| 1658 | |
| 1659 | if (likely(*ptr == '\r')) |
| 1660 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1661 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1662 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1663 | http_msg_hdr_name: |
| 1664 | case HTTP_MSG_HDR_NAME: |
| 1665 | /* assumes msg->sol points to the first char */ |
| 1666 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1667 | 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] | 1668 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1669 | if (likely(*ptr == ':')) { |
| 1670 | msg->col = ptr - buf->data; |
| 1671 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| 1672 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1673 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1674 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1675 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1676 | http_msg_hdr_l1_sp: |
| 1677 | case HTTP_MSG_HDR_L1_SP: |
| 1678 | /* assumes msg->sol points to the first char and msg->col to the colon */ |
| 1679 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1680 | 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] | 1681 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1682 | /* header value can be basically anything except CR/LF */ |
| 1683 | msg->sov = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1684 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1685 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1686 | goto http_msg_hdr_val; |
| 1687 | } |
| 1688 | |
| 1689 | if (likely(*ptr == '\r')) |
| 1690 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1691 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1692 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1693 | http_msg_hdr_l1_lf: |
| 1694 | case HTTP_MSG_HDR_L1_LF: |
| 1695 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1696 | 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] | 1697 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1698 | http_msg_hdr_l1_lws: |
| 1699 | case HTTP_MSG_HDR_L1_LWS: |
| 1700 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1701 | /* replace HT,CR,LF with spaces */ |
| 1702 | for (; buf->data+msg->sov < ptr; msg->sov++) |
| 1703 | buf->data[msg->sov] = ' '; |
| 1704 | goto http_msg_hdr_l1_sp; |
| 1705 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1706 | /* we had a header consisting only in spaces ! */ |
| 1707 | msg->eol = buf->data + msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1708 | goto http_msg_complete_header; |
| 1709 | |
| 1710 | http_msg_hdr_val: |
| 1711 | case HTTP_MSG_HDR_VAL: |
| 1712 | /* assumes msg->sol points to the first char, msg->col to the |
| 1713 | * colon, and msg->sov points to the first character of the |
| 1714 | * value. |
| 1715 | */ |
| 1716 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1717 | 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] | 1718 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1719 | msg->eol = ptr; |
| 1720 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1721 | * real header end in case it ends with lots of LWS, but is this |
| 1722 | * really needed ? |
| 1723 | */ |
| 1724 | if (likely(*ptr == '\r')) |
| 1725 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1726 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1727 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1728 | http_msg_hdr_l2_lf: |
| 1729 | case HTTP_MSG_HDR_L2_LF: |
| 1730 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1731 | 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] | 1732 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1733 | http_msg_hdr_l2_lws: |
| 1734 | case HTTP_MSG_HDR_L2_LWS: |
| 1735 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1736 | /* LWS: replace HT,CR,LF with spaces */ |
| 1737 | for (; msg->eol < ptr; msg->eol++) |
| 1738 | *msg->eol = ' '; |
| 1739 | goto http_msg_hdr_val; |
| 1740 | } |
| 1741 | http_msg_complete_header: |
| 1742 | /* |
| 1743 | * It was a new header, so the last one is finished. |
| 1744 | * Assumes msg->sol points to the first char, msg->col to the |
| 1745 | * colon, msg->sov points to the first character of the value |
| 1746 | * and msg->eol to the first CR or LF so we know how the line |
| 1747 | * ends. We insert last header into the index. |
| 1748 | */ |
| 1749 | /* |
| 1750 | fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol); |
| 1751 | write(2, msg->sol, msg->eol-msg->sol); |
| 1752 | fprintf(stderr,"\n"); |
| 1753 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1754 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1755 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r', |
| 1756 | idx, idx->tail) < 0)) |
| 1757 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1758 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1759 | msg->sol = ptr; |
| 1760 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1761 | goto http_msg_hdr_name; |
| 1762 | } |
| 1763 | |
| 1764 | if (likely(*ptr == '\r')) |
| 1765 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1766 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1767 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1768 | http_msg_last_lf: |
| 1769 | case HTTP_MSG_LAST_LF: |
| 1770 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1771 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1772 | ptr++; |
| 1773 | buf->lr = ptr; |
| 1774 | msg->eoh = msg->sol - buf->data; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1775 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1776 | return; |
| 1777 | #ifdef DEBUG_FULL |
| 1778 | default: |
| 1779 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1780 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1781 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1782 | } |
| 1783 | http_msg_ood: |
| 1784 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1785 | msg->msg_state = state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1786 | buf->lr = ptr; |
| 1787 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1788 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1789 | http_msg_invalid: |
| 1790 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1791 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1792 | return; |
| 1793 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1794 | |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1795 | /* This function performs all the processing enabled for the current request. |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1796 | * It normally returns zero, but may return 1 if it absolutely needs to be |
| 1797 | * called again after other functions. It relies on buffers flags, and updates |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1798 | * 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] | 1799 | * functions. Its behaviour is rather simple : |
| 1800 | * - all enabled analysers are called in turn from the lower to the higher |
| 1801 | * bit. |
| 1802 | * - 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] | 1803 | * 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] | 1804 | * that unprocessed data will not be forwarded. But that probably depends on |
| 1805 | * the protocol. Generally it is not reset in case of errors. |
| 1806 | * - 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] | 1807 | * analyser without touching BF_WRITE_ENA (it is enabled prior to |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 1808 | * analysis). |
| 1809 | * - if an analyser thinks it has no added value anymore staying here, it must |
| 1810 | * reset its bit from the analysers flags in order not to be called anymore. |
| 1811 | * |
| 1812 | * In the future, analysers should be able to indicate that they want to be |
| 1813 | * called after XXX bytes have been received (or transfered), and the min of |
| 1814 | * all's wishes will be used to ring back (unless a special condition occurs). |
| 1815 | * |
| 1816 | * |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1817 | */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1818 | int process_request(struct session *t) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1819 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1820 | struct buffer *req = t->req; |
| 1821 | struct buffer *rep = t->rep; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1822 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 1823 | 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] | 1824 | now_ms, __FUNCTION__, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 1825 | t, |
| 1826 | req, |
| 1827 | req->rex, req->wex, |
| 1828 | req->flags, |
| 1829 | req->l, |
| 1830 | req->analysers); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1831 | |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 1832 | /* The tcp-inspect analyser is always called alone */ |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1833 | if (req->analysers & AN_REQ_INSPECT) { |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1834 | struct tcp_rule *rule; |
| 1835 | int partial; |
| 1836 | |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 1837 | /* We will abort if we encounter a read error. In theory, we |
| 1838 | * should not abort if we get a close, it might be valid, |
| 1839 | * although very unlikely. FIXME: we'll abort for now, this |
| 1840 | * will be easier to change later. |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1841 | */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1842 | if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1843 | req->analysers = 0; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 1844 | //t->fe->failed_req++; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1845 | if (!(t->flags & SN_ERR_MASK)) |
| 1846 | t->flags |= SN_ERR_CLICL; |
| 1847 | if (!(t->flags & SN_FINST_MASK)) |
| 1848 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1849 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1850 | } |
| 1851 | |
| 1852 | /* Abort if client read timeout has expired */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1853 | else if (req->flags & BF_READ_TIMEOUT) { |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1854 | req->analysers = 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1855 | t->fe->failed_req++; |
| 1856 | if (!(t->flags & SN_ERR_MASK)) |
| 1857 | t->flags |= SN_ERR_CLITO; |
| 1858 | if (!(t->flags & SN_FINST_MASK)) |
| 1859 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1860 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1861 | } |
| 1862 | |
| 1863 | /* We don't know whether we have enough data, so must proceed |
| 1864 | * this way : |
| 1865 | * - iterate through all rules in their declaration order |
| 1866 | * - if one rule returns MISS, it means the inspect delay is |
| 1867 | * not over yet, then return immediately, otherwise consider |
| 1868 | * it as a non-match. |
| 1869 | * - if one rule returns OK, then return OK |
| 1870 | * - if one rule returns KO, then return KO |
| 1871 | */ |
| 1872 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 1873 | 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] | 1874 | partial = 0; |
| 1875 | else |
| 1876 | partial = ACL_PARTIAL; |
| 1877 | |
| 1878 | list_for_each_entry(rule, &t->fe->tcp_req.inspect_rules, list) { |
| 1879 | int ret = ACL_PAT_PASS; |
| 1880 | |
| 1881 | if (rule->cond) { |
| 1882 | ret = acl_exec_cond(rule->cond, t->fe, t, NULL, ACL_DIR_REQ | partial); |
| 1883 | if (ret == ACL_PAT_MISS) { |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 1884 | buffer_write_dis(req); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1885 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 1886 | if (!tick_isset(req->analyse_exp)) |
| 1887 | 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] | 1888 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1889 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1890 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1891 | ret = acl_pass(ret); |
| 1892 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 1893 | ret = !ret; |
| 1894 | } |
| 1895 | |
| 1896 | if (ret) { |
| 1897 | /* we have a matching rule. */ |
| 1898 | if (rule->action == TCP_ACT_REJECT) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1899 | buffer_abort(req); |
| 1900 | buffer_abort(rep); |
| 1901 | //FIXME: this delete this |
| 1902 | //fd_delete(t->cli_fd); |
| 1903 | //t->cli_state = CL_STCLOSE; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1904 | req->analysers = 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1905 | t->fe->failed_req++; |
| 1906 | if (!(t->flags & SN_ERR_MASK)) |
| 1907 | t->flags |= SN_ERR_PRXCOND; |
| 1908 | if (!(t->flags & SN_FINST_MASK)) |
| 1909 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1910 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1911 | } |
| 1912 | /* otherwise accept */ |
| 1913 | break; |
| 1914 | } |
| 1915 | } |
| 1916 | |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1917 | /* if we get there, it means we have no rule which matches, or |
| 1918 | * we have an explicit accept, so we apply the default accept. |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1919 | */ |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1920 | req->analysers &= ~AN_REQ_INSPECT; |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 1921 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1922 | } |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1923 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1924 | if (req->analysers & AN_REQ_HTTP_HDR) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1925 | /* |
| 1926 | * Now parse the partial (or complete) lines. |
| 1927 | * We will check the request syntax, and also join multi-line |
| 1928 | * headers. An index of all the lines will be elaborated while |
| 1929 | * parsing. |
| 1930 | * |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1931 | * For the parsing, we use a 28 states FSM. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1932 | * |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1933 | * Here is the information we currently have : |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1934 | * req->data + req->som = beginning of request |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1935 | * req->data + req->eoh = end of processed headers / start of current one |
| 1936 | * req->data + req->eol = end of current header or line (LF or CRLF) |
| 1937 | * req->lr = first non-visited byte |
| 1938 | * req->r = end of data |
| 1939 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1940 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1941 | int cur_idx; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1942 | struct http_txn *txn = &t->txn; |
| 1943 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1944 | struct proxy *cur_proxy; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1945 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1946 | if (likely(req->lr < req->r)) |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1947 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1948 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1949 | /* 1: we might have to print this header in debug mode */ |
| 1950 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1951 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1952 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1953 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1954 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1955 | sol = req->data + msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1956 | eol = sol + msg->sl.rq.l; |
| 1957 | debug_hdr("clireq", t, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 1958 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1959 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 1960 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1961 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1962 | while (cur_idx) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1963 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1964 | debug_hdr("clihdr", t, sol, eol); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1965 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 1966 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1967 | } |
| 1968 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1969 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1970 | |
| 1971 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1972 | * Now we quickly check if we have found a full valid request. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1973 | * If not so, we check the FD and buffer states before leaving. |
| 1974 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1975 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 1976 | * requests are checked first. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1977 | * |
| 1978 | */ |
| 1979 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1980 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1981 | /* |
| 1982 | * First, let's catch bad requests. |
| 1983 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1984 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1985 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1986 | |
| 1987 | /* 1: Since we are in header mode, if there's no space |
| 1988 | * left for headers, we won't be able to free more |
| 1989 | * later, so the session will never terminate. We |
| 1990 | * must terminate it now. |
| 1991 | */ |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 1992 | if (unlikely(req->flags & BF_FULL)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1993 | /* FIXME: check if URI is set and return Status |
| 1994 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1995 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1996 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1997 | } |
| 1998 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 1999 | /* 2: have we encountered a read error ? */ |
| 2000 | else if (req->flags & BF_READ_ERROR) { |
| 2001 | /* we cannot return any message on error */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2002 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2003 | req->analysers = 0; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2004 | //t->fe->failed_req++; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2005 | if (!(t->flags & SN_ERR_MASK)) |
| 2006 | t->flags |= SN_ERR_CLICL; |
| 2007 | if (!(t->flags & SN_FINST_MASK)) |
| 2008 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2009 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2010 | } |
| 2011 | |
| 2012 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 2013 | 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] | 2014 | /* read timeout : give up with an error message. */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2015 | txn->status = 408; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2016 | client_retnclose(t, error_message(t, HTTP_ERR_408)); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2017 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2018 | req->analysers = 0; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 2019 | t->fe->failed_req++; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2020 | if (!(t->flags & SN_ERR_MASK)) |
| 2021 | t->flags |= SN_ERR_CLITO; |
| 2022 | if (!(t->flags & SN_FINST_MASK)) |
| 2023 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2024 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2025 | } |
| 2026 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2027 | /* 4: have we encountered a close ? */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 2028 | else if (req->flags & BF_SHUTR) { |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2029 | txn->status = 400; |
| 2030 | client_retnclose(t, error_message(t, HTTP_ERR_400)); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2031 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2032 | req->analysers = 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2033 | t->fe->failed_req++; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2034 | |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2035 | if (!(t->flags & SN_ERR_MASK)) |
| 2036 | t->flags |= SN_ERR_CLICL; |
| 2037 | if (!(t->flags & SN_FINST_MASK)) |
| 2038 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2039 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2040 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2041 | |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 2042 | buffer_write_dis(req); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2043 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 2044 | if (!tick_isset(req->analyse_exp)) |
| 2045 | req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2046 | |
| 2047 | /* we're not ready yet */ |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2048 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2049 | } |
| 2050 | |
| 2051 | |
| 2052 | /**************************************************************** |
| 2053 | * More interesting part now : we know that we have a complete * |
| 2054 | * request which at least looks like HTTP. We have an indicator * |
| 2055 | * of each header's length, so we can parse them quickly. * |
| 2056 | ****************************************************************/ |
| 2057 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2058 | req->analysers &= ~AN_REQ_HTTP_HDR; |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 2059 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 2060 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2061 | /* ensure we keep this pointer to the beginning of the message */ |
| 2062 | msg->sol = req->data + msg->som; |
| 2063 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2064 | /* |
| 2065 | * 1: identify the method |
| 2066 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2067 | 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] | 2068 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2069 | /* we can make use of server redirect on GET and HEAD */ |
| 2070 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2071 | t->flags |= SN_REDIRECTABLE; |
| 2072 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2073 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2074 | * 2: check if the URI matches the monitor_uri. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2075 | * We have to do this for every request which gets in, because |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2076 | * the monitor-uri is defined by the frontend. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2077 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2078 | if (unlikely((t->fe->monitor_uri_len != 0) && |
| 2079 | (t->fe->monitor_uri_len == msg->sl.rq.u_l) && |
| 2080 | !memcmp(&req->data[msg->sl.rq.u], |
| 2081 | t->fe->monitor_uri, |
| 2082 | t->fe->monitor_uri_len))) { |
| 2083 | /* |
| 2084 | * We have found the monitor URI |
| 2085 | */ |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2086 | struct acl_cond *cond; |
| 2087 | cur_proxy = t->fe; |
| 2088 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2089 | t->flags |= SN_MONITOR; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2090 | |
| 2091 | /* Check if we want to fail this monitor request or not */ |
| 2092 | list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) { |
| 2093 | 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] | 2094 | |
| 2095 | ret = acl_pass(ret); |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2096 | if (cond->pol == ACL_COND_UNLESS) |
| 2097 | ret = !ret; |
| 2098 | |
| 2099 | if (ret) { |
| 2100 | /* we fail this request, let's return 503 service unavail */ |
| 2101 | txn->status = 503; |
| 2102 | client_retnclose(t, error_message(t, HTTP_ERR_503)); |
| 2103 | goto return_prx_cond; |
| 2104 | } |
| 2105 | } |
| 2106 | |
| 2107 | /* nothing to fail, let's reply normaly */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2108 | txn->status = 200; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2109 | client_retnclose(t, &http_200_chunk); |
| 2110 | goto return_prx_cond; |
| 2111 | } |
| 2112 | |
| 2113 | /* |
| 2114 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2115 | * Note: we cannot log anymore if the request has been |
| 2116 | * classified as invalid. |
| 2117 | */ |
| 2118 | if (unlikely(t->logs.logwait & LW_REQ)) { |
| 2119 | /* we have a complete HTTP request that we must log */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 2120 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2121 | int urilen = msg->sl.rq.l; |
| 2122 | |
| 2123 | if (urilen >= REQURI_LEN) |
| 2124 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2125 | memcpy(txn->uri, &req->data[msg->som], urilen); |
| 2126 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2127 | |
| 2128 | if (!(t->logs.logwait &= ~LW_REQ)) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 2129 | http_sess_log(t); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2130 | } else { |
| 2131 | Alert("HTTP logging : out of memory.\n"); |
| 2132 | } |
| 2133 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2134 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2135 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2136 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 2137 | if (unlikely(msg->sl.rq.v_l == 0)) { |
| 2138 | int delta; |
| 2139 | char *cur_end; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2140 | msg->sol = req->data + msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2141 | cur_end = msg->sol + msg->sl.rq.l; |
| 2142 | delta = 0; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2143 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2144 | if (msg->sl.rq.u_l == 0) { |
| 2145 | /* if no URI was set, add "/" */ |
| 2146 | delta = buffer_replace2(req, cur_end, cur_end, " /", 2); |
| 2147 | cur_end += delta; |
| 2148 | msg->eoh += delta; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2149 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2150 | /* add HTTP version */ |
| 2151 | delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11); |
| 2152 | msg->eoh += delta; |
| 2153 | cur_end += delta; |
| 2154 | cur_end = (char *)http_parse_reqline(msg, req->data, |
| 2155 | HTTP_MSG_RQMETH, |
| 2156 | msg->sol, cur_end + 1, |
| 2157 | NULL, NULL); |
| 2158 | if (unlikely(!cur_end)) |
| 2159 | goto return_bad_req; |
| 2160 | |
| 2161 | /* we have a full HTTP/1.0 request now and we know that |
| 2162 | * we have either a CR or an LF at <ptr>. |
| 2163 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2164 | 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] | 2165 | } |
| 2166 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2167 | |
| 2168 | /* 5: we may need to capture headers */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2169 | if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap)) |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 2170 | capture_headers(req->data + msg->som, &txn->hdr_idx, |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2171 | txn->req.cap, t->fe->req_cap); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2172 | |
| 2173 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2174 | * 6: we will have to evaluate the filters. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2175 | * As opposed to version 1.2, now they will be evaluated in the |
| 2176 | * filters order and not in the header order. This means that |
| 2177 | * each filter has to be validated among all headers. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2178 | * |
| 2179 | * We can now check whether we want to switch to another |
| 2180 | * backend, in which case we will re-check the backend's |
| 2181 | * filters and various options. In order to support 3-level |
| 2182 | * switching, here's how we should proceed : |
| 2183 | * |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2184 | * a) run be. |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2185 | * if (switch) then switch ->be to the new backend. |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2186 | * b) run be if (be != fe). |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2187 | * There cannot be any switch from there, so ->be cannot be |
| 2188 | * changed anymore. |
| 2189 | * |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2190 | * => filters always apply to ->be, then ->be may change. |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2191 | * |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2192 | * The response path will be able to apply either ->be, or |
| 2193 | * ->be then ->fe filters in order to match the reverse of |
| 2194 | * the forward sequence. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2195 | */ |
| 2196 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2197 | do { |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2198 | struct acl_cond *cond; |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2199 | struct redirect_rule *rule; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2200 | struct proxy *rule_set = t->be; |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2201 | cur_proxy = t->be; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2202 | |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2203 | /* first check whether we have some ACLs set to redirect this request */ |
| 2204 | list_for_each_entry(rule, &cur_proxy->redirect_rules, list) { |
| 2205 | 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] | 2206 | |
| 2207 | ret = acl_pass(ret); |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2208 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2209 | ret = !ret; |
| 2210 | |
| 2211 | if (ret) { |
| 2212 | struct chunk rdr = { trash, 0 }; |
| 2213 | const char *msg_fmt; |
| 2214 | |
| 2215 | /* build redirect message */ |
| 2216 | switch(rule->code) { |
| 2217 | case 303: |
| 2218 | rdr.len = strlen(HTTP_303); |
| 2219 | msg_fmt = HTTP_303; |
| 2220 | break; |
| 2221 | case 301: |
| 2222 | rdr.len = strlen(HTTP_301); |
| 2223 | msg_fmt = HTTP_301; |
| 2224 | break; |
| 2225 | case 302: |
| 2226 | default: |
| 2227 | rdr.len = strlen(HTTP_302); |
| 2228 | msg_fmt = HTTP_302; |
| 2229 | break; |
| 2230 | } |
| 2231 | |
| 2232 | if (unlikely(rdr.len > sizeof(trash))) |
| 2233 | goto return_bad_req; |
| 2234 | memcpy(rdr.str, msg_fmt, rdr.len); |
| 2235 | |
| 2236 | switch(rule->type) { |
| 2237 | case REDIRECT_TYPE_PREFIX: { |
| 2238 | const char *path; |
| 2239 | int pathlen; |
| 2240 | |
| 2241 | path = http_get_path(txn); |
| 2242 | /* build message using path */ |
| 2243 | if (path) { |
| 2244 | pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path; |
| 2245 | } else { |
| 2246 | path = "/"; |
| 2247 | pathlen = 1; |
| 2248 | } |
| 2249 | |
| 2250 | if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4) |
| 2251 | goto return_bad_req; |
| 2252 | |
| 2253 | /* add prefix */ |
| 2254 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 2255 | rdr.len += rule->rdr_len; |
| 2256 | |
| 2257 | /* add path */ |
| 2258 | memcpy(rdr.str + rdr.len, path, pathlen); |
| 2259 | rdr.len += pathlen; |
| 2260 | break; |
| 2261 | } |
| 2262 | case REDIRECT_TYPE_LOCATION: |
| 2263 | default: |
| 2264 | if (rdr.len + rule->rdr_len > sizeof(trash) - 4) |
| 2265 | goto return_bad_req; |
| 2266 | |
| 2267 | /* add location */ |
| 2268 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 2269 | rdr.len += rule->rdr_len; |
| 2270 | break; |
| 2271 | } |
| 2272 | |
| 2273 | /* add end of headers */ |
| 2274 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 2275 | rdr.len += 4; |
| 2276 | |
| 2277 | txn->status = rule->code; |
| 2278 | /* let's log the request time */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2279 | t->logs.tv_request = now; |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2280 | client_retnclose(t, &rdr); |
| 2281 | goto return_prx_cond; |
| 2282 | } |
| 2283 | } |
| 2284 | |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2285 | /* first check whether we have some ACLs set to block this request */ |
| 2286 | list_for_each_entry(cond, &cur_proxy->block_cond, list) { |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 2287 | 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] | 2288 | |
| 2289 | ret = acl_pass(ret); |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2290 | if (cond->pol == ACL_COND_UNLESS) |
| 2291 | ret = !ret; |
| 2292 | |
| 2293 | if (ret) { |
| 2294 | txn->status = 403; |
| 2295 | /* let's log the request time */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2296 | t->logs.tv_request = now; |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2297 | client_retnclose(t, error_message(t, HTTP_ERR_403)); |
| 2298 | goto return_prx_cond; |
| 2299 | } |
| 2300 | } |
| 2301 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2302 | /* try headers filters */ |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2303 | if (rule_set->req_exp != NULL) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2304 | if (apply_filters_to_request(t, req, rule_set->req_exp) < 0) |
| 2305 | goto return_bad_req; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2306 | } |
| 2307 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2308 | if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) { |
| 2309 | /* to ensure correct connection accounting on |
| 2310 | * the backend, we count the connection for the |
| 2311 | * one managing the queue. |
| 2312 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2313 | t->be->beconn++; |
| 2314 | if (t->be->beconn > t->be->beconn_max) |
| 2315 | t->be->beconn_max = t->be->beconn; |
| 2316 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2317 | t->flags |= SN_BE_ASSIGNED; |
| 2318 | } |
| 2319 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2320 | /* has the request been denied ? */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2321 | if (txn->flags & TX_CLDENY) { |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2322 | /* no need to go further */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2323 | txn->status = 403; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2324 | /* let's log the request time */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2325 | t->logs.tv_request = now; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2326 | client_retnclose(t, error_message(t, HTTP_ERR_403)); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2327 | goto return_prx_cond; |
| 2328 | } |
| 2329 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2330 | /* We might have to check for "Connection:" */ |
Krzysztof Oledzki | 336d475 | 2007-12-25 02:40:22 +0100 | [diff] [blame] | 2331 | 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] | 2332 | !(t->flags & SN_CONN_CLOSED)) { |
| 2333 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2334 | int cur_idx, old_idx, delta, val; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2335 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2336 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2337 | 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] | 2338 | old_idx = 0; |
| 2339 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2340 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 2341 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2342 | cur_ptr = cur_next; |
| 2343 | cur_end = cur_ptr + cur_hdr->len; |
| 2344 | cur_next = cur_end + cur_hdr->cr + 1; |
| 2345 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2346 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 2347 | if (val) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2348 | /* 3 possibilities : |
| 2349 | * - we have already set Connection: close, |
| 2350 | * so we remove this line. |
| 2351 | * - we have not yet set Connection: close, |
| 2352 | * but this line indicates close. We leave |
| 2353 | * it untouched and set the flag. |
| 2354 | * - we have not yet set Connection: close, |
| 2355 | * and this line indicates non-close. We |
| 2356 | * replace it. |
| 2357 | */ |
| 2358 | if (t->flags & SN_CONN_CLOSED) { |
| 2359 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2360 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2361 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2362 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 2363 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2364 | cur_hdr->len = 0; |
| 2365 | } else { |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2366 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 2367 | delta = buffer_replace2(req, cur_ptr + val, cur_end, |
| 2368 | "close", 5); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2369 | cur_next += delta; |
| 2370 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2371 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2372 | } |
| 2373 | t->flags |= SN_CONN_CLOSED; |
| 2374 | } |
| 2375 | } |
| 2376 | old_idx = cur_idx; |
| 2377 | } |
Willy Tarreau | f2f0ee8 | 2007-03-30 12:02:43 +0200 | [diff] [blame] | 2378 | } |
| 2379 | /* add request headers from the rule sets in the same order */ |
| 2380 | for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) { |
| 2381 | if (unlikely(http_header_add_tail(req, |
| 2382 | &txn->req, |
| 2383 | &txn->hdr_idx, |
| 2384 | rule_set->req_add[cur_idx])) < 0) |
| 2385 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2386 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2387 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2388 | /* check if stats URI was requested, and if an auth is needed */ |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 2389 | if (rule_set->uri_auth != NULL && |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2390 | (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) { |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2391 | /* we have to check the URI and auth for this request. |
| 2392 | * FIXME!!! that one is rather dangerous, we want to |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2393 | * make it follow standard rules (eg: clear req->analysers). |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2394 | */ |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2395 | if (stats_check_uri_auth(t, rule_set)) |
| 2396 | return 1; |
| 2397 | } |
| 2398 | |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2399 | /* now check whether we have some switching rules for this request */ |
| 2400 | if (!(t->flags & SN_BE_ASSIGNED)) { |
| 2401 | struct switching_rule *rule; |
| 2402 | |
| 2403 | list_for_each_entry(rule, &cur_proxy->switching_rules, list) { |
| 2404 | int ret; |
| 2405 | |
| 2406 | 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] | 2407 | |
| 2408 | ret = acl_pass(ret); |
Willy Tarreau | a8cfa34 | 2008-07-09 11:23:31 +0200 | [diff] [blame] | 2409 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2410 | ret = !ret; |
| 2411 | |
| 2412 | if (ret) { |
| 2413 | t->be = rule->be.backend; |
| 2414 | t->be->beconn++; |
| 2415 | if (t->be->beconn > t->be->beconn_max) |
| 2416 | t->be->beconn_max = t->be->beconn; |
| 2417 | t->be->cum_beconn++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2418 | |
| 2419 | /* assign new parameters to the session from the new backend */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2420 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 2421 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2422 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2423 | t->flags |= SN_BE_ASSIGNED; |
| 2424 | break; |
| 2425 | } |
| 2426 | } |
| 2427 | } |
| 2428 | |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 2429 | if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) { |
| 2430 | /* No backend was set, but there was a default |
| 2431 | * backend set in the frontend, so we use it and |
| 2432 | * loop again. |
| 2433 | */ |
| 2434 | t->be = cur_proxy->defbe.be; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2435 | t->be->beconn++; |
| 2436 | if (t->be->beconn > t->be->beconn_max) |
| 2437 | t->be->beconn_max = t->be->beconn; |
| 2438 | t->be->cum_beconn++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2439 | |
| 2440 | /* assign new parameters to the session from the new backend */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2441 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 2442 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2443 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 2444 | t->flags |= SN_BE_ASSIGNED; |
| 2445 | } |
| 2446 | } 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] | 2447 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2448 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2449 | if (!(t->flags & SN_BE_ASSIGNED)) { |
| 2450 | /* To ensure correct connection accounting on |
| 2451 | * the backend, we count the connection for the |
| 2452 | * one managing the queue. |
| 2453 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2454 | t->be->beconn++; |
| 2455 | if (t->be->beconn > t->be->beconn_max) |
| 2456 | t->be->beconn_max = t->be->beconn; |
| 2457 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2458 | t->flags |= SN_BE_ASSIGNED; |
| 2459 | } |
| 2460 | |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2461 | /* |
| 2462 | * Right now, we know that we have processed the entire headers |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2463 | * and that unwanted requests have been filtered out. We can do |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2464 | * whatever we want with the remaining request. Also, now we |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2465 | * may have separate values for ->fe, ->be. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2466 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2467 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 2468 | /* |
| 2469 | * If HTTP PROXY is set we simply get remote server address |
| 2470 | * parsing incoming request. |
| 2471 | */ |
| 2472 | if ((t->be->options & PR_O_HTTP_PROXY) && !(t->flags & SN_ADDR_SET)) { |
| 2473 | url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &t->srv_addr); |
| 2474 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2475 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2476 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2477 | * 7: the appsession cookie was looked up very early in 1.2, |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2478 | * so let's do the same now. |
| 2479 | */ |
| 2480 | |
| 2481 | /* It needs to look into the URI */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2482 | if (t->be->appsession_name) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2483 | 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] | 2484 | } |
| 2485 | |
| 2486 | |
| 2487 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2488 | * 8: Now we can work with the cookies. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2489 | * Note that doing so might move headers in the request, but |
| 2490 | * the fields will stay coherent and the URI will not move. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2491 | * This should only be performed in the backend. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2492 | */ |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 2493 | if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name) |
| 2494 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2495 | manage_client_side_cookies(t, req); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2496 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2497 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2498 | /* |
Willy Tarreau | bb046ac | 2007-03-03 19:17:03 +0100 | [diff] [blame] | 2499 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 2500 | * asks for it. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2501 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2502 | if ((t->fe->options | t->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2503 | if (t->cli_addr.ss_family == AF_INET) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2504 | /* Add an X-Forwarded-For header unless the source IP is |
| 2505 | * in the 'except' network range. |
| 2506 | */ |
| 2507 | if ((!t->fe->except_mask.s_addr || |
| 2508 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr) |
| 2509 | != t->fe->except_net.s_addr) && |
| 2510 | (!t->be->except_mask.s_addr || |
| 2511 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr) |
| 2512 | != t->be->except_net.s_addr)) { |
| 2513 | int len; |
| 2514 | unsigned char *pn; |
| 2515 | pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr; |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2516 | |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2517 | /* Note: we rely on the backend to get the header name to be used for |
| 2518 | * x-forwarded-for, because the header is really meant for the backends. |
| 2519 | * However, if the backend did not specify any option, we have to rely |
| 2520 | * on the frontend's header name. |
| 2521 | */ |
| 2522 | if (t->be->fwdfor_hdr_len) { |
| 2523 | len = t->be->fwdfor_hdr_len; |
| 2524 | memcpy(trash, t->be->fwdfor_hdr_name, len); |
| 2525 | } else { |
| 2526 | len = t->fe->fwdfor_hdr_len; |
| 2527 | memcpy(trash, t->fe->fwdfor_hdr_name, len); |
| 2528 | } |
| 2529 | 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] | 2530 | |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2531 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2532 | &txn->hdr_idx, trash, len)) < 0) |
| 2533 | goto return_bad_req; |
| 2534 | } |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2535 | } |
| 2536 | else if (t->cli_addr.ss_family == AF_INET6) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2537 | /* FIXME: for the sake of completeness, we should also support |
| 2538 | * 'except' here, although it is mostly useless in this case. |
| 2539 | */ |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2540 | int len; |
| 2541 | char pn[INET6_ADDRSTRLEN]; |
| 2542 | inet_ntop(AF_INET6, |
| 2543 | (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr, |
| 2544 | pn, sizeof(pn)); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2545 | |
| 2546 | /* Note: we rely on the backend to get the header name to be used for |
| 2547 | * x-forwarded-for, because the header is really meant for the backends. |
| 2548 | * However, if the backend did not specify any option, we have to rely |
| 2549 | * on the frontend's header name. |
| 2550 | */ |
| 2551 | if (t->be->fwdfor_hdr_len) { |
| 2552 | len = t->be->fwdfor_hdr_len; |
| 2553 | memcpy(trash, t->be->fwdfor_hdr_name, len); |
| 2554 | } else { |
| 2555 | len = t->fe->fwdfor_hdr_len; |
| 2556 | memcpy(trash, t->fe->fwdfor_hdr_name, len); |
| 2557 | } |
| 2558 | len += sprintf(trash + len, ": %s", pn); |
| 2559 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2560 | if (unlikely(http_header_add_tail2(req, &txn->req, |
| 2561 | &txn->hdr_idx, trash, len)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2562 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2563 | } |
| 2564 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2565 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2566 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2567 | * 10: add "Connection: close" if needed and not yet set. |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2568 | * 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] | 2569 | */ |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2570 | if (!(t->flags & SN_CONN_CLOSED) && |
Krzysztof Oledzki | 336d475 | 2007-12-25 02:40:22 +0100 | [diff] [blame] | 2571 | ((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] | 2572 | if ((unlikely(msg->sl.rq.v_l != 8) || |
| 2573 | unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) && |
| 2574 | unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx, |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2575 | "Connection: close", 17)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2576 | goto return_bad_req; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2577 | t->flags |= SN_CONN_CLOSED; |
Willy Tarreau | e15d913 | 2006-12-14 22:26:42 +0100 | [diff] [blame] | 2578 | } |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2579 | /* Before we switch to data, was assignment set in manage_client_side_cookie? |
| 2580 | * If not assigned, perhaps we are balancing on url_param, but this is a |
| 2581 | * POST; and the parameters are in the body, maybe scan there to find our server. |
| 2582 | * (unless headers overflowed the buffer?) |
| 2583 | */ |
| 2584 | if (!(t->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 2585 | t->txn.meth == HTTP_METH_POST && t->be->url_param_name != NULL && |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2586 | 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] | 2587 | memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) { |
| 2588 | /* are there enough bytes here? total == l || r || rlim ? |
| 2589 | * len is unsigned, but eoh is int, |
| 2590 | * how many bytes of body have we received? |
| 2591 | * eoh is the first empty line of the header |
| 2592 | */ |
| 2593 | /* 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] | 2594 | 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] | 2595 | |
| 2596 | /* If we have HTTP/1.1 and Expect: 100-continue, then abort. |
| 2597 | * We can't assume responsibility for the server's decision, |
| 2598 | * on this URI and header set. See rfc2616: 14.20, 8.2.3, |
| 2599 | * We also can't change our mind later, about which server to choose, so round robin. |
| 2600 | */ |
| 2601 | if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) { |
| 2602 | struct hdr_ctx ctx; |
| 2603 | ctx.idx = 0; |
| 2604 | /* Expect is allowed in 1.1, look for it */ |
| 2605 | http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx); |
| 2606 | if (ctx.idx != 0 && |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2607 | 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] | 2608 | /* We can't reliablly stall and wait for data, because of |
| 2609 | * .NET clients that don't conform to rfc2616; so, no need for |
| 2610 | * the next block to check length expectations. |
| 2611 | * We could send 100 status back to the client, but then we need to |
| 2612 | * re-write headers, and send the message. And this isn't the right |
| 2613 | * place for that action. |
| 2614 | * TODO: support Expect elsewhere and delete this block. |
| 2615 | */ |
| 2616 | goto end_check_maybe_wait_for_body; |
| 2617 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2618 | |
| 2619 | if (likely(len > t->be->url_param_post_limit)) { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2620 | /* nothing to do, we got enough */ |
| 2621 | } else { |
| 2622 | /* limit implies we are supposed to need this many bytes |
| 2623 | * to find the parameter. Let's see how many bytes we can wait for. |
| 2624 | */ |
| 2625 | long long hint = len; |
| 2626 | struct hdr_ctx ctx; |
| 2627 | ctx.idx = 0; |
| 2628 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx); |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2629 | 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] | 2630 | buffer_write_dis(req); |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2631 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2632 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2633 | else { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2634 | ctx.idx = 0; |
| 2635 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx); |
| 2636 | /* now if we have a length, we'll take the hint */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2637 | if (ctx.idx) { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2638 | /* We have Content-Length */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2639 | if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint)) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2640 | hint = 0; /* parse failure, untrusted client */ |
| 2641 | else { |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2642 | if (hint > 0) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2643 | msg->hdr_content_len = hint; |
| 2644 | else |
| 2645 | hint = 0; /* bad client, sent negative length */ |
| 2646 | } |
| 2647 | } |
| 2648 | /* 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] | 2649 | if (t->be->url_param_post_limit < hint) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2650 | hint = t->be->url_param_post_limit; |
| 2651 | /* now do we really need to buffer more data? */ |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2652 | if (len < hint) { |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 2653 | buffer_write_dis(req); |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2654 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2655 | } |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2656 | /* else... There are no body bytes to wait for */ |
| 2657 | } |
| 2658 | } |
| 2659 | } |
| 2660 | end_check_maybe_wait_for_body: |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2661 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2662 | /************************************************************* |
| 2663 | * OK, that's finished for the headers. We have done what we * |
| 2664 | * could. Let's switch to the DATA state. * |
| 2665 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2666 | |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2667 | buffer_set_rlim(req, BUFSIZE); /* no more rewrite needed */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2668 | t->logs.tv_request = now; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2669 | |
Willy Tarreau | 1fa3126 | 2007-12-03 00:36:16 +0100 | [diff] [blame] | 2670 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 2671 | * which may be the same as the connect timeout if unspecified. |
| 2672 | * 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] | 2673 | * eventually expire. We build the tarpit as an analyser. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2674 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2675 | if (txn->flags & TX_CLTARPIT) { |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2676 | buffer_flush(t->req); |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2677 | /* flush the request so that we can drop the connection early |
| 2678 | * if the client closes first. |
| 2679 | */ |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 2680 | buffer_write_dis(req); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2681 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 2682 | req->analyse_exp = tick_add_ifset(now_ms, t->be->timeout.tarpit); |
| 2683 | if (!req->analyse_exp) |
| 2684 | req->analyse_exp = now_ms; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2685 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2686 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2687 | /* OK let's go on with the BODY now */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2688 | goto end_of_headers; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2689 | |
| 2690 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2691 | txn->req.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2692 | txn->status = 400; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2693 | req->analysers = 0; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2694 | client_retnclose(t, error_message(t, HTTP_ERR_400)); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 2695 | t->fe->failed_req++; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2696 | return_prx_cond: |
| 2697 | if (!(t->flags & SN_ERR_MASK)) |
| 2698 | t->flags |= SN_ERR_PRXCOND; |
| 2699 | if (!(t->flags & SN_FINST_MASK)) |
| 2700 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2701 | return 0; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2702 | end_of_headers: |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2703 | ; // to keep gcc happy |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2704 | } |
| 2705 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2706 | if (req->analysers & AN_REQ_HTTP_TARPIT) { |
| 2707 | struct http_txn *txn = &t->txn; |
| 2708 | |
| 2709 | /* This connection is being tarpitted. The CLIENT side has |
| 2710 | * already set the connect expiration date to the right |
| 2711 | * timeout. We just have to check that the client is still |
| 2712 | * there and that the timeout has not expired. |
| 2713 | */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 2714 | if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 && |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2715 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 2716 | return 0; |
| 2717 | |
| 2718 | /* We will set the queue timer to the time spent, just for |
| 2719 | * logging purposes. We fake a 500 server error, so that the |
| 2720 | * attacker will not suspect his connection has been tarpitted. |
| 2721 | * It will not cause trouble to the logs because we can exclude |
| 2722 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 2723 | */ |
| 2724 | trace_term(t, TT_HTTP_SRV_2); |
| 2725 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 2726 | |
| 2727 | txn->status = 500; |
| 2728 | if (req->flags != BF_READ_ERROR) |
| 2729 | client_retnclose(t, error_message(t, HTTP_ERR_500)); |
| 2730 | |
| 2731 | req->analysers = 0; |
| 2732 | req->analyse_exp = TICK_ETERNITY; |
| 2733 | |
| 2734 | t->fe->failed_req++; |
| 2735 | if (!(t->flags & SN_ERR_MASK)) |
| 2736 | t->flags |= SN_ERR_PRXCOND; |
| 2737 | if (!(t->flags & SN_FINST_MASK)) |
| 2738 | t->flags |= SN_FINST_T; |
| 2739 | return 0; |
| 2740 | } |
| 2741 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2742 | if (req->analysers & AN_REQ_HTTP_BODY) { |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2743 | /* We have to parse the HTTP request body to find any required data. |
| 2744 | * "balance url_param check_post" should have been the only way to get |
| 2745 | * into this. We were brought here after HTTP header analysis, so all |
| 2746 | * related structures are ready. |
| 2747 | */ |
| 2748 | struct http_msg *msg = &t->txn.req; |
| 2749 | unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1; |
| 2750 | long long limit = t->be->url_param_post_limit; |
| 2751 | struct hdr_ctx ctx; |
| 2752 | |
| 2753 | ctx.idx = 0; |
| 2754 | |
| 2755 | /* now if we have a length, we'll take the hint */ |
| 2756 | http_find_header2("Transfer-Encoding", 17, msg->sol, &t->txn.hdr_idx, &ctx); |
| 2757 | if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) { |
| 2758 | unsigned int chunk = 0; |
| 2759 | while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) { |
| 2760 | char c = msg->sol[body]; |
| 2761 | if (ishex(c)) { |
| 2762 | unsigned int hex = toupper(c) - '0'; |
| 2763 | if (hex > 9) |
| 2764 | hex -= 'A' - '9' - 1; |
| 2765 | chunk = (chunk << 4) | hex; |
| 2766 | } else |
| 2767 | break; |
| 2768 | body++; |
| 2769 | } |
| 2770 | if (body + 2 >= req->l) /* we want CRLF too */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2771 | goto http_body_end; /* end of buffer? data missing! */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2772 | |
| 2773 | if (memcmp(msg->sol+body, "\r\n", 2) != 0) |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2774 | 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] | 2775 | |
| 2776 | body += 2; // skip CRLF |
| 2777 | |
| 2778 | /* if we support more then one chunk here, we have to do it again when assigning server |
| 2779 | * 1. how much entity data do we have? new var |
| 2780 | * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here |
| 2781 | * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found) |
| 2782 | */ |
| 2783 | |
| 2784 | if (chunk < limit) |
| 2785 | limit = chunk; /* only reading one chunk */ |
| 2786 | } else { |
| 2787 | if (msg->hdr_content_len < limit) |
| 2788 | limit = msg->hdr_content_len; |
| 2789 | } |
| 2790 | |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2791 | http_body_end: |
| 2792 | /* we leave once we know we have nothing left to do. This means that we have |
| 2793 | * enough bytes, or that we know we'll not get any more (buffer full, read |
| 2794 | * buffer closed). |
| 2795 | */ |
| 2796 | if (req->l - body >= limit || /* enough bytes! */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 2797 | req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR | BF_READ_TIMEOUT) || |
Willy Tarreau | c52164a | 2008-08-17 19:17:57 +0200 | [diff] [blame] | 2798 | tick_is_expired(req->analyse_exp, now_ms)) { |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2799 | /* 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] | 2800 | 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] | 2801 | req->analysers &= ~AN_REQ_HTTP_BODY; |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 2802 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2803 | } |
Willy Tarreau | c52164a | 2008-08-17 19:17:57 +0200 | [diff] [blame] | 2804 | else { |
| 2805 | /* Not enough data. We'll re-use the http-request |
| 2806 | * timeout here. Ideally, we should set the timeout |
| 2807 | * relative to the accept() date. We just set the |
| 2808 | * request timeout once at the beginning of the |
| 2809 | * request. |
| 2810 | */ |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 2811 | buffer_write_dis(req); |
Willy Tarreau | c52164a | 2008-08-17 19:17:57 +0200 | [diff] [blame] | 2812 | if (!tick_isset(req->analyse_exp)) |
| 2813 | req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq); |
| 2814 | return 0; |
| 2815 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2816 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2817 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2818 | /* Note: eventhough nobody should set an unknown flag, clearing them right now will |
| 2819 | * probably reduce one day's debugging session. |
| 2820 | */ |
| 2821 | #ifdef DEBUG_DEV |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2822 | 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] | 2823 | fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n", |
| 2824 | __FILE__, __LINE__, req->analysers); |
| 2825 | ABORT_NOW(); |
| 2826 | } |
| 2827 | #endif |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2828 | 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] | 2829 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2830 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2831 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2832 | /* This function performs all the processing enabled for the current response. |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2833 | * It normally returns zero, but may return 1 if it absolutely needs to be |
| 2834 | * called again after other functions. It relies on buffers flags, and updates |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2835 | * 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] | 2836 | * functions. It works like process_request (see indications above). |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2837 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2838 | int process_response(struct session *t) |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2839 | { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2840 | struct http_txn *txn = &t->txn; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2841 | struct buffer *req = t->req; |
| 2842 | struct buffer *rep = t->rep; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2843 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 2844 | 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] | 2845 | now_ms, __FUNCTION__, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 2846 | t, |
| 2847 | rep, |
| 2848 | rep->rex, rep->wex, |
| 2849 | rep->flags, |
| 2850 | rep->l, |
| 2851 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 2852 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2853 | if (rep->analysers & AN_RTR_HTTP_HDR) { /* receiving server headers */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2854 | /* |
| 2855 | * Now parse the partial (or complete) lines. |
| 2856 | * We will check the response syntax, and also join multi-line |
| 2857 | * headers. An index of all the lines will be elaborated while |
| 2858 | * parsing. |
| 2859 | * |
| 2860 | * For the parsing, we use a 28 states FSM. |
| 2861 | * |
| 2862 | * Here is the information we currently have : |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2863 | * rep->data + rep->som = beginning of response |
| 2864 | * rep->data + rep->eoh = end of processed headers / start of current one |
| 2865 | * 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] | 2866 | * rep->lr = first non-visited byte |
| 2867 | * rep->r = end of data |
| 2868 | */ |
Willy Tarreau | 7f875f6 | 2008-08-11 17:35:01 +0200 | [diff] [blame] | 2869 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2870 | int cur_idx; |
| 2871 | struct http_msg *msg = &txn->rsp; |
| 2872 | struct proxy *cur_proxy; |
| 2873 | |
| 2874 | if (likely(rep->lr < rep->r)) |
| 2875 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
| 2876 | |
| 2877 | /* 1: we might have to print this header in debug mode */ |
| 2878 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2879 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
| 2880 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
| 2881 | char *eol, *sol; |
| 2882 | |
| 2883 | sol = rep->data + msg->som; |
| 2884 | eol = sol + msg->sl.rq.l; |
| 2885 | debug_hdr("srvrep", t, sol, eol); |
| 2886 | |
| 2887 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2888 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
| 2889 | |
| 2890 | while (cur_idx) { |
| 2891 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2892 | debug_hdr("srvhdr", t, sol, eol); |
| 2893 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2894 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2895 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2896 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2897 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2898 | /* |
| 2899 | * Now we quickly check if we have found a full valid response. |
| 2900 | * If not so, we check the FD and buffer states before leaving. |
| 2901 | * A full response is indicated by the fact that we have seen |
| 2902 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 2903 | * responses are checked first. |
| 2904 | * |
| 2905 | * Depending on whether the client is still there or not, we |
| 2906 | * may send an error response back or not. Note that normally |
| 2907 | * we should only check for HTTP status there, and check I/O |
| 2908 | * errors somewhere else. |
| 2909 | */ |
| 2910 | |
| 2911 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2912 | /* Invalid response */ |
| 2913 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 2914 | hdr_response_bad: |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2915 | //buffer_shutr(rep); |
| 2916 | //buffer_shutw(req); |
| 2917 | //fd_delete(req->cons->fd); |
| 2918 | //req->cons->state = SI_ST_CLO; |
| 2919 | buffer_shutr_now(rep); |
| 2920 | buffer_shutw_now(req); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2921 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2922 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2923 | t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2924 | //sess_change_server(t, NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2925 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2926 | t->be->failed_resp++; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2927 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2928 | txn->status = 502; |
| 2929 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 2930 | if (!(t->flags & SN_ERR_MASK)) |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2931 | t->flags |= SN_ERR_PRXCOND; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2932 | if (!(t->flags & SN_FINST_MASK)) |
| 2933 | t->flags |= SN_FINST_H; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 2934 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2935 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 2936 | // process_srv_queue(t->srv); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2937 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2938 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2939 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2940 | /* too large response does not fit in buffer. */ |
| 2941 | else if (rep->flags & BF_FULL) { |
| 2942 | goto hdr_response_bad; |
| 2943 | } |
| 2944 | /* read error */ |
| 2945 | else if (rep->flags & BF_READ_ERROR) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2946 | buffer_shutr_now(rep); |
| 2947 | buffer_shutw_now(req); |
| 2948 | //fd_delete(req->cons->fd); |
| 2949 | //req->cons->state = SI_ST_CLO; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2950 | //if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2951 | //t->srv->cur_sess--; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2952 | //t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2953 | //sess_change_server(t, NULL); |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2954 | //} |
| 2955 | //t->be->failed_resp++; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2956 | rep->analysers = 0; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2957 | txn->status = 502; |
| 2958 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 2959 | if (!(t->flags & SN_ERR_MASK)) |
| 2960 | t->flags |= SN_ERR_SRVCL; |
| 2961 | if (!(t->flags & SN_FINST_MASK)) |
| 2962 | t->flags |= SN_FINST_H; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 2963 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2964 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 2965 | // process_srv_queue(t->srv); |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2966 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2967 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2968 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2969 | /* read timeout : return a 504 to the client. */ |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2970 | else if (rep->flags & BF_READ_TIMEOUT) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2971 | buffer_shutr_now(rep); |
| 2972 | buffer_shutw_now(req); |
| 2973 | //fd_delete(req->cons->fd); |
| 2974 | //req->cons->state = SI_ST_CLO; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2975 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2976 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2977 | t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2978 | //sess_change_server(t, NULL); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2979 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2980 | t->be->failed_resp++; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2981 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2982 | txn->status = 504; |
| 2983 | client_return(t, error_message(t, HTTP_ERR_504)); |
| 2984 | if (!(t->flags & SN_ERR_MASK)) |
| 2985 | t->flags |= SN_ERR_SRVTO; |
| 2986 | if (!(t->flags & SN_FINST_MASK)) |
| 2987 | t->flags |= SN_FINST_H; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 2988 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2989 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 2990 | // process_srv_queue(t->srv); |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2991 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2992 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2993 | /* write error to client, or close from server */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 2994 | else if (rep->flags & (BF_WRITE_ERROR|BF_SHUTR)) { |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2995 | buffer_shutr_now(rep); |
| 2996 | buffer_shutw_now(req); |
| 2997 | //fd_delete(req->cons->fd); |
| 2998 | //req->cons->state = SI_ST_CLO; |
| 2999 | if (t->srv) { |
| 3000 | //t->srv->cur_sess--; |
| 3001 | t->srv->failed_resp++; |
| 3002 | //sess_change_server(t, NULL); |
| 3003 | } |
| 3004 | t->be->failed_resp++; |
| 3005 | rep->analysers = 0; |
| 3006 | txn->status = 502; |
| 3007 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 3008 | if (!(t->flags & SN_ERR_MASK)) |
| 3009 | t->flags |= SN_ERR_SRVCL; |
| 3010 | if (!(t->flags & SN_FINST_MASK)) |
| 3011 | t->flags |= SN_FINST_H; |
| 3012 | |
| 3013 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 3014 | // process_srv_queue(t->srv); |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 3015 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 3016 | return 0; |
| 3017 | } |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 3018 | buffer_write_dis(rep); |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 3019 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3020 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3021 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3022 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3023 | /***************************************************************** |
| 3024 | * More interesting part now : we know that we have a complete * |
| 3025 | * response which at least looks like HTTP. We have an indicator * |
| 3026 | * of each header's length, so we can parse them quickly. * |
| 3027 | ****************************************************************/ |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3028 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3029 | rep->analysers &= ~AN_RTR_HTTP_HDR; |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 3030 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3031 | /* ensure we keep this pointer to the beginning of the message */ |
| 3032 | msg->sol = rep->data + msg->som; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3033 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3034 | /* |
| 3035 | * 1: get the status code and check for cacheability. |
| 3036 | */ |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3037 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3038 | t->logs.logwait &= ~LW_RESP; |
| 3039 | 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] | 3040 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3041 | switch (txn->status) { |
| 3042 | case 200: |
| 3043 | case 203: |
| 3044 | case 206: |
| 3045 | case 300: |
| 3046 | case 301: |
| 3047 | case 410: |
| 3048 | /* RFC2616 @13.4: |
| 3049 | * "A response received with a status code of |
| 3050 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 3051 | * by a cache (...) unless a cache-control |
| 3052 | * directive prohibits caching." |
| 3053 | * |
| 3054 | * RFC2616 @9.5: POST method : |
| 3055 | * "Responses to this method are not cacheable, |
| 3056 | * unless the response includes appropriate |
| 3057 | * Cache-Control or Expires header fields." |
| 3058 | */ |
| 3059 | if (likely(txn->meth != HTTP_METH_POST) && |
| 3060 | (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
| 3061 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 3062 | break; |
| 3063 | default: |
| 3064 | break; |
| 3065 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3066 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3067 | /* |
| 3068 | * 2: we may need to capture headers |
| 3069 | */ |
| 3070 | if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap)) |
| 3071 | capture_headers(rep->data + msg->som, &txn->hdr_idx, |
| 3072 | txn->rsp.cap, t->fe->rsp_cap); |
| 3073 | |
| 3074 | /* |
| 3075 | * 3: we will have to evaluate the filters. |
| 3076 | * As opposed to version 1.2, now they will be evaluated in the |
| 3077 | * filters order and not in the header order. This means that |
| 3078 | * each filter has to be validated among all headers. |
| 3079 | * |
| 3080 | * Filters are tried with ->be first, then with ->fe if it is |
| 3081 | * different from ->be. |
| 3082 | */ |
| 3083 | |
| 3084 | t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */ |
| 3085 | |
| 3086 | cur_proxy = t->be; |
| 3087 | while (1) { |
| 3088 | struct proxy *rule_set = cur_proxy; |
| 3089 | |
| 3090 | /* try headers filters */ |
| 3091 | if (rule_set->rsp_exp != NULL) { |
| 3092 | if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) { |
| 3093 | return_bad_resp: |
| 3094 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3095 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3096 | t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3097 | //sess_change_server(t, NULL); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3098 | } |
| 3099 | cur_proxy->failed_resp++; |
| 3100 | return_srv_prx_502: |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3101 | buffer_shutr_now(rep); |
| 3102 | buffer_shutw_now(req); |
| 3103 | //fd_delete(req->cons->fd); |
| 3104 | //req->cons->state = SI_ST_CLO; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3105 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3106 | txn->status = 502; |
| 3107 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 3108 | if (!(t->flags & SN_ERR_MASK)) |
| 3109 | t->flags |= SN_ERR_PRXCOND; |
| 3110 | if (!(t->flags & SN_FINST_MASK)) |
| 3111 | t->flags |= SN_FINST_H; |
| 3112 | /* We used to have a free connection slot. Since we'll never use it, |
| 3113 | * we have to inform the server that it may be used by another session. |
| 3114 | */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3115 | //if (t->srv && may_dequeue_tasks(t->srv, cur_proxy)) |
| 3116 | // process_srv_queue(t->srv); |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3117 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3118 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3119 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3120 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3121 | /* has the response been denied ? */ |
| 3122 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | f899b94 | 2008-03-28 18:09:38 +0100 | [diff] [blame] | 3123 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3124 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3125 | t->srv->failed_secu++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3126 | //sess_change_server(t, NULL); |
Willy Tarreau | f899b94 | 2008-03-28 18:09:38 +0100 | [diff] [blame] | 3127 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3128 | cur_proxy->denied_resp++; |
| 3129 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 3130 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3131 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3132 | /* We might have to check for "Connection:" */ |
| 3133 | if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) && |
| 3134 | !(t->flags & SN_CONN_CLOSED)) { |
| 3135 | char *cur_ptr, *cur_end, *cur_next; |
| 3136 | int cur_idx, old_idx, delta, val; |
| 3137 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3138 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3139 | cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 3140 | old_idx = 0; |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 3141 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3142 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 3143 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 3144 | cur_ptr = cur_next; |
| 3145 | cur_end = cur_ptr + cur_hdr->len; |
| 3146 | cur_next = cur_end + cur_hdr->cr + 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3147 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3148 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 3149 | if (val) { |
| 3150 | /* 3 possibilities : |
| 3151 | * - we have already set Connection: close, |
| 3152 | * so we remove this line. |
| 3153 | * - we have not yet set Connection: close, |
| 3154 | * but this line indicates close. We leave |
| 3155 | * it untouched and set the flag. |
| 3156 | * - we have not yet set Connection: close, |
| 3157 | * and this line indicates non-close. We |
| 3158 | * replace it. |
| 3159 | */ |
| 3160 | if (t->flags & SN_CONN_CLOSED) { |
| 3161 | delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0); |
| 3162 | txn->rsp.eoh += delta; |
| 3163 | cur_next += delta; |
| 3164 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 3165 | txn->hdr_idx.used--; |
| 3166 | cur_hdr->len = 0; |
| 3167 | } else { |
| 3168 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 3169 | delta = buffer_replace2(rep, cur_ptr + val, cur_end, |
| 3170 | "close", 5); |
| 3171 | cur_next += delta; |
| 3172 | cur_hdr->len += delta; |
| 3173 | txn->rsp.eoh += delta; |
| 3174 | } |
| 3175 | t->flags |= SN_CONN_CLOSED; |
| 3176 | } |
| 3177 | } |
| 3178 | old_idx = cur_idx; |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 3179 | } |
| 3180 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3181 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3182 | /* add response headers from the rule sets in the same order */ |
| 3183 | for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) { |
| 3184 | if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, |
| 3185 | rule_set->rsp_add[cur_idx])) < 0) |
| 3186 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 3187 | } |
| 3188 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3189 | /* check whether we're already working on the frontend */ |
| 3190 | if (cur_proxy == t->fe) |
| 3191 | break; |
| 3192 | cur_proxy = t->fe; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3193 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3194 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3195 | /* |
| 3196 | * 4: check for server cookie. |
| 3197 | */ |
| 3198 | if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name |
| 3199 | || (t->be->options & PR_O_CHK_CACHE)) |
| 3200 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3201 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3202 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3203 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3204 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3205 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3206 | if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0) |
| 3207 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3208 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3209 | /* |
| 3210 | * 6: add server cookie in the response if needed |
| 3211 | */ |
| 3212 | if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) && |
| 3213 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) { |
| 3214 | int len; |
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 | /* the server is known, it's not the one the client requested, we have to |
| 3217 | * insert a set-cookie here, except if we want to insert only on POST |
| 3218 | * requests and this one isn't. Note that servers which don't have cookies |
| 3219 | * (eg: some backup servers) will return a full cookie removal request. |
| 3220 | */ |
| 3221 | len = sprintf(trash, "Set-Cookie: %s=%s; path=/", |
| 3222 | t->be->cookie_name, |
| 3223 | 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] | 3224 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3225 | if (t->be->cookie_domain) |
| 3226 | len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3227 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3228 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3229 | trash, len)) < 0) |
| 3230 | goto return_bad_resp; |
| 3231 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3232 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3233 | /* Here, we will tell an eventual cache on the client side that we don't |
| 3234 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 3235 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 3236 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 3237 | */ |
| 3238 | if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3239 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3240 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3241 | |
| 3242 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3243 | "Cache-control: private", 22)) < 0) |
| 3244 | goto return_bad_resp; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3245 | } |
| 3246 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3247 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3248 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3249 | /* |
| 3250 | * 7: check if result will be cacheable with a cookie. |
| 3251 | * We'll block the response if security checks have caught |
| 3252 | * nasty things such as a cacheable cookie. |
| 3253 | */ |
| 3254 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) == |
| 3255 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) && |
| 3256 | (t->be->options & PR_O_CHK_CACHE)) { |
| 3257 | |
| 3258 | /* we're in presence of a cacheable response containing |
| 3259 | * a set-cookie header. We'll block it as requested by |
| 3260 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3261 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3262 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3263 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3264 | t->srv->failed_secu++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3265 | //sess_change_server(t, NULL); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3266 | } |
| 3267 | t->be->denied_resp++; |
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 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 3270 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 3271 | send_log(t->be, LOG_ALERT, |
| 3272 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 3273 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 3274 | goto return_srv_prx_502; |
| 3275 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3276 | |
| 3277 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3278 | * 8: add "Connection: close" if needed and not yet set. |
| 3279 | * 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] | 3280 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3281 | if (!(t->flags & SN_CONN_CLOSED) && |
| 3282 | ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) { |
| 3283 | if ((unlikely(msg->sl.st.v_l != 8) || |
| 3284 | unlikely(req->data[msg->som + 7] != '0')) && |
| 3285 | unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3286 | "Connection: close", 17)) < 0) |
| 3287 | goto return_bad_resp; |
| 3288 | t->flags |= SN_CONN_CLOSED; |
| 3289 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3290 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3291 | /************************************************************* |
| 3292 | * OK, that's finished for the headers. We have done what we * |
| 3293 | * could. Let's switch to the DATA state. * |
| 3294 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3295 | |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 3296 | buffer_set_rlim(rep, BUFSIZE); /* no more rewrite needed */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3297 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3298 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3299 | #ifdef CONFIG_HAP_TCPSPLICE |
| 3300 | if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
| 3301 | /* TCP splicing supported by both FE and BE */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 3302 | tcp_splice_splicefd(rep->cons->fd, rep->prod->fd, 0); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3303 | } |
| 3304 | #endif |
| 3305 | /* if the user wants to log as soon as possible, without counting |
| 3306 | * bytes from the server, then this is the right moment. We have |
| 3307 | * to temporarily assign bytes_out to log what we currently have. |
| 3308 | */ |
| 3309 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 3310 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 3311 | t->logs.bytes_out = txn->rsp.eoh; |
| 3312 | if (t->fe->to_log & LW_REQ) |
| 3313 | http_sess_log(t); |
| 3314 | else |
| 3315 | tcp_sess_log(t); |
| 3316 | t->logs.bytes_out = 0; |
| 3317 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3318 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3319 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 3320 | * otherwise we would not let the client side wake up. |
| 3321 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3322 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3323 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3324 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3325 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3326 | /* Note: eventhough nobody should set an unknown flag, clearing them right now will |
| 3327 | * probably reduce one day's debugging session. |
| 3328 | */ |
| 3329 | #ifdef DEBUG_DEV |
| 3330 | if (rep->analysers & ~(AN_RTR_HTTP_HDR)) { |
| 3331 | fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n", |
| 3332 | __FILE__, __LINE__, rep->analysers); |
| 3333 | ABORT_NOW(); |
| 3334 | } |
| 3335 | #endif |
| 3336 | rep->analysers &= AN_RTR_HTTP_HDR; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3337 | return 0; |
| 3338 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3339 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 3340 | ///* |
| 3341 | // * Manages the client FSM and its socket. It normally returns zero, but may |
| 3342 | // * return 1 if it absolutely wants to be called again. |
| 3343 | // * |
| 3344 | // * Note: process_cli is the ONLY function allowed to set cli_state to anything |
| 3345 | // * but CL_STCLOSE. |
| 3346 | // */ |
| 3347 | //int process_cli(struct session *t) |
| 3348 | //{ |
| 3349 | // struct buffer *req = t->req; |
| 3350 | // struct buffer *rep = t->rep; |
| 3351 | // |
| 3352 | // 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", |
| 3353 | // now_ms, __FUNCTION__, |
| 3354 | // t->cli_fd, t->cli_fd >= 0 ? fdtab[t->cli_fd].state : 0, /* fd,state*/ |
| 3355 | // cli_stnames[t->cli_state], |
| 3356 | // t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_RD) : 0, |
| 3357 | // t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_WR) : 0, |
| 3358 | // req->rex, rep->wex, |
| 3359 | // req->flags, rep->flags, |
| 3360 | // req->l, rep->l); |
| 3361 | // |
| 3362 | // update_state: |
| 3363 | // /* FIXME: we still have to check for CL_STSHUTR because client_retnclose |
| 3364 | // * still set this state (and will do until unix sockets are converted). |
| 3365 | // */ |
| 3366 | // if (t->cli_state == CL_STDATA || t->cli_state == CL_STSHUTR) { |
| 3367 | // /* we can skip most of the tests at once if some conditions are not met */ |
| 3368 | // if (!((fdtab[t->cli_fd].state == FD_STERROR) || |
| 3369 | // (req->flags & (BF_READ_TIMEOUT|BF_READ_ERROR|BF_SHUTR_NOW)) || |
| 3370 | // (rep->flags & (BF_WRITE_TIMEOUT|BF_WRITE_ERROR|BF_SHUTW_NOW)) || |
| 3371 | // (!(req->flags & BF_SHUTR) && req->flags & (BF_READ_NULL|BF_SHUTW)) || |
| 3372 | // (!(rep->flags & BF_SHUTW) && |
| 3373 | // (rep->flags & (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR)) == (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR)))) |
| 3374 | // goto update_timeouts; |
| 3375 | // |
| 3376 | // /* read or write error */ |
| 3377 | // if (fdtab[t->cli_fd].state == FD_STERROR) { |
| 3378 | // buffer_shutr(req); |
| 3379 | // req->flags |= BF_READ_ERROR; |
| 3380 | // buffer_shutw(rep); |
| 3381 | // rep->flags |= BF_WRITE_ERROR; |
| 3382 | // fd_delete(t->cli_fd); |
| 3383 | // t->cli_state = CL_STCLOSE; |
| 3384 | // trace_term(t, TT_HTTP_CLI_1); |
| 3385 | // if (!req->analysers) { |
| 3386 | // if (!(t->flags & SN_ERR_MASK)) |
| 3387 | // t->flags |= SN_ERR_CLICL; |
| 3388 | // if (!(t->flags & SN_FINST_MASK)) { |
| 3389 | // if (req->cons->err_type <= SI_ET_QUEUE_ABRT) |
| 3390 | // t->flags |= SN_FINST_Q; |
| 3391 | // else if (req->cons->err_type <= SI_ET_CONN_OTHER) |
| 3392 | // t->flags |= SN_FINST_C; |
| 3393 | // else |
| 3394 | // t->flags |= SN_FINST_D; |
| 3395 | // } |
| 3396 | // } |
| 3397 | // goto update_state; |
| 3398 | // } |
| 3399 | // /* last read, or end of server write */ |
| 3400 | // else if (!(req->flags & BF_SHUTR) && /* not already done */ |
| 3401 | // req->flags & (BF_READ_NULL|BF_SHUTR_NOW|BF_SHUTW)) { |
| 3402 | // buffer_shutr(req); |
| 3403 | // if (!(rep->flags & BF_SHUTW)) { |
| 3404 | // EV_FD_CLR(t->cli_fd, DIR_RD); |
| 3405 | // trace_term(t, TT_HTTP_CLI_2); |
| 3406 | // } else { |
| 3407 | // /* output was already closed */ |
| 3408 | // fd_delete(t->cli_fd); |
| 3409 | // t->cli_state = CL_STCLOSE; |
| 3410 | // trace_term(t, TT_HTTP_CLI_3); |
| 3411 | // } |
| 3412 | // goto update_state; |
| 3413 | // } |
| 3414 | // /* last server read and buffer empty : we only check them when we're |
| 3415 | // * allowed to forward the data. |
| 3416 | // */ |
| 3417 | // else if (!(rep->flags & BF_SHUTW) && /* not already done */ |
| 3418 | // ((rep->flags & BF_SHUTW_NOW) || |
| 3419 | // (rep->flags & BF_EMPTY && rep->flags & BF_MAY_FORWARD && |
| 3420 | // rep->flags & BF_SHUTR && !(t->flags & SN_SELF_GEN)))) { |
| 3421 | // buffer_shutw(rep); |
| 3422 | // if (!(req->flags & BF_SHUTR)) { |
| 3423 | // EV_FD_CLR(t->cli_fd, DIR_WR); |
| 3424 | // shutdown(t->cli_fd, SHUT_WR); |
| 3425 | // trace_term(t, TT_HTTP_CLI_4); |
| 3426 | // } else { |
| 3427 | // fd_delete(t->cli_fd); |
| 3428 | // t->cli_state = CL_STCLOSE; |
| 3429 | // trace_term(t, TT_HTTP_CLI_5); |
| 3430 | // } |
| 3431 | // goto update_state; |
| 3432 | // } |
| 3433 | // /* read timeout */ |
| 3434 | // else if ((req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT) { |
| 3435 | // buffer_shutr(req); |
| 3436 | // if (!(rep->flags & BF_SHUTW)) { |
| 3437 | // EV_FD_CLR(t->cli_fd, DIR_RD); |
| 3438 | // trace_term(t, TT_HTTP_CLI_6); |
| 3439 | // } else { |
| 3440 | // /* output was already closed */ |
| 3441 | // fd_delete(t->cli_fd); |
| 3442 | // t->cli_state = CL_STCLOSE; |
| 3443 | // trace_term(t, TT_HTTP_CLI_7); |
| 3444 | // } |
| 3445 | // if (!req->analysers) { |
| 3446 | // if (!(t->flags & SN_ERR_MASK)) |
| 3447 | // t->flags |= SN_ERR_CLITO; |
| 3448 | // if (!(t->flags & SN_FINST_MASK)) { |
| 3449 | // if (req->cons->err_type <= SI_ET_QUEUE_ABRT) |
| 3450 | // t->flags |= SN_FINST_Q; |
| 3451 | // else if (req->cons->err_type <= SI_ET_CONN_OTHER) |
| 3452 | // t->flags |= SN_FINST_C; |
| 3453 | // else |
| 3454 | // t->flags |= SN_FINST_D; |
| 3455 | // } |
| 3456 | // } |
| 3457 | // goto update_state; |
| 3458 | // } |
| 3459 | // /* write timeout */ |
| 3460 | // else if ((rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT) { |
| 3461 | // buffer_shutw(rep); |
| 3462 | // if (!(req->flags & BF_SHUTR)) { |
| 3463 | // EV_FD_CLR(t->cli_fd, DIR_WR); |
| 3464 | // shutdown(t->cli_fd, SHUT_WR); |
| 3465 | // trace_term(t, TT_HTTP_CLI_8); |
| 3466 | // } else { |
| 3467 | // fd_delete(t->cli_fd); |
| 3468 | // t->cli_state = CL_STCLOSE; |
| 3469 | // trace_term(t, TT_HTTP_CLI_9); |
| 3470 | // } |
| 3471 | // if (!req->analysers) { |
| 3472 | // if (!(t->flags & SN_ERR_MASK)) |
| 3473 | // t->flags |= SN_ERR_CLITO; |
| 3474 | // if (!(t->flags & SN_FINST_MASK)) { |
| 3475 | // if (req->cons->err_type <= SI_ET_QUEUE_ABRT) |
| 3476 | // t->flags |= SN_FINST_Q; |
| 3477 | // else if (req->cons->err_type <= SI_ET_CONN_OTHER) |
| 3478 | // t->flags |= SN_FINST_C; |
| 3479 | // else |
| 3480 | // t->flags |= SN_FINST_D; |
| 3481 | // } |
| 3482 | // } |
| 3483 | // goto update_state; |
| 3484 | // } |
| 3485 | // |
| 3486 | // update_timeouts: |
| 3487 | // /* manage read timeout */ |
| 3488 | // if (!(req->flags & BF_SHUTR)) { |
| 3489 | // if (req->flags & BF_FULL) { |
| 3490 | // /* no room to read more data */ |
| 3491 | // if (EV_FD_COND_C(t->cli_fd, DIR_RD)) { |
| 3492 | // /* stop reading until we get some space */ |
| 3493 | // req->rex = TICK_ETERNITY; |
| 3494 | // } |
| 3495 | // } else { |
| 3496 | // EV_FD_COND_S(t->cli_fd, DIR_RD); |
| 3497 | // req->rex = tick_add_ifset(now_ms, t->fe->timeout.client); |
| 3498 | // } |
| 3499 | // } |
| 3500 | // |
| 3501 | // /* manage write timeout */ |
| 3502 | // if (!(rep->flags & BF_SHUTW)) { |
| 3503 | // /* first, we may have to produce data (eg: stats). |
| 3504 | // * right now, this is limited to the SHUTR state. |
| 3505 | // */ |
| 3506 | // if (req->flags & BF_SHUTR && t->flags & SN_SELF_GEN) { |
| 3507 | // produce_content(t); |
| 3508 | // if (rep->flags & BF_EMPTY) { |
| 3509 | // buffer_shutw(rep); |
| 3510 | // fd_delete(t->cli_fd); |
| 3511 | // t->cli_state = CL_STCLOSE; |
| 3512 | // trace_term(t, TT_HTTP_CLI_10); |
| 3513 | // goto update_state; |
| 3514 | // } |
| 3515 | // } |
| 3516 | // |
| 3517 | // /* we don't enable client write if the buffer is empty, nor if the server has to analyze it */ |
| 3518 | // if ((rep->flags & (BF_EMPTY|BF_MAY_FORWARD)) != BF_MAY_FORWARD) { |
| 3519 | // if (EV_FD_COND_C(t->cli_fd, DIR_WR)) { |
| 3520 | // /* stop writing */ |
| 3521 | // rep->wex = TICK_ETERNITY; |
| 3522 | // } |
| 3523 | // } else { |
| 3524 | // /* buffer not empty */ |
| 3525 | // EV_FD_COND_S(t->cli_fd, DIR_WR); |
| 3526 | // if (!tick_isset(rep->wex)) { |
| 3527 | // /* restart writing */ |
| 3528 | // rep->wex = tick_add_ifset(now_ms, t->fe->timeout.client); |
| 3529 | // if (!(req->flags & BF_SHUTR) && tick_isset(rep->wex) && tick_isset(req->rex)) { |
| 3530 | // /* FIXME: to prevent the client from expiring read timeouts during writes, |
| 3531 | // * we refresh it, except if it was already infinite. */ |
| 3532 | // req->rex = rep->wex; |
| 3533 | // } |
| 3534 | // } |
| 3535 | // } |
| 3536 | // } |
| 3537 | // return 0; /* other cases change nothing */ |
| 3538 | // } |
| 3539 | // else if (t->cli_state == CL_STCLOSE) { /* CL_STCLOSE: nothing to do */ |
| 3540 | // if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 3541 | // int len; |
| 3542 | // 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); |
| 3543 | // write(1, trash, len); |
| 3544 | // } |
| 3545 | // return 0; |
| 3546 | // } |
| 3547 | //#ifdef DEBUG_DEV |
| 3548 | // fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, t->cli_state); |
| 3549 | // ABORT_NOW(); |
| 3550 | //#endif |
| 3551 | // return 0; |
| 3552 | //} |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3553 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3554 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3555 | /* Return 1 if the pending connection has failed and should be retried, |
| 3556 | * otherwise zero. We may only come here in SI_ST_CON state, which means that |
| 3557 | * the socket's file descriptor is known. |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3558 | */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3559 | int tcp_connection_status(struct session *t) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3560 | { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3561 | struct buffer *req = t->req; |
| 3562 | struct buffer *rep = t->rep; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3563 | int conn_err = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3564 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3565 | DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n", |
| 3566 | now_ms, __FUNCTION__, |
| 3567 | cli_stnames[t->cli_state], |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3568 | rep->rex, req->wex, |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 3569 | req->flags, rep->flags, |
| 3570 | req->l, rep->l); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3571 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3572 | if ((req->flags & BF_SHUTW_NOW) || |
| 3573 | (rep->flags & BF_SHUTW) || |
| 3574 | ((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] | 3575 | ((req->flags & BF_EMPTY && !(req->flags & BF_WRITE_ACTIVITY)) || |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3576 | t->be->options & PR_O_ABRT_CLOSE))) { /* give up */ |
| 3577 | |
| 3578 | trace_term(t, TT_HTTP_SRV_5); |
| 3579 | req->wex = TICK_ETERNITY; |
| 3580 | fd_delete(req->cons->fd); |
| 3581 | if (t->srv) { |
| 3582 | t->srv->cur_sess--; |
| 3583 | sess_change_server(t, NULL); |
| 3584 | } |
| 3585 | /* note that this must not return any error because it would be able to |
| 3586 | * overwrite the client_retnclose() output. |
| 3587 | */ |
| 3588 | //srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL); |
| 3589 | |
| 3590 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3591 | buffer_shutw(req); |
| 3592 | buffer_shutr(rep); |
| 3593 | req->cons->state = SI_ST_CLO; |
| 3594 | if (!req->cons->err_type) |
| 3595 | req->cons->err_type = SI_ET_CONN_ABRT; |
| 3596 | req->cons->err_loc = t->srv; |
| 3597 | return 0; |
| 3598 | } |
| 3599 | |
| 3600 | /* check for timeouts and asynchronous connect errors */ |
| 3601 | if (fdtab[req->cons->fd].state == FD_STERROR) { |
| 3602 | conn_err = SI_ET_CONN_ERR; |
| 3603 | if (!req->cons->err_type) |
| 3604 | req->cons->err_type = SI_ET_CONN_ERR; |
| 3605 | } |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 3606 | else if (!(req->flags & BF_WRITE_ACTIVITY)) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3607 | /* nothing happened, maybe we timed out */ |
| 3608 | if (tick_is_expired(req->wex, now_ms)) { |
| 3609 | conn_err = SI_ET_CONN_TO; |
| 3610 | if (!req->cons->err_type) |
| 3611 | req->cons->err_type = SI_ET_CONN_TO; |
| 3612 | } |
| 3613 | else |
| 3614 | return 0; /* let's wait a bit more */ |
| 3615 | } |
| 3616 | |
| 3617 | if (conn_err) { |
| 3618 | fd_delete(req->cons->fd); |
| 3619 | req->cons->state = SI_ST_CLO; |
| 3620 | |
| 3621 | if (t->srv) { |
| 3622 | t->srv->cur_sess--; |
| 3623 | sess_change_server(t, NULL); |
| 3624 | req->cons->err_loc = t->srv; |
| 3625 | } |
| 3626 | |
| 3627 | /* ensure that we have enough retries left */ |
| 3628 | if (srv_count_retry_down(t, conn_err)) |
| 3629 | return 0; |
| 3630 | |
| 3631 | if (conn_err == SI_ET_CONN_ERR) { |
| 3632 | /* we encountered an immediate connection error, and we |
| 3633 | * will have to retry connecting to the same server, most |
| 3634 | * likely leading to the same result. To avoid this, we |
| 3635 | * fake a connection timeout to retry after a turn-around |
| 3636 | * time of 1 second. We will wait in the previous if block. |
| 3637 | */ |
| 3638 | req->cons->state = SI_ST_TAR; |
Willy Tarreau | 3537467 | 2008-09-03 18:11:02 +0200 | [diff] [blame] | 3639 | req->cons->exp = tick_add(now_ms, MS_TO_TICKS(1000)); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3640 | return 0; |
| 3641 | } |
| 3642 | |
| 3643 | if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) { |
| 3644 | /* We're on our last chance, and the REDISP option was specified. |
| 3645 | * We will ignore cookie and force to balance or use the dispatcher. |
| 3646 | */ |
| 3647 | /* let's try to offer this slot to anybody */ |
| 3648 | if (may_dequeue_tasks(t->srv, t->be)) |
| 3649 | process_srv_queue(t->srv); |
| 3650 | |
| 3651 | /* it's left to the dispatcher to choose a server */ |
| 3652 | t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
| 3653 | t->prev_srv = t->srv; |
| 3654 | } else { |
| 3655 | /* we just want to retry */ |
| 3656 | if (t->srv) |
| 3657 | t->srv->retries++; |
| 3658 | t->be->retries++; |
| 3659 | |
| 3660 | /* Now we will try to either reconnect to the same server or |
| 3661 | * connect to another server. If the connection gets queued |
| 3662 | * because all servers are saturated, then we will go back to |
| 3663 | * the idle state where the buffer's consumer is marked as |
| 3664 | * unknown. |
| 3665 | */ |
| 3666 | if (srv_retryable_connect(t)) { |
| 3667 | /* success or unrecoverable error */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3668 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3669 | return 0; |
| 3670 | } |
| 3671 | } |
| 3672 | |
| 3673 | /* We'll rely on the caller to try to get a connection again */ |
| 3674 | return 1; |
| 3675 | } |
| 3676 | else { |
| 3677 | /* no error and write OK : connection succeeded */ |
| 3678 | t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 3679 | req->cons->state = SI_ST_EST; |
| 3680 | req->cons->err_type = SI_ET_NONE; |
| 3681 | req->cons->err_loc = NULL; |
| 3682 | |
| 3683 | if (req->flags & BF_EMPTY) { |
| 3684 | EV_FD_CLR(req->cons->fd, DIR_WR); |
| 3685 | req->wex = TICK_ETERNITY; |
| 3686 | } else { |
| 3687 | EV_FD_SET(req->cons->fd, DIR_WR); |
| 3688 | req->wex = tick_add_ifset(now_ms, t->be->timeout.server); |
| 3689 | if (tick_isset(req->wex)) { |
| 3690 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 3691 | * we refresh it. */ |
| 3692 | rep->rex = req->wex; |
| 3693 | } |
| 3694 | } |
| 3695 | |
| 3696 | if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */ |
| 3697 | if (!(rep->flags & BF_HIJACK)) { |
| 3698 | EV_FD_SET(req->cons->fd, DIR_RD); |
| 3699 | rep->rex = tick_add_ifset(now_ms, t->be->timeout.server); |
| 3700 | } |
| 3701 | buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */ |
| 3702 | |
| 3703 | /* if the user wants to log as soon as possible, without counting |
| 3704 | bytes from the server, then this is the right moment. */ |
| 3705 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 3706 | t->logs.t_close = t->logs.t_connect; /* to get a valid end date */ |
| 3707 | tcp_sess_log(t); |
| 3708 | } |
| 3709 | #ifdef CONFIG_HAP_TCPSPLICE |
| 3710 | if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
| 3711 | /* TCP splicing supported by both FE and BE */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 3712 | tcp_splice_splicefd(req->prod->fd, req->cons->fd, 0); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3713 | } |
| 3714 | #endif |
| 3715 | } |
| 3716 | else { |
| 3717 | rep->analysers |= AN_RTR_HTTP_HDR; |
| 3718 | buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */ |
| 3719 | t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE; |
| 3720 | /* reset hdr_idx which was already initialized by the request. |
| 3721 | * right now, the http parser does it. |
| 3722 | * hdr_idx_init(&t->txn.hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3723 | */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3724 | } |
| 3725 | |
Willy Tarreau | 9a2d154 | 2008-08-30 12:31:07 +0200 | [diff] [blame] | 3726 | rep->flags |= BF_READ_ATTACHED; /* producer is now attached */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3727 | req->wex = TICK_ETERNITY; |
| 3728 | return 0; |
| 3729 | } |
| 3730 | } |
| 3731 | |
| 3732 | |
| 3733 | /* |
| 3734 | * This function tries to assign a server to a stream_sock interface. |
| 3735 | * It may be called only for t->req->cons->state = one of { SI_ST_INI, |
| 3736 | * SI_ST_TAR, SI_ST_QUE }. It returns one of those states, SI_ST_ASS |
| 3737 | * in case of success, or SI_ST_CLO in case of failure. It returns 1 if |
| 3738 | * it returns SI_ST_ASS, otherwise zero. |
| 3739 | */ |
| 3740 | int stream_sock_assign_server(struct session *t) |
| 3741 | { |
| 3742 | DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n", |
| 3743 | now_ms, __FUNCTION__, |
| 3744 | cli_stnames[t->cli_state], |
| 3745 | t->rep->rex, t->req->wex, |
| 3746 | t->req->flags, t->rep->flags, |
| 3747 | t->req->l, t->rep->l); |
| 3748 | |
| 3749 | if (t->req->cons->state == SI_ST_TAR) { |
| 3750 | /* connection might be aborted */ |
| 3751 | if ((t->req->flags & BF_SHUTW_NOW) || |
| 3752 | (t->rep->flags & BF_SHUTW) || |
| 3753 | ((t->req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */ |
| 3754 | (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] | 3755 | |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3756 | trace_term(t, TT_HTTP_SRV_1); |
Willy Tarreau | 3537467 | 2008-09-03 18:11:02 +0200 | [diff] [blame] | 3757 | t->req->cons->exp = TICK_ETERNITY; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3758 | |
| 3759 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3760 | buffer_shutr(t->rep); |
| 3761 | buffer_shutw(t->req); |
| 3762 | if (!t->req->cons->err_type) |
| 3763 | t->req->cons->err_type = SI_ET_CONN_ABRT; |
| 3764 | t->req->cons->state = SI_ST_CLO; |
| 3765 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3766 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3767 | |
Willy Tarreau | 3537467 | 2008-09-03 18:11:02 +0200 | [diff] [blame] | 3768 | if (!tick_is_expired(t->req->cons->exp, now_ms)) |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3769 | return 0; /* still in turn-around */ |
| 3770 | |
| 3771 | t->req->cons->state = SI_ST_INI; |
Willy Tarreau | 3537467 | 2008-09-03 18:11:02 +0200 | [diff] [blame] | 3772 | t->req->cons->exp = TICK_ETERNITY; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3773 | } |
| 3774 | else if (t->req->cons->state == SI_ST_QUE) { |
| 3775 | if (t->pend_pos) { |
| 3776 | /* request still in queue... */ |
Willy Tarreau | 3537467 | 2008-09-03 18:11:02 +0200 | [diff] [blame] | 3777 | if (tick_is_expired(t->req->cons->exp, now_ms)) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3778 | /* ... and timeout expired */ |
| 3779 | trace_term(t, TT_HTTP_SRV_3); |
Willy Tarreau | 3537467 | 2008-09-03 18:11:02 +0200 | [diff] [blame] | 3780 | t->req->cons->exp = TICK_ETERNITY; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3781 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3782 | if (t->srv) |
| 3783 | t->srv->failed_conns++; |
| 3784 | t->be->failed_conns++; |
| 3785 | |
| 3786 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3787 | buffer_shutr(t->rep); |
| 3788 | buffer_shutw(t->req); |
| 3789 | t->req->flags |= BF_WRITE_TIMEOUT; |
| 3790 | if (!t->req->cons->err_type) |
| 3791 | t->req->cons->err_type = SI_ET_QUEUE_TO; |
| 3792 | t->req->cons->state = SI_ST_CLO; |
| 3793 | return 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3794 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3795 | /* connection remains in queue, check if we have to abort it */ |
| 3796 | if ((t->req->flags & BF_SHUTW_NOW) || |
| 3797 | (t->rep->flags & BF_SHUTW) || |
| 3798 | ((t->req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */ |
| 3799 | (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { |
| 3800 | /* give up */ |
| 3801 | trace_term(t, TT_HTTP_SRV_1); |
Willy Tarreau | 3537467 | 2008-09-03 18:11:02 +0200 | [diff] [blame] | 3802 | t->req->cons->exp = TICK_ETERNITY; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3803 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
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 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3806 | buffer_shutr(t->rep); |
| 3807 | buffer_shutw(t->req); |
| 3808 | if (!t->req->cons->err_type) |
| 3809 | t->req->cons->err_type = SI_ET_QUEUE_ABRT; |
| 3810 | t->req->cons->state = SI_ST_CLO; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3811 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3812 | return 0; |
| 3813 | } |
| 3814 | /* The connection is not in the queue anymore */ |
| 3815 | t->req->cons->state = SI_ST_INI; |
Willy Tarreau | 3537467 | 2008-09-03 18:11:02 +0200 | [diff] [blame] | 3816 | t->req->cons->exp = TICK_ETERNITY; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3817 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3818 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3819 | /* we may get here from above */ |
| 3820 | if (t->req->cons->state == SI_ST_INI) { |
| 3821 | /* no connection in progress, we have to get a new one */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3822 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3823 | /* first, check if the connection has been aborted */ |
| 3824 | if ((t->req->flags & BF_SHUTW_NOW) || |
| 3825 | (t->rep->flags & BF_SHUTW) || |
| 3826 | ((t->req->flags & BF_SHUTR) && |
| 3827 | (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] | 3828 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3829 | trace_term(t, TT_HTTP_SRV_1); |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3830 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3831 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3832 | buffer_shutr(t->rep); |
| 3833 | buffer_shutw(t->req); |
| 3834 | if (!t->req->cons->err_type) |
| 3835 | t->req->cons->err_type = SI_ET_CONN_ABRT; |
| 3836 | t->req->cons->state = SI_ST_CLO; |
| 3837 | return 0; |
| 3838 | } |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3839 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3840 | /* try to get a server assigned */ |
| 3841 | if (srv_redispatch_connect(t) != 0) { |
| 3842 | /* we did not get any server, let's check the cause */ |
| 3843 | if (t->req->cons->state == SI_ST_QUE) { |
| 3844 | /* the connection was queued, that's OK */ |
| 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 | trace_term(t, TT_HTTP_SRV_2); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3849 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3850 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3851 | buffer_shutr(t->rep); |
| 3852 | buffer_shutw(t->req); |
| 3853 | t->req->flags |= BF_WRITE_ERROR; |
| 3854 | if (!t->req->cons->err_type) |
| 3855 | t->req->cons->err_type = SI_ET_CONN_OTHER; |
| 3856 | t->req->cons->state = SI_ST_CLO; |
| 3857 | return 0; |
| 3858 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3859 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3860 | t->req->cons->state = SI_ST_ASS; |
| 3861 | /* Once the server is assigned, we have to return because |
| 3862 | * the caller might be interested in checking several |
| 3863 | * things before connecting. |
| 3864 | */ |
| 3865 | return 1; |
| 3866 | } |
| 3867 | return 0; |
| 3868 | } |
Krzysztof Piotr Oledzki | efe3b6f | 2008-05-23 23:49:32 +0200 | [diff] [blame] | 3869 | |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3870 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3871 | /* |
| 3872 | * This function tries to establish a connection to an assigned server. It also |
| 3873 | * performs connection retries. It may only be called with t->req->cons->state |
| 3874 | * in { SI_ST_ASS, SI_ST_CON }. It may also set the state to SI_ST_INI, |
| 3875 | * SI_ST_EST, or SI_ST_CLO. |
| 3876 | */ |
| 3877 | int stream_sock_connect_server(struct session *t) |
| 3878 | { |
| 3879 | if (t->req->cons->state == SI_ST_ASS) { |
| 3880 | /* server assigned to request, we have to try to connect now */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3881 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3882 | if (!srv_retryable_connect(t)) { |
| 3883 | /* we need to redispatch */ |
| 3884 | t->req->cons->state = SI_ST_INI; |
| 3885 | return 0; |
| 3886 | } |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3887 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3888 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 3889 | if (t->req->cons->state != SI_ST_CON) { |
| 3890 | /* it was an error */ |
| 3891 | trace_term(t, TT_HTTP_SRV_4); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3892 | |
| 3893 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3894 | buffer_shutr(t->rep); |
| 3895 | buffer_shutw(t->req); |
| 3896 | t->req->flags |= BF_WRITE_ERROR; |
| 3897 | if (!t->req->cons->err_type) |
| 3898 | t->req->cons->err_type = SI_ET_CONN_OTHER; |
| 3899 | t->req->cons->state = SI_ST_CLO; |
| 3900 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3901 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3902 | /* We have a socket and switched to SI_ST_CON */ |
| 3903 | } |
| 3904 | |
| 3905 | /* we may also get here from above */ |
| 3906 | if (t->req->cons->state == SI_ST_CON) { |
| 3907 | /* connection in progress or just completed */ |
| 3908 | if (!tcp_connection_status(t)) |
| 3909 | return 0; |
| 3910 | } |
| 3911 | return 0; |
| 3912 | } |
| 3913 | |
| 3914 | |
| 3915 | /* |
| 3916 | * Tries to establish a connection to the server and associate it to the |
| 3917 | * 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] | 3918 | * 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] | 3919 | * returns zero, but may return 1 if it absolutely wants to be called again. |
| 3920 | */ |
| 3921 | int process_srv_conn(struct session *t) |
| 3922 | { |
| 3923 | DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n", |
| 3924 | now_ms, __FUNCTION__, |
| 3925 | cli_stnames[t->cli_state], |
| 3926 | t->rep->rex, t->req->wex, |
| 3927 | t->req->flags, t->rep->flags, |
| 3928 | t->req->l, t->rep->l); |
| 3929 | |
| 3930 | do { |
| 3931 | if (t->req->cons->state == SI_ST_INI || |
| 3932 | t->req->cons->state == SI_ST_TAR || |
| 3933 | t->req->cons->state == SI_ST_QUE) { |
| 3934 | /* try to assign a server */ |
| 3935 | if (!stream_sock_assign_server(t)) |
| 3936 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3937 | } |
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 | if (t->req->cons->state == SI_ST_ASS && |
| 3940 | t->srv && t->srv->rdr_len && t->flags & SN_REDIRECTABLE) { |
| 3941 | /* Server supporting redirection and it is possible. |
| 3942 | * Invalid requests are reported as such. It concerns all |
| 3943 | * the largest ones. |
| 3944 | */ |
| 3945 | struct http_txn *txn = &t->txn; |
| 3946 | struct chunk rdr; |
| 3947 | char *path; |
| 3948 | int len; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3949 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3950 | /* 1: create the response header */ |
| 3951 | rdr.len = strlen(HTTP_302); |
| 3952 | rdr.str = trash; |
| 3953 | memcpy(rdr.str, HTTP_302, rdr.len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3954 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3955 | /* 2: add the server's prefix */ |
| 3956 | if (rdr.len + t->srv->rdr_len > sizeof(trash)) |
| 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, t->srv->rdr_pfx, t->srv->rdr_len); |
| 3960 | rdr.len += t->srv->rdr_len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3961 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3962 | /* 3: add the request URI */ |
| 3963 | path = http_get_path(txn); |
| 3964 | if (!path) |
| 3965 | goto cancel_redir; |
| 3966 | len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path; |
| 3967 | if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */ |
| 3968 | goto cancel_redir; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3969 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3970 | memcpy(rdr.str + rdr.len, path, len); |
| 3971 | rdr.len += len; |
| 3972 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 3973 | rdr.len += 4; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3974 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3975 | srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr); |
| 3976 | trace_term(t, TT_HTTP_SRV_3); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3977 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3978 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
| 3979 | if (t->srv) |
| 3980 | t->srv->cum_sess++; |
| 3981 | |
| 3982 | t->req->cons->state = SI_ST_CLO; |
| 3983 | return 0; |
| 3984 | cancel_redir: |
| 3985 | //txn->status = 400; |
| 3986 | //t->fe->failed_req++; |
| 3987 | //srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, |
| 3988 | // 400, error_message(t, HTTP_ERR_400)); |
| 3989 | trace_term(t, TT_HTTP_SRV_4); |
| 3990 | |
| 3991 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3992 | buffer_shutw(t->req); |
| 3993 | buffer_shutr(t->rep); |
| 3994 | if (!t->req->cons->err_type) |
| 3995 | t->req->cons->err_type = SI_ET_CONN_OTHER; |
| 3996 | t->req->cons->state = SI_ST_CLO; |
| 3997 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3998 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3999 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 4000 | if (t->req->cons->state == SI_ST_CON || |
| 4001 | t->req->cons->state == SI_ST_ASS) { |
| 4002 | stream_sock_connect_server(t); |
| 4003 | } |
| 4004 | } while (t->req->cons->state != SI_ST_CLO && |
| 4005 | t->req->cons->state != SI_ST_CON && |
| 4006 | t->req->cons->state != SI_ST_EST); |
| 4007 | return 0; |
| 4008 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4009 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 4010 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4011 | /* |
| 4012 | * 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] | 4013 | * 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] | 4014 | * be produced. It stops by itself by unsetting the BF_HIJACK flag from the |
| 4015 | * 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] | 4016 | * 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] | 4017 | * when it wants to stop sending data, otherwise 0. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4018 | */ |
| 4019 | int produce_content(struct session *s) |
| 4020 | { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4021 | if (s->data_source == DATA_SRC_NONE) { |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 4022 | buffer_stop_hijack(s->rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4023 | return 1; |
| 4024 | } |
| 4025 | else if (s->data_source == DATA_SRC_STATS) { |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 4026 | /* dump server statistics */ |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 4027 | int ret = stats_dump_http(s, s->be->uri_auth); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 4028 | if (ret >= 0) |
| 4029 | return ret; |
| 4030 | /* -1 indicates an error */ |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 4031 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4032 | |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 4033 | /* unknown data source or internal error */ |
| 4034 | s->txn.status = 500; |
| 4035 | client_retnclose(s, error_message(s, HTTP_ERR_500)); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 4036 | trace_term(s, TT_HTTP_CNT_1); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 4037 | if (!(s->flags & SN_ERR_MASK)) |
| 4038 | s->flags |= SN_ERR_PRXCOND; |
| 4039 | if (!(s->flags & SN_FINST_MASK)) |
| 4040 | s->flags |= SN_FINST_R; |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 4041 | buffer_stop_hijack(s->rep); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 4042 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4043 | } |
| 4044 | |
| 4045 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4046 | /* Iterate the same filter through all request headers. |
| 4047 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4048 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 4049 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4050 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4051 | 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] | 4052 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4053 | char term; |
| 4054 | char *cur_ptr, *cur_end, *cur_next; |
| 4055 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4056 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4057 | struct hdr_idx_elem *cur_hdr; |
| 4058 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 4059 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4060 | last_hdr = 0; |
| 4061 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4062 | 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] | 4063 | old_idx = 0; |
| 4064 | |
| 4065 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4066 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4067 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4068 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4069 | (exp->action == ACT_ALLOW || |
| 4070 | exp->action == ACT_DENY || |
| 4071 | exp->action == ACT_TARPIT)) |
| 4072 | return 0; |
| 4073 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4074 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4075 | if (!cur_idx) |
| 4076 | break; |
| 4077 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4078 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4079 | cur_ptr = cur_next; |
| 4080 | cur_end = cur_ptr + cur_hdr->len; |
| 4081 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4082 | |
| 4083 | /* Now we have one header between cur_ptr and cur_end, |
| 4084 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4085 | */ |
| 4086 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4087 | /* The annoying part is that pattern matching needs |
| 4088 | * that we modify the contents to null-terminate all |
| 4089 | * strings before testing them. |
| 4090 | */ |
| 4091 | |
| 4092 | term = *cur_end; |
| 4093 | *cur_end = '\0'; |
| 4094 | |
| 4095 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4096 | switch (exp->action) { |
| 4097 | case ACT_SETBE: |
| 4098 | /* It is not possible to jump a second time. |
| 4099 | * FIXME: should we return an HTTP/500 here so that |
| 4100 | * the admin knows there's a problem ? |
| 4101 | */ |
| 4102 | if (t->be != t->fe) |
| 4103 | break; |
| 4104 | |
| 4105 | /* Swithing Proxy */ |
| 4106 | t->be = (struct proxy *) exp->replace; |
| 4107 | |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4108 | /* right now, the backend switch is not overly complicated |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4109 | * because we have associated req_cap and rsp_cap to the |
| 4110 | * frontend, and the beconn will be updated later. |
| 4111 | */ |
| 4112 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 4113 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 4114 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 4115 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4116 | last_hdr = 1; |
| 4117 | break; |
| 4118 | |
| 4119 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4120 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4121 | last_hdr = 1; |
| 4122 | break; |
| 4123 | |
| 4124 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4125 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4126 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4127 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4128 | break; |
| 4129 | |
| 4130 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4131 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4132 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4133 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4134 | break; |
| 4135 | |
| 4136 | case ACT_REPLACE: |
| 4137 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4138 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 4139 | /* FIXME: if the user adds a newline in the replacement, the |
| 4140 | * index will not be recalculated for now, and the new line |
| 4141 | * will not be counted as a new header. |
| 4142 | */ |
| 4143 | |
| 4144 | cur_end += delta; |
| 4145 | cur_next += delta; |
| 4146 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4147 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4148 | break; |
| 4149 | |
| 4150 | case ACT_REMOVE: |
| 4151 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 4152 | cur_next += delta; |
| 4153 | |
| 4154 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4155 | txn->req.eoh += delta; |
| 4156 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4157 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4158 | cur_hdr->len = 0; |
| 4159 | cur_end = NULL; /* null-term has been rewritten */ |
| 4160 | break; |
| 4161 | |
| 4162 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4163 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4164 | if (cur_end) |
| 4165 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4166 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4167 | /* keep the link from this header to next one in case of later |
| 4168 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4169 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4170 | old_idx = cur_idx; |
| 4171 | } |
| 4172 | return 0; |
| 4173 | } |
| 4174 | |
| 4175 | |
| 4176 | /* Apply the filter to the request line. |
| 4177 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 4178 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4179 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 4180 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4181 | */ |
| 4182 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 4183 | { |
| 4184 | char term; |
| 4185 | char *cur_ptr, *cur_end; |
| 4186 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4187 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4188 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4189 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4190 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4191 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4192 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4193 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4194 | (exp->action == ACT_ALLOW || |
| 4195 | exp->action == ACT_DENY || |
| 4196 | exp->action == ACT_TARPIT)) |
| 4197 | return 0; |
| 4198 | else if (exp->action == ACT_REMOVE) |
| 4199 | return 0; |
| 4200 | |
| 4201 | done = 0; |
| 4202 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4203 | 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] | 4204 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4205 | |
| 4206 | /* Now we have the request line between cur_ptr and cur_end */ |
| 4207 | |
| 4208 | /* The annoying part is that pattern matching needs |
| 4209 | * that we modify the contents to null-terminate all |
| 4210 | * strings before testing them. |
| 4211 | */ |
| 4212 | |
| 4213 | term = *cur_end; |
| 4214 | *cur_end = '\0'; |
| 4215 | |
| 4216 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4217 | switch (exp->action) { |
| 4218 | case ACT_SETBE: |
| 4219 | /* It is not possible to jump a second time. |
| 4220 | * FIXME: should we return an HTTP/500 here so that |
| 4221 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4222 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4223 | if (t->be != t->fe) |
| 4224 | break; |
| 4225 | |
| 4226 | /* Swithing Proxy */ |
| 4227 | t->be = (struct proxy *) exp->replace; |
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 | /* right now, the backend switch is not too much complicated |
| 4230 | * because we have associated req_cap and rsp_cap to the |
| 4231 | * frontend, and the beconn will be updated later. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4232 | */ |
| 4233 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 4234 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 4235 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 4236 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4237 | done = 1; |
| 4238 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4239 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4240 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4241 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4242 | done = 1; |
| 4243 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4244 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4245 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4246 | txn->flags |= TX_CLDENY; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4247 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4248 | done = 1; |
| 4249 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4250 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4251 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4252 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4253 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4254 | done = 1; |
| 4255 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4256 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4257 | case ACT_REPLACE: |
| 4258 | *cur_end = term; /* restore the string terminator */ |
| 4259 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4260 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 4261 | /* FIXME: if the user adds a newline in the replacement, the |
| 4262 | * index will not be recalculated for now, and the new line |
| 4263 | * will not be counted as a new header. |
| 4264 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4265 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4266 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4267 | cur_end += delta; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4268 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4269 | 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] | 4270 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4271 | HTTP_MSG_RQMETH, |
| 4272 | cur_ptr, cur_end + 1, |
| 4273 | NULL, NULL); |
| 4274 | if (unlikely(!cur_end)) |
| 4275 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4276 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4277 | /* we have a full request and we know that we have either a CR |
| 4278 | * or an LF at <ptr>. |
| 4279 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4280 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 4281 | 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] | 4282 | /* there is no point trying this regex on headers */ |
| 4283 | return 1; |
| 4284 | } |
| 4285 | } |
| 4286 | *cur_end = term; /* restore the string terminator */ |
| 4287 | return done; |
| 4288 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 4289 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4290 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4291 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4292 | /* |
| 4293 | * Apply all the req filters <exp> to all headers in buffer <req> of session <t>. |
| 4294 | * 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] | 4295 | * unparsable request. Since it can manage the switch to another backend, it |
| 4296 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4297 | */ |
| 4298 | int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 4299 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4300 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4301 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4302 | while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4303 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4304 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4305 | /* |
| 4306 | * The interleaving of transformations and verdicts |
| 4307 | * makes it difficult to decide to continue or stop |
| 4308 | * the evaluation. |
| 4309 | */ |
| 4310 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4311 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4312 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 4313 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) { |
| 4314 | exp = exp->next; |
| 4315 | continue; |
| 4316 | } |
| 4317 | |
| 4318 | /* Apply the filter to the request line. */ |
| 4319 | ret = apply_filter_to_req_line(t, req, exp); |
| 4320 | if (unlikely(ret < 0)) |
| 4321 | return -1; |
| 4322 | |
| 4323 | if (likely(ret == 0)) { |
| 4324 | /* The filter did not match the request, it can be |
| 4325 | * iterated through all headers. |
| 4326 | */ |
| 4327 | apply_filter_to_req_headers(t, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4328 | } |
| 4329 | exp = exp->next; |
| 4330 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4331 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4332 | } |
| 4333 | |
| 4334 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4335 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4336 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 4337 | * Manage client-side cookie. It can impact performance by about 2% so it is |
| 4338 | * desirable to call it only when needed. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4339 | */ |
| 4340 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 4341 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4342 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4343 | char *p1, *p2, *p3, *p4; |
| 4344 | char *del_colon, *del_cookie, *colon; |
| 4345 | int app_cookies; |
| 4346 | |
| 4347 | appsess *asession_temp = NULL; |
| 4348 | appsess local_asession; |
| 4349 | |
| 4350 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4351 | int cur_idx, old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4352 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4353 | /* Iterate through the headers. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4354 | * we start with the start line. |
| 4355 | */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 4356 | old_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4357 | 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] | 4358 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4359 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4360 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4361 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4362 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4363 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4364 | cur_ptr = cur_next; |
| 4365 | cur_end = cur_ptr + cur_hdr->len; |
| 4366 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4367 | |
| 4368 | /* We have one full header between cur_ptr and cur_end, and the |
| 4369 | * next header starts at cur_next. We're only interested in |
| 4370 | * "Cookie:" headers. |
| 4371 | */ |
| 4372 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4373 | val = http_header_match2(cur_ptr, cur_end, "Cookie", 6); |
| 4374 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4375 | old_idx = cur_idx; |
| 4376 | continue; |
| 4377 | } |
| 4378 | |
| 4379 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 4380 | * attributes whose name begin with a '$', and associate them with |
| 4381 | * the right cookie, if we want to delete this cookie. |
| 4382 | * So there are 3 cases for each cookie read : |
| 4383 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 4384 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 4385 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 4386 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 4387 | * "special" cookie. |
| 4388 | * At the end of loop, if a "special" cookie remains, we may have to |
| 4389 | * remove it. If no application cookie persists in the header, we |
| 4390 | * *MUST* delete it |
| 4391 | */ |
| 4392 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4393 | colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4394 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4395 | /* del_cookie == NULL => nothing to be deleted */ |
| 4396 | del_colon = del_cookie = NULL; |
| 4397 | app_cookies = 0; |
| 4398 | |
| 4399 | while (p1 < cur_end) { |
| 4400 | /* skip spaces and colons, but keep an eye on these ones */ |
| 4401 | while (p1 < cur_end) { |
| 4402 | if (*p1 == ';' || *p1 == ',') |
| 4403 | colon = p1; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4404 | else if (!isspace((unsigned char)*p1)) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4405 | break; |
| 4406 | p1++; |
| 4407 | } |
| 4408 | |
| 4409 | if (p1 == cur_end) |
| 4410 | break; |
| 4411 | |
| 4412 | /* p1 is at the beginning of the cookie name */ |
| 4413 | p2 = p1; |
| 4414 | while (p2 < cur_end && *p2 != '=') |
| 4415 | p2++; |
| 4416 | |
| 4417 | if (p2 == cur_end) |
| 4418 | break; |
| 4419 | |
| 4420 | p3 = p2 + 1; /* skips the '=' sign */ |
| 4421 | if (p3 == cur_end) |
| 4422 | break; |
| 4423 | |
| 4424 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4425 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4426 | p4++; |
| 4427 | |
| 4428 | /* here, we have the cookie name between p1 and p2, |
| 4429 | * and its value between p3 and p4. |
| 4430 | * we can process it : |
| 4431 | * |
| 4432 | * Cookie: NAME=VALUE; |
| 4433 | * | || || | |
| 4434 | * | || || +--> p4 |
| 4435 | * | || |+-------> p3 |
| 4436 | * | || +--------> p2 |
| 4437 | * | |+------------> p1 |
| 4438 | * | +-------------> colon |
| 4439 | * +--------------------> cur_ptr |
| 4440 | */ |
| 4441 | |
| 4442 | if (*p1 == '$') { |
| 4443 | /* skip this one */ |
| 4444 | } |
| 4445 | else { |
| 4446 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4447 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4448 | txn->cli_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4449 | (p4 - p1 >= t->fe->capture_namelen) && |
| 4450 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4451 | int log_len = p4 - p1; |
| 4452 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 4453 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4454 | Alert("HTTP logging : out of memory.\n"); |
| 4455 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4456 | if (log_len > t->fe->capture_len) |
| 4457 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4458 | memcpy(txn->cli_cookie, p1, log_len); |
| 4459 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4460 | } |
| 4461 | } |
| 4462 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4463 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 4464 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4465 | /* Cool... it's the right one */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4466 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4467 | char *delim; |
| 4468 | |
| 4469 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 4470 | * have the server ID betweek p3 and delim, and the original cookie between |
| 4471 | * delim+1 and p4. Otherwise, delim==p4 : |
| 4472 | * |
| 4473 | * Cookie: NAME=SRV~VALUE; |
| 4474 | * | || || | | |
| 4475 | * | || || | +--> p4 |
| 4476 | * | || || +--------> delim |
| 4477 | * | || |+-----------> p3 |
| 4478 | * | || +------------> p2 |
| 4479 | * | |+----------------> p1 |
| 4480 | * | +-----------------> colon |
| 4481 | * +------------------------> cur_ptr |
| 4482 | */ |
| 4483 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4484 | if (t->be->options & PR_O_COOK_PFX) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4485 | for (delim = p3; delim < p4; delim++) |
| 4486 | if (*delim == COOKIE_DELIM) |
| 4487 | break; |
| 4488 | } |
| 4489 | else |
| 4490 | delim = p4; |
| 4491 | |
| 4492 | |
| 4493 | /* Here, we'll look for the first running server which supports the cookie. |
| 4494 | * This allows to share a same cookie between several servers, for example |
| 4495 | * to dedicate backup servers to specific servers only. |
| 4496 | * However, to prevent clients from sticking to cookie-less backup server |
| 4497 | * when they have incidentely learned an empty cookie, we simply ignore |
| 4498 | * empty cookies and mark them as invalid. |
| 4499 | */ |
| 4500 | if (delim == p3) |
| 4501 | srv = NULL; |
| 4502 | |
| 4503 | while (srv) { |
Willy Tarreau | 92f2ab1 | 2007-02-02 22:14:47 +0100 | [diff] [blame] | 4504 | if (srv->cookie && (srv->cklen == delim - p3) && |
| 4505 | !memcmp(p3, srv->cookie, delim - p3)) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4506 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4507 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4508 | txn->flags &= ~TX_CK_MASK; |
| 4509 | txn->flags |= TX_CK_VALID; |
| 4510 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4511 | t->srv = srv; |
| 4512 | break; |
| 4513 | } else { |
| 4514 | /* we found a server, but it's down */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4515 | txn->flags &= ~TX_CK_MASK; |
| 4516 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4517 | } |
| 4518 | } |
| 4519 | srv = srv->next; |
| 4520 | } |
| 4521 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4522 | if (!srv && !(txn->flags & TX_CK_DOWN)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4523 | /* no server matched this cookie */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4524 | txn->flags &= ~TX_CK_MASK; |
| 4525 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4526 | } |
| 4527 | |
| 4528 | /* depending on the cookie mode, we may have to either : |
| 4529 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 4530 | * the server never sees it ; |
| 4531 | * - remove the server id from the cookie value, and tag the cookie as an |
| 4532 | * application cookie so that it does not get accidentely removed later, |
| 4533 | * if we're in cookie prefix mode |
| 4534 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4535 | if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4536 | int delta; /* negative */ |
| 4537 | |
| 4538 | delta = buffer_replace2(req, p3, delim + 1, NULL, 0); |
| 4539 | p4 += delta; |
| 4540 | cur_end += delta; |
| 4541 | cur_next += delta; |
| 4542 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4543 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4544 | |
| 4545 | del_cookie = del_colon = NULL; |
| 4546 | app_cookies++; /* protect the header from deletion */ |
| 4547 | } |
| 4548 | else if (del_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4549 | (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] | 4550 | del_cookie = p1; |
| 4551 | del_colon = colon; |
| 4552 | } |
| 4553 | } else { |
| 4554 | /* now we know that we must keep this cookie since it's |
| 4555 | * not ours. But if we wanted to delete our cookie |
| 4556 | * earlier, we cannot remove the complete header, but we |
| 4557 | * can remove the previous block itself. |
| 4558 | */ |
| 4559 | app_cookies++; |
| 4560 | |
| 4561 | if (del_cookie != NULL) { |
| 4562 | int delta; /* negative */ |
| 4563 | |
| 4564 | delta = buffer_replace2(req, del_cookie, p1, NULL, 0); |
| 4565 | p4 += delta; |
| 4566 | cur_end += delta; |
| 4567 | cur_next += delta; |
| 4568 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4569 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4570 | del_cookie = del_colon = NULL; |
| 4571 | } |
| 4572 | } |
| 4573 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4574 | if ((t->be->appsession_name != NULL) && |
| 4575 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4576 | /* first, let's see if the cookie is our appcookie*/ |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4577 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4578 | /* Cool... it's the right one */ |
| 4579 | |
| 4580 | asession_temp = &local_asession; |
| 4581 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4582 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4583 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 4584 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 4585 | return; |
| 4586 | } |
| 4587 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4588 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 4589 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4590 | asession_temp->serverid = NULL; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4591 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4592 | /* only do insert, if lookup fails */ |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4593 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 4594 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4595 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4596 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4597 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4598 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 4599 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 4600 | return; |
| 4601 | } |
| 4602 | |
| 4603 | asession_temp->sessid = local_asession.sessid; |
| 4604 | asession_temp->serverid = local_asession.serverid; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4605 | asession_temp->request_count = 0; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4606 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4607 | } else { |
| 4608 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4609 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4610 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4611 | if (asession_temp->serverid == NULL) { |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4612 | /* TODO redispatch request */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4613 | Alert("Found Application Session without matching server.\n"); |
| 4614 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4615 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4616 | while (srv) { |
| 4617 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4618 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4619 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4620 | txn->flags &= ~TX_CK_MASK; |
| 4621 | txn->flags |= TX_CK_VALID; |
| 4622 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4623 | t->srv = srv; |
| 4624 | break; |
| 4625 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4626 | txn->flags &= ~TX_CK_MASK; |
| 4627 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4628 | } |
| 4629 | } |
| 4630 | srv = srv->next; |
| 4631 | }/* end while(srv) */ |
| 4632 | }/* end else if server == NULL */ |
| 4633 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 4634 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4635 | asession_temp->request_count++; |
| 4636 | #if defined(DEBUG_HASH) |
| 4637 | Alert("manage_client_side_cookies\n"); |
| 4638 | appsession_hash_dump(&(t->be->htbl_proxy)); |
| 4639 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4640 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 4641 | } |
| 4642 | |
| 4643 | /* we'll have to look for another cookie ... */ |
| 4644 | p1 = p4; |
| 4645 | } /* while (p1 < cur_end) */ |
| 4646 | |
| 4647 | /* There's no more cookie on this line. |
| 4648 | * We may have marked the last one(s) for deletion. |
| 4649 | * We must do this now in two ways : |
| 4650 | * - if there is no app cookie, we simply delete the header ; |
| 4651 | * - if there are app cookies, we must delete the end of the |
| 4652 | * string properly, including the colon/semi-colon before |
| 4653 | * the cookie name. |
| 4654 | */ |
| 4655 | if (del_cookie != NULL) { |
| 4656 | int delta; |
| 4657 | if (app_cookies) { |
| 4658 | delta = buffer_replace2(req, del_colon, cur_end, NULL, 0); |
| 4659 | cur_end = del_colon; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4660 | cur_hdr->len += delta; |
| 4661 | } else { |
| 4662 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4663 | |
| 4664 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4665 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4666 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4667 | cur_hdr->len = 0; |
| 4668 | } |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 4669 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4670 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4671 | } |
| 4672 | |
| 4673 | /* keep the link from this header to next one */ |
| 4674 | old_idx = cur_idx; |
| 4675 | } /* end of cookie processing on this header */ |
| 4676 | } |
| 4677 | |
| 4678 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4679 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 4680 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 4681 | */ |
| 4682 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4683 | { |
| 4684 | char term; |
| 4685 | char *cur_ptr, *cur_end, *cur_next; |
| 4686 | int cur_idx, old_idx, last_hdr; |
| 4687 | struct http_txn *txn = &t->txn; |
| 4688 | struct hdr_idx_elem *cur_hdr; |
| 4689 | int len, delta; |
| 4690 | |
| 4691 | last_hdr = 0; |
| 4692 | |
| 4693 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4694 | old_idx = 0; |
| 4695 | |
| 4696 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4697 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4698 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4699 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4700 | (exp->action == ACT_ALLOW || |
| 4701 | exp->action == ACT_DENY)) |
| 4702 | return 0; |
| 4703 | |
| 4704 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 4705 | if (!cur_idx) |
| 4706 | break; |
| 4707 | |
| 4708 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4709 | cur_ptr = cur_next; |
| 4710 | cur_end = cur_ptr + cur_hdr->len; |
| 4711 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4712 | |
| 4713 | /* Now we have one header between cur_ptr and cur_end, |
| 4714 | * and the next header starts at cur_next. |
| 4715 | */ |
| 4716 | |
| 4717 | /* The annoying part is that pattern matching needs |
| 4718 | * that we modify the contents to null-terminate all |
| 4719 | * strings before testing them. |
| 4720 | */ |
| 4721 | |
| 4722 | term = *cur_end; |
| 4723 | *cur_end = '\0'; |
| 4724 | |
| 4725 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4726 | switch (exp->action) { |
| 4727 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4728 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4729 | last_hdr = 1; |
| 4730 | break; |
| 4731 | |
| 4732 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4733 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4734 | last_hdr = 1; |
| 4735 | break; |
| 4736 | |
| 4737 | case ACT_REPLACE: |
| 4738 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4739 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 4740 | /* FIXME: if the user adds a newline in the replacement, the |
| 4741 | * index will not be recalculated for now, and the new line |
| 4742 | * will not be counted as a new header. |
| 4743 | */ |
| 4744 | |
| 4745 | cur_end += delta; |
| 4746 | cur_next += delta; |
| 4747 | cur_hdr->len += delta; |
| 4748 | txn->rsp.eoh += delta; |
| 4749 | break; |
| 4750 | |
| 4751 | case ACT_REMOVE: |
| 4752 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 4753 | cur_next += delta; |
| 4754 | |
| 4755 | /* FIXME: this should be a separate function */ |
| 4756 | txn->rsp.eoh += delta; |
| 4757 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4758 | txn->hdr_idx.used--; |
| 4759 | cur_hdr->len = 0; |
| 4760 | cur_end = NULL; /* null-term has been rewritten */ |
| 4761 | break; |
| 4762 | |
| 4763 | } |
| 4764 | } |
| 4765 | if (cur_end) |
| 4766 | *cur_end = term; /* restore the string terminator */ |
| 4767 | |
| 4768 | /* keep the link from this header to next one in case of later |
| 4769 | * removal of next header. |
| 4770 | */ |
| 4771 | old_idx = cur_idx; |
| 4772 | } |
| 4773 | return 0; |
| 4774 | } |
| 4775 | |
| 4776 | |
| 4777 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 4778 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 4779 | * or -1 if a replacement resulted in an invalid status line. |
| 4780 | */ |
| 4781 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4782 | { |
| 4783 | char term; |
| 4784 | char *cur_ptr, *cur_end; |
| 4785 | int done; |
| 4786 | struct http_txn *txn = &t->txn; |
| 4787 | int len, delta; |
| 4788 | |
| 4789 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4790 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4791 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4792 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4793 | (exp->action == ACT_ALLOW || |
| 4794 | exp->action == ACT_DENY)) |
| 4795 | return 0; |
| 4796 | else if (exp->action == ACT_REMOVE) |
| 4797 | return 0; |
| 4798 | |
| 4799 | done = 0; |
| 4800 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4801 | 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] | 4802 | cur_end = cur_ptr + txn->rsp.sl.rq.l; |
| 4803 | |
| 4804 | /* Now we have the status line between cur_ptr and cur_end */ |
| 4805 | |
| 4806 | /* The annoying part is that pattern matching needs |
| 4807 | * that we modify the contents to null-terminate all |
| 4808 | * strings before testing them. |
| 4809 | */ |
| 4810 | |
| 4811 | term = *cur_end; |
| 4812 | *cur_end = '\0'; |
| 4813 | |
| 4814 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4815 | switch (exp->action) { |
| 4816 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4817 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4818 | done = 1; |
| 4819 | break; |
| 4820 | |
| 4821 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4822 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4823 | done = 1; |
| 4824 | break; |
| 4825 | |
| 4826 | case ACT_REPLACE: |
| 4827 | *cur_end = term; /* restore the string terminator */ |
| 4828 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4829 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 4830 | /* FIXME: if the user adds a newline in the replacement, the |
| 4831 | * index will not be recalculated for now, and the new line |
| 4832 | * will not be counted as a new header. |
| 4833 | */ |
| 4834 | |
| 4835 | txn->rsp.eoh += delta; |
| 4836 | cur_end += delta; |
| 4837 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4838 | 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] | 4839 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 4840 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4841 | cur_ptr, cur_end + 1, |
| 4842 | NULL, NULL); |
| 4843 | if (unlikely(!cur_end)) |
| 4844 | return -1; |
| 4845 | |
| 4846 | /* we have a full respnse and we know that we have either a CR |
| 4847 | * or an LF at <ptr>. |
| 4848 | */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4849 | 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] | 4850 | hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r'); |
| 4851 | /* there is no point trying this regex on headers */ |
| 4852 | return 1; |
| 4853 | } |
| 4854 | } |
| 4855 | *cur_end = term; /* restore the string terminator */ |
| 4856 | return done; |
| 4857 | } |
| 4858 | |
| 4859 | |
| 4860 | |
| 4861 | /* |
| 4862 | * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>. |
| 4863 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 4864 | * unparsable response. |
| 4865 | */ |
| 4866 | int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4867 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4868 | struct http_txn *txn = &t->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4869 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4870 | while (exp && !(txn->flags & TX_SVDENY)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4871 | int ret; |
| 4872 | |
| 4873 | /* |
| 4874 | * The interleaving of transformations and verdicts |
| 4875 | * makes it difficult to decide to continue or stop |
| 4876 | * the evaluation. |
| 4877 | */ |
| 4878 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4879 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4880 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 4881 | exp->action == ACT_PASS)) { |
| 4882 | exp = exp->next; |
| 4883 | continue; |
| 4884 | } |
| 4885 | |
| 4886 | /* Apply the filter to the status line. */ |
| 4887 | ret = apply_filter_to_sts_line(t, rtr, exp); |
| 4888 | if (unlikely(ret < 0)) |
| 4889 | return -1; |
| 4890 | |
| 4891 | if (likely(ret == 0)) { |
| 4892 | /* The filter did not match the response, it can be |
| 4893 | * iterated through all headers. |
| 4894 | */ |
| 4895 | apply_filter_to_resp_headers(t, rtr, exp); |
| 4896 | } |
| 4897 | exp = exp->next; |
| 4898 | } |
| 4899 | return 0; |
| 4900 | } |
| 4901 | |
| 4902 | |
| 4903 | |
| 4904 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 4905 | * Manage server-side cookies. It can impact performance by about 2% so it is |
| 4906 | * desirable to call it only when needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4907 | */ |
| 4908 | void manage_server_side_cookies(struct session *t, struct buffer *rtr) |
| 4909 | { |
| 4910 | struct http_txn *txn = &t->txn; |
| 4911 | char *p1, *p2, *p3, *p4; |
| 4912 | |
| 4913 | appsess *asession_temp = NULL; |
| 4914 | appsess local_asession; |
| 4915 | |
| 4916 | char *cur_ptr, *cur_end, *cur_next; |
| 4917 | int cur_idx, old_idx, delta; |
| 4918 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4919 | /* Iterate through the headers. |
| 4920 | * we start with the start line. |
| 4921 | */ |
| 4922 | old_idx = 0; |
| 4923 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4924 | |
| 4925 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 4926 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4927 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4928 | |
| 4929 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4930 | cur_ptr = cur_next; |
| 4931 | cur_end = cur_ptr + cur_hdr->len; |
| 4932 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4933 | |
| 4934 | /* We have one full header between cur_ptr and cur_end, and the |
| 4935 | * next header starts at cur_next. We're only interested in |
| 4936 | * "Cookie:" headers. |
| 4937 | */ |
| 4938 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4939 | val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10); |
| 4940 | if (!val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4941 | old_idx = cur_idx; |
| 4942 | continue; |
| 4943 | } |
| 4944 | |
| 4945 | /* 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] | 4946 | txn->flags |= TX_SCK_ANY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4947 | |
| 4948 | |
| 4949 | /* maybe we only wanted to see if there was a set-cookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4950 | if (t->be->cookie_name == NULL && |
| 4951 | t->be->appsession_name == NULL && |
| 4952 | t->be->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4953 | return; |
| 4954 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4955 | p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4956 | |
| 4957 | 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] | 4958 | if (p1 == cur_end || *p1 == ';') /* end of cookie */ |
| 4959 | break; |
| 4960 | |
| 4961 | /* p1 is at the beginning of the cookie name */ |
| 4962 | p2 = p1; |
| 4963 | |
| 4964 | while (p2 < cur_end && *p2 != '=' && *p2 != ';') |
| 4965 | p2++; |
| 4966 | |
| 4967 | if (p2 == cur_end || *p2 == ';') /* next cookie */ |
| 4968 | break; |
| 4969 | |
| 4970 | p3 = p2 + 1; /* skip the '=' sign */ |
| 4971 | if (p3 == cur_end) |
| 4972 | break; |
| 4973 | |
| 4974 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4975 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';') |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4976 | p4++; |
| 4977 | |
| 4978 | /* here, we have the cookie name between p1 and p2, |
| 4979 | * and its value between p3 and p4. |
| 4980 | * we can process it. |
| 4981 | */ |
| 4982 | |
| 4983 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4984 | if (t->be->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4985 | txn->srv_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4986 | (p4 - p1 >= t->be->capture_namelen) && |
| 4987 | memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4988 | int log_len = p4 - p1; |
| 4989 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 4990 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4991 | Alert("HTTP logging : out of memory.\n"); |
| 4992 | } |
| 4993 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4994 | if (log_len > t->be->capture_len) |
| 4995 | log_len = t->be->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4996 | memcpy(txn->srv_cookie, p1, log_len); |
| 4997 | txn->srv_cookie[log_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4998 | } |
| 4999 | |
| 5000 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5001 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 5002 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5003 | /* Cool... it's the right one */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5004 | txn->flags |= TX_SCK_SEEN; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5005 | |
| 5006 | /* If the cookie is in insert mode on a known server, we'll delete |
| 5007 | * this occurrence because we'll insert another one later. |
| 5008 | * We'll delete it too if the "indirect" option is set and we're in |
| 5009 | * a direct access. */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5010 | if (((t->srv) && (t->be->options & PR_O_COOK_INS)) || |
| 5011 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5012 | /* this header must be deleted */ |
| 5013 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 5014 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5015 | txn->hdr_idx.used--; |
| 5016 | cur_hdr->len = 0; |
| 5017 | cur_next += delta; |
| 5018 | txn->rsp.eoh += delta; |
| 5019 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5020 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5021 | } |
| 5022 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5023 | (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5024 | /* replace bytes p3->p4 with the cookie name associated |
| 5025 | * with this server since we know it. |
| 5026 | */ |
| 5027 | delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen); |
| 5028 | cur_hdr->len += delta; |
| 5029 | cur_next += delta; |
| 5030 | txn->rsp.eoh += delta; |
| 5031 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5032 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5033 | } |
| 5034 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5035 | (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5036 | /* insert the cookie name associated with this server |
| 5037 | * before existing cookie, and insert a delimitor between them.. |
| 5038 | */ |
| 5039 | delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1); |
| 5040 | cur_hdr->len += delta; |
| 5041 | cur_next += delta; |
| 5042 | txn->rsp.eoh += delta; |
| 5043 | |
| 5044 | p3[t->srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5045 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5046 | } |
| 5047 | } |
| 5048 | /* next, let's see if the cookie is our appcookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5049 | else if ((t->be->appsession_name != NULL) && |
| 5050 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5051 | |
| 5052 | /* Cool... it's the right one */ |
| 5053 | |
| 5054 | size_t server_id_len = strlen(t->srv->id) + 1; |
| 5055 | asession_temp = &local_asession; |
| 5056 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5057 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5058 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5059 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5060 | return; |
| 5061 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5062 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 5063 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5064 | asession_temp->serverid = NULL; |
| 5065 | |
| 5066 | /* only do insert, if lookup fails */ |
Ryan Warnick | 6d0b1fa | 2008-02-17 11:24:35 +0100 | [diff] [blame] | 5067 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 5068 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5069 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5070 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 5071 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 5072 | return; |
| 5073 | } |
| 5074 | asession_temp->sessid = local_asession.sessid; |
| 5075 | asession_temp->serverid = local_asession.serverid; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5076 | asession_temp->request_count = 0; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5077 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
| 5078 | } else { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5079 | /* free wasted memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5080 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5081 | } |
| 5082 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5083 | if (asession_temp->serverid == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5084 | if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5085 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5086 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5087 | return; |
| 5088 | } |
| 5089 | asession_temp->serverid[0] = '\0'; |
| 5090 | } |
| 5091 | |
| 5092 | if (asession_temp->serverid[0] == '\0') |
| 5093 | memcpy(asession_temp->serverid, t->srv->id, server_id_len); |
| 5094 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 5095 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5096 | asession_temp->request_count++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5097 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5098 | Alert("manage_server_side_cookies\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5099 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5100 | #endif |
| 5101 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 5102 | break; /* we don't want to loop again since there cannot be another cookie on the same line */ |
| 5103 | } /* we're now at the end of the cookie value */ |
| 5104 | |
| 5105 | /* keep the link from this header to next one */ |
| 5106 | old_idx = cur_idx; |
| 5107 | } /* end of cookie processing on this header */ |
| 5108 | } |
| 5109 | |
| 5110 | |
| 5111 | |
| 5112 | /* |
| 5113 | * Check if response is cacheable or not. Updates t->flags. |
| 5114 | */ |
| 5115 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 5116 | { |
| 5117 | struct http_txn *txn = &t->txn; |
| 5118 | char *p1, *p2; |
| 5119 | |
| 5120 | char *cur_ptr, *cur_end, *cur_next; |
| 5121 | int cur_idx; |
| 5122 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 5123 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5124 | return; |
| 5125 | |
| 5126 | /* Iterate through the headers. |
| 5127 | * we start with the start line. |
| 5128 | */ |
| 5129 | cur_idx = 0; |
| 5130 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 5131 | |
| 5132 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 5133 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5134 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5135 | |
| 5136 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 5137 | cur_ptr = cur_next; |
| 5138 | cur_end = cur_ptr + cur_hdr->len; |
| 5139 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5140 | |
| 5141 | /* We have one full header between cur_ptr and cur_end, and the |
| 5142 | * next header starts at cur_next. We're only interested in |
| 5143 | * "Cookie:" headers. |
| 5144 | */ |
| 5145 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5146 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 5147 | if (val) { |
| 5148 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 5149 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 5150 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 5151 | return; |
| 5152 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5153 | } |
| 5154 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5155 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 5156 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5157 | continue; |
| 5158 | |
| 5159 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 5160 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5161 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5162 | |
| 5163 | if (p1 >= cur_end) /* no more info */ |
| 5164 | continue; |
| 5165 | |
| 5166 | /* p1 is at the beginning of the value */ |
| 5167 | p2 = p1; |
| 5168 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 5169 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5170 | p2++; |
| 5171 | |
| 5172 | /* we have a complete value between p1 and p2 */ |
| 5173 | if (p2 < cur_end && *p2 == '=') { |
| 5174 | /* we have something of the form no-cache="set-cookie" */ |
| 5175 | if ((cur_end - p1 >= 21) && |
| 5176 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 5177 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5178 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5179 | continue; |
| 5180 | } |
| 5181 | |
| 5182 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 5183 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 5184 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 5185 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 5186 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5187 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5188 | return; |
| 5189 | } |
| 5190 | |
| 5191 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5192 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5193 | continue; |
| 5194 | } |
| 5195 | } |
| 5196 | } |
| 5197 | |
| 5198 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5199 | /* |
| 5200 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 5201 | * If the server is found, it's assigned to the session. |
| 5202 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5203 | 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] | 5204 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5205 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5206 | appsess *asession_temp = NULL; |
| 5207 | appsess local_asession; |
| 5208 | char *request_line; |
| 5209 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5210 | if (t->be->appsession_name == NULL || |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 5211 | (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) || |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5212 | (request_line = memchr(begin, ';', len)) == NULL || |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5213 | ((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] | 5214 | return; |
| 5215 | |
| 5216 | /* skip ';' */ |
| 5217 | request_line++; |
| 5218 | |
| 5219 | /* look if we have a jsessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5220 | 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] | 5221 | return; |
| 5222 | |
| 5223 | /* skip jsessionid= */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5224 | request_line += t->be->appsession_name_len + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5225 | |
| 5226 | /* First try if we already have an appsession */ |
| 5227 | asession_temp = &local_asession; |
| 5228 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5229 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5230 | Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n"); |
| 5231 | send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n"); |
| 5232 | return; |
| 5233 | } |
| 5234 | |
| 5235 | /* Copy the sessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5236 | memcpy(asession_temp->sessid, request_line, t->be->appsession_len); |
| 5237 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5238 | asession_temp->serverid = NULL; |
| 5239 | |
| 5240 | /* only do insert, if lookup fails */ |
Ryan Warnick | 6d0b1fa | 2008-02-17 11:24:35 +0100 | [diff] [blame] | 5241 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 5242 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5243 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5244 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5245 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5246 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 5247 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 5248 | return; |
| 5249 | } |
| 5250 | asession_temp->sessid = local_asession.sessid; |
| 5251 | asession_temp->serverid = local_asession.serverid; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5252 | asession_temp->request_count=0; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5253 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5254 | } |
| 5255 | else { |
| 5256 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5257 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5258 | } |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5259 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 5260 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5261 | asession_temp->request_count++; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5262 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5263 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5264 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5265 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5266 | #endif |
| 5267 | if (asession_temp->serverid == NULL) { |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5268 | /* TODO redispatch request */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5269 | Alert("Found Application Session without matching server.\n"); |
| 5270 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5271 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5272 | while (srv) { |
| 5273 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5274 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5275 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5276 | txn->flags &= ~TX_CK_MASK; |
| 5277 | txn->flags |= TX_CK_VALID; |
| 5278 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5279 | t->srv = srv; |
| 5280 | break; |
| 5281 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5282 | txn->flags &= ~TX_CK_MASK; |
| 5283 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5284 | } |
| 5285 | } |
| 5286 | srv = srv->next; |
| 5287 | } |
| 5288 | } |
| 5289 | } |
| 5290 | |
| 5291 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5292 | /* |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5293 | * In a GET or HEAD request, check if the requested URI matches the stats uri |
| 5294 | * 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] | 5295 | * |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5296 | * 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] | 5297 | * 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] | 5298 | * produce_content() can be called to start sending data. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5299 | * |
| 5300 | * Returns 1 if the session's state changes, otherwise 0. |
| 5301 | */ |
| 5302 | int stats_check_uri_auth(struct session *t, struct proxy *backend) |
| 5303 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5304 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5305 | struct uri_auth *uri_auth = backend->uri_auth; |
| 5306 | struct user_auth *user; |
| 5307 | int authenticated, cur_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5308 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5309 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5310 | memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats)); |
| 5311 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5312 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5313 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5314 | return 0; |
| 5315 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5316 | h = t->req->data + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5317 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5318 | /* the URI is in h */ |
| 5319 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5320 | return 0; |
| 5321 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 5322 | h += uri_auth->uri_len; |
| 5323 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) { |
| 5324 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5325 | t->data_ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 5326 | break; |
| 5327 | } |
| 5328 | h++; |
| 5329 | } |
| 5330 | |
| 5331 | if (uri_auth->refresh) { |
| 5332 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 5333 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) { |
| 5334 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5335 | t->data_ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 5336 | break; |
| 5337 | } |
| 5338 | h++; |
| 5339 | } |
| 5340 | } |
| 5341 | |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 5342 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 5343 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) { |
| 5344 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5345 | t->data_ctx.stats.flags |= STAT_FMT_CSV; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 5346 | break; |
| 5347 | } |
| 5348 | h++; |
| 5349 | } |
| 5350 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5351 | t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO; |
| 5352 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5353 | /* we are in front of a interceptable URI. Let's check |
| 5354 | * if there's an authentication and if it's valid. |
| 5355 | */ |
| 5356 | user = uri_auth->users; |
| 5357 | if (!user) { |
| 5358 | /* no user auth required, it's OK */ |
| 5359 | authenticated = 1; |
| 5360 | } else { |
| 5361 | authenticated = 0; |
| 5362 | |
| 5363 | /* a user list is defined, we have to check. |
| 5364 | * skip 21 chars for "Authorization: Basic ". |
| 5365 | */ |
| 5366 | |
| 5367 | /* FIXME: this should move to an earlier place */ |
| 5368 | cur_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5369 | h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 5370 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 5371 | int len = txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5372 | if (len > 14 && |
| 5373 | !strncasecmp("Authorization:", h, 14)) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5374 | txn->auth_hdr.str = h; |
| 5375 | txn->auth_hdr.len = len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5376 | break; |
| 5377 | } |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5378 | h += len + txn->hdr_idx.v[cur_idx].cr + 1; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5379 | } |
| 5380 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5381 | if (txn->auth_hdr.len < 21 || |
| 5382 | memcmp(txn->auth_hdr.str + 14, " Basic ", 7)) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5383 | user = NULL; |
| 5384 | |
| 5385 | while (user) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5386 | if ((txn->auth_hdr.len == user->user_len + 14 + 7) |
| 5387 | && !memcmp(txn->auth_hdr.str + 14 + 7, |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5388 | user->user_pwd, user->user_len)) { |
| 5389 | authenticated = 1; |
| 5390 | break; |
| 5391 | } |
| 5392 | user = user->next; |
| 5393 | } |
| 5394 | } |
| 5395 | |
| 5396 | if (!authenticated) { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5397 | struct chunk msg; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5398 | |
| 5399 | /* no need to go further */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5400 | msg.str = trash; |
| 5401 | msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm); |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5402 | txn->status = 401; |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5403 | client_retnclose(t, &msg); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 5404 | trace_term(t, TT_HTTP_URI_1); |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 5405 | t->req->analysers = 0; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5406 | if (!(t->flags & SN_ERR_MASK)) |
| 5407 | t->flags |= SN_ERR_PRXCOND; |
| 5408 | if (!(t->flags & SN_FINST_MASK)) |
| 5409 | t->flags |= SN_FINST_R; |
| 5410 | return 1; |
| 5411 | } |
| 5412 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5413 | /* The request is valid, the user is authenticated. Let's start sending |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5414 | * data. |
| 5415 | */ |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 5416 | buffer_shutw_now(t->req); |
| 5417 | buffer_shutr_now(t->rep); |
| 5418 | buffer_start_hijack(t->rep); |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 5419 | t->logs.tv_request = now; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5420 | t->data_source = DATA_SRC_STATS; |
| 5421 | t->data_state = DATA_ST_INIT; |
Willy Tarreau | 91e9993 | 2008-06-30 07:51:00 +0200 | [diff] [blame] | 5422 | t->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5423 | produce_content(t); |
| 5424 | return 1; |
| 5425 | } |
| 5426 | |
| 5427 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5428 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5429 | * Print a debug line with a header |
| 5430 | */ |
| 5431 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 5432 | { |
| 5433 | int len, max; |
| 5434 | 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] | 5435 | 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] | 5436 | max = end - start; |
| 5437 | UBOUND(max, sizeof(trash) - len - 1); |
| 5438 | len += strlcpy2(trash + len, start, max + 1); |
| 5439 | trash[len++] = '\n'; |
| 5440 | write(1, trash, len); |
| 5441 | } |
| 5442 | |
| 5443 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5444 | /************************************************************************/ |
| 5445 | /* The code below is dedicated to ACL parsing and matching */ |
| 5446 | /************************************************************************/ |
| 5447 | |
| 5448 | |
| 5449 | |
| 5450 | |
| 5451 | /* 1. Check on METHOD |
| 5452 | * We use the pre-parsed method if it is known, and store its number as an |
| 5453 | * integer. If it is unknown, we use the pointer and the length. |
| 5454 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5455 | 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] | 5456 | { |
| 5457 | int len, meth; |
| 5458 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5459 | len = strlen(*text); |
| 5460 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5461 | |
| 5462 | pattern->val.i = meth; |
| 5463 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5464 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5465 | if (!pattern->ptr.str) |
| 5466 | return 0; |
| 5467 | pattern->len = len; |
| 5468 | } |
| 5469 | return 1; |
| 5470 | } |
| 5471 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5472 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5473 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5474 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5475 | { |
| 5476 | int meth; |
| 5477 | struct http_txn *txn = l7; |
| 5478 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5479 | if (!txn) |
| 5480 | return 0; |
| 5481 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5482 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5483 | return 0; |
| 5484 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5485 | meth = txn->meth; |
| 5486 | test->i = meth; |
| 5487 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5488 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5489 | /* ensure the indexes are not affected */ |
| 5490 | return 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5491 | test->len = txn->req.sl.rq.m_l; |
| 5492 | test->ptr = txn->req.sol; |
| 5493 | } |
| 5494 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5495 | return 1; |
| 5496 | } |
| 5497 | |
| 5498 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 5499 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 5500 | int icase; |
| 5501 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5502 | if (test->i != pattern->val.i) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5503 | return ACL_PAT_FAIL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5504 | |
| 5505 | if (test->i != HTTP_METH_OTHER) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5506 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5507 | |
| 5508 | /* Other method, we must compare the strings */ |
| 5509 | if (pattern->len != test->len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5510 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 5511 | |
| 5512 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 5513 | if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) || |
| 5514 | (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5515 | return ACL_PAT_FAIL; |
| 5516 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5517 | } |
| 5518 | |
| 5519 | /* 2. Check on Request/Status Version |
| 5520 | * We simply compare strings here. |
| 5521 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5522 | 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] | 5523 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5524 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5525 | if (!pattern->ptr.str) |
| 5526 | return 0; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5527 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5528 | return 1; |
| 5529 | } |
| 5530 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5531 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5532 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5533 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5534 | { |
| 5535 | struct http_txn *txn = l7; |
| 5536 | char *ptr; |
| 5537 | int len; |
| 5538 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5539 | if (!txn) |
| 5540 | return 0; |
| 5541 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5542 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5543 | return 0; |
| 5544 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5545 | len = txn->req.sl.rq.v_l; |
| 5546 | ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som; |
| 5547 | |
| 5548 | while ((len-- > 0) && (*ptr++ != '/')); |
| 5549 | if (len <= 0) |
| 5550 | return 0; |
| 5551 | |
| 5552 | test->ptr = ptr; |
| 5553 | test->len = len; |
| 5554 | |
| 5555 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5556 | return 1; |
| 5557 | } |
| 5558 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5559 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5560 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5561 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5562 | { |
| 5563 | struct http_txn *txn = l7; |
| 5564 | char *ptr; |
| 5565 | int len; |
| 5566 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5567 | if (!txn) |
| 5568 | return 0; |
| 5569 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5570 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5571 | return 0; |
| 5572 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5573 | len = txn->rsp.sl.st.v_l; |
| 5574 | ptr = txn->rsp.sol; |
| 5575 | |
| 5576 | while ((len-- > 0) && (*ptr++ != '/')); |
| 5577 | if (len <= 0) |
| 5578 | return 0; |
| 5579 | |
| 5580 | test->ptr = ptr; |
| 5581 | test->len = len; |
| 5582 | |
| 5583 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5584 | return 1; |
| 5585 | } |
| 5586 | |
| 5587 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5588 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5589 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5590 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5591 | { |
| 5592 | struct http_txn *txn = l7; |
| 5593 | char *ptr; |
| 5594 | int len; |
| 5595 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5596 | if (!txn) |
| 5597 | return 0; |
| 5598 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5599 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5600 | return 0; |
| 5601 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5602 | len = txn->rsp.sl.st.c_l; |
| 5603 | ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som; |
| 5604 | |
| 5605 | test->i = __strl2ui(ptr, len); |
| 5606 | test->flags = ACL_TEST_F_VOL_1ST; |
| 5607 | return 1; |
| 5608 | } |
| 5609 | |
| 5610 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5611 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5612 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5613 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5614 | { |
| 5615 | struct http_txn *txn = l7; |
| 5616 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5617 | if (!txn) |
| 5618 | return 0; |
| 5619 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5620 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5621 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5622 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5623 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5624 | /* ensure the indexes are not affected */ |
| 5625 | return 0; |
| 5626 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5627 | test->len = txn->req.sl.rq.u_l; |
| 5628 | test->ptr = txn->req.sol + txn->req.sl.rq.u; |
| 5629 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 5630 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 5631 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5632 | return 1; |
| 5633 | } |
| 5634 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5635 | static int |
| 5636 | acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5637 | struct acl_expr *expr, struct acl_test *test) |
| 5638 | { |
| 5639 | struct http_txn *txn = l7; |
| 5640 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5641 | if (!txn) |
| 5642 | return 0; |
| 5643 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5644 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5645 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5646 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5647 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5648 | /* ensure the indexes are not affected */ |
| 5649 | return 0; |
| 5650 | |
| 5651 | /* Parse HTTP request */ |
| 5652 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
| 5653 | test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr; |
| 5654 | test->i = AF_INET; |
| 5655 | |
| 5656 | /* |
| 5657 | * If we are parsing url in frontend space, we prepare backend stage |
| 5658 | * to not parse again the same url ! optimization lazyness... |
| 5659 | */ |
| 5660 | if (px->options & PR_O_HTTP_PROXY) |
| 5661 | l4->flags |= SN_ADDR_SET; |
| 5662 | |
| 5663 | test->flags = ACL_TEST_F_READ_ONLY; |
| 5664 | return 1; |
| 5665 | } |
| 5666 | |
| 5667 | static int |
| 5668 | acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5669 | struct acl_expr *expr, struct acl_test *test) |
| 5670 | { |
| 5671 | struct http_txn *txn = l7; |
| 5672 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5673 | if (!txn) |
| 5674 | return 0; |
| 5675 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5676 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5677 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5678 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5679 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5680 | /* ensure the indexes are not affected */ |
| 5681 | return 0; |
| 5682 | |
| 5683 | /* Same optimization as url_ip */ |
| 5684 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
| 5685 | test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port); |
| 5686 | |
| 5687 | if (px->options & PR_O_HTTP_PROXY) |
| 5688 | l4->flags |= SN_ADDR_SET; |
| 5689 | |
| 5690 | test->flags = ACL_TEST_F_READ_ONLY; |
| 5691 | return 1; |
| 5692 | } |
| 5693 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5694 | /* 5. Check on HTTP header. A pointer to the beginning of the value is returned. |
| 5695 | * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr(). |
| 5696 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5697 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5698 | 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] | 5699 | struct acl_expr *expr, struct acl_test *test) |
| 5700 | { |
| 5701 | struct http_txn *txn = l7; |
| 5702 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5703 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5704 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5705 | if (!txn) |
| 5706 | return 0; |
| 5707 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5708 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 5709 | /* search for header from the beginning */ |
| 5710 | ctx->idx = 0; |
| 5711 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5712 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 5713 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 5714 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5715 | test->len = ctx->vlen; |
| 5716 | test->ptr = (char *)ctx->line + ctx->val; |
| 5717 | return 1; |
| 5718 | } |
| 5719 | |
| 5720 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 5721 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5722 | return 0; |
| 5723 | } |
| 5724 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5725 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5726 | acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5727 | struct acl_expr *expr, struct acl_test *test) |
| 5728 | { |
| 5729 | struct http_txn *txn = l7; |
| 5730 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5731 | if (!txn) |
| 5732 | return 0; |
| 5733 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5734 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5735 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5736 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5737 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5738 | /* ensure the indexes are not affected */ |
| 5739 | return 0; |
| 5740 | |
| 5741 | return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test); |
| 5742 | } |
| 5743 | |
| 5744 | static int |
| 5745 | acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5746 | struct acl_expr *expr, struct acl_test *test) |
| 5747 | { |
| 5748 | struct http_txn *txn = l7; |
| 5749 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5750 | if (!txn) |
| 5751 | return 0; |
| 5752 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5753 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5754 | return 0; |
| 5755 | |
| 5756 | return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test); |
| 5757 | } |
| 5758 | |
| 5759 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 5760 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 5761 | */ |
| 5762 | static int |
| 5763 | 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] | 5764 | struct acl_expr *expr, struct acl_test *test) |
| 5765 | { |
| 5766 | struct http_txn *txn = l7; |
| 5767 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5768 | struct hdr_ctx ctx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5769 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5770 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5771 | if (!txn) |
| 5772 | return 0; |
| 5773 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5774 | ctx.idx = 0; |
| 5775 | cnt = 0; |
| 5776 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx)) |
| 5777 | cnt++; |
| 5778 | |
| 5779 | test->i = cnt; |
| 5780 | test->flags = ACL_TEST_F_VOL_HDR; |
| 5781 | return 1; |
| 5782 | } |
| 5783 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5784 | static int |
| 5785 | acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5786 | struct acl_expr *expr, struct acl_test *test) |
| 5787 | { |
| 5788 | struct http_txn *txn = l7; |
| 5789 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5790 | if (!txn) |
| 5791 | return 0; |
| 5792 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5793 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5794 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5795 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5796 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5797 | /* ensure the indexes are not affected */ |
| 5798 | return 0; |
| 5799 | |
| 5800 | return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test); |
| 5801 | } |
| 5802 | |
| 5803 | static int |
| 5804 | acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5805 | struct acl_expr *expr, struct acl_test *test) |
| 5806 | { |
| 5807 | struct http_txn *txn = l7; |
| 5808 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5809 | if (!txn) |
| 5810 | return 0; |
| 5811 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5812 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5813 | return 0; |
| 5814 | |
| 5815 | return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test); |
| 5816 | } |
| 5817 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5818 | /* 7. Check on HTTP header's integer value. The integer value is returned. |
| 5819 | * FIXME: the type is 'int', it may not be appropriate for everything. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5820 | * 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] | 5821 | */ |
| 5822 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5823 | 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] | 5824 | struct acl_expr *expr, struct acl_test *test) |
| 5825 | { |
| 5826 | struct http_txn *txn = l7; |
| 5827 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5828 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5829 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5830 | if (!txn) |
| 5831 | return 0; |
| 5832 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5833 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 5834 | /* search for header from the beginning */ |
| 5835 | ctx->idx = 0; |
| 5836 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5837 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 5838 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 5839 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5840 | test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen); |
| 5841 | return 1; |
| 5842 | } |
| 5843 | |
| 5844 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 5845 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5846 | return 0; |
| 5847 | } |
| 5848 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5849 | static int |
| 5850 | acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5851 | struct acl_expr *expr, struct acl_test *test) |
| 5852 | { |
| 5853 | struct http_txn *txn = l7; |
| 5854 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5855 | if (!txn) |
| 5856 | return 0; |
| 5857 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5858 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5859 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5860 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5861 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5862 | /* ensure the indexes are not affected */ |
| 5863 | return 0; |
| 5864 | |
| 5865 | return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test); |
| 5866 | } |
| 5867 | |
| 5868 | static int |
| 5869 | acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5870 | struct acl_expr *expr, struct acl_test *test) |
| 5871 | { |
| 5872 | struct http_txn *txn = l7; |
| 5873 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5874 | if (!txn) |
| 5875 | return 0; |
| 5876 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5877 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5878 | return 0; |
| 5879 | |
| 5880 | return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test); |
| 5881 | } |
| 5882 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5883 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 5884 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 5885 | */ |
| 5886 | static int |
| 5887 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5888 | struct acl_expr *expr, struct acl_test *test) |
| 5889 | { |
| 5890 | struct http_txn *txn = l7; |
| 5891 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5892 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5893 | if (!txn) |
| 5894 | return 0; |
| 5895 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5896 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5897 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5898 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5899 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5900 | /* ensure the indexes are not affected */ |
| 5901 | return 0; |
| 5902 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5903 | end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 5904 | ptr = http_get_path(txn); |
| 5905 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5906 | return 0; |
| 5907 | |
| 5908 | /* OK, we got the '/' ! */ |
| 5909 | test->ptr = ptr; |
| 5910 | |
| 5911 | while (ptr < end && *ptr != '?') |
| 5912 | ptr++; |
| 5913 | |
| 5914 | test->len = ptr - test->ptr; |
| 5915 | |
| 5916 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 5917 | test->flags = ACL_TEST_F_VOL_1ST; |
| 5918 | return 1; |
| 5919 | } |
| 5920 | |
| 5921 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5922 | |
| 5923 | /************************************************************************/ |
| 5924 | /* All supported keywords must be declared here. */ |
| 5925 | /************************************************************************/ |
| 5926 | |
| 5927 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 5928 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5929 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT }, |
| 5930 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5931 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 5932 | { "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] | 5933 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5934 | { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5935 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 5936 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 5937 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 5938 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 5939 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 5940 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 5941 | { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE }, |
| 5942 | { "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] | 5943 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5944 | /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */ |
| 5945 | { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5946 | { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 5947 | { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 5948 | { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 5949 | { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 5950 | { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 5951 | { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 5952 | { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
| 5953 | { "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] | 5954 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5955 | { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 5956 | { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE }, |
| 5957 | { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE }, |
| 5958 | { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE }, |
| 5959 | { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE }, |
| 5960 | { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE }, |
| 5961 | { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE }, |
| 5962 | { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
| 5963 | { "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] | 5964 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5965 | { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5966 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 5967 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 5968 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 5969 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 5970 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 5971 | { "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] | 5972 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 5973 | { NULL, NULL, NULL, NULL }, |
| 5974 | |
| 5975 | #if 0 |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5976 | { "line", acl_parse_str, acl_fetch_line, acl_match_str }, |
| 5977 | { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg }, |
| 5978 | { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg }, |
| 5979 | { "line_end", acl_parse_str, acl_fetch_line, acl_match_end }, |
| 5980 | { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub }, |
| 5981 | { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir }, |
| 5982 | { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom }, |
| 5983 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5984 | { "cook", acl_parse_str, acl_fetch_cook, acl_match_str }, |
| 5985 | { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg }, |
| 5986 | { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg }, |
| 5987 | { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end }, |
| 5988 | { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub }, |
| 5989 | { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir }, |
| 5990 | { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom }, |
| 5991 | { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst }, |
| 5992 | |
| 5993 | { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str }, |
| 5994 | { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg }, |
| 5995 | { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str }, |
| 5996 | { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 }, |
| 5997 | { NULL, NULL, NULL, NULL }, |
| 5998 | #endif |
| 5999 | }}; |
| 6000 | |
| 6001 | |
| 6002 | __attribute__((constructor)) |
| 6003 | static void __http_protocol_init(void) |
| 6004 | { |
| 6005 | acl_register_keywords(&acl_kws); |
| 6006 | } |
| 6007 | |
| 6008 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6009 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6010 | * Local variables: |
| 6011 | * c-indent-level: 8 |
| 6012 | * c-basic-offset: 8 |
| 6013 | * End: |
| 6014 | */ |