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> |
| 53 | #include <proto/task.h> |
| 54 | |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 55 | #ifdef CONFIG_HAP_TCPSPLICE |
| 56 | #include <libtcpsplice.h> |
| 57 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 58 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 59 | #define DEBUG_PARSE_NO_SPEEDUP |
| 60 | #undef DEBUG_PARSE_NO_SPEEDUP |
| 61 | |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 62 | /* This is used to perform a quick jump as an alternative to a break/continue |
| 63 | * instruction. The first argument is the label for normal operation, and the |
| 64 | * second one is the break/continue instruction in the no_speedup mode. |
| 65 | */ |
| 66 | |
| 67 | #ifdef DEBUG_PARSE_NO_SPEEDUP |
| 68 | #define QUICK_JUMP(x,y) y |
| 69 | #else |
| 70 | #define QUICK_JUMP(x,y) goto x |
| 71 | #endif |
| 72 | |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 73 | /* This is used by remote monitoring */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 74 | const char HTTP_200[] = |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 75 | "HTTP/1.0 200 OK\r\n" |
| 76 | "Cache-Control: no-cache\r\n" |
| 77 | "Connection: close\r\n" |
| 78 | "Content-Type: text/html\r\n" |
| 79 | "\r\n" |
| 80 | "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n"; |
| 81 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 82 | const struct chunk http_200_chunk = { |
| 83 | .str = (char *)&HTTP_200, |
| 84 | .len = sizeof(HTTP_200)-1 |
| 85 | }; |
| 86 | |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 87 | const char *HTTP_301 = |
| 88 | "HTTP/1.0 301 Moved Permantenly\r\n" |
| 89 | "Cache-Control: no-cache\r\n" |
| 90 | "Connection: close\r\n" |
| 91 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 92 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 93 | const char *HTTP_302 = |
| 94 | "HTTP/1.0 302 Found\r\n" |
| 95 | "Cache-Control: no-cache\r\n" |
| 96 | "Connection: close\r\n" |
| 97 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 98 | |
| 99 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 100 | const char *HTTP_303 = |
| 101 | "HTTP/1.0 303 See Other\r\n" |
| 102 | "Cache-Control: no-cache\r\n" |
| 103 | "Connection: close\r\n" |
| 104 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 105 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 106 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 107 | const char *HTTP_401_fmt = |
| 108 | "HTTP/1.0 401 Unauthorized\r\n" |
| 109 | "Cache-Control: no-cache\r\n" |
| 110 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 111 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 112 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 113 | "\r\n" |
| 114 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 115 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 116 | |
| 117 | const int http_err_codes[HTTP_ERR_SIZE] = { |
| 118 | [HTTP_ERR_400] = 400, |
| 119 | [HTTP_ERR_403] = 403, |
| 120 | [HTTP_ERR_408] = 408, |
| 121 | [HTTP_ERR_500] = 500, |
| 122 | [HTTP_ERR_502] = 502, |
| 123 | [HTTP_ERR_503] = 503, |
| 124 | [HTTP_ERR_504] = 504, |
| 125 | }; |
| 126 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 127 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 128 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 129 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 130 | "Cache-Control: no-cache\r\n" |
| 131 | "Connection: close\r\n" |
| 132 | "Content-Type: text/html\r\n" |
| 133 | "\r\n" |
| 134 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 135 | |
| 136 | [HTTP_ERR_403] = |
| 137 | "HTTP/1.0 403 Forbidden\r\n" |
| 138 | "Cache-Control: no-cache\r\n" |
| 139 | "Connection: close\r\n" |
| 140 | "Content-Type: text/html\r\n" |
| 141 | "\r\n" |
| 142 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 143 | |
| 144 | [HTTP_ERR_408] = |
| 145 | "HTTP/1.0 408 Request Time-out\r\n" |
| 146 | "Cache-Control: no-cache\r\n" |
| 147 | "Connection: close\r\n" |
| 148 | "Content-Type: text/html\r\n" |
| 149 | "\r\n" |
| 150 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 151 | |
| 152 | [HTTP_ERR_500] = |
| 153 | "HTTP/1.0 500 Server Error\r\n" |
| 154 | "Cache-Control: no-cache\r\n" |
| 155 | "Connection: close\r\n" |
| 156 | "Content-Type: text/html\r\n" |
| 157 | "\r\n" |
| 158 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 159 | |
| 160 | [HTTP_ERR_502] = |
| 161 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 162 | "Cache-Control: no-cache\r\n" |
| 163 | "Connection: close\r\n" |
| 164 | "Content-Type: text/html\r\n" |
| 165 | "\r\n" |
| 166 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 167 | |
| 168 | [HTTP_ERR_503] = |
| 169 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 170 | "Cache-Control: no-cache\r\n" |
| 171 | "Connection: close\r\n" |
| 172 | "Content-Type: text/html\r\n" |
| 173 | "\r\n" |
| 174 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 175 | |
| 176 | [HTTP_ERR_504] = |
| 177 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 178 | "Cache-Control: no-cache\r\n" |
| 179 | "Connection: close\r\n" |
| 180 | "Content-Type: text/html\r\n" |
| 181 | "\r\n" |
| 182 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 183 | |
| 184 | }; |
| 185 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 186 | /* We must put the messages here since GCC cannot initialize consts depending |
| 187 | * on strlen(). |
| 188 | */ |
| 189 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 190 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 191 | #define FD_SETS_ARE_BITFIELDS |
| 192 | #ifdef FD_SETS_ARE_BITFIELDS |
| 193 | /* |
| 194 | * This map is used with all the FD_* macros to check whether a particular bit |
| 195 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 196 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 197 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 198 | * exclusively to the macros. |
| 199 | */ |
| 200 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 201 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 202 | |
| 203 | #else |
| 204 | #error "Check if your OS uses bitfields for fd_sets" |
| 205 | #endif |
| 206 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 207 | void init_proto_http() |
| 208 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 209 | int i; |
| 210 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 211 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 212 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 213 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 214 | if (!http_err_msgs[msg]) { |
| 215 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 216 | abort(); |
| 217 | } |
| 218 | |
| 219 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 220 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 221 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 222 | |
| 223 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 224 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 225 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 226 | * printable characters above. |
| 227 | */ |
| 228 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 229 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 230 | for (i = 0; i < 32; i++) { |
| 231 | FD_SET(i, hdr_encode_map); |
| 232 | FD_SET(i, url_encode_map); |
| 233 | } |
| 234 | for (i = 127; i < 256; i++) { |
| 235 | FD_SET(i, hdr_encode_map); |
| 236 | FD_SET(i, url_encode_map); |
| 237 | } |
| 238 | |
| 239 | tmp = "\"#{|}"; |
| 240 | while (*tmp) { |
| 241 | FD_SET(*tmp, hdr_encode_map); |
| 242 | tmp++; |
| 243 | } |
| 244 | |
| 245 | tmp = "\"#"; |
| 246 | while (*tmp) { |
| 247 | FD_SET(*tmp, url_encode_map); |
| 248 | tmp++; |
| 249 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 250 | |
| 251 | /* memory allocations */ |
| 252 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 253 | pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 254 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 255 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 256 | /* |
| 257 | * We have 26 list of methods (1 per first letter), each of which can have |
| 258 | * up to 3 entries (2 valid, 1 null). |
| 259 | */ |
| 260 | struct http_method_desc { |
| 261 | http_meth_t meth; |
| 262 | int len; |
| 263 | const char text[8]; |
| 264 | }; |
| 265 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 266 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 267 | ['C' - 'A'] = { |
| 268 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 269 | }, |
| 270 | ['D' - 'A'] = { |
| 271 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 272 | }, |
| 273 | ['G' - 'A'] = { |
| 274 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 275 | }, |
| 276 | ['H' - 'A'] = { |
| 277 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 278 | }, |
| 279 | ['P' - 'A'] = { |
| 280 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 281 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 282 | }, |
| 283 | ['T' - 'A'] = { |
| 284 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 285 | }, |
| 286 | /* rest is empty like this : |
| 287 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 288 | */ |
| 289 | }; |
| 290 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 291 | /* 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] | 292 | * 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] | 293 | * RFC2616 for those chars. |
| 294 | */ |
| 295 | |
| 296 | const char http_is_spht[256] = { |
| 297 | [' '] = 1, ['\t'] = 1, |
| 298 | }; |
| 299 | |
| 300 | const char http_is_crlf[256] = { |
| 301 | ['\r'] = 1, ['\n'] = 1, |
| 302 | }; |
| 303 | |
| 304 | const char http_is_lws[256] = { |
| 305 | [' '] = 1, ['\t'] = 1, |
| 306 | ['\r'] = 1, ['\n'] = 1, |
| 307 | }; |
| 308 | |
| 309 | const char http_is_sep[256] = { |
| 310 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 311 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 312 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 313 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 314 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 315 | }; |
| 316 | |
| 317 | const char http_is_ctl[256] = { |
| 318 | [0 ... 31] = 1, |
| 319 | [127] = 1, |
| 320 | }; |
| 321 | |
| 322 | /* |
| 323 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 324 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 325 | * them correctly. Instead, define every non-CTL char's status. |
| 326 | */ |
| 327 | const char http_is_token[256] = { |
| 328 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 329 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 330 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 331 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 332 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 333 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 334 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 335 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 336 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 337 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 338 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 339 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 340 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 341 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 342 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 343 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 344 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 345 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 346 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 347 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 348 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 349 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 350 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 351 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 352 | }; |
| 353 | |
| 354 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 355 | /* |
| 356 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 357 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 358 | */ |
| 359 | const char http_is_ver_token[256] = { |
| 360 | ['.'] = 1, ['/'] = 1, |
| 361 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 362 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| 363 | ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| 364 | }; |
| 365 | |
| 366 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 367 | #ifdef DEBUG_FULL |
Willy Tarreau | 6468d92 | 2008-08-03 19:15:35 +0200 | [diff] [blame] | 368 | static char *cli_stnames[6] = {"INS", "HDR", "DAT", "SHR", "SHW", "CLS" }; |
| 369 | static char *srv_stnames[8] = {"IDL", "ANA", "CON", "HDR", "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 | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 540 | /* This function turns the server state into the SV_STCLOSE, and sets |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 541 | * indicators accordingly. Note that if <status> is 0, or if the message |
| 542 | * 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 | { |
| 547 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 548 | buffer_shutw_done(t->req); |
| 549 | buffer_shutr_done(t->rep); |
Willy Tarreau | 718f0ef | 2008-08-10 16:21:32 +0200 | [diff] [blame] | 550 | t->rep->flags |= BF_MAY_FORWARD; |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 551 | if (status > 0 && msg) { |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 552 | t->txn.status = status; |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 553 | if (t->fe->mode == PR_MODE_HTTP) |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 554 | client_return(t, msg); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 555 | } |
| 556 | if (!(t->flags & SN_ERR_MASK)) |
| 557 | t->flags |= err; |
| 558 | if (!(t->flags & SN_FINST_MASK)) |
| 559 | t->flags |= finst; |
| 560 | } |
| 561 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 562 | /* This function returns the appropriate error location for the given session |
| 563 | * and message. |
| 564 | */ |
| 565 | |
| 566 | struct chunk *error_message(struct session *s, int msgnum) |
| 567 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 568 | if (s->be->errmsg[msgnum].str) |
| 569 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 570 | else if (s->fe->errmsg[msgnum].str) |
| 571 | return &s->fe->errmsg[msgnum]; |
| 572 | else |
| 573 | return &http_err_chunks[msgnum]; |
| 574 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 575 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 576 | /* |
| 577 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 578 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 579 | */ |
| 580 | static http_meth_t find_http_meth(const char *str, const int len) |
| 581 | { |
| 582 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 583 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 584 | |
| 585 | m = ((unsigned)*str - 'A'); |
| 586 | |
| 587 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 588 | for (h = http_methods[m]; h->len > 0; h++) { |
| 589 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 590 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 591 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 592 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 593 | }; |
| 594 | return HTTP_METH_OTHER; |
| 595 | } |
| 596 | return HTTP_METH_NONE; |
| 597 | |
| 598 | } |
| 599 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 600 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 601 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 602 | * It is returned otherwise. |
| 603 | */ |
| 604 | static char * |
| 605 | http_get_path(struct http_txn *txn) |
| 606 | { |
| 607 | char *ptr, *end; |
| 608 | |
| 609 | ptr = txn->req.sol + txn->req.sl.rq.u; |
| 610 | end = ptr + txn->req.sl.rq.u_l; |
| 611 | |
| 612 | if (ptr >= end) |
| 613 | return NULL; |
| 614 | |
| 615 | /* RFC2616, par. 5.1.2 : |
| 616 | * Request-URI = "*" | absuri | abspath | authority |
| 617 | */ |
| 618 | |
| 619 | if (*ptr == '*') |
| 620 | return NULL; |
| 621 | |
| 622 | if (isalpha((unsigned char)*ptr)) { |
| 623 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 624 | ptr++; |
| 625 | while (ptr < end && |
| 626 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 627 | ptr++; |
| 628 | /* skip '://' */ |
| 629 | if (ptr == end || *ptr++ != ':') |
| 630 | return NULL; |
| 631 | if (ptr == end || *ptr++ != '/') |
| 632 | return NULL; |
| 633 | if (ptr == end || *ptr++ != '/') |
| 634 | return NULL; |
| 635 | } |
| 636 | /* skip [user[:passwd]@]host[:[port]] */ |
| 637 | |
| 638 | while (ptr < end && *ptr != '/') |
| 639 | ptr++; |
| 640 | |
| 641 | if (ptr == end) |
| 642 | return NULL; |
| 643 | |
| 644 | /* OK, we got the '/' ! */ |
| 645 | return ptr; |
| 646 | } |
| 647 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 648 | /* Processes the client and server jobs of a session task, then |
| 649 | * puts it back to the wait queue in a clean state, or |
| 650 | * cleans up its resources if it must be deleted. Returns |
| 651 | * the time the task accepts to wait, or TIME_ETERNITY for |
| 652 | * infinity. |
| 653 | */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 654 | void process_session(struct task *t, int *next) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 655 | { |
| 656 | struct session *s = t->context; |
| 657 | int fsm_resync = 0; |
| 658 | |
| 659 | do { |
| 660 | fsm_resync = 0; |
| 661 | //fprintf(stderr,"before_cli:cli=%d, srv=%d\n", s->cli_state, s->srv_state); |
| 662 | fsm_resync |= process_cli(s); |
| 663 | //fprintf(stderr,"cli/srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state); |
| 664 | fsm_resync |= process_srv(s); |
| 665 | //fprintf(stderr,"after_srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state); |
| 666 | } while (fsm_resync); |
| 667 | |
Willy Tarreau | f41d4b1 | 2007-04-28 23:26:14 +0200 | [diff] [blame] | 668 | if (likely(s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE)) { |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 669 | |
| 670 | if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED)) |
| 671 | session_process_counters(s); |
| 672 | |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 673 | s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE; |
| 674 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 675 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 676 | t->expire = tick_first(tick_first(s->req->rex, s->req->wex), |
| 677 | tick_first(s->rep->rex, s->rep->wex)); |
| 678 | t->expire = tick_first(t->expire, s->req->cex); |
Willy Tarreau | 036fae0 | 2008-01-06 13:24:40 +0100 | [diff] [blame] | 679 | if (s->cli_state == CL_STHEADERS) |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 680 | t->expire = tick_first(t->expire, s->txn.exp); |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 681 | else if (s->cli_state == CL_STINSPECT) |
| 682 | t->expire = tick_first(t->expire, s->inspect_exp); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 683 | |
| 684 | /* restore t to its place in the task list */ |
| 685 | task_queue(t); |
| 686 | |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 687 | *next = t->expire; |
| 688 | return; /* nothing more to do */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 689 | } |
| 690 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 691 | s->fe->feconn--; |
| 692 | if (s->flags & SN_BE_ASSIGNED) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 693 | s->be->beconn--; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 694 | actconn--; |
| 695 | |
Willy Tarreau | f41d4b1 | 2007-04-28 23:26:14 +0200 | [diff] [blame] | 696 | if (unlikely((global.mode & MODE_DEBUG) && |
| 697 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 698 | int len; |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 699 | len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 700 | s->uniq_id, s->be->id, |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 701 | (unsigned short)s->cli_fd, (unsigned short)s->srv_fd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 702 | write(1, trash, len); |
| 703 | } |
| 704 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 705 | 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] | 706 | session_process_counters(s); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 707 | |
| 708 | /* let's do a final log if we need it */ |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 709 | if (s->logs.logwait && |
| 710 | !(s->flags & SN_MONITOR) && |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 711 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 712 | if (s->fe->to_log & LW_REQ) |
| 713 | http_sess_log(s); |
| 714 | else |
| 715 | tcp_sess_log(s); |
| 716 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 717 | |
| 718 | /* the task MUST not be in the run queue anymore */ |
| 719 | task_delete(t); |
| 720 | session_free(s); |
| 721 | task_free(t); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 722 | *next = TICK_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 726 | extern const char sess_term_cond[8]; |
| 727 | extern const char sess_fin_state[8]; |
| 728 | extern const char *monthname[12]; |
| 729 | const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */ |
| 730 | const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown, |
| 731 | Set-cookie seen and left unchanged (passive), Set-cookie Deleted, |
| 732 | unknown, Set-cookie Rewritten */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 733 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 734 | struct pool_head *pool2_capture; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 735 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 736 | /* |
| 737 | * send a log for the session when we have enough info about it. |
| 738 | * Will not log if the frontend has no log defined. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 739 | */ |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 740 | static void http_sess_log(struct session *s) |
| 741 | { |
| 742 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 743 | struct proxy *fe = s->fe; |
| 744 | struct proxy *be = s->be; |
| 745 | struct proxy *prx_log; |
| 746 | struct http_txn *txn = &s->txn; |
| 747 | int tolog; |
| 748 | char *uri, *h; |
| 749 | char *svid; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 750 | struct tm tm; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 751 | static char tmpline[MAX_SYSLOG_LEN]; |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 752 | int t_request; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 753 | int hdr; |
| 754 | |
| 755 | if (fe->logfac1 < 0 && fe->logfac2 < 0) |
| 756 | return; |
| 757 | prx_log = fe; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 758 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 759 | if (s->cli_addr.ss_family == AF_INET) |
| 760 | inet_ntop(AF_INET, |
| 761 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 762 | pn, sizeof(pn)); |
| 763 | else |
| 764 | inet_ntop(AF_INET6, |
| 765 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 766 | pn, sizeof(pn)); |
| 767 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 768 | get_localtime(s->logs.accept_date.tv_sec, &tm); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 769 | |
| 770 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 771 | tolog = fe->to_log; |
| 772 | |
| 773 | h = tmpline; |
| 774 | if (fe->to_log & LW_REQHDR && |
| 775 | txn->req.cap && |
| 776 | (h < tmpline + sizeof(tmpline) - 10)) { |
| 777 | *(h++) = ' '; |
| 778 | *(h++) = '{'; |
| 779 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 780 | if (hdr) |
| 781 | *(h++) = '|'; |
| 782 | if (txn->req.cap[hdr] != NULL) |
| 783 | h = encode_string(h, tmpline + sizeof(tmpline) - 7, |
| 784 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 785 | } |
| 786 | *(h++) = '}'; |
| 787 | } |
| 788 | |
| 789 | if (fe->to_log & LW_RSPHDR && |
| 790 | txn->rsp.cap && |
| 791 | (h < tmpline + sizeof(tmpline) - 7)) { |
| 792 | *(h++) = ' '; |
| 793 | *(h++) = '{'; |
| 794 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 795 | if (hdr) |
| 796 | *(h++) = '|'; |
| 797 | if (txn->rsp.cap[hdr] != NULL) |
| 798 | h = encode_string(h, tmpline + sizeof(tmpline) - 4, |
| 799 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 800 | } |
| 801 | *(h++) = '}'; |
| 802 | } |
| 803 | |
| 804 | if (h < tmpline + sizeof(tmpline) - 4) { |
| 805 | *(h++) = ' '; |
| 806 | *(h++) = '"'; |
| 807 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 808 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 809 | '#', url_encode_map, uri); |
| 810 | *(h++) = '"'; |
| 811 | } |
| 812 | *h = '\0'; |
| 813 | |
| 814 | svid = (tolog & LW_SVID) ? |
| 815 | (s->data_source != DATA_SRC_STATS) ? |
| 816 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 817 | |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 818 | t_request = -1; |
| 819 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 820 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 821 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 822 | send_log(prx_log, LOG_INFO, |
| 823 | "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]" |
| 824 | " %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] | 825 | " %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] | 826 | pn, |
| 827 | (s->cli_addr.ss_family == AF_INET) ? |
| 828 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 829 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 830 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 831 | 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] | 832 | fe->id, be->id, svid, |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 833 | t_request, |
| 834 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 835 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 836 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 837 | (tolog & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 838 | txn->status, |
Willy Tarreau | 8b3977f | 2008-01-18 11:16:32 +0100 | [diff] [blame] | 839 | (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 840 | txn->cli_cookie ? txn->cli_cookie : "-", |
| 841 | txn->srv_cookie ? txn->srv_cookie : "-", |
| 842 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 843 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 844 | (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 845 | (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-', |
| 846 | 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] | 847 | (s->flags & SN_REDISP)?"+":"", |
| 848 | (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] | 849 | s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline); |
| 850 | |
| 851 | s->logs.logwait = 0; |
| 852 | } |
| 853 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 854 | |
| 855 | /* |
| 856 | * Capture headers from message starting at <som> according to header list |
| 857 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 858 | */ |
| 859 | void capture_headers(char *som, struct hdr_idx *idx, |
| 860 | char **cap, struct cap_hdr *cap_hdr) |
| 861 | { |
| 862 | char *eol, *sol, *col, *sov; |
| 863 | int cur_idx; |
| 864 | struct cap_hdr *h; |
| 865 | int len; |
| 866 | |
| 867 | sol = som + hdr_idx_first_pos(idx); |
| 868 | cur_idx = hdr_idx_first_idx(idx); |
| 869 | |
| 870 | while (cur_idx) { |
| 871 | eol = sol + idx->v[cur_idx].len; |
| 872 | |
| 873 | col = sol; |
| 874 | while (col < eol && *col != ':') |
| 875 | col++; |
| 876 | |
| 877 | sov = col + 1; |
| 878 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 879 | sov++; |
| 880 | |
| 881 | for (h = cap_hdr; h; h = h->next) { |
| 882 | if ((h->namelen == col - sol) && |
| 883 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 884 | if (cap[h->index] == NULL) |
| 885 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 886 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 887 | |
| 888 | if (cap[h->index] == NULL) { |
| 889 | Alert("HTTP capture : out of memory.\n"); |
| 890 | continue; |
| 891 | } |
| 892 | |
| 893 | len = eol - sov; |
| 894 | if (len > h->len) |
| 895 | len = h->len; |
| 896 | |
| 897 | memcpy(cap[h->index], sov, len); |
| 898 | cap[h->index][len]=0; |
| 899 | } |
| 900 | } |
| 901 | sol = eol + idx->v[cur_idx].cr + 1; |
| 902 | cur_idx = idx->v[cur_idx].next; |
| 903 | } |
| 904 | } |
| 905 | |
| 906 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 907 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 908 | */ |
| 909 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 910 | |
| 911 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 912 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 913 | */ |
| 914 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 915 | ptr++; \ |
| 916 | if (likely(ptr < end)) \ |
| 917 | goto good; \ |
| 918 | else { \ |
| 919 | state = (st); \ |
| 920 | goto http_msg_ood; \ |
| 921 | } \ |
| 922 | } while (0) |
| 923 | |
| 924 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 925 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 926 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 927 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 928 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 929 | * will give undefined results. |
| 930 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 931 | * and that msg->sol points to the beginning of the response. |
| 932 | * If a complete line is found (which implies that at least one CR or LF is |
| 933 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 934 | * returned indicating an incomplete line (which does not mean that parts have |
| 935 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 936 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 937 | * upon next call. |
| 938 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 939 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 940 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 941 | * 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] | 942 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 943 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 944 | const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, |
| 945 | unsigned int state, const char *ptr, const char *end, |
| 946 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 947 | { |
| 948 | __label__ |
| 949 | http_msg_rpver, |
| 950 | http_msg_rpver_sp, |
| 951 | http_msg_rpcode, |
| 952 | http_msg_rpcode_sp, |
| 953 | http_msg_rpreason, |
| 954 | http_msg_rpline_eol, |
| 955 | http_msg_ood, /* out of data */ |
| 956 | http_msg_invalid; |
| 957 | |
| 958 | switch (state) { |
| 959 | http_msg_rpver: |
| 960 | case HTTP_MSG_RPVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 961 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 962 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 963 | |
| 964 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 965 | msg->sl.st.v_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 966 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 967 | } |
| 968 | goto http_msg_invalid; |
| 969 | |
| 970 | http_msg_rpver_sp: |
| 971 | case HTTP_MSG_RPVER_SP: |
| 972 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 973 | msg->sl.st.c = ptr - msg_buf; |
| 974 | goto http_msg_rpcode; |
| 975 | } |
| 976 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 977 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 978 | /* so it's a CR/LF, this is invalid */ |
| 979 | goto http_msg_invalid; |
| 980 | |
| 981 | http_msg_rpcode: |
| 982 | case HTTP_MSG_RPCODE: |
| 983 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 984 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 985 | |
| 986 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 987 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 988 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 989 | } |
| 990 | |
| 991 | /* so it's a CR/LF, so there is no reason phrase */ |
| 992 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 993 | http_msg_rsp_reason: |
| 994 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
| 995 | msg->sl.st.r = ptr - msg_buf; |
| 996 | msg->sl.st.r_l = 0; |
| 997 | goto http_msg_rpline_eol; |
| 998 | |
| 999 | http_msg_rpcode_sp: |
| 1000 | case HTTP_MSG_RPCODE_SP: |
| 1001 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1002 | msg->sl.st.r = ptr - msg_buf; |
| 1003 | goto http_msg_rpreason; |
| 1004 | } |
| 1005 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1006 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1007 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1008 | goto http_msg_rsp_reason; |
| 1009 | |
| 1010 | http_msg_rpreason: |
| 1011 | case HTTP_MSG_RPREASON: |
| 1012 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1013 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
| 1014 | msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r; |
| 1015 | http_msg_rpline_eol: |
| 1016 | /* We have seen the end of line. Note that we do not |
| 1017 | * necessarily have the \n yet, but at least we know that we |
| 1018 | * have EITHER \r OR \n, otherwise the response would not be |
| 1019 | * complete. We can then record the response length and return |
| 1020 | * to the caller which will be able to register it. |
| 1021 | */ |
| 1022 | msg->sl.st.l = ptr - msg->sol; |
| 1023 | return ptr; |
| 1024 | |
| 1025 | #ifdef DEBUG_FULL |
| 1026 | default: |
| 1027 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1028 | exit(1); |
| 1029 | #endif |
| 1030 | } |
| 1031 | |
| 1032 | http_msg_ood: |
| 1033 | /* out of data */ |
| 1034 | if (ret_state) |
| 1035 | *ret_state = state; |
| 1036 | if (ret_ptr) |
| 1037 | *ret_ptr = (char *)ptr; |
| 1038 | return NULL; |
| 1039 | |
| 1040 | http_msg_invalid: |
| 1041 | /* invalid message */ |
| 1042 | if (ret_state) |
| 1043 | *ret_state = HTTP_MSG_ERROR; |
| 1044 | return NULL; |
| 1045 | } |
| 1046 | |
| 1047 | |
| 1048 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1049 | * This function parses a request line between <ptr> and <end>, starting with |
| 1050 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1051 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1052 | * will give undefined results. |
| 1053 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1054 | * and that msg->sol points to the beginning of the request. |
| 1055 | * If a complete line is found (which implies that at least one CR or LF is |
| 1056 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1057 | * returned indicating an incomplete line (which does not mean that parts have |
| 1058 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1059 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1060 | * upon next call. |
| 1061 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1062 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1063 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1064 | * 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] | 1065 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1066 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1067 | const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, |
| 1068 | unsigned int state, const char *ptr, const char *end, |
| 1069 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1070 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1071 | __label__ |
| 1072 | http_msg_rqmeth, |
| 1073 | http_msg_rqmeth_sp, |
| 1074 | http_msg_rquri, |
| 1075 | http_msg_rquri_sp, |
| 1076 | http_msg_rqver, |
| 1077 | http_msg_rqline_eol, |
| 1078 | http_msg_ood, /* out of data */ |
| 1079 | http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1080 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1081 | switch (state) { |
| 1082 | http_msg_rqmeth: |
| 1083 | case HTTP_MSG_RQMETH: |
| 1084 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1085 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1086 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1087 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1088 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1089 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1090 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1091 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1092 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1093 | /* HTTP 0.9 request */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1094 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1095 | http_msg_req09_uri: |
| 1096 | msg->sl.rq.u = ptr - msg_buf; |
| 1097 | http_msg_req09_uri_e: |
| 1098 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1099 | http_msg_req09_ver: |
| 1100 | msg->sl.rq.v = ptr - msg_buf; |
| 1101 | msg->sl.rq.v_l = 0; |
| 1102 | goto http_msg_rqline_eol; |
| 1103 | } |
| 1104 | goto http_msg_invalid; |
| 1105 | |
| 1106 | http_msg_rqmeth_sp: |
| 1107 | case HTTP_MSG_RQMETH_SP: |
| 1108 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1109 | msg->sl.rq.u = ptr - msg_buf; |
| 1110 | goto http_msg_rquri; |
| 1111 | } |
| 1112 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1113 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1114 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1115 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1116 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1117 | http_msg_rquri: |
| 1118 | case HTTP_MSG_RQURI: |
| 1119 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1120 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1121 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1122 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1123 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1124 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1125 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1126 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1127 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1128 | goto http_msg_req09_uri_e; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1129 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1130 | http_msg_rquri_sp: |
| 1131 | case HTTP_MSG_RQURI_SP: |
| 1132 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1133 | msg->sl.rq.v = ptr - msg_buf; |
| 1134 | goto http_msg_rqver; |
| 1135 | } |
| 1136 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1137 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1138 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1139 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1140 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1141 | http_msg_rqver: |
| 1142 | case HTTP_MSG_RQVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1143 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1144 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1145 | |
| 1146 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1147 | msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v; |
| 1148 | http_msg_rqline_eol: |
| 1149 | /* We have seen the end of line. Note that we do not |
| 1150 | * necessarily have the \n yet, but at least we know that we |
| 1151 | * have EITHER \r OR \n, otherwise the request would not be |
| 1152 | * complete. We can then record the request length and return |
| 1153 | * to the caller which will be able to register it. |
| 1154 | */ |
| 1155 | msg->sl.rq.l = ptr - msg->sol; |
| 1156 | return ptr; |
| 1157 | } |
| 1158 | |
| 1159 | /* neither an HTTP_VER token nor a CRLF */ |
| 1160 | goto http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1161 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1162 | #ifdef DEBUG_FULL |
| 1163 | default: |
| 1164 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1165 | exit(1); |
| 1166 | #endif |
| 1167 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1168 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1169 | http_msg_ood: |
| 1170 | /* out of data */ |
| 1171 | if (ret_state) |
| 1172 | *ret_state = state; |
| 1173 | if (ret_ptr) |
| 1174 | *ret_ptr = (char *)ptr; |
| 1175 | return NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1176 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1177 | http_msg_invalid: |
| 1178 | /* invalid message */ |
| 1179 | if (ret_state) |
| 1180 | *ret_state = HTTP_MSG_ERROR; |
| 1181 | return NULL; |
| 1182 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1183 | |
| 1184 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1185 | /* |
| 1186 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1187 | * depending on the initial msg->msg_state. It can be preempted everywhere |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1188 | * when data are missing and recalled at the exact same location with no |
| 1189 | * information loss. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1190 | * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other |
| 1191 | * fields. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1192 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1193 | void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx) |
| 1194 | { |
| 1195 | __label__ |
| 1196 | http_msg_rqbefore, |
| 1197 | http_msg_rqbefore_cr, |
| 1198 | http_msg_rqmeth, |
| 1199 | http_msg_rqline_end, |
| 1200 | http_msg_hdr_first, |
| 1201 | http_msg_hdr_name, |
| 1202 | http_msg_hdr_l1_sp, |
| 1203 | http_msg_hdr_l1_lf, |
| 1204 | http_msg_hdr_l1_lws, |
| 1205 | http_msg_hdr_val, |
| 1206 | http_msg_hdr_l2_lf, |
| 1207 | http_msg_hdr_l2_lws, |
| 1208 | http_msg_complete_header, |
| 1209 | http_msg_last_lf, |
| 1210 | http_msg_ood, /* out of data */ |
| 1211 | http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1212 | |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1213 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1214 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1215 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1216 | state = msg->msg_state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1217 | ptr = buf->lr; |
| 1218 | end = buf->r; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1219 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1220 | if (unlikely(ptr >= end)) |
| 1221 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1222 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1223 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1224 | /* |
| 1225 | * First, states that are specific to the response only. |
| 1226 | * We check them first so that request and headers are |
| 1227 | * closer to each other (accessed more often). |
| 1228 | */ |
| 1229 | http_msg_rpbefore: |
| 1230 | case HTTP_MSG_RPBEFORE: |
| 1231 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1232 | if (likely(ptr == buf->data)) { |
| 1233 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1234 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1235 | } else { |
| 1236 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1237 | /* only skip empty leading lines, don't remove them */ |
| 1238 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1239 | msg->som = ptr - buf->data; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1240 | #else |
| 1241 | /* Remove empty leading lines, as recommended by |
| 1242 | * RFC2616. This takes a lot of time because we |
| 1243 | * must move all the buffer backwards, but this |
| 1244 | * is rarely needed. The method above will be |
| 1245 | * cleaner when we'll be able to start sending |
| 1246 | * the request from any place in the buffer. |
| 1247 | */ |
| 1248 | buf->lr = ptr; |
| 1249 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1250 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1251 | msg->sol = buf->data; |
| 1252 | ptr = buf->data; |
| 1253 | end = buf->r; |
| 1254 | #endif |
| 1255 | } |
| 1256 | hdr_idx_init(idx); |
| 1257 | state = HTTP_MSG_RPVER; |
| 1258 | goto http_msg_rpver; |
| 1259 | } |
| 1260 | |
| 1261 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1262 | goto http_msg_invalid; |
| 1263 | |
| 1264 | if (unlikely(*ptr == '\n')) |
| 1265 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1266 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1267 | /* stop here */ |
| 1268 | |
| 1269 | http_msg_rpbefore_cr: |
| 1270 | case HTTP_MSG_RPBEFORE_CR: |
| 1271 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1272 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1273 | /* stop here */ |
| 1274 | |
| 1275 | http_msg_rpver: |
| 1276 | case HTTP_MSG_RPVER: |
| 1277 | case HTTP_MSG_RPVER_SP: |
| 1278 | case HTTP_MSG_RPCODE: |
| 1279 | case HTTP_MSG_RPCODE_SP: |
| 1280 | case HTTP_MSG_RPREASON: |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1281 | ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1282 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1283 | if (unlikely(!ptr)) |
| 1284 | return; |
| 1285 | |
| 1286 | /* we have a full response and we know that we have either a CR |
| 1287 | * or an LF at <ptr>. |
| 1288 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1289 | //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] | 1290 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1291 | |
| 1292 | msg->sol = ptr; |
| 1293 | if (likely(*ptr == '\r')) |
| 1294 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1295 | goto http_msg_rpline_end; |
| 1296 | |
| 1297 | http_msg_rpline_end: |
| 1298 | case HTTP_MSG_RPLINE_END: |
| 1299 | /* msg->sol must point to the first of CR or LF. */ |
| 1300 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1301 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1302 | /* stop here */ |
| 1303 | |
| 1304 | /* |
| 1305 | * Second, states that are specific to the request only |
| 1306 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1307 | http_msg_rqbefore: |
| 1308 | case HTTP_MSG_RQBEFORE: |
| 1309 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1310 | if (likely(ptr == buf->data)) { |
| 1311 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1312 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1313 | } else { |
| 1314 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1315 | /* only skip empty leading lines, don't remove them */ |
| 1316 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1317 | msg->som = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1318 | #else |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1319 | /* Remove empty leading lines, as recommended by |
| 1320 | * RFC2616. This takes a lot of time because we |
| 1321 | * must move all the buffer backwards, but this |
| 1322 | * is rarely needed. The method above will be |
| 1323 | * cleaner when we'll be able to start sending |
| 1324 | * the request from any place in the buffer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1325 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1326 | buf->lr = ptr; |
| 1327 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1328 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1329 | msg->sol = buf->data; |
| 1330 | ptr = buf->data; |
| 1331 | end = buf->r; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1332 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1333 | } |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1334 | /* we will need this when keep-alive will be supported |
| 1335 | hdr_idx_init(idx); |
| 1336 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1337 | state = HTTP_MSG_RQMETH; |
| 1338 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1339 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1340 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1341 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1342 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1343 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1344 | if (unlikely(*ptr == '\n')) |
| 1345 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1346 | 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] | 1347 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1348 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1349 | http_msg_rqbefore_cr: |
| 1350 | case HTTP_MSG_RQBEFORE_CR: |
| 1351 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1352 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1353 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1354 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1355 | http_msg_rqmeth: |
| 1356 | case HTTP_MSG_RQMETH: |
| 1357 | case HTTP_MSG_RQMETH_SP: |
| 1358 | case HTTP_MSG_RQURI: |
| 1359 | case HTTP_MSG_RQURI_SP: |
| 1360 | case HTTP_MSG_RQVER: |
| 1361 | ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1362 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1363 | if (unlikely(!ptr)) |
| 1364 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1365 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1366 | /* we have a full request and we know that we have either a CR |
| 1367 | * or an LF at <ptr>. |
| 1368 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1369 | //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] | 1370 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1371 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1372 | msg->sol = ptr; |
| 1373 | if (likely(*ptr == '\r')) |
| 1374 | 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] | 1375 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1376 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1377 | http_msg_rqline_end: |
| 1378 | case HTTP_MSG_RQLINE_END: |
| 1379 | /* check for HTTP/0.9 request : no version information available. |
| 1380 | * msg->sol must point to the first of CR or LF. |
| 1381 | */ |
| 1382 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1383 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1384 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1385 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1386 | 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] | 1387 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1388 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1389 | /* |
| 1390 | * Common states below |
| 1391 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1392 | http_msg_hdr_first: |
| 1393 | case HTTP_MSG_HDR_FIRST: |
| 1394 | msg->sol = ptr; |
| 1395 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1396 | goto http_msg_hdr_name; |
| 1397 | } |
| 1398 | |
| 1399 | if (likely(*ptr == '\r')) |
| 1400 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1401 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1402 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1403 | http_msg_hdr_name: |
| 1404 | case HTTP_MSG_HDR_NAME: |
| 1405 | /* assumes msg->sol points to the first char */ |
| 1406 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1407 | 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] | 1408 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1409 | if (likely(*ptr == ':')) { |
| 1410 | msg->col = ptr - buf->data; |
| 1411 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| 1412 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1413 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1414 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1415 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1416 | http_msg_hdr_l1_sp: |
| 1417 | case HTTP_MSG_HDR_L1_SP: |
| 1418 | /* assumes msg->sol points to the first char and msg->col to the colon */ |
| 1419 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1420 | 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] | 1421 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1422 | /* header value can be basically anything except CR/LF */ |
| 1423 | msg->sov = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1424 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1425 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1426 | goto http_msg_hdr_val; |
| 1427 | } |
| 1428 | |
| 1429 | if (likely(*ptr == '\r')) |
| 1430 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1431 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1432 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1433 | http_msg_hdr_l1_lf: |
| 1434 | case HTTP_MSG_HDR_L1_LF: |
| 1435 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1436 | 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] | 1437 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1438 | http_msg_hdr_l1_lws: |
| 1439 | case HTTP_MSG_HDR_L1_LWS: |
| 1440 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1441 | /* replace HT,CR,LF with spaces */ |
| 1442 | for (; buf->data+msg->sov < ptr; msg->sov++) |
| 1443 | buf->data[msg->sov] = ' '; |
| 1444 | goto http_msg_hdr_l1_sp; |
| 1445 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1446 | /* we had a header consisting only in spaces ! */ |
| 1447 | msg->eol = buf->data + msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1448 | goto http_msg_complete_header; |
| 1449 | |
| 1450 | http_msg_hdr_val: |
| 1451 | case HTTP_MSG_HDR_VAL: |
| 1452 | /* assumes msg->sol points to the first char, msg->col to the |
| 1453 | * colon, and msg->sov points to the first character of the |
| 1454 | * value. |
| 1455 | */ |
| 1456 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1457 | 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] | 1458 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1459 | msg->eol = ptr; |
| 1460 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1461 | * real header end in case it ends with lots of LWS, but is this |
| 1462 | * really needed ? |
| 1463 | */ |
| 1464 | if (likely(*ptr == '\r')) |
| 1465 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1466 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1467 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1468 | http_msg_hdr_l2_lf: |
| 1469 | case HTTP_MSG_HDR_L2_LF: |
| 1470 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1471 | 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] | 1472 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1473 | http_msg_hdr_l2_lws: |
| 1474 | case HTTP_MSG_HDR_L2_LWS: |
| 1475 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1476 | /* LWS: replace HT,CR,LF with spaces */ |
| 1477 | for (; msg->eol < ptr; msg->eol++) |
| 1478 | *msg->eol = ' '; |
| 1479 | goto http_msg_hdr_val; |
| 1480 | } |
| 1481 | http_msg_complete_header: |
| 1482 | /* |
| 1483 | * It was a new header, so the last one is finished. |
| 1484 | * Assumes msg->sol points to the first char, msg->col to the |
| 1485 | * colon, msg->sov points to the first character of the value |
| 1486 | * and msg->eol to the first CR or LF so we know how the line |
| 1487 | * ends. We insert last header into the index. |
| 1488 | */ |
| 1489 | /* |
| 1490 | fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol); |
| 1491 | write(2, msg->sol, msg->eol-msg->sol); |
| 1492 | fprintf(stderr,"\n"); |
| 1493 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1494 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1495 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r', |
| 1496 | idx, idx->tail) < 0)) |
| 1497 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1498 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1499 | msg->sol = ptr; |
| 1500 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1501 | goto http_msg_hdr_name; |
| 1502 | } |
| 1503 | |
| 1504 | if (likely(*ptr == '\r')) |
| 1505 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1506 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1507 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1508 | http_msg_last_lf: |
| 1509 | case HTTP_MSG_LAST_LF: |
| 1510 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1511 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1512 | ptr++; |
| 1513 | buf->lr = ptr; |
| 1514 | msg->eoh = msg->sol - buf->data; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1515 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1516 | return; |
| 1517 | #ifdef DEBUG_FULL |
| 1518 | default: |
| 1519 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1520 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1521 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1522 | } |
| 1523 | http_msg_ood: |
| 1524 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1525 | msg->msg_state = state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1526 | buf->lr = ptr; |
| 1527 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1528 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1529 | http_msg_invalid: |
| 1530 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1531 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1532 | return; |
| 1533 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1534 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1535 | /* |
| 1536 | * manages the client FSM and its socket. BTW, it also tries to handle the |
| 1537 | * cookie. It returns 1 if a state has changed (and a resync may be needed), |
| 1538 | * 0 else. |
| 1539 | */ |
| 1540 | int process_cli(struct session *t) |
| 1541 | { |
| 1542 | int s = t->srv_state; |
| 1543 | int c = t->cli_state; |
| 1544 | struct buffer *req = t->req; |
| 1545 | struct buffer *rep = t->rep; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1546 | |
Willy Tarreau | 6468d92 | 2008-08-03 19:15:35 +0200 | [diff] [blame] | 1547 | DPRINTF(stderr,"process_cli: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%u,%u\n", |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1548 | cli_stnames[c], srv_stnames[s], |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 1549 | EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR), |
Willy Tarreau | 6468d92 | 2008-08-03 19:15:35 +0200 | [diff] [blame] | 1550 | req->rex, rep->wex); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1551 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1552 | if (c == CL_STINSPECT) { |
| 1553 | struct tcp_rule *rule; |
| 1554 | int partial; |
| 1555 | |
| 1556 | /* We will abort if we encounter a read error. In theory, |
| 1557 | * we should not abort if we get a close, it might be |
| 1558 | * valid, also very unlikely. FIXME: we'll abort for now, |
| 1559 | * this will be easier to change later. |
| 1560 | */ |
| 1561 | if (unlikely(req->flags & (BF_READ_ERROR | BF_READ_NULL))) { |
| 1562 | t->inspect_exp = TICK_ETERNITY; |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 1563 | buffer_shutr_done(req); |
| 1564 | buffer_shutw_done(rep); |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1565 | fd_delete(t->cli_fd); |
| 1566 | t->cli_state = CL_STCLOSE; |
| 1567 | t->fe->failed_req++; |
| 1568 | if (!(t->flags & SN_ERR_MASK)) |
| 1569 | t->flags |= SN_ERR_CLICL; |
| 1570 | if (!(t->flags & SN_FINST_MASK)) |
| 1571 | t->flags |= SN_FINST_R; |
| 1572 | return 1; |
| 1573 | } |
| 1574 | |
| 1575 | /* Abort if client read timeout has expired */ |
| 1576 | else if (unlikely(tick_is_expired(req->rex, now_ms))) { |
| 1577 | t->inspect_exp = TICK_ETERNITY; |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 1578 | buffer_shutr_done(req); |
| 1579 | buffer_shutw_done(rep); |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1580 | fd_delete(t->cli_fd); |
| 1581 | t->cli_state = CL_STCLOSE; |
| 1582 | t->fe->failed_req++; |
| 1583 | if (!(t->flags & SN_ERR_MASK)) |
| 1584 | t->flags |= SN_ERR_CLITO; |
| 1585 | if (!(t->flags & SN_FINST_MASK)) |
| 1586 | t->flags |= SN_FINST_R; |
| 1587 | return 1; |
| 1588 | } |
| 1589 | |
| 1590 | /* We don't know whether we have enough data, so must proceed |
| 1591 | * this way : |
| 1592 | * - iterate through all rules in their declaration order |
| 1593 | * - if one rule returns MISS, it means the inspect delay is |
| 1594 | * not over yet, then return immediately, otherwise consider |
| 1595 | * it as a non-match. |
| 1596 | * - if one rule returns OK, then return OK |
| 1597 | * - if one rule returns KO, then return KO |
| 1598 | */ |
| 1599 | |
| 1600 | if (tick_is_expired(t->inspect_exp, now_ms)) |
| 1601 | partial = 0; |
| 1602 | else |
| 1603 | partial = ACL_PARTIAL; |
| 1604 | |
| 1605 | list_for_each_entry(rule, &t->fe->tcp_req.inspect_rules, list) { |
| 1606 | int ret = ACL_PAT_PASS; |
| 1607 | |
| 1608 | if (rule->cond) { |
| 1609 | ret = acl_exec_cond(rule->cond, t->fe, t, NULL, ACL_DIR_REQ | partial); |
| 1610 | if (ret == ACL_PAT_MISS) { |
| 1611 | req->rex = tick_add_ifset(now_ms, t->fe->timeout.client); |
| 1612 | return 0; |
| 1613 | } |
| 1614 | ret = acl_pass(ret); |
| 1615 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 1616 | ret = !ret; |
| 1617 | } |
| 1618 | |
| 1619 | if (ret) { |
| 1620 | /* we have a matching rule. */ |
| 1621 | if (rule->action == TCP_ACT_REJECT) { |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 1622 | buffer_shutr_done(req); |
| 1623 | buffer_shutw_done(rep); |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1624 | fd_delete(t->cli_fd); |
| 1625 | t->cli_state = CL_STCLOSE; |
| 1626 | t->fe->failed_req++; |
| 1627 | if (!(t->flags & SN_ERR_MASK)) |
| 1628 | t->flags |= SN_ERR_PRXCOND; |
| 1629 | if (!(t->flags & SN_FINST_MASK)) |
| 1630 | t->flags |= SN_FINST_R; |
| 1631 | t->inspect_exp = TICK_ETERNITY; |
| 1632 | return 1; |
| 1633 | } |
| 1634 | /* otherwise accept */ |
| 1635 | break; |
| 1636 | } |
| 1637 | } |
| 1638 | |
| 1639 | /* if we get there, it means we have no rule which matches, so |
| 1640 | * we apply the default accept. |
| 1641 | */ |
| 1642 | req->rex = tick_add_ifset(now_ms, t->fe->timeout.client); |
| 1643 | if (t->fe->mode == PR_MODE_HTTP) { |
| 1644 | t->cli_state = CL_STHEADERS; |
| 1645 | t->txn.exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq); |
| 1646 | } else { |
| 1647 | t->cli_state = CL_STDATA; |
Willy Tarreau | 718f0ef | 2008-08-10 16:21:32 +0200 | [diff] [blame] | 1648 | req->flags |= BF_MAY_CONNECT | BF_MAY_FORWARD; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1649 | } |
| 1650 | t->inspect_exp = TICK_ETERNITY; |
| 1651 | return 1; |
| 1652 | } |
| 1653 | else if (c == CL_STHEADERS) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1654 | /* |
| 1655 | * Now parse the partial (or complete) lines. |
| 1656 | * We will check the request syntax, and also join multi-line |
| 1657 | * headers. An index of all the lines will be elaborated while |
| 1658 | * parsing. |
| 1659 | * |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1660 | * For the parsing, we use a 28 states FSM. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1661 | * |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1662 | * Here is the information we currently have : |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1663 | * req->data + req->som = beginning of request |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1664 | * req->data + req->eoh = end of processed headers / start of current one |
| 1665 | * req->data + req->eol = end of current header or line (LF or CRLF) |
| 1666 | * req->lr = first non-visited byte |
| 1667 | * req->r = end of data |
| 1668 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1669 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1670 | int cur_idx; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1671 | struct http_txn *txn = &t->txn; |
| 1672 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1673 | struct proxy *cur_proxy; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1674 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1675 | if (likely(req->lr < req->r)) |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1676 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1677 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1678 | /* 1: we might have to print this header in debug mode */ |
| 1679 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1680 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1681 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1682 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1683 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1684 | sol = req->data + msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1685 | eol = sol + msg->sl.rq.l; |
| 1686 | debug_hdr("clireq", t, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 1687 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1688 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 1689 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1690 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1691 | while (cur_idx) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1692 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1693 | debug_hdr("clihdr", t, sol, eol); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1694 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 1695 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1696 | } |
| 1697 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1698 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1699 | |
| 1700 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1701 | * Now we quickly check if we have found a full valid request. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1702 | * If not so, we check the FD and buffer states before leaving. |
| 1703 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1704 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 1705 | * requests are checked first. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1706 | * |
| 1707 | */ |
| 1708 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1709 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1710 | /* |
| 1711 | * First, let's catch bad requests. |
| 1712 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1713 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1714 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1715 | |
| 1716 | /* 1: Since we are in header mode, if there's no space |
| 1717 | * left for headers, we won't be able to free more |
| 1718 | * later, so the session will never terminate. We |
| 1719 | * must terminate it now. |
| 1720 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1721 | if (unlikely(req->l >= req->rlim - req->data)) { |
| 1722 | /* FIXME: check if URI is set and return Status |
| 1723 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1724 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1725 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1726 | } |
| 1727 | |
| 1728 | /* 2: have we encountered a read error or a close ? */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1729 | else if (unlikely(req->flags & (BF_READ_ERROR | BF_READ_NULL))) { |
| 1730 | /* read error, or last read : give up. */ |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 1731 | buffer_shutr_done(req); |
| 1732 | buffer_shutw_done(rep); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1733 | fd_delete(t->cli_fd); |
| 1734 | t->cli_state = CL_STCLOSE; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 1735 | t->fe->failed_req++; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1736 | if (!(t->flags & SN_ERR_MASK)) |
| 1737 | t->flags |= SN_ERR_CLICL; |
| 1738 | if (!(t->flags & SN_FINST_MASK)) |
| 1739 | t->flags |= SN_FINST_R; |
| 1740 | return 1; |
| 1741 | } |
| 1742 | |
| 1743 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1744 | else if (unlikely(tick_is_expired(req->rex, now_ms) || |
| 1745 | tick_is_expired(txn->exp, now_ms))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1746 | /* read timeout : give up with an error message. */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1747 | txn->status = 408; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 1748 | client_retnclose(t, error_message(t, HTTP_ERR_408)); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 1749 | t->fe->failed_req++; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1750 | if (!(t->flags & SN_ERR_MASK)) |
| 1751 | t->flags |= SN_ERR_CLITO; |
| 1752 | if (!(t->flags & SN_FINST_MASK)) |
| 1753 | t->flags |= SN_FINST_R; |
| 1754 | return 1; |
| 1755 | } |
| 1756 | |
| 1757 | /* 4: do we need to re-enable the read socket ? */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 1758 | else if (unlikely(EV_FD_COND_S(t->cli_fd, DIR_RD))) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 1759 | /* fd in DIR_RD was disabled, perhaps because of a previous buffer |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1760 | * full. We cannot loop here since stream_sock_read will disable it only if |
| 1761 | * req->l == rlim-data |
| 1762 | */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1763 | req->rex = tick_add_ifset(now_ms, t->fe->timeout.client); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1764 | } |
| 1765 | return t->cli_state != CL_STHEADERS; |
| 1766 | } |
| 1767 | |
| 1768 | |
| 1769 | /**************************************************************** |
| 1770 | * More interesting part now : we know that we have a complete * |
| 1771 | * request which at least looks like HTTP. We have an indicator * |
| 1772 | * of each header's length, so we can parse them quickly. * |
| 1773 | ****************************************************************/ |
| 1774 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1775 | /* ensure we keep this pointer to the beginning of the message */ |
| 1776 | msg->sol = req->data + msg->som; |
| 1777 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1778 | /* |
| 1779 | * 1: identify the method |
| 1780 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1781 | 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] | 1782 | |
| 1783 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1784 | * 2: check if the URI matches the monitor_uri. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1785 | * We have to do this for every request which gets in, because |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1786 | * the monitor-uri is defined by the frontend. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1787 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1788 | if (unlikely((t->fe->monitor_uri_len != 0) && |
| 1789 | (t->fe->monitor_uri_len == msg->sl.rq.u_l) && |
| 1790 | !memcmp(&req->data[msg->sl.rq.u], |
| 1791 | t->fe->monitor_uri, |
| 1792 | t->fe->monitor_uri_len))) { |
| 1793 | /* |
| 1794 | * We have found the monitor URI |
| 1795 | */ |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1796 | struct acl_cond *cond; |
| 1797 | cur_proxy = t->fe; |
| 1798 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1799 | t->flags |= SN_MONITOR; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1800 | |
| 1801 | /* Check if we want to fail this monitor request or not */ |
| 1802 | list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) { |
| 1803 | 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] | 1804 | |
| 1805 | ret = acl_pass(ret); |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1806 | if (cond->pol == ACL_COND_UNLESS) |
| 1807 | ret = !ret; |
| 1808 | |
| 1809 | if (ret) { |
| 1810 | /* we fail this request, let's return 503 service unavail */ |
| 1811 | txn->status = 503; |
| 1812 | client_retnclose(t, error_message(t, HTTP_ERR_503)); |
| 1813 | goto return_prx_cond; |
| 1814 | } |
| 1815 | } |
| 1816 | |
| 1817 | /* nothing to fail, let's reply normaly */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1818 | txn->status = 200; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1819 | client_retnclose(t, &http_200_chunk); |
| 1820 | goto return_prx_cond; |
| 1821 | } |
| 1822 | |
| 1823 | /* |
| 1824 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 1825 | * Note: we cannot log anymore if the request has been |
| 1826 | * classified as invalid. |
| 1827 | */ |
| 1828 | if (unlikely(t->logs.logwait & LW_REQ)) { |
| 1829 | /* we have a complete HTTP request that we must log */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 1830 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1831 | int urilen = msg->sl.rq.l; |
| 1832 | |
| 1833 | if (urilen >= REQURI_LEN) |
| 1834 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1835 | memcpy(txn->uri, &req->data[msg->som], urilen); |
| 1836 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1837 | |
| 1838 | if (!(t->logs.logwait &= ~LW_REQ)) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1839 | http_sess_log(t); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1840 | } else { |
| 1841 | Alert("HTTP logging : out of memory.\n"); |
| 1842 | } |
| 1843 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1844 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1845 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1846 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 1847 | if (unlikely(msg->sl.rq.v_l == 0)) { |
| 1848 | int delta; |
| 1849 | char *cur_end; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1850 | msg->sol = req->data + msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1851 | cur_end = msg->sol + msg->sl.rq.l; |
| 1852 | delta = 0; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1853 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1854 | if (msg->sl.rq.u_l == 0) { |
| 1855 | /* if no URI was set, add "/" */ |
| 1856 | delta = buffer_replace2(req, cur_end, cur_end, " /", 2); |
| 1857 | cur_end += delta; |
| 1858 | msg->eoh += delta; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1859 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1860 | /* add HTTP version */ |
| 1861 | delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11); |
| 1862 | msg->eoh += delta; |
| 1863 | cur_end += delta; |
| 1864 | cur_end = (char *)http_parse_reqline(msg, req->data, |
| 1865 | HTTP_MSG_RQMETH, |
| 1866 | msg->sol, cur_end + 1, |
| 1867 | NULL, NULL); |
| 1868 | if (unlikely(!cur_end)) |
| 1869 | goto return_bad_req; |
| 1870 | |
| 1871 | /* we have a full HTTP/1.0 request now and we know that |
| 1872 | * we have either a CR or an LF at <ptr>. |
| 1873 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1874 | 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] | 1875 | } |
| 1876 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1877 | |
| 1878 | /* 5: we may need to capture headers */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1879 | if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap)) |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1880 | capture_headers(req->data + msg->som, &txn->hdr_idx, |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1881 | txn->req.cap, t->fe->req_cap); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1882 | |
| 1883 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1884 | * 6: we will have to evaluate the filters. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1885 | * As opposed to version 1.2, now they will be evaluated in the |
| 1886 | * filters order and not in the header order. This means that |
| 1887 | * each filter has to be validated among all headers. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1888 | * |
| 1889 | * We can now check whether we want to switch to another |
| 1890 | * backend, in which case we will re-check the backend's |
| 1891 | * filters and various options. In order to support 3-level |
| 1892 | * switching, here's how we should proceed : |
| 1893 | * |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1894 | * a) run be. |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1895 | * if (switch) then switch ->be to the new backend. |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1896 | * b) run be if (be != fe). |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1897 | * There cannot be any switch from there, so ->be cannot be |
| 1898 | * changed anymore. |
| 1899 | * |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1900 | * => filters always apply to ->be, then ->be may change. |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1901 | * |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1902 | * The response path will be able to apply either ->be, or |
| 1903 | * ->be then ->fe filters in order to match the reverse of |
| 1904 | * the forward sequence. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1905 | */ |
| 1906 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1907 | do { |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 1908 | struct acl_cond *cond; |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 1909 | struct redirect_rule *rule; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1910 | struct proxy *rule_set = t->be; |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1911 | cur_proxy = t->be; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1912 | |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 1913 | /* first check whether we have some ACLs set to redirect this request */ |
| 1914 | list_for_each_entry(rule, &cur_proxy->redirect_rules, list) { |
| 1915 | 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] | 1916 | |
| 1917 | ret = acl_pass(ret); |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 1918 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 1919 | ret = !ret; |
| 1920 | |
| 1921 | if (ret) { |
| 1922 | struct chunk rdr = { trash, 0 }; |
| 1923 | const char *msg_fmt; |
| 1924 | |
| 1925 | /* build redirect message */ |
| 1926 | switch(rule->code) { |
| 1927 | case 303: |
| 1928 | rdr.len = strlen(HTTP_303); |
| 1929 | msg_fmt = HTTP_303; |
| 1930 | break; |
| 1931 | case 301: |
| 1932 | rdr.len = strlen(HTTP_301); |
| 1933 | msg_fmt = HTTP_301; |
| 1934 | break; |
| 1935 | case 302: |
| 1936 | default: |
| 1937 | rdr.len = strlen(HTTP_302); |
| 1938 | msg_fmt = HTTP_302; |
| 1939 | break; |
| 1940 | } |
| 1941 | |
| 1942 | if (unlikely(rdr.len > sizeof(trash))) |
| 1943 | goto return_bad_req; |
| 1944 | memcpy(rdr.str, msg_fmt, rdr.len); |
| 1945 | |
| 1946 | switch(rule->type) { |
| 1947 | case REDIRECT_TYPE_PREFIX: { |
| 1948 | const char *path; |
| 1949 | int pathlen; |
| 1950 | |
| 1951 | path = http_get_path(txn); |
| 1952 | /* build message using path */ |
| 1953 | if (path) { |
| 1954 | pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path; |
| 1955 | } else { |
| 1956 | path = "/"; |
| 1957 | pathlen = 1; |
| 1958 | } |
| 1959 | |
| 1960 | if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4) |
| 1961 | goto return_bad_req; |
| 1962 | |
| 1963 | /* add prefix */ |
| 1964 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 1965 | rdr.len += rule->rdr_len; |
| 1966 | |
| 1967 | /* add path */ |
| 1968 | memcpy(rdr.str + rdr.len, path, pathlen); |
| 1969 | rdr.len += pathlen; |
| 1970 | break; |
| 1971 | } |
| 1972 | case REDIRECT_TYPE_LOCATION: |
| 1973 | default: |
| 1974 | if (rdr.len + rule->rdr_len > sizeof(trash) - 4) |
| 1975 | goto return_bad_req; |
| 1976 | |
| 1977 | /* add location */ |
| 1978 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 1979 | rdr.len += rule->rdr_len; |
| 1980 | break; |
| 1981 | } |
| 1982 | |
| 1983 | /* add end of headers */ |
| 1984 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 1985 | rdr.len += 4; |
| 1986 | |
| 1987 | txn->status = rule->code; |
| 1988 | /* let's log the request time */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1989 | t->logs.tv_request = now; |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 1990 | client_retnclose(t, &rdr); |
| 1991 | goto return_prx_cond; |
| 1992 | } |
| 1993 | } |
| 1994 | |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 1995 | /* first check whether we have some ACLs set to block this request */ |
| 1996 | list_for_each_entry(cond, &cur_proxy->block_cond, list) { |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 1997 | 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] | 1998 | |
| 1999 | ret = acl_pass(ret); |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2000 | if (cond->pol == ACL_COND_UNLESS) |
| 2001 | ret = !ret; |
| 2002 | |
| 2003 | if (ret) { |
| 2004 | txn->status = 403; |
| 2005 | /* let's log the request time */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2006 | t->logs.tv_request = now; |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2007 | client_retnclose(t, error_message(t, HTTP_ERR_403)); |
| 2008 | goto return_prx_cond; |
| 2009 | } |
| 2010 | } |
| 2011 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2012 | /* try headers filters */ |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2013 | if (rule_set->req_exp != NULL) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2014 | if (apply_filters_to_request(t, req, rule_set->req_exp) < 0) |
| 2015 | goto return_bad_req; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2016 | } |
| 2017 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2018 | if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) { |
| 2019 | /* to ensure correct connection accounting on |
| 2020 | * the backend, we count the connection for the |
| 2021 | * one managing the queue. |
| 2022 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2023 | t->be->beconn++; |
| 2024 | if (t->be->beconn > t->be->beconn_max) |
| 2025 | t->be->beconn_max = t->be->beconn; |
| 2026 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2027 | t->flags |= SN_BE_ASSIGNED; |
| 2028 | } |
| 2029 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2030 | /* has the request been denied ? */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2031 | if (txn->flags & TX_CLDENY) { |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2032 | /* no need to go further */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2033 | txn->status = 403; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2034 | /* let's log the request time */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2035 | t->logs.tv_request = now; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2036 | client_retnclose(t, error_message(t, HTTP_ERR_403)); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2037 | goto return_prx_cond; |
| 2038 | } |
| 2039 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2040 | /* We might have to check for "Connection:" */ |
Krzysztof Oledzki | 336d475 | 2007-12-25 02:40:22 +0100 | [diff] [blame] | 2041 | 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] | 2042 | !(t->flags & SN_CONN_CLOSED)) { |
| 2043 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2044 | int cur_idx, old_idx, delta, val; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2045 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2046 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2047 | 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] | 2048 | old_idx = 0; |
| 2049 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2050 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 2051 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2052 | cur_ptr = cur_next; |
| 2053 | cur_end = cur_ptr + cur_hdr->len; |
| 2054 | cur_next = cur_end + cur_hdr->cr + 1; |
| 2055 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2056 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 2057 | if (val) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2058 | /* 3 possibilities : |
| 2059 | * - we have already set Connection: close, |
| 2060 | * so we remove this line. |
| 2061 | * - we have not yet set Connection: close, |
| 2062 | * but this line indicates close. We leave |
| 2063 | * it untouched and set the flag. |
| 2064 | * - we have not yet set Connection: close, |
| 2065 | * and this line indicates non-close. We |
| 2066 | * replace it. |
| 2067 | */ |
| 2068 | if (t->flags & SN_CONN_CLOSED) { |
| 2069 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2070 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2071 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2072 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 2073 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2074 | cur_hdr->len = 0; |
| 2075 | } else { |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2076 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 2077 | delta = buffer_replace2(req, cur_ptr + val, cur_end, |
| 2078 | "close", 5); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2079 | cur_next += delta; |
| 2080 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2081 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2082 | } |
| 2083 | t->flags |= SN_CONN_CLOSED; |
| 2084 | } |
| 2085 | } |
| 2086 | old_idx = cur_idx; |
| 2087 | } |
Willy Tarreau | f2f0ee8 | 2007-03-30 12:02:43 +0200 | [diff] [blame] | 2088 | } |
| 2089 | /* add request headers from the rule sets in the same order */ |
| 2090 | for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) { |
| 2091 | if (unlikely(http_header_add_tail(req, |
| 2092 | &txn->req, |
| 2093 | &txn->hdr_idx, |
| 2094 | rule_set->req_add[cur_idx])) < 0) |
| 2095 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2096 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2097 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2098 | /* check if stats URI was requested, and if an auth is needed */ |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 2099 | if (rule_set->uri_auth != NULL && |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2100 | (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) { |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2101 | /* we have to check the URI and auth for this request */ |
| 2102 | if (stats_check_uri_auth(t, rule_set)) |
| 2103 | return 1; |
| 2104 | } |
| 2105 | |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2106 | /* now check whether we have some switching rules for this request */ |
| 2107 | if (!(t->flags & SN_BE_ASSIGNED)) { |
| 2108 | struct switching_rule *rule; |
| 2109 | |
| 2110 | list_for_each_entry(rule, &cur_proxy->switching_rules, list) { |
| 2111 | int ret; |
| 2112 | |
| 2113 | 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] | 2114 | |
| 2115 | ret = acl_pass(ret); |
Willy Tarreau | a8cfa34 | 2008-07-09 11:23:31 +0200 | [diff] [blame] | 2116 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2117 | ret = !ret; |
| 2118 | |
| 2119 | if (ret) { |
| 2120 | t->be = rule->be.backend; |
| 2121 | t->be->beconn++; |
| 2122 | if (t->be->beconn > t->be->beconn_max) |
| 2123 | t->be->beconn_max = t->be->beconn; |
| 2124 | t->be->cum_beconn++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2125 | |
| 2126 | /* assign new parameters to the session from the new backend */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2127 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 2128 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2129 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2130 | t->flags |= SN_BE_ASSIGNED; |
| 2131 | break; |
| 2132 | } |
| 2133 | } |
| 2134 | } |
| 2135 | |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 2136 | if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) { |
| 2137 | /* No backend was set, but there was a default |
| 2138 | * backend set in the frontend, so we use it and |
| 2139 | * loop again. |
| 2140 | */ |
| 2141 | t->be = cur_proxy->defbe.be; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2142 | t->be->beconn++; |
| 2143 | if (t->be->beconn > t->be->beconn_max) |
| 2144 | t->be->beconn_max = t->be->beconn; |
| 2145 | t->be->cum_beconn++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2146 | |
| 2147 | /* assign new parameters to the session from the new backend */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2148 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 2149 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2150 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 2151 | t->flags |= SN_BE_ASSIGNED; |
| 2152 | } |
| 2153 | } 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] | 2154 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2155 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2156 | if (!(t->flags & SN_BE_ASSIGNED)) { |
| 2157 | /* To ensure correct connection accounting on |
| 2158 | * the backend, we count the connection for the |
| 2159 | * one managing the queue. |
| 2160 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2161 | t->be->beconn++; |
| 2162 | if (t->be->beconn > t->be->beconn_max) |
| 2163 | t->be->beconn_max = t->be->beconn; |
| 2164 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2165 | t->flags |= SN_BE_ASSIGNED; |
| 2166 | } |
| 2167 | |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2168 | /* |
| 2169 | * Right now, we know that we have processed the entire headers |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2170 | * and that unwanted requests have been filtered out. We can do |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2171 | * whatever we want with the remaining request. Also, now we |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2172 | * may have separate values for ->fe, ->be. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2173 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2174 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 2175 | /* |
| 2176 | * If HTTP PROXY is set we simply get remote server address |
| 2177 | * parsing incoming request. |
| 2178 | */ |
| 2179 | if ((t->be->options & PR_O_HTTP_PROXY) && !(t->flags & SN_ADDR_SET)) { |
| 2180 | url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &t->srv_addr); |
| 2181 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2182 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2183 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2184 | * 7: the appsession cookie was looked up very early in 1.2, |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2185 | * so let's do the same now. |
| 2186 | */ |
| 2187 | |
| 2188 | /* It needs to look into the URI */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2189 | if (t->be->appsession_name) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2190 | 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] | 2191 | } |
| 2192 | |
| 2193 | |
| 2194 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2195 | * 8: Now we can work with the cookies. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2196 | * Note that doing so might move headers in the request, but |
| 2197 | * the fields will stay coherent and the URI will not move. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2198 | * This should only be performed in the backend. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2199 | */ |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 2200 | if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name) |
| 2201 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2202 | manage_client_side_cookies(t, req); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2203 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2204 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2205 | /* |
Willy Tarreau | bb046ac | 2007-03-03 19:17:03 +0100 | [diff] [blame] | 2206 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 2207 | * asks for it. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2208 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2209 | if ((t->fe->options | t->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2210 | if (t->cli_addr.ss_family == AF_INET) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2211 | /* Add an X-Forwarded-For header unless the source IP is |
| 2212 | * in the 'except' network range. |
| 2213 | */ |
| 2214 | if ((!t->fe->except_mask.s_addr || |
| 2215 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr) |
| 2216 | != t->fe->except_net.s_addr) && |
| 2217 | (!t->be->except_mask.s_addr || |
| 2218 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr) |
| 2219 | != t->be->except_net.s_addr)) { |
| 2220 | int len; |
| 2221 | unsigned char *pn; |
| 2222 | pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr; |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2223 | |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2224 | /* Note: we rely on the backend to get the header name to be used for |
| 2225 | * x-forwarded-for, because the header is really meant for the backends. |
| 2226 | * However, if the backend did not specify any option, we have to rely |
| 2227 | * on the frontend's header name. |
| 2228 | */ |
| 2229 | if (t->be->fwdfor_hdr_len) { |
| 2230 | len = t->be->fwdfor_hdr_len; |
| 2231 | memcpy(trash, t->be->fwdfor_hdr_name, len); |
| 2232 | } else { |
| 2233 | len = t->fe->fwdfor_hdr_len; |
| 2234 | memcpy(trash, t->fe->fwdfor_hdr_name, len); |
| 2235 | } |
| 2236 | 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] | 2237 | |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2238 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2239 | &txn->hdr_idx, trash, len)) < 0) |
| 2240 | goto return_bad_req; |
| 2241 | } |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2242 | } |
| 2243 | else if (t->cli_addr.ss_family == AF_INET6) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2244 | /* FIXME: for the sake of completeness, we should also support |
| 2245 | * 'except' here, although it is mostly useless in this case. |
| 2246 | */ |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2247 | int len; |
| 2248 | char pn[INET6_ADDRSTRLEN]; |
| 2249 | inet_ntop(AF_INET6, |
| 2250 | (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr, |
| 2251 | pn, sizeof(pn)); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2252 | |
| 2253 | /* Note: we rely on the backend to get the header name to be used for |
| 2254 | * x-forwarded-for, because the header is really meant for the backends. |
| 2255 | * However, if the backend did not specify any option, we have to rely |
| 2256 | * on the frontend's header name. |
| 2257 | */ |
| 2258 | if (t->be->fwdfor_hdr_len) { |
| 2259 | len = t->be->fwdfor_hdr_len; |
| 2260 | memcpy(trash, t->be->fwdfor_hdr_name, len); |
| 2261 | } else { |
| 2262 | len = t->fe->fwdfor_hdr_len; |
| 2263 | memcpy(trash, t->fe->fwdfor_hdr_name, len); |
| 2264 | } |
| 2265 | len += sprintf(trash + len, ": %s", pn); |
| 2266 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2267 | if (unlikely(http_header_add_tail2(req, &txn->req, |
| 2268 | &txn->hdr_idx, trash, len)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2269 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2270 | } |
| 2271 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2272 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2273 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2274 | * 10: add "Connection: close" if needed and not yet set. |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2275 | * 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] | 2276 | */ |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2277 | if (!(t->flags & SN_CONN_CLOSED) && |
Krzysztof Oledzki | 336d475 | 2007-12-25 02:40:22 +0100 | [diff] [blame] | 2278 | ((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] | 2279 | if ((unlikely(msg->sl.rq.v_l != 8) || |
| 2280 | unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) && |
| 2281 | unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx, |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2282 | "Connection: close", 17)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2283 | goto return_bad_req; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2284 | t->flags |= SN_CONN_CLOSED; |
Willy Tarreau | e15d913 | 2006-12-14 22:26:42 +0100 | [diff] [blame] | 2285 | } |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2286 | /* Before we switch to data, was assignment set in manage_client_side_cookie? |
| 2287 | * If not assigned, perhaps we are balancing on url_param, but this is a |
| 2288 | * POST; and the parameters are in the body, maybe scan there to find our server. |
| 2289 | * (unless headers overflowed the buffer?) |
| 2290 | */ |
| 2291 | if (!(t->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 2292 | t->txn.meth == HTTP_METH_POST && t->be->url_param_name != NULL && |
Willy Tarreau | fb0528b | 2008-08-11 00:21:56 +0200 | [diff] [blame] | 2293 | t->be->url_param_post_limit != 0 && req->l < BUFSIZE && |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2294 | memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) { |
| 2295 | /* are there enough bytes here? total == l || r || rlim ? |
| 2296 | * len is unsigned, but eoh is int, |
| 2297 | * how many bytes of body have we received? |
| 2298 | * eoh is the first empty line of the header |
| 2299 | */ |
| 2300 | /* 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] | 2301 | 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] | 2302 | |
| 2303 | /* If we have HTTP/1.1 and Expect: 100-continue, then abort. |
| 2304 | * We can't assume responsibility for the server's decision, |
| 2305 | * on this URI and header set. See rfc2616: 14.20, 8.2.3, |
| 2306 | * We also can't change our mind later, about which server to choose, so round robin. |
| 2307 | */ |
| 2308 | if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) { |
| 2309 | struct hdr_ctx ctx; |
| 2310 | ctx.idx = 0; |
| 2311 | /* Expect is allowed in 1.1, look for it */ |
| 2312 | http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx); |
| 2313 | if (ctx.idx != 0 && |
| 2314 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val,"100-continue",12)==0)) |
| 2315 | /* We can't reliablly stall and wait for data, because of |
| 2316 | * .NET clients that don't conform to rfc2616; so, no need for |
| 2317 | * the next block to check length expectations. |
| 2318 | * We could send 100 status back to the client, but then we need to |
| 2319 | * re-write headers, and send the message. And this isn't the right |
| 2320 | * place for that action. |
| 2321 | * TODO: support Expect elsewhere and delete this block. |
| 2322 | */ |
| 2323 | goto end_check_maybe_wait_for_body; |
| 2324 | } |
| 2325 | if ( likely(len > t->be->url_param_post_limit) ) { |
| 2326 | /* nothing to do, we got enough */ |
| 2327 | } else { |
| 2328 | /* limit implies we are supposed to need this many bytes |
| 2329 | * to find the parameter. Let's see how many bytes we can wait for. |
| 2330 | */ |
| 2331 | long long hint = len; |
| 2332 | struct hdr_ctx ctx; |
| 2333 | ctx.idx = 0; |
| 2334 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx); |
| 2335 | if (unlikely(ctx.idx && strncasecmp(ctx.line+ctx.val,"chunked",7)==0)) { |
| 2336 | t->srv_state = SV_STANALYZE; |
| 2337 | } else { |
| 2338 | ctx.idx = 0; |
| 2339 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx); |
| 2340 | /* now if we have a length, we'll take the hint */ |
| 2341 | if ( ctx.idx ) { |
| 2342 | /* We have Content-Length */ |
| 2343 | if ( strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint) ) |
| 2344 | hint = 0; /* parse failure, untrusted client */ |
| 2345 | else { |
| 2346 | if ( hint > 0 ) |
| 2347 | msg->hdr_content_len = hint; |
| 2348 | else |
| 2349 | hint = 0; /* bad client, sent negative length */ |
| 2350 | } |
| 2351 | } |
| 2352 | /* but limited to what we care about, maybe we don't expect any entity data (hint == 0) */ |
| 2353 | if ( t->be->url_param_post_limit < hint ) |
| 2354 | hint = t->be->url_param_post_limit; |
| 2355 | /* now do we really need to buffer more data? */ |
| 2356 | if ( len < hint ) |
| 2357 | t->srv_state = SV_STANALYZE; |
| 2358 | /* else... There are no body bytes to wait for */ |
| 2359 | } |
| 2360 | } |
| 2361 | } |
| 2362 | end_check_maybe_wait_for_body: |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2363 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2364 | /************************************************************* |
| 2365 | * OK, that's finished for the headers. We have done what we * |
| 2366 | * could. Let's switch to the DATA state. * |
| 2367 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2368 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2369 | t->cli_state = CL_STDATA; |
Willy Tarreau | 718f0ef | 2008-08-10 16:21:32 +0200 | [diff] [blame] | 2370 | req->flags |= BF_MAY_CONNECT | BF_MAY_FORWARD; |
Willy Tarreau | dc0a6a0 | 2008-08-03 20:38:13 +0200 | [diff] [blame] | 2371 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2372 | req->rlim = req->data + BUFSIZE; /* no more rewrite needed */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2373 | |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2374 | t->logs.tv_request = now; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2375 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2376 | if (!t->fe->timeout.client || |
Willy Tarreau | 718f0ef | 2008-08-10 16:21:32 +0200 | [diff] [blame] | 2377 | (!(rep->flags & BF_MAY_FORWARD) && t->be->timeout.server)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2378 | /* If the client has no timeout, or if the server is not ready yet, |
| 2379 | * and we know for sure that it can expire, then it's cleaner to |
| 2380 | * disable the timeout on the client side so that too low values |
| 2381 | * cannot make the sessions abort too early. |
| 2382 | * |
| 2383 | * FIXME-20050705: the server needs a way to re-enable this time-out |
| 2384 | * when it switches its state, otherwise a client can stay connected |
| 2385 | * indefinitely. This now seems to be OK. |
| 2386 | */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2387 | req->rex = TICK_ETERNITY; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2388 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2389 | |
Willy Tarreau | 1fa3126 | 2007-12-03 00:36:16 +0100 | [diff] [blame] | 2390 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 2391 | * which may be the same as the connect timeout if unspecified. |
| 2392 | * If unset, then set it to zero because we really want it to |
| 2393 | * eventually expire. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2394 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2395 | if (txn->flags & TX_CLTARPIT) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2396 | t->req->l = 0; |
| 2397 | /* flush the request so that we can drop the connection early |
| 2398 | * if the client closes first. |
| 2399 | */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2400 | req->cex = tick_add_ifset(now_ms, t->be->timeout.tarpit); |
| 2401 | if (!req->cex) |
| 2402 | req->cex = now_ms; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2403 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2404 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2405 | /* OK let's go on with the BODY now */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2406 | goto process_data; |
| 2407 | |
| 2408 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2409 | txn->req.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2410 | txn->status = 400; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2411 | client_retnclose(t, error_message(t, HTTP_ERR_400)); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 2412 | t->fe->failed_req++; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2413 | return_prx_cond: |
| 2414 | if (!(t->flags & SN_ERR_MASK)) |
| 2415 | t->flags |= SN_ERR_PRXCOND; |
| 2416 | if (!(t->flags & SN_FINST_MASK)) |
| 2417 | t->flags |= SN_FINST_R; |
| 2418 | return 1; |
| 2419 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2420 | } |
| 2421 | else if (c == CL_STDATA) { |
| 2422 | process_data: |
| 2423 | /* FIXME: this error handling is partly buggy because we always report |
| 2424 | * a 'DATA' phase while we don't know if the server was in IDLE, CONN |
| 2425 | * or HEADER phase. BTW, it's not logical to expire the client while |
| 2426 | * we're waiting for the server to connect. |
| 2427 | */ |
| 2428 | /* read or write error */ |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2429 | if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) { |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 2430 | buffer_shutr_done(req); |
| 2431 | buffer_shutw_done(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2432 | fd_delete(t->cli_fd); |
| 2433 | t->cli_state = CL_STCLOSE; |
| 2434 | if (!(t->flags & SN_ERR_MASK)) |
| 2435 | t->flags |= SN_ERR_CLICL; |
| 2436 | if (!(t->flags & SN_FINST_MASK)) { |
| 2437 | if (t->pend_pos) |
| 2438 | t->flags |= SN_FINST_Q; |
| 2439 | else if (s == SV_STCONN) |
| 2440 | t->flags |= SN_FINST_C; |
| 2441 | else |
| 2442 | t->flags |= SN_FINST_D; |
| 2443 | } |
| 2444 | return 1; |
| 2445 | } |
| 2446 | /* last read, or end of server write */ |
Willy Tarreau | 718f0ef | 2008-08-10 16:21:32 +0200 | [diff] [blame] | 2447 | else if (req->flags & (BF_READ_NULL | BF_SHUTW_STATUS)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2448 | EV_FD_CLR(t->cli_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2449 | buffer_shutr(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2450 | t->cli_state = CL_STSHUTR; |
| 2451 | return 1; |
| 2452 | } |
| 2453 | /* last server read and buffer empty */ |
Willy Tarreau | 718f0ef | 2008-08-10 16:21:32 +0200 | [diff] [blame] | 2454 | else if (rep->flags & BF_SHUTR_STATUS && rep->l == 0) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2455 | EV_FD_CLR(t->cli_fd, DIR_WR); |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 2456 | buffer_shutw_done(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2457 | shutdown(t->cli_fd, SHUT_WR); |
| 2458 | /* We must ensure that the read part is still alive when switching |
| 2459 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2460 | EV_FD_SET(t->cli_fd, DIR_RD); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2461 | req->rex = tick_add_ifset(now_ms, t->fe->timeout.client); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2462 | t->cli_state = CL_STSHUTW; |
| 2463 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 2464 | return 1; |
| 2465 | } |
| 2466 | /* read timeout */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2467 | else if (tick_is_expired(req->rex, now_ms)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2468 | EV_FD_CLR(t->cli_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2469 | buffer_shutr(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2470 | t->cli_state = CL_STSHUTR; |
| 2471 | if (!(t->flags & SN_ERR_MASK)) |
| 2472 | t->flags |= SN_ERR_CLITO; |
| 2473 | if (!(t->flags & SN_FINST_MASK)) { |
| 2474 | if (t->pend_pos) |
| 2475 | t->flags |= SN_FINST_Q; |
| 2476 | else if (s == SV_STCONN) |
| 2477 | t->flags |= SN_FINST_C; |
| 2478 | else |
| 2479 | t->flags |= SN_FINST_D; |
| 2480 | } |
| 2481 | return 1; |
| 2482 | } |
| 2483 | /* write timeout */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2484 | else if (tick_is_expired(rep->wex, now_ms)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2485 | EV_FD_CLR(t->cli_fd, DIR_WR); |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 2486 | buffer_shutw_done(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2487 | shutdown(t->cli_fd, SHUT_WR); |
| 2488 | /* We must ensure that the read part is still alive when switching |
| 2489 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2490 | EV_FD_SET(t->cli_fd, DIR_RD); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2491 | req->rex = tick_add_ifset(now_ms, t->fe->timeout.client); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2492 | |
| 2493 | t->cli_state = CL_STSHUTW; |
| 2494 | if (!(t->flags & SN_ERR_MASK)) |
| 2495 | t->flags |= SN_ERR_CLITO; |
| 2496 | if (!(t->flags & SN_FINST_MASK)) { |
| 2497 | if (t->pend_pos) |
| 2498 | t->flags |= SN_FINST_Q; |
| 2499 | else if (s == SV_STCONN) |
| 2500 | t->flags |= SN_FINST_C; |
| 2501 | else |
| 2502 | t->flags |= SN_FINST_D; |
| 2503 | } |
| 2504 | return 1; |
| 2505 | } |
| 2506 | |
| 2507 | if (req->l >= req->rlim - req->data) { |
| 2508 | /* no room to read more data */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2509 | if (EV_FD_COND_C(t->cli_fd, DIR_RD)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2510 | /* stop reading until we get some space */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2511 | req->rex = TICK_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2512 | } |
| 2513 | } else { |
| 2514 | /* there's still some space in the buffer */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2515 | if (EV_FD_COND_S(t->cli_fd, DIR_RD)) { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2516 | if (!t->fe->timeout.client || |
Willy Tarreau | 718f0ef | 2008-08-10 16:21:32 +0200 | [diff] [blame] | 2517 | (!(rep->flags & BF_MAY_FORWARD) && t->be->timeout.server)) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2518 | /* If the client has no timeout, or if the server not ready yet, and we |
| 2519 | * know for sure that it can expire, then it's cleaner to disable the |
| 2520 | * timeout on the client side so that too low values cannot make the |
Willy Tarreau | 718f0ef | 2008-08-10 16:21:32 +0200 | [diff] [blame] | 2521 | * sessions abort too early. NB: we should only do this in HTTP states |
| 2522 | * before HEADERS. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2523 | */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2524 | req->rex = TICK_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2525 | else |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2526 | req->rex = tick_add(now_ms, t->fe->timeout.client); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2527 | } |
| 2528 | } |
| 2529 | |
Willy Tarreau | 718f0ef | 2008-08-10 16:21:32 +0200 | [diff] [blame] | 2530 | /* we don't enable client write if the buffer is empty, nor if the server has to analyze it */ |
| 2531 | if ((rep->l == 0) || !(rep->flags & BF_MAY_FORWARD)) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2532 | if (EV_FD_COND_C(t->cli_fd, DIR_WR)) { |
| 2533 | /* stop writing */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2534 | rep->wex = TICK_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2535 | } |
| 2536 | } else { |
| 2537 | /* buffer not empty */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2538 | if (EV_FD_COND_S(t->cli_fd, DIR_WR)) { |
| 2539 | /* restart writing */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2540 | rep->wex = tick_add_ifset(now_ms, t->fe->timeout.client); |
| 2541 | if (rep->wex) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2542 | /* FIXME: to prevent the client from expiring read timeouts during writes, |
| 2543 | * we refresh it. */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2544 | req->rex = rep->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2545 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2546 | } |
| 2547 | } |
| 2548 | return 0; /* other cases change nothing */ |
| 2549 | } |
| 2550 | else if (c == CL_STSHUTR) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2551 | if (rep->flags & BF_WRITE_ERROR) { |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 2552 | buffer_shutw_done(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2553 | fd_delete(t->cli_fd); |
| 2554 | t->cli_state = CL_STCLOSE; |
| 2555 | if (!(t->flags & SN_ERR_MASK)) |
| 2556 | t->flags |= SN_ERR_CLICL; |
| 2557 | if (!(t->flags & SN_FINST_MASK)) { |
| 2558 | if (t->pend_pos) |
| 2559 | t->flags |= SN_FINST_Q; |
| 2560 | else if (s == SV_STCONN) |
| 2561 | t->flags |= SN_FINST_C; |
| 2562 | else |
| 2563 | t->flags |= SN_FINST_D; |
| 2564 | } |
| 2565 | return 1; |
| 2566 | } |
Willy Tarreau | 718f0ef | 2008-08-10 16:21:32 +0200 | [diff] [blame] | 2567 | else if ((rep->flags & BF_SHUTR_STATUS) && (rep->l == 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2568 | && !(t->flags & SN_SELF_GEN)) { |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 2569 | buffer_shutw_done(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2570 | fd_delete(t->cli_fd); |
| 2571 | t->cli_state = CL_STCLOSE; |
| 2572 | return 1; |
| 2573 | } |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2574 | else if (tick_is_expired(rep->wex, now_ms)) { |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 2575 | buffer_shutw_done(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2576 | fd_delete(t->cli_fd); |
| 2577 | t->cli_state = CL_STCLOSE; |
| 2578 | if (!(t->flags & SN_ERR_MASK)) |
| 2579 | t->flags |= SN_ERR_CLITO; |
| 2580 | if (!(t->flags & SN_FINST_MASK)) { |
| 2581 | if (t->pend_pos) |
| 2582 | t->flags |= SN_FINST_Q; |
| 2583 | else if (s == SV_STCONN) |
| 2584 | t->flags |= SN_FINST_C; |
| 2585 | else |
| 2586 | t->flags |= SN_FINST_D; |
| 2587 | } |
| 2588 | return 1; |
| 2589 | } |
| 2590 | |
| 2591 | if (t->flags & SN_SELF_GEN) { |
| 2592 | produce_content(t); |
| 2593 | if (rep->l == 0) { |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 2594 | buffer_shutw_done(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2595 | fd_delete(t->cli_fd); |
| 2596 | t->cli_state = CL_STCLOSE; |
| 2597 | return 1; |
| 2598 | } |
| 2599 | } |
| 2600 | |
Willy Tarreau | 718f0ef | 2008-08-10 16:21:32 +0200 | [diff] [blame] | 2601 | if ((rep->l == 0) || !(rep->flags & BF_MAY_FORWARD)) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2602 | if (EV_FD_COND_C(t->cli_fd, DIR_WR)) { |
| 2603 | /* stop writing */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2604 | rep->wex = TICK_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2605 | } |
| 2606 | } else { |
| 2607 | /* buffer not empty */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2608 | if (EV_FD_COND_S(t->cli_fd, DIR_WR)) { |
| 2609 | /* restart writing */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2610 | rep->wex = tick_add_ifset(now_ms, t->fe->timeout.client); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2611 | } |
| 2612 | } |
| 2613 | return 0; |
| 2614 | } |
| 2615 | else if (c == CL_STSHUTW) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2616 | if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 2617 | buffer_shutr_done(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2618 | fd_delete(t->cli_fd); |
| 2619 | t->cli_state = CL_STCLOSE; |
| 2620 | if (!(t->flags & SN_ERR_MASK)) |
| 2621 | t->flags |= SN_ERR_CLICL; |
| 2622 | if (!(t->flags & SN_FINST_MASK)) { |
| 2623 | if (t->pend_pos) |
| 2624 | t->flags |= SN_FINST_Q; |
| 2625 | else if (s == SV_STCONN) |
| 2626 | t->flags |= SN_FINST_C; |
| 2627 | else |
| 2628 | t->flags |= SN_FINST_D; |
| 2629 | } |
| 2630 | return 1; |
| 2631 | } |
Willy Tarreau | 718f0ef | 2008-08-10 16:21:32 +0200 | [diff] [blame] | 2632 | else if (req->flags & (BF_READ_NULL | BF_SHUTW_STATUS)) { |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 2633 | buffer_shutr_done(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2634 | fd_delete(t->cli_fd); |
| 2635 | t->cli_state = CL_STCLOSE; |
| 2636 | return 1; |
| 2637 | } |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2638 | else if (tick_is_expired(req->rex, now_ms)) { |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 2639 | buffer_shutr_done(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2640 | fd_delete(t->cli_fd); |
| 2641 | t->cli_state = CL_STCLOSE; |
| 2642 | if (!(t->flags & SN_ERR_MASK)) |
| 2643 | t->flags |= SN_ERR_CLITO; |
| 2644 | if (!(t->flags & SN_FINST_MASK)) { |
| 2645 | if (t->pend_pos) |
| 2646 | t->flags |= SN_FINST_Q; |
| 2647 | else if (s == SV_STCONN) |
| 2648 | t->flags |= SN_FINST_C; |
| 2649 | else |
| 2650 | t->flags |= SN_FINST_D; |
| 2651 | } |
| 2652 | return 1; |
| 2653 | } |
| 2654 | else if (req->l >= req->rlim - req->data) { |
| 2655 | /* no room to read more data */ |
| 2656 | |
| 2657 | /* FIXME-20050705: is it possible for a client to maintain a session |
| 2658 | * after the timeout by sending more data after it receives a close ? |
| 2659 | */ |
| 2660 | |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2661 | if (EV_FD_COND_C(t->cli_fd, DIR_RD)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2662 | /* stop reading until we get some space */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2663 | req->rex = TICK_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2664 | } |
| 2665 | } else { |
| 2666 | /* there's still some space in the buffer */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2667 | if (EV_FD_COND_S(t->cli_fd, DIR_RD)) { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2668 | req->rex = tick_add_ifset(now_ms, t->fe->timeout.client); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2669 | } |
| 2670 | } |
| 2671 | return 0; |
| 2672 | } |
| 2673 | else { /* CL_STCLOSE: nothing to do */ |
| 2674 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 2675 | int len; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2676 | len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2677 | write(1, trash, len); |
| 2678 | } |
| 2679 | return 0; |
| 2680 | } |
| 2681 | return 0; |
| 2682 | } |
| 2683 | |
| 2684 | |
| 2685 | /* |
| 2686 | * manages the server FSM and its socket. It returns 1 if a state has changed |
| 2687 | * (and a resync may be needed), 0 else. |
| 2688 | */ |
| 2689 | int process_srv(struct session *t) |
| 2690 | { |
| 2691 | int s = t->srv_state; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2692 | struct http_txn *txn = &t->txn; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2693 | struct buffer *req = t->req; |
| 2694 | struct buffer *rep = t->rep; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2695 | int conn_err; |
| 2696 | |
Willy Tarreau | 6468d92 | 2008-08-03 19:15:35 +0200 | [diff] [blame] | 2697 | DPRINTF(stderr,"process_srv: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%u,%u\n", |
Willy Tarreau | 9f1f24b | 2008-08-11 11:20:03 +0200 | [diff] [blame^] | 2698 | cli_stnames[t->cli_state], srv_stnames[t->srv_state], |
Willy Tarreau | 6468d92 | 2008-08-03 19:15:35 +0200 | [diff] [blame] | 2699 | EV_FD_ISSET(t->srv_fd, DIR_RD), EV_FD_ISSET(t->srv_fd, DIR_WR), |
| 2700 | rep->rex, req->wex); |
Willy Tarreau | ee99136 | 2007-05-14 14:37:50 +0200 | [diff] [blame] | 2701 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2702 | if (s == SV_STIDLE) { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2703 | /* NOTE: The client processor may switch to SV_STANALYZE, which switches back SV_STIDLE. |
| 2704 | * This is logcially after CL_STHEADERS completed, CL_STDATA has started, but |
| 2705 | * we need to defer server selection until more data arrives, if possible. |
| 2706 | * This is rare, and only if balancing on parameter hash with values in the entity of a POST |
| 2707 | */ |
Willy Tarreau | dc0a6a0 | 2008-08-03 20:38:13 +0200 | [diff] [blame] | 2708 | if ((rep->flags & BF_SHUTW_STATUS) || |
Willy Tarreau | 6468d92 | 2008-08-03 19:15:35 +0200 | [diff] [blame] | 2709 | ((req->flags & BF_SHUTR_STATUS) && |
| 2710 | (req->l == 0 || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2711 | req->cex = TICK_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2712 | if (t->pend_pos) |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2713 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2714 | /* note that this must not return any error because it would be able to |
| 2715 | * overwrite the client_retnclose() output. |
| 2716 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2717 | if (txn->flags & TX_CLTARPIT) |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 2718 | srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_T, 0, NULL); |
Willy Tarreau | 08fa2e3 | 2006-09-03 10:47:37 +0200 | [diff] [blame] | 2719 | else |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 2720 | srv_close_with_err(t, SN_ERR_CLICL, t->pend_pos ? SN_FINST_Q : SN_FINST_C, 0, NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2721 | |
| 2722 | return 1; |
| 2723 | } |
Willy Tarreau | dc0a6a0 | 2008-08-03 20:38:13 +0200 | [diff] [blame] | 2724 | else if (req->flags & BF_MAY_CONNECT) { |
| 2725 | /* the client allows the server to connect */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2726 | if (txn->flags & TX_CLTARPIT) { |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 2727 | /* This connection is being tarpitted. The CLIENT side has |
| 2728 | * already set the connect expiration date to the right |
| 2729 | * timeout. We just have to check that it has not expired. |
| 2730 | */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2731 | if (!tick_is_expired(req->cex, now_ms)) |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 2732 | return 0; |
| 2733 | |
| 2734 | /* We will set the queue timer to the time spent, just for |
| 2735 | * logging purposes. We fake a 500 server error, so that the |
| 2736 | * attacker will not suspect his connection has been tarpitted. |
| 2737 | * It will not cause trouble to the logs because we can exclude |
| 2738 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 2739 | */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2740 | req->cex = TICK_ETERNITY; |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2741 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 2742 | srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T, |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2743 | 500, error_message(t, HTTP_ERR_500)); |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 2744 | return 1; |
| 2745 | } |
| 2746 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2747 | /* Right now, we will need to create a connection to the server. |
| 2748 | * We might already have tried, and got a connection pending, in |
| 2749 | * which case we will not do anything till it's pending. It's up |
| 2750 | * to any other session to release it and wake us up again. |
| 2751 | */ |
| 2752 | if (t->pend_pos) { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2753 | if (!tick_is_expired(req->cex, now_ms)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2754 | return 0; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 2755 | } else { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2756 | /* we've been waiting too long here */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2757 | req->cex = TICK_ETERNITY; |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2758 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2759 | srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q, |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2760 | 503, error_message(t, HTTP_ERR_503)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2761 | if (t->srv) |
| 2762 | t->srv->failed_conns++; |
Willy Tarreau | 50fd1e1 | 2007-12-10 15:25:35 +0100 | [diff] [blame] | 2763 | t->be->failed_conns++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2764 | return 1; |
| 2765 | } |
| 2766 | } |
| 2767 | |
| 2768 | do { |
| 2769 | /* first, get a connection */ |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2770 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2771 | t->flags |= SN_REDIRECTABLE; |
| 2772 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2773 | if (srv_redispatch_connect(t)) |
| 2774 | return t->srv_state != SV_STIDLE; |
| 2775 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2776 | if ((t->flags & SN_REDIRECTABLE) && t->srv && t->srv->rdr_len) { |
| 2777 | /* Server supporting redirection and it is possible. |
| 2778 | * Invalid requests are reported as such. It concerns all |
| 2779 | * the largest ones. |
| 2780 | */ |
| 2781 | struct chunk rdr; |
| 2782 | char *path; |
| 2783 | int len; |
| 2784 | |
| 2785 | /* 1: create the response header */ |
| 2786 | rdr.len = strlen(HTTP_302); |
| 2787 | rdr.str = trash; |
| 2788 | memcpy(rdr.str, HTTP_302, rdr.len); |
| 2789 | |
| 2790 | /* 2: add the server's prefix */ |
| 2791 | if (rdr.len + t->srv->rdr_len > sizeof(trash)) |
| 2792 | goto cancel_redir; |
| 2793 | |
| 2794 | memcpy(rdr.str + rdr.len, t->srv->rdr_pfx, t->srv->rdr_len); |
| 2795 | rdr.len += t->srv->rdr_len; |
| 2796 | |
| 2797 | /* 3: add the request URI */ |
| 2798 | path = http_get_path(txn); |
| 2799 | if (!path) |
| 2800 | goto cancel_redir; |
| 2801 | len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path; |
| 2802 | if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */ |
| 2803 | goto cancel_redir; |
| 2804 | |
| 2805 | memcpy(rdr.str + rdr.len, path, len); |
| 2806 | rdr.len += len; |
| 2807 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 2808 | rdr.len += 4; |
| 2809 | |
| 2810 | srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr); |
| 2811 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
| 2812 | if (t->srv) |
| 2813 | t->srv->cum_sess++; |
| 2814 | return 1; |
| 2815 | cancel_redir: |
| 2816 | txn->status = 400; |
| 2817 | t->fe->failed_req++; |
| 2818 | srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, |
| 2819 | 400, error_message(t, HTTP_ERR_400)); |
| 2820 | return 1; |
| 2821 | } |
| 2822 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2823 | /* try to (re-)connect to the server, and fail if we expire the |
| 2824 | * number of retries. |
| 2825 | */ |
| 2826 | if (srv_retryable_connect(t)) { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2827 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2828 | return t->srv_state != SV_STIDLE; |
| 2829 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2830 | } while (1); |
| 2831 | } |
| 2832 | } |
| 2833 | else if (s == SV_STCONN) { /* connection in progress */ |
Willy Tarreau | 6468d92 | 2008-08-03 19:15:35 +0200 | [diff] [blame] | 2834 | if ((rep->flags & BF_SHUTW_STATUS) || |
| 2835 | ((req->flags & BF_SHUTR_STATUS) && |
| 2836 | ((req->l == 0 && !(req->flags & BF_WRITE_STATUS)) || |
Willy Tarreau | c9b654b | 2007-05-08 14:46:53 +0200 | [diff] [blame] | 2837 | t->be->options & PR_O_ABRT_CLOSE))) { /* give up */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2838 | req->cex = TICK_ETERNITY; |
Willy Tarreau | f899b94 | 2008-03-28 18:09:38 +0100 | [diff] [blame] | 2839 | if (!(t->flags & SN_CONN_TAR)) { |
| 2840 | /* if we are in turn-around, we have already closed the FD */ |
| 2841 | fd_delete(t->srv_fd); |
| 2842 | if (t->srv) { |
| 2843 | t->srv->cur_sess--; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 2844 | sess_change_server(t, NULL); |
Willy Tarreau | f899b94 | 2008-03-28 18:09:38 +0100 | [diff] [blame] | 2845 | } |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 2846 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2847 | |
| 2848 | /* note that this must not return any error because it would be able to |
| 2849 | * overwrite the client_retnclose() output. |
| 2850 | */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 2851 | srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2852 | return 1; |
| 2853 | } |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2854 | if (!(req->flags & BF_WRITE_STATUS) && !tick_is_expired(req->cex, now_ms)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2855 | return 0; /* nothing changed */ |
| 2856 | } |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2857 | else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2858 | /* timeout, asynchronous connect error or first write error */ |
| 2859 | //fprintf(stderr,"2: c=%d, s=%d\n", c, s); |
| 2860 | |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 2861 | if (t->flags & SN_CONN_TAR) { |
| 2862 | /* We are doing a turn-around waiting for a new connection attempt. */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2863 | if (!tick_is_expired(req->cex, now_ms)) |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 2864 | return 0; |
| 2865 | t->flags &= ~SN_CONN_TAR; |
| 2866 | } |
| 2867 | else { |
| 2868 | fd_delete(t->srv_fd); |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 2869 | if (t->srv) { |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 2870 | t->srv->cur_sess--; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 2871 | sess_change_server(t, NULL); |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 2872 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2873 | |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 2874 | if (!(req->flags & BF_WRITE_STATUS)) |
| 2875 | conn_err = SN_ERR_SRVTO; // it was a connect timeout. |
| 2876 | else |
| 2877 | conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error. |
| 2878 | |
| 2879 | /* ensure that we have enough retries left */ |
| 2880 | if (srv_count_retry_down(t, conn_err)) |
| 2881 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2882 | |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 2883 | if (req->flags & BF_WRITE_ERROR) { |
| 2884 | /* we encountered an immediate connection error, and we |
| 2885 | * will have to retry connecting to the same server, most |
| 2886 | * likely leading to the same result. To avoid this, we |
| 2887 | * fake a connection timeout to retry after a turn-around |
| 2888 | * time of 1 second. We will wait in the previous if block. |
| 2889 | */ |
| 2890 | t->flags |= SN_CONN_TAR; |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2891 | req->cex = tick_add(now_ms, MS_TO_TICKS(1000)); |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 2892 | return 0; |
| 2893 | } |
| 2894 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2895 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2896 | if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) { |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 2897 | /* We're on our last chance, and the REDISP option was specified. |
| 2898 | * We will ignore cookie and force to balance or use the dispatcher. |
| 2899 | */ |
| 2900 | /* let's try to offer this slot to anybody */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2901 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 2902 | process_srv_queue(t->srv); |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 2903 | |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 2904 | /* it's left to the dispatcher to choose a server */ |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 2905 | t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 2906 | t->prev_srv = t->srv; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 2907 | |
| 2908 | /* first, get a connection */ |
| 2909 | if (srv_redispatch_connect(t)) |
Willy Tarreau | 00559e7 | 2008-01-06 23:46:19 +0100 | [diff] [blame] | 2910 | return t->srv_state != SV_STCONN; |
Krzysztof Piotr Oledzki | 626a19b | 2008-02-04 02:10:09 +0100 | [diff] [blame] | 2911 | } else { |
| 2912 | if (t->srv) |
| 2913 | t->srv->retries++; |
| 2914 | t->be->retries++; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 2915 | } |
| 2916 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2917 | do { |
| 2918 | /* Now we will try to either reconnect to the same server or |
| 2919 | * connect to another server. If the connection gets queued |
| 2920 | * because all servers are saturated, then we will go back to |
| 2921 | * the SV_STIDLE state. |
| 2922 | */ |
| 2923 | if (srv_retryable_connect(t)) { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2924 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2925 | return t->srv_state != SV_STCONN; |
| 2926 | } |
| 2927 | |
| 2928 | /* we need to redispatch the connection to another server */ |
| 2929 | if (srv_redispatch_connect(t)) |
| 2930 | return t->srv_state != SV_STCONN; |
| 2931 | } while (1); |
| 2932 | } |
| 2933 | else { /* no error or write 0 */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2934 | t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2935 | |
| 2936 | //fprintf(stderr,"3: c=%d, s=%d\n", c, s); |
| 2937 | if (req->l == 0) /* nothing to write */ { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2938 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2939 | req->wex = TICK_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2940 | } else /* need the right to write */ { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2941 | EV_FD_SET(t->srv_fd, DIR_WR); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2942 | req->wex = tick_add_ifset(now_ms, t->be->timeout.server); |
| 2943 | if (req->wex) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2944 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 2945 | * we refresh it. */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2946 | rep->rex = req->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2947 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2948 | } |
| 2949 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2950 | if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2951 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2952 | rep->rex = tick_add_ifset(now_ms, t->be->timeout.server); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2953 | t->srv_state = SV_STDATA; |
Willy Tarreau | 718f0ef | 2008-08-10 16:21:32 +0200 | [diff] [blame] | 2954 | rep->flags |= BF_MAY_FORWARD; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2955 | rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */ |
| 2956 | |
| 2957 | /* if the user wants to log as soon as possible, without counting |
| 2958 | bytes from the server, then this is the right moment. */ |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 2959 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2960 | t->logs.t_close = t->logs.t_connect; /* to get a valid end date */ |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 2961 | tcp_sess_log(t); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2962 | } |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 2963 | #ifdef CONFIG_HAP_TCPSPLICE |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2964 | if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 2965 | /* TCP splicing supported by both FE and BE */ |
| 2966 | tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0); |
| 2967 | } |
| 2968 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2969 | } |
| 2970 | else { |
| 2971 | t->srv_state = SV_STHEADERS; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2972 | rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2973 | t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE; |
| 2974 | /* reset hdr_idx which was already initialized by the request. |
| 2975 | * right now, the http parser does it. |
| 2976 | * hdr_idx_init(&t->txn.hdr_idx); |
| 2977 | */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2978 | } |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2979 | req->cex = TICK_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2980 | return 1; |
| 2981 | } |
| 2982 | } |
| 2983 | else if (s == SV_STHEADERS) { /* receiving server headers */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2984 | /* |
| 2985 | * Now parse the partial (or complete) lines. |
| 2986 | * We will check the response syntax, and also join multi-line |
| 2987 | * headers. An index of all the lines will be elaborated while |
| 2988 | * parsing. |
| 2989 | * |
| 2990 | * For the parsing, we use a 28 states FSM. |
| 2991 | * |
| 2992 | * Here is the information we currently have : |
| 2993 | * rep->data + req->som = beginning of response |
| 2994 | * rep->data + req->eoh = end of processed headers / start of current one |
| 2995 | * rep->data + req->eol = end of current header or line (LF or CRLF) |
| 2996 | * rep->lr = first non-visited byte |
| 2997 | * rep->r = end of data |
| 2998 | */ |
| 2999 | |
| 3000 | int cur_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3001 | struct http_msg *msg = &txn->rsp; |
| 3002 | struct proxy *cur_proxy; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3003 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3004 | if (likely(rep->lr < rep->r)) |
| 3005 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3006 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3007 | /* 1: we might have to print this header in debug mode */ |
| 3008 | if (unlikely((global.mode & MODE_DEBUG) && |
| 3009 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
| 3010 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
| 3011 | char *eol, *sol; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3012 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3013 | sol = rep->data + msg->som; |
| 3014 | eol = sol + msg->sl.rq.l; |
| 3015 | debug_hdr("srvrep", t, sol, eol); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3016 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3017 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 3018 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3019 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3020 | while (cur_idx) { |
| 3021 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 3022 | debug_hdr("srvhdr", t, sol, eol); |
| 3023 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 3024 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 3025 | } |
| 3026 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3027 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3028 | |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3029 | if ((rep->l < rep->rlim - rep->data) && EV_FD_COND_S(t->srv_fd, DIR_RD)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3030 | /* fd in DIR_RD was disabled, perhaps because of a previous buffer |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3031 | * full. We cannot loop here since stream_sock_read will disable it only if |
| 3032 | * rep->l == rlim-data |
| 3033 | */ |
Willy Tarreau | ce09c52 | 2008-08-11 10:35:07 +0200 | [diff] [blame] | 3034 | rep->rex = tick_add_ifset(now_ms, t->be->timeout.server); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3035 | } |
| 3036 | |
| 3037 | |
| 3038 | /* |
| 3039 | * Now we quickly check if we have found a full valid response. |
| 3040 | * If not so, we check the FD and buffer states before leaving. |
| 3041 | * A full response is indicated by the fact that we have seen |
| 3042 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 3043 | * responses are checked first. |
| 3044 | * |
| 3045 | * Depending on whether the client is still there or not, we |
| 3046 | * may send an error response back or not. Note that normally |
| 3047 | * we should only check for HTTP status there, and check I/O |
| 3048 | * errors somewhere else. |
| 3049 | */ |
| 3050 | |
| 3051 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
| 3052 | |
| 3053 | /* Invalid response, or read error or write error */ |
| 3054 | if (unlikely((msg->msg_state == HTTP_MSG_ERROR) || |
| 3055 | (req->flags & BF_WRITE_ERROR) || |
| 3056 | (rep->flags & BF_READ_ERROR))) { |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 3057 | buffer_shutr_done(rep); |
| 3058 | buffer_shutw_done(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3059 | fd_delete(t->srv_fd); |
| 3060 | if (t->srv) { |
| 3061 | t->srv->cur_sess--; |
| 3062 | t->srv->failed_resp++; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3063 | sess_change_server(t, NULL); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3064 | } |
| 3065 | t->be->failed_resp++; |
| 3066 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 3067 | txn->status = 502; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3068 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 3069 | if (!(t->flags & SN_ERR_MASK)) |
| 3070 | t->flags |= SN_ERR_SRVCL; |
| 3071 | if (!(t->flags & SN_FINST_MASK)) |
| 3072 | t->flags |= SN_FINST_H; |
| 3073 | /* We used to have a free connection slot. Since we'll never use it, |
| 3074 | * we have to inform the server that it may be used by another session. |
| 3075 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3076 | if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3077 | process_srv_queue(t->srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3078 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3079 | return 1; |
| 3080 | } |
| 3081 | |
| 3082 | /* end of client write or end of server read. |
| 3083 | * since we are in header mode, if there's no space left for headers, we |
| 3084 | * won't be able to free more later, so the session will never terminate. |
| 3085 | */ |
Willy Tarreau | 6468d92 | 2008-08-03 19:15:35 +0200 | [diff] [blame] | 3086 | else if (unlikely(rep->flags & (BF_READ_NULL | BF_SHUTW_STATUS) || |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3087 | rep->l >= rep->rlim - rep->data)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3088 | EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 3089 | buffer_shutr_done(rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3090 | t->srv_state = SV_STSHUTR; |
| 3091 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 3092 | return 1; |
| 3093 | } |
| 3094 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3095 | /* read timeout : return a 504 to the client. */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3096 | else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_RD) && |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3097 | tick_is_expired(rep->rex, now_ms))) { |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 3098 | buffer_shutr_done(rep); |
| 3099 | buffer_shutw_done(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3100 | fd_delete(t->srv_fd); |
| 3101 | if (t->srv) { |
| 3102 | t->srv->cur_sess--; |
| 3103 | t->srv->failed_resp++; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3104 | sess_change_server(t, NULL); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3105 | } |
| 3106 | t->be->failed_resp++; |
| 3107 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 3108 | txn->status = 504; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3109 | client_return(t, error_message(t, HTTP_ERR_504)); |
| 3110 | if (!(t->flags & SN_ERR_MASK)) |
| 3111 | t->flags |= SN_ERR_SRVTO; |
| 3112 | if (!(t->flags & SN_FINST_MASK)) |
| 3113 | t->flags |= SN_FINST_H; |
| 3114 | /* We used to have a free connection slot. Since we'll never use it, |
| 3115 | * we have to inform the server that it may be used by another session. |
| 3116 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3117 | if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3118 | process_srv_queue(t->srv); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3119 | return 1; |
| 3120 | } |
| 3121 | |
| 3122 | /* last client read and buffer empty */ |
| 3123 | /* FIXME!!! here, we don't want to switch to SHUTW if the |
| 3124 | * client shuts read too early, because we may still have |
| 3125 | * some work to do on the headers. |
| 3126 | * The side-effect is that if the client completely closes its |
| 3127 | * connection during SV_STHEADER, the connection to the server |
| 3128 | * is kept until a response comes back or the timeout is reached. |
| 3129 | */ |
Willy Tarreau | 6468d92 | 2008-08-03 19:15:35 +0200 | [diff] [blame] | 3130 | else if (0 && /* we don't want to switch to shutw for now */ |
| 3131 | unlikely(req->flags & BF_SHUTR_STATUS && (req->l == 0))) { |
| 3132 | |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3133 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 3134 | buffer_shutw_done(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3135 | |
| 3136 | /* We must ensure that the read part is still |
| 3137 | * alive when switching to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3138 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3139 | rep->rex = tick_add_ifset(now_ms, t->be->timeout.server); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3140 | |
| 3141 | shutdown(t->srv_fd, SHUT_WR); |
| 3142 | t->srv_state = SV_STSHUTW; |
| 3143 | return 1; |
| 3144 | } |
| 3145 | |
| 3146 | /* write timeout */ |
| 3147 | /* FIXME!!! here, we don't want to switch to SHUTW if the |
| 3148 | * client shuts read too early, because we may still have |
| 3149 | * some work to do on the headers. |
| 3150 | */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3151 | else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_WR) && |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3152 | tick_is_expired(req->wex, now_ms))) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3153 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 3154 | buffer_shutw_done(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3155 | shutdown(t->srv_fd, SHUT_WR); |
| 3156 | /* We must ensure that the read part is still alive |
| 3157 | * when switching to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3158 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3159 | rep->rex = tick_add_ifset(now_ms, t->be->timeout.server); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3160 | |
| 3161 | t->srv_state = SV_STSHUTW; |
| 3162 | if (!(t->flags & SN_ERR_MASK)) |
| 3163 | t->flags |= SN_ERR_SRVTO; |
| 3164 | if (!(t->flags & SN_FINST_MASK)) |
| 3165 | t->flags |= SN_FINST_H; |
| 3166 | return 1; |
| 3167 | } |
| 3168 | |
| 3169 | /* |
| 3170 | * And now the non-error cases. |
| 3171 | */ |
| 3172 | |
| 3173 | /* Data remaining in the request buffer. |
| 3174 | * This happens during the first pass here, and during |
| 3175 | * long posts. |
| 3176 | */ |
| 3177 | else if (likely(req->l)) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3178 | if (EV_FD_COND_S(t->srv_fd, DIR_WR)) { |
| 3179 | /* restart writing */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3180 | req->wex = tick_add_ifset(now_ms, t->be->timeout.server); |
| 3181 | if (req->wex) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3182 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 3183 | * we refresh it. */ |
| 3184 | rep->rex = req->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3185 | } |
| 3186 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3187 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3188 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3189 | /* nothing left in the request buffer */ |
| 3190 | else { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3191 | if (EV_FD_COND_C(t->srv_fd, DIR_WR)) { |
| 3192 | /* stop writing */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3193 | req->wex = TICK_ETERNITY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3194 | } |
| 3195 | } |
| 3196 | |
| 3197 | return t->srv_state != SV_STHEADERS; |
| 3198 | } |
| 3199 | |
| 3200 | |
| 3201 | /***************************************************************** |
| 3202 | * More interesting part now : we know that we have a complete * |
| 3203 | * response which at least looks like HTTP. We have an indicator * |
| 3204 | * of each header's length, so we can parse them quickly. * |
| 3205 | ****************************************************************/ |
| 3206 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 3207 | /* ensure we keep this pointer to the beginning of the message */ |
| 3208 | msg->sol = rep->data + msg->som; |
| 3209 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3210 | /* |
| 3211 | * 1: get the status code and check for cacheability. |
| 3212 | */ |
| 3213 | |
| 3214 | t->logs.logwait &= ~LW_RESP; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 3215 | txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3216 | |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 3217 | switch (txn->status) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3218 | case 200: |
| 3219 | case 203: |
| 3220 | case 206: |
| 3221 | case 300: |
| 3222 | case 301: |
| 3223 | case 410: |
| 3224 | /* RFC2616 @13.4: |
| 3225 | * "A response received with a status code of |
| 3226 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 3227 | * by a cache (...) unless a cache-control |
| 3228 | * directive prohibits caching." |
| 3229 | * |
| 3230 | * RFC2616 @9.5: POST method : |
| 3231 | * "Responses to this method are not cacheable, |
| 3232 | * unless the response includes appropriate |
| 3233 | * Cache-Control or Expires header fields." |
| 3234 | */ |
| 3235 | if (likely(txn->meth != HTTP_METH_POST) && |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3236 | (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3237 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3238 | break; |
| 3239 | default: |
| 3240 | break; |
| 3241 | } |
| 3242 | |
| 3243 | /* |
| 3244 | * 2: we may need to capture headers |
| 3245 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3246 | if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3247 | capture_headers(rep->data + msg->som, &txn->hdr_idx, |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3248 | txn->rsp.cap, t->fe->rsp_cap); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3249 | |
| 3250 | /* |
| 3251 | * 3: we will have to evaluate the filters. |
| 3252 | * As opposed to version 1.2, now they will be evaluated in the |
| 3253 | * filters order and not in the header order. This means that |
| 3254 | * each filter has to be validated among all headers. |
| 3255 | * |
| 3256 | * Filters are tried with ->be first, then with ->fe if it is |
| 3257 | * different from ->be. |
| 3258 | */ |
| 3259 | |
| 3260 | t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */ |
| 3261 | |
| 3262 | cur_proxy = t->be; |
| 3263 | while (1) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3264 | struct proxy *rule_set = cur_proxy; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3265 | |
| 3266 | /* try headers filters */ |
| 3267 | if (rule_set->rsp_exp != NULL) { |
| 3268 | if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) { |
| 3269 | return_bad_resp: |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3270 | if (t->srv) { |
| 3271 | t->srv->cur_sess--; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3272 | t->srv->failed_resp++; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3273 | sess_change_server(t, NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3274 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3275 | cur_proxy->failed_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3276 | return_srv_prx_502: |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 3277 | buffer_shutr_done(rep); |
| 3278 | buffer_shutw_done(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3279 | fd_delete(t->srv_fd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3280 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 3281 | txn->status = 502; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 3282 | client_return(t, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3283 | if (!(t->flags & SN_ERR_MASK)) |
| 3284 | t->flags |= SN_ERR_PRXCOND; |
| 3285 | if (!(t->flags & SN_FINST_MASK)) |
| 3286 | t->flags |= SN_FINST_H; |
| 3287 | /* We used to have a free connection slot. Since we'll never use it, |
| 3288 | * we have to inform the server that it may be used by another session. |
| 3289 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3290 | if (t->srv && may_dequeue_tasks(t->srv, cur_proxy)) |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3291 | process_srv_queue(t->srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3292 | return 1; |
| 3293 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3294 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3295 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3296 | /* has the response been denied ? */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3297 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3298 | if (t->srv) { |
| 3299 | t->srv->cur_sess--; |
| 3300 | t->srv->failed_secu++; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3301 | sess_change_server(t, NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3302 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3303 | cur_proxy->denied_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3304 | goto return_srv_prx_502; |
| 3305 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3306 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3307 | /* We might have to check for "Connection:" */ |
Krzysztof Oledzki | 336d475 | 2007-12-25 02:40:22 +0100 | [diff] [blame] | 3308 | if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3309 | !(t->flags & SN_CONN_CLOSED)) { |
| 3310 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 3311 | int cur_idx, old_idx, delta, val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3312 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3313 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3314 | cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 3315 | old_idx = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3316 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3317 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 3318 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 3319 | cur_ptr = cur_next; |
| 3320 | cur_end = cur_ptr + cur_hdr->len; |
| 3321 | cur_next = cur_end + cur_hdr->cr + 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3322 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 3323 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 3324 | if (val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3325 | /* 3 possibilities : |
| 3326 | * - we have already set Connection: close, |
| 3327 | * so we remove this line. |
| 3328 | * - we have not yet set Connection: close, |
| 3329 | * but this line indicates close. We leave |
| 3330 | * it untouched and set the flag. |
| 3331 | * - we have not yet set Connection: close, |
| 3332 | * and this line indicates non-close. We |
| 3333 | * replace it. |
| 3334 | */ |
| 3335 | if (t->flags & SN_CONN_CLOSED) { |
| 3336 | delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0); |
| 3337 | txn->rsp.eoh += delta; |
| 3338 | cur_next += delta; |
| 3339 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 3340 | txn->hdr_idx.used--; |
| 3341 | cur_hdr->len = 0; |
| 3342 | } else { |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 3343 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 3344 | delta = buffer_replace2(rep, cur_ptr + val, cur_end, |
| 3345 | "close", 5); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3346 | cur_next += delta; |
| 3347 | cur_hdr->len += delta; |
| 3348 | txn->rsp.eoh += delta; |
| 3349 | } |
| 3350 | t->flags |= SN_CONN_CLOSED; |
| 3351 | } |
| 3352 | } |
| 3353 | old_idx = cur_idx; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3354 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3355 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3356 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3357 | /* add response headers from the rule sets in the same order */ |
| 3358 | for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) { |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3359 | if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, |
| 3360 | rule_set->rsp_add[cur_idx])) < 0) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3361 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3362 | } |
| 3363 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3364 | /* check whether we're already working on the frontend */ |
| 3365 | if (cur_proxy == t->fe) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3366 | break; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3367 | cur_proxy = t->fe; |
| 3368 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3369 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3370 | /* |
| 3371 | * 4: check for server cookie. |
| 3372 | */ |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 3373 | if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name |
| 3374 | || (t->be->options & PR_O_CHK_CACHE)) |
| 3375 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3376 | |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3377 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3378 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 3379 | * 5: check for cache-control or pragma headers if required. |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3380 | */ |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 3381 | if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0) |
| 3382 | check_response_for_cacheability(t, rep); |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3383 | |
| 3384 | /* |
| 3385 | * 6: add server cookie in the response if needed |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3386 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3387 | if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) && |
| 3388 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3389 | int len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3390 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3391 | /* the server is known, it's not the one the client requested, we have to |
| 3392 | * insert a set-cookie here, except if we want to insert only on POST |
| 3393 | * requests and this one isn't. Note that servers which don't have cookies |
| 3394 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3395 | */ |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3396 | len = sprintf(trash, "Set-Cookie: %s=%s; path=/", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3397 | t->be->cookie_name, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3398 | 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] | 3399 | |
Krzysztof Piotr Oledzki | 1acf217 | 2008-05-29 23:03:34 +0200 | [diff] [blame] | 3400 | if (t->be->cookie_domain) |
| 3401 | len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain); |
Krzysztof Piotr Oledzki | efe3b6f | 2008-05-23 23:49:32 +0200 | [diff] [blame] | 3402 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3403 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3404 | trash, len)) < 0) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3405 | goto return_bad_resp; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3406 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3407 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3408 | /* Here, we will tell an eventual cache on the client side that we don't |
| 3409 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 3410 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 3411 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 3412 | */ |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3413 | if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) { |
| 3414 | |
| 3415 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3416 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3417 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3418 | "Cache-control: private", 22)) < 0) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3419 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3420 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3421 | } |
| 3422 | |
| 3423 | |
| 3424 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3425 | * 7: check if result will be cacheable with a cookie. |
| 3426 | * We'll block the response if security checks have caught |
| 3427 | * nasty things such as a cacheable cookie. |
| 3428 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3429 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) == |
| 3430 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3431 | (t->be->options & PR_O_CHK_CACHE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3432 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3433 | /* we're in presence of a cacheable response containing |
| 3434 | * a set-cookie header. We'll block it as requested by |
| 3435 | * the 'checkcache' option, and send an alert. |
| 3436 | */ |
| 3437 | if (t->srv) { |
| 3438 | t->srv->cur_sess--; |
| 3439 | t->srv->failed_secu++; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3440 | sess_change_server(t, NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3441 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3442 | t->be->denied_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3443 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3444 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3445 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3446 | send_log(t->be, LOG_ALERT, |
| 3447 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3448 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3449 | goto return_srv_prx_502; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3450 | } |
| 3451 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3452 | /* |
| 3453 | * 8: add "Connection: close" if needed and not yet set. |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 3454 | * 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] | 3455 | */ |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 3456 | if (!(t->flags & SN_CONN_CLOSED) && |
Krzysztof Oledzki | 336d475 | 2007-12-25 02:40:22 +0100 | [diff] [blame] | 3457 | ((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] | 3458 | if ((unlikely(msg->sl.st.v_l != 8) || |
| 3459 | unlikely(req->data[msg->som + 7] != '0')) && |
| 3460 | unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3461 | "Connection: close", 17)) < 0) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3462 | goto return_bad_resp; |
| 3463 | t->flags |= SN_CONN_CLOSED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3464 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3465 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3466 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3467 | /************************************************************* |
| 3468 | * OK, that's finished for the headers. We have done what we * |
| 3469 | * could. Let's switch to the DATA state. * |
| 3470 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3471 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3472 | t->srv_state = SV_STDATA; |
Willy Tarreau | 718f0ef | 2008-08-10 16:21:32 +0200 | [diff] [blame] | 3473 | rep->flags |= BF_MAY_FORWARD; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3474 | rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 3475 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3476 | |
| 3477 | /* client connection already closed or option 'forceclose' required : |
| 3478 | * we close the server's outgoing connection right now. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3479 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3480 | if ((req->l == 0) && |
Willy Tarreau | 6468d92 | 2008-08-03 19:15:35 +0200 | [diff] [blame] | 3481 | (req->flags & BF_SHUTR_STATUS || t->be->options & PR_O_FORCE_CLO)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3482 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 3483 | buffer_shutw_done(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3484 | |
| 3485 | /* We must ensure that the read part is still alive when switching |
| 3486 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3487 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3488 | rep->rex = tick_add_ifset(now_ms, t->be->timeout.server); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3489 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3490 | shutdown(t->srv_fd, SHUT_WR); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3491 | t->srv_state = SV_STSHUTW; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3492 | } |
| 3493 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3494 | #ifdef CONFIG_HAP_TCPSPLICE |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3495 | if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3496 | /* TCP splicing supported by both FE and BE */ |
| 3497 | tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3498 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3499 | #endif |
| 3500 | /* if the user wants to log as soon as possible, without counting |
Krzysztof Piotr Oledzki | f1e1cb4 | 2008-01-20 23:27:02 +0100 | [diff] [blame] | 3501 | * bytes from the server, then this is the right moment. We have |
| 3502 | * to temporarily assign bytes_out to log what we currently have. |
| 3503 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3504 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 3505 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
Willy Tarreau | 8b3977f | 2008-01-18 11:16:32 +0100 | [diff] [blame] | 3506 | t->logs.bytes_out = txn->rsp.eoh; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 3507 | if (t->fe->to_log & LW_REQ) |
| 3508 | http_sess_log(t); |
| 3509 | else |
| 3510 | tcp_sess_log(t); |
Krzysztof Piotr Oledzki | f1e1cb4 | 2008-01-20 23:27:02 +0100 | [diff] [blame] | 3511 | t->logs.bytes_out = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3512 | } |
| 3513 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3514 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 3515 | * otherwise we would not let the client side wake up. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3516 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3517 | |
| 3518 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3519 | } |
| 3520 | else if (s == SV_STDATA) { |
| 3521 | /* read or write error */ |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3522 | if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) { |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 3523 | buffer_shutr_done(rep); |
| 3524 | buffer_shutw_done(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3525 | fd_delete(t->srv_fd); |
| 3526 | if (t->srv) { |
| 3527 | t->srv->cur_sess--; |
| 3528 | t->srv->failed_resp++; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3529 | sess_change_server(t, NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3530 | } |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 3531 | t->be->failed_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3532 | t->srv_state = SV_STCLOSE; |
| 3533 | if (!(t->flags & SN_ERR_MASK)) |
| 3534 | t->flags |= SN_ERR_SRVCL; |
| 3535 | if (!(t->flags & SN_FINST_MASK)) |
| 3536 | t->flags |= SN_FINST_D; |
| 3537 | /* We used to have a free connection slot. Since we'll never use it, |
| 3538 | * we have to inform the server that it may be used by another session. |
| 3539 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3540 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3541 | process_srv_queue(t->srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3542 | |
| 3543 | return 1; |
| 3544 | } |
| 3545 | /* last read, or end of client write */ |
Willy Tarreau | 6468d92 | 2008-08-03 19:15:35 +0200 | [diff] [blame] | 3546 | else if (rep->flags & (BF_READ_NULL | BF_SHUTW_STATUS)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3547 | EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3548 | buffer_shutr(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3549 | t->srv_state = SV_STSHUTR; |
| 3550 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 3551 | return 1; |
| 3552 | } |
| 3553 | /* end of client read and no more data to send */ |
Willy Tarreau | 6468d92 | 2008-08-03 19:15:35 +0200 | [diff] [blame] | 3554 | else if (req->flags & BF_SHUTR_STATUS && (req->l == 0)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3555 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 3556 | buffer_shutw_done(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3557 | shutdown(t->srv_fd, SHUT_WR); |
| 3558 | /* We must ensure that the read part is still alive when switching |
| 3559 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3560 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3561 | rep->rex = tick_add_ifset(now_ms, t->be->timeout.server); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3562 | |
| 3563 | t->srv_state = SV_STSHUTW; |
| 3564 | return 1; |
| 3565 | } |
| 3566 | /* read timeout */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3567 | else if (tick_is_expired(rep->rex, now_ms)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3568 | EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3569 | buffer_shutr(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3570 | t->srv_state = SV_STSHUTR; |
| 3571 | if (!(t->flags & SN_ERR_MASK)) |
| 3572 | t->flags |= SN_ERR_SRVTO; |
| 3573 | if (!(t->flags & SN_FINST_MASK)) |
| 3574 | t->flags |= SN_FINST_D; |
| 3575 | return 1; |
| 3576 | } |
| 3577 | /* write timeout */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3578 | else if (tick_is_expired(req->wex, now_ms)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3579 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 3580 | buffer_shutw_done(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3581 | shutdown(t->srv_fd, SHUT_WR); |
| 3582 | /* We must ensure that the read part is still alive when switching |
| 3583 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3584 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3585 | rep->cex = tick_add_ifset(now_ms, t->be->timeout.server); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3586 | t->srv_state = SV_STSHUTW; |
| 3587 | if (!(t->flags & SN_ERR_MASK)) |
| 3588 | t->flags |= SN_ERR_SRVTO; |
| 3589 | if (!(t->flags & SN_FINST_MASK)) |
| 3590 | t->flags |= SN_FINST_D; |
| 3591 | return 1; |
| 3592 | } |
| 3593 | |
| 3594 | /* recompute request time-outs */ |
| 3595 | if (req->l == 0) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3596 | if (EV_FD_COND_C(t->srv_fd, DIR_WR)) { |
| 3597 | /* stop writing */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3598 | req->wex = TICK_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3599 | } |
| 3600 | } |
| 3601 | else { /* buffer not empty, there are still data to be transferred */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3602 | if (EV_FD_COND_S(t->srv_fd, DIR_WR)) { |
| 3603 | /* restart writing */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3604 | req->wex = tick_add_ifset(now_ms, t->be->timeout.server); |
| 3605 | if (req->wex) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3606 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 3607 | * we refresh it. */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3608 | rep->rex = req->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3609 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3610 | } |
| 3611 | } |
| 3612 | |
| 3613 | /* recompute response time-outs */ |
| 3614 | if (rep->l == BUFSIZE) { /* no room to read more data */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3615 | if (EV_FD_COND_C(t->srv_fd, DIR_RD)) { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3616 | rep->rex = TICK_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3617 | } |
| 3618 | } |
| 3619 | else { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3620 | if (EV_FD_COND_S(t->srv_fd, DIR_RD)) { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3621 | rep->rex = tick_add_ifset(now_ms, t->be->timeout.server); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3622 | } |
| 3623 | } |
| 3624 | |
| 3625 | return 0; /* other cases change nothing */ |
| 3626 | } |
| 3627 | else if (s == SV_STSHUTR) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3628 | if (req->flags & BF_WRITE_ERROR) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3629 | //EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 3630 | buffer_shutw_done(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3631 | fd_delete(t->srv_fd); |
| 3632 | if (t->srv) { |
| 3633 | t->srv->cur_sess--; |
| 3634 | t->srv->failed_resp++; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3635 | sess_change_server(t, NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3636 | } |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 3637 | t->be->failed_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3638 | //close(t->srv_fd); |
| 3639 | t->srv_state = SV_STCLOSE; |
| 3640 | if (!(t->flags & SN_ERR_MASK)) |
| 3641 | t->flags |= SN_ERR_SRVCL; |
| 3642 | if (!(t->flags & SN_FINST_MASK)) |
| 3643 | t->flags |= SN_FINST_D; |
| 3644 | /* We used to have a free connection slot. Since we'll never use it, |
| 3645 | * we have to inform the server that it may be used by another session. |
| 3646 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3647 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3648 | process_srv_queue(t->srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3649 | |
| 3650 | return 1; |
| 3651 | } |
Willy Tarreau | 6468d92 | 2008-08-03 19:15:35 +0200 | [diff] [blame] | 3652 | else if (req->flags & BF_SHUTR_STATUS && (req->l == 0)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3653 | //EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 3654 | buffer_shutw_done(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3655 | fd_delete(t->srv_fd); |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 3656 | if (t->srv) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3657 | t->srv->cur_sess--; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3658 | sess_change_server(t, NULL); |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 3659 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3660 | //close(t->srv_fd); |
| 3661 | t->srv_state = SV_STCLOSE; |
| 3662 | /* We used to have a free connection slot. Since we'll never use it, |
| 3663 | * we have to inform the server that it may be used by another session. |
| 3664 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3665 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3666 | process_srv_queue(t->srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3667 | |
| 3668 | return 1; |
| 3669 | } |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3670 | else if (tick_is_expired(req->wex, now_ms)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3671 | //EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 3672 | buffer_shutw_done(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3673 | fd_delete(t->srv_fd); |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 3674 | if (t->srv) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3675 | t->srv->cur_sess--; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3676 | sess_change_server(t, NULL); |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 3677 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3678 | //close(t->srv_fd); |
| 3679 | t->srv_state = SV_STCLOSE; |
| 3680 | if (!(t->flags & SN_ERR_MASK)) |
| 3681 | t->flags |= SN_ERR_SRVTO; |
| 3682 | if (!(t->flags & SN_FINST_MASK)) |
| 3683 | t->flags |= SN_FINST_D; |
| 3684 | /* We used to have a free connection slot. Since we'll never use it, |
| 3685 | * we have to inform the server that it may be used by another session. |
| 3686 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3687 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3688 | process_srv_queue(t->srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3689 | |
| 3690 | return 1; |
| 3691 | } |
| 3692 | else if (req->l == 0) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3693 | if (EV_FD_COND_C(t->srv_fd, DIR_WR)) { |
| 3694 | /* stop writing */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3695 | req->wex = TICK_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3696 | } |
| 3697 | } |
| 3698 | else { /* buffer not empty */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3699 | if (EV_FD_COND_S(t->srv_fd, DIR_WR)) { |
| 3700 | /* restart writing */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3701 | req->wex = tick_add_ifset(now_ms, t->be->timeout.server); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3702 | } |
| 3703 | } |
| 3704 | return 0; |
| 3705 | } |
| 3706 | else if (s == SV_STSHUTW) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3707 | if (rep->flags & BF_READ_ERROR) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3708 | //EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 3709 | buffer_shutr_done(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3710 | fd_delete(t->srv_fd); |
| 3711 | if (t->srv) { |
| 3712 | t->srv->cur_sess--; |
| 3713 | t->srv->failed_resp++; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3714 | sess_change_server(t, NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3715 | } |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 3716 | t->be->failed_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3717 | //close(t->srv_fd); |
| 3718 | t->srv_state = SV_STCLOSE; |
| 3719 | if (!(t->flags & SN_ERR_MASK)) |
| 3720 | t->flags |= SN_ERR_SRVCL; |
| 3721 | if (!(t->flags & SN_FINST_MASK)) |
| 3722 | t->flags |= SN_FINST_D; |
| 3723 | /* We used to have a free connection slot. Since we'll never use it, |
| 3724 | * we have to inform the server that it may be used by another session. |
| 3725 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3726 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3727 | process_srv_queue(t->srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3728 | |
| 3729 | return 1; |
| 3730 | } |
Willy Tarreau | 6468d92 | 2008-08-03 19:15:35 +0200 | [diff] [blame] | 3731 | else if (rep->flags & (BF_READ_NULL | BF_SHUTW_STATUS)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3732 | //EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 3733 | buffer_shutr_done(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3734 | fd_delete(t->srv_fd); |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 3735 | if (t->srv) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3736 | t->srv->cur_sess--; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3737 | sess_change_server(t, NULL); |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 3738 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3739 | //close(t->srv_fd); |
| 3740 | t->srv_state = SV_STCLOSE; |
| 3741 | /* We used to have a free connection slot. Since we'll never use it, |
| 3742 | * we have to inform the server that it may be used by another session. |
| 3743 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3744 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3745 | process_srv_queue(t->srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3746 | |
| 3747 | return 1; |
| 3748 | } |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3749 | else if (tick_is_expired(rep->rex, now_ms)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3750 | //EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 3751 | buffer_shutr_done(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3752 | fd_delete(t->srv_fd); |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 3753 | if (t->srv) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3754 | t->srv->cur_sess--; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3755 | sess_change_server(t, NULL); |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 3756 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3757 | //close(t->srv_fd); |
| 3758 | t->srv_state = SV_STCLOSE; |
| 3759 | if (!(t->flags & SN_ERR_MASK)) |
| 3760 | t->flags |= SN_ERR_SRVTO; |
| 3761 | if (!(t->flags & SN_FINST_MASK)) |
| 3762 | t->flags |= SN_FINST_D; |
| 3763 | /* We used to have a free connection slot. Since we'll never use it, |
| 3764 | * we have to inform the server that it may be used by another session. |
| 3765 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3766 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3767 | process_srv_queue(t->srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3768 | |
| 3769 | return 1; |
| 3770 | } |
| 3771 | else if (rep->l == BUFSIZE) { /* no room to read more data */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3772 | if (EV_FD_COND_C(t->srv_fd, DIR_RD)) { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3773 | rep->rex = TICK_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3774 | } |
| 3775 | } |
| 3776 | else { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3777 | if (EV_FD_COND_S(t->srv_fd, DIR_RD)) { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3778 | rep->rex = tick_add_ifset(now_ms, t->be->timeout.server); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3779 | } |
| 3780 | } |
| 3781 | return 0; |
| 3782 | } |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3783 | else if (s == SV_STANALYZE){ |
| 3784 | /* this server state is set by the client to study the body for server assignment */ |
| 3785 | |
| 3786 | /* Have we been through this long enough to timeout? */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3787 | if (!tick_is_expired(req->rex, now_ms)) { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3788 | /* balance url_param check_post should have been the only to get into this. |
| 3789 | * just wait for data, check to compare how much |
| 3790 | */ |
| 3791 | struct http_msg * msg = &t->txn.req; |
| 3792 | unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 :msg->eoh + 1; |
Willy Tarreau | fb0528b | 2008-08-11 00:21:56 +0200 | [diff] [blame] | 3793 | unsigned long len = req->l - body; |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3794 | long long limit = t->be->url_param_post_limit; |
| 3795 | struct hdr_ctx ctx; |
| 3796 | ctx.idx = 0; |
| 3797 | /* now if we have a length, we'll take the hint */ |
| 3798 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx); |
| 3799 | if ( ctx.idx && strncasecmp(ctx.line+ctx.val,"chunked",ctx.vlen)==0) { |
| 3800 | unsigned int chunk = 0; |
Willy Tarreau | fb0528b | 2008-08-11 00:21:56 +0200 | [diff] [blame] | 3801 | while ( body < req->l && !HTTP_IS_CRLF(msg->sol[body])) { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3802 | char c = msg->sol[body]; |
| 3803 | if (ishex(c)) { |
| 3804 | unsigned int hex = toupper(c) - '0'; |
| 3805 | if ( hex > 9 ) |
| 3806 | hex -= 'A' - '9' - 1; |
| 3807 | chunk = (chunk << 4) | hex; |
| 3808 | } |
| 3809 | else break; |
| 3810 | body++; |
| 3811 | len--; |
| 3812 | } |
Willy Tarreau | fb0528b | 2008-08-11 00:21:56 +0200 | [diff] [blame] | 3813 | if ( body + 2 >= req->l ) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3814 | return 0; /* end of buffer? data missing! */ |
| 3815 | |
| 3816 | if ( memcmp(msg->sol+body, "\r\n", 2) != 0 ) |
| 3817 | return 0; /* chunked encoding len ends with CRLF, and we don't have it yet */ |
| 3818 | |
| 3819 | /* if we support more then one chunk here, we have to do it again when assigning server |
| 3820 | 1. how much entity data do we have? new var |
| 3821 | 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here |
| 3822 | 3. test if elen > limit, or set new limit to elen if 0 (end of entity found) |
| 3823 | */ |
| 3824 | |
| 3825 | if ( chunk < limit ) |
| 3826 | limit = chunk; /* only reading one chunk */ |
| 3827 | } else { |
| 3828 | if ( msg->hdr_content_len < limit ) |
| 3829 | limit = msg->hdr_content_len; |
| 3830 | } |
| 3831 | if ( len < limit ) |
| 3832 | return 0; |
| 3833 | } |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3834 | t->srv_state = SV_STIDLE; |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3835 | return 1; |
| 3836 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3837 | else { /* SV_STCLOSE : nothing to do */ |
| 3838 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 3839 | int len; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3840 | len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3841 | t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3842 | write(1, trash, len); |
| 3843 | } |
| 3844 | return 0; |
| 3845 | } |
| 3846 | return 0; |
| 3847 | } |
| 3848 | |
| 3849 | |
| 3850 | /* |
| 3851 | * Produces data for the session <s> depending on its source. Expects to be |
| 3852 | * called with s->cli_state == CL_STSHUTR. Right now, only statistics can be |
| 3853 | * produced. It stops by itself by unsetting the SN_SELF_GEN flag from the |
| 3854 | * session, 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] | 3855 | * the buffer, or simply by letting an empty buffer upon return. It returns 1 |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3856 | * if it changes the session state from CL_STSHUTR, otherwise 0. |
| 3857 | */ |
| 3858 | int produce_content(struct session *s) |
| 3859 | { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3860 | if (s->data_source == DATA_SRC_NONE) { |
| 3861 | s->flags &= ~SN_SELF_GEN; |
| 3862 | return 1; |
| 3863 | } |
| 3864 | else if (s->data_source == DATA_SRC_STATS) { |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3865 | /* dump server statistics */ |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 3866 | int ret = stats_dump_http(s, s->be->uri_auth); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 3867 | if (ret >= 0) |
| 3868 | return ret; |
| 3869 | /* -1 indicates an error */ |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3870 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3871 | |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 3872 | /* unknown data source or internal error */ |
| 3873 | s->txn.status = 500; |
| 3874 | client_retnclose(s, error_message(s, HTTP_ERR_500)); |
| 3875 | if (!(s->flags & SN_ERR_MASK)) |
| 3876 | s->flags |= SN_ERR_PRXCOND; |
| 3877 | if (!(s->flags & SN_FINST_MASK)) |
| 3878 | s->flags |= SN_FINST_R; |
| 3879 | s->flags &= ~SN_SELF_GEN; |
| 3880 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3881 | } |
| 3882 | |
| 3883 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3884 | /* Iterate the same filter through all request headers. |
| 3885 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3886 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 3887 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3888 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3889 | 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] | 3890 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3891 | char term; |
| 3892 | char *cur_ptr, *cur_end, *cur_next; |
| 3893 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3894 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3895 | struct hdr_idx_elem *cur_hdr; |
| 3896 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 3897 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3898 | last_hdr = 0; |
| 3899 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3900 | 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] | 3901 | old_idx = 0; |
| 3902 | |
| 3903 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3904 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3905 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3906 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3907 | (exp->action == ACT_ALLOW || |
| 3908 | exp->action == ACT_DENY || |
| 3909 | exp->action == ACT_TARPIT)) |
| 3910 | return 0; |
| 3911 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3912 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3913 | if (!cur_idx) |
| 3914 | break; |
| 3915 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3916 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3917 | cur_ptr = cur_next; |
| 3918 | cur_end = cur_ptr + cur_hdr->len; |
| 3919 | cur_next = cur_end + cur_hdr->cr + 1; |
| 3920 | |
| 3921 | /* Now we have one header between cur_ptr and cur_end, |
| 3922 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3923 | */ |
| 3924 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3925 | /* The annoying part is that pattern matching needs |
| 3926 | * that we modify the contents to null-terminate all |
| 3927 | * strings before testing them. |
| 3928 | */ |
| 3929 | |
| 3930 | term = *cur_end; |
| 3931 | *cur_end = '\0'; |
| 3932 | |
| 3933 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 3934 | switch (exp->action) { |
| 3935 | case ACT_SETBE: |
| 3936 | /* It is not possible to jump a second time. |
| 3937 | * FIXME: should we return an HTTP/500 here so that |
| 3938 | * the admin knows there's a problem ? |
| 3939 | */ |
| 3940 | if (t->be != t->fe) |
| 3941 | break; |
| 3942 | |
| 3943 | /* Swithing Proxy */ |
| 3944 | t->be = (struct proxy *) exp->replace; |
| 3945 | |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3946 | /* right now, the backend switch is not overly complicated |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3947 | * because we have associated req_cap and rsp_cap to the |
| 3948 | * frontend, and the beconn will be updated later. |
| 3949 | */ |
| 3950 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 3951 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 3952 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3953 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3954 | last_hdr = 1; |
| 3955 | break; |
| 3956 | |
| 3957 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3958 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3959 | last_hdr = 1; |
| 3960 | break; |
| 3961 | |
| 3962 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3963 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3964 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3965 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3966 | break; |
| 3967 | |
| 3968 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3969 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3970 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3971 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3972 | break; |
| 3973 | |
| 3974 | case ACT_REPLACE: |
| 3975 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 3976 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 3977 | /* FIXME: if the user adds a newline in the replacement, the |
| 3978 | * index will not be recalculated for now, and the new line |
| 3979 | * will not be counted as a new header. |
| 3980 | */ |
| 3981 | |
| 3982 | cur_end += delta; |
| 3983 | cur_next += delta; |
| 3984 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3985 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3986 | break; |
| 3987 | |
| 3988 | case ACT_REMOVE: |
| 3989 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 3990 | cur_next += delta; |
| 3991 | |
| 3992 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3993 | txn->req.eoh += delta; |
| 3994 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 3995 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3996 | cur_hdr->len = 0; |
| 3997 | cur_end = NULL; /* null-term has been rewritten */ |
| 3998 | break; |
| 3999 | |
| 4000 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4001 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4002 | if (cur_end) |
| 4003 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4004 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4005 | /* keep the link from this header to next one in case of later |
| 4006 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4007 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4008 | old_idx = cur_idx; |
| 4009 | } |
| 4010 | return 0; |
| 4011 | } |
| 4012 | |
| 4013 | |
| 4014 | /* Apply the filter to the request line. |
| 4015 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 4016 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4017 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 4018 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4019 | */ |
| 4020 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 4021 | { |
| 4022 | char term; |
| 4023 | char *cur_ptr, *cur_end; |
| 4024 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4025 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4026 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4027 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4028 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4029 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4030 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4031 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4032 | (exp->action == ACT_ALLOW || |
| 4033 | exp->action == ACT_DENY || |
| 4034 | exp->action == ACT_TARPIT)) |
| 4035 | return 0; |
| 4036 | else if (exp->action == ACT_REMOVE) |
| 4037 | return 0; |
| 4038 | |
| 4039 | done = 0; |
| 4040 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4041 | 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] | 4042 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4043 | |
| 4044 | /* Now we have the request line between cur_ptr and cur_end */ |
| 4045 | |
| 4046 | /* The annoying part is that pattern matching needs |
| 4047 | * that we modify the contents to null-terminate all |
| 4048 | * strings before testing them. |
| 4049 | */ |
| 4050 | |
| 4051 | term = *cur_end; |
| 4052 | *cur_end = '\0'; |
| 4053 | |
| 4054 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4055 | switch (exp->action) { |
| 4056 | case ACT_SETBE: |
| 4057 | /* It is not possible to jump a second time. |
| 4058 | * FIXME: should we return an HTTP/500 here so that |
| 4059 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4060 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4061 | if (t->be != t->fe) |
| 4062 | break; |
| 4063 | |
| 4064 | /* Swithing Proxy */ |
| 4065 | t->be = (struct proxy *) exp->replace; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4066 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4067 | /* right now, the backend switch is not too much complicated |
| 4068 | * because we have associated req_cap and rsp_cap to the |
| 4069 | * frontend, and the beconn will be updated later. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4070 | */ |
| 4071 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 4072 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 4073 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 4074 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4075 | done = 1; |
| 4076 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4077 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4078 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4079 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4080 | done = 1; |
| 4081 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4082 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4083 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4084 | txn->flags |= TX_CLDENY; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4085 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4086 | done = 1; |
| 4087 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4088 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4089 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4090 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4091 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4092 | done = 1; |
| 4093 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4094 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4095 | case ACT_REPLACE: |
| 4096 | *cur_end = term; /* restore the string terminator */ |
| 4097 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4098 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 4099 | /* FIXME: if the user adds a newline in the replacement, the |
| 4100 | * index will not be recalculated for now, and the new line |
| 4101 | * will not be counted as a new header. |
| 4102 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4103 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4104 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4105 | cur_end += delta; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4106 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4107 | 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] | 4108 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4109 | HTTP_MSG_RQMETH, |
| 4110 | cur_ptr, cur_end + 1, |
| 4111 | NULL, NULL); |
| 4112 | if (unlikely(!cur_end)) |
| 4113 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4114 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4115 | /* we have a full request and we know that we have either a CR |
| 4116 | * or an LF at <ptr>. |
| 4117 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4118 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 4119 | 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] | 4120 | /* there is no point trying this regex on headers */ |
| 4121 | return 1; |
| 4122 | } |
| 4123 | } |
| 4124 | *cur_end = term; /* restore the string terminator */ |
| 4125 | return done; |
| 4126 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 4127 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4128 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4129 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4130 | /* |
| 4131 | * Apply all the req filters <exp> to all headers in buffer <req> of session <t>. |
| 4132 | * 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] | 4133 | * unparsable request. Since it can manage the switch to another backend, it |
| 4134 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4135 | */ |
| 4136 | int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 4137 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4138 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4139 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4140 | while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4141 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4142 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4143 | /* |
| 4144 | * The interleaving of transformations and verdicts |
| 4145 | * makes it difficult to decide to continue or stop |
| 4146 | * the evaluation. |
| 4147 | */ |
| 4148 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4149 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4150 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 4151 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) { |
| 4152 | exp = exp->next; |
| 4153 | continue; |
| 4154 | } |
| 4155 | |
| 4156 | /* Apply the filter to the request line. */ |
| 4157 | ret = apply_filter_to_req_line(t, req, exp); |
| 4158 | if (unlikely(ret < 0)) |
| 4159 | return -1; |
| 4160 | |
| 4161 | if (likely(ret == 0)) { |
| 4162 | /* The filter did not match the request, it can be |
| 4163 | * iterated through all headers. |
| 4164 | */ |
| 4165 | apply_filter_to_req_headers(t, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4166 | } |
| 4167 | exp = exp->next; |
| 4168 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4169 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4170 | } |
| 4171 | |
| 4172 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4173 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4174 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 4175 | * Manage client-side cookie. It can impact performance by about 2% so it is |
| 4176 | * desirable to call it only when needed. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4177 | */ |
| 4178 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 4179 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4180 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4181 | char *p1, *p2, *p3, *p4; |
| 4182 | char *del_colon, *del_cookie, *colon; |
| 4183 | int app_cookies; |
| 4184 | |
| 4185 | appsess *asession_temp = NULL; |
| 4186 | appsess local_asession; |
| 4187 | |
| 4188 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4189 | int cur_idx, old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4190 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4191 | /* Iterate through the headers. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4192 | * we start with the start line. |
| 4193 | */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 4194 | old_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4195 | 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] | 4196 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4197 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4198 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4199 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4200 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4201 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4202 | cur_ptr = cur_next; |
| 4203 | cur_end = cur_ptr + cur_hdr->len; |
| 4204 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4205 | |
| 4206 | /* We have one full header between cur_ptr and cur_end, and the |
| 4207 | * next header starts at cur_next. We're only interested in |
| 4208 | * "Cookie:" headers. |
| 4209 | */ |
| 4210 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4211 | val = http_header_match2(cur_ptr, cur_end, "Cookie", 6); |
| 4212 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4213 | old_idx = cur_idx; |
| 4214 | continue; |
| 4215 | } |
| 4216 | |
| 4217 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 4218 | * attributes whose name begin with a '$', and associate them with |
| 4219 | * the right cookie, if we want to delete this cookie. |
| 4220 | * So there are 3 cases for each cookie read : |
| 4221 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 4222 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 4223 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 4224 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 4225 | * "special" cookie. |
| 4226 | * At the end of loop, if a "special" cookie remains, we may have to |
| 4227 | * remove it. If no application cookie persists in the header, we |
| 4228 | * *MUST* delete it |
| 4229 | */ |
| 4230 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4231 | colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4232 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4233 | /* del_cookie == NULL => nothing to be deleted */ |
| 4234 | del_colon = del_cookie = NULL; |
| 4235 | app_cookies = 0; |
| 4236 | |
| 4237 | while (p1 < cur_end) { |
| 4238 | /* skip spaces and colons, but keep an eye on these ones */ |
| 4239 | while (p1 < cur_end) { |
| 4240 | if (*p1 == ';' || *p1 == ',') |
| 4241 | colon = p1; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4242 | else if (!isspace((unsigned char)*p1)) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4243 | break; |
| 4244 | p1++; |
| 4245 | } |
| 4246 | |
| 4247 | if (p1 == cur_end) |
| 4248 | break; |
| 4249 | |
| 4250 | /* p1 is at the beginning of the cookie name */ |
| 4251 | p2 = p1; |
| 4252 | while (p2 < cur_end && *p2 != '=') |
| 4253 | p2++; |
| 4254 | |
| 4255 | if (p2 == cur_end) |
| 4256 | break; |
| 4257 | |
| 4258 | p3 = p2 + 1; /* skips the '=' sign */ |
| 4259 | if (p3 == cur_end) |
| 4260 | break; |
| 4261 | |
| 4262 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4263 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4264 | p4++; |
| 4265 | |
| 4266 | /* here, we have the cookie name between p1 and p2, |
| 4267 | * and its value between p3 and p4. |
| 4268 | * we can process it : |
| 4269 | * |
| 4270 | * Cookie: NAME=VALUE; |
| 4271 | * | || || | |
| 4272 | * | || || +--> p4 |
| 4273 | * | || |+-------> p3 |
| 4274 | * | || +--------> p2 |
| 4275 | * | |+------------> p1 |
| 4276 | * | +-------------> colon |
| 4277 | * +--------------------> cur_ptr |
| 4278 | */ |
| 4279 | |
| 4280 | if (*p1 == '$') { |
| 4281 | /* skip this one */ |
| 4282 | } |
| 4283 | else { |
| 4284 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4285 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4286 | txn->cli_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4287 | (p4 - p1 >= t->fe->capture_namelen) && |
| 4288 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4289 | int log_len = p4 - p1; |
| 4290 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 4291 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4292 | Alert("HTTP logging : out of memory.\n"); |
| 4293 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4294 | if (log_len > t->fe->capture_len) |
| 4295 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4296 | memcpy(txn->cli_cookie, p1, log_len); |
| 4297 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4298 | } |
| 4299 | } |
| 4300 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4301 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 4302 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4303 | /* Cool... it's the right one */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4304 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4305 | char *delim; |
| 4306 | |
| 4307 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 4308 | * have the server ID betweek p3 and delim, and the original cookie between |
| 4309 | * delim+1 and p4. Otherwise, delim==p4 : |
| 4310 | * |
| 4311 | * Cookie: NAME=SRV~VALUE; |
| 4312 | * | || || | | |
| 4313 | * | || || | +--> p4 |
| 4314 | * | || || +--------> delim |
| 4315 | * | || |+-----------> p3 |
| 4316 | * | || +------------> p2 |
| 4317 | * | |+----------------> p1 |
| 4318 | * | +-----------------> colon |
| 4319 | * +------------------------> cur_ptr |
| 4320 | */ |
| 4321 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4322 | if (t->be->options & PR_O_COOK_PFX) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4323 | for (delim = p3; delim < p4; delim++) |
| 4324 | if (*delim == COOKIE_DELIM) |
| 4325 | break; |
| 4326 | } |
| 4327 | else |
| 4328 | delim = p4; |
| 4329 | |
| 4330 | |
| 4331 | /* Here, we'll look for the first running server which supports the cookie. |
| 4332 | * This allows to share a same cookie between several servers, for example |
| 4333 | * to dedicate backup servers to specific servers only. |
| 4334 | * However, to prevent clients from sticking to cookie-less backup server |
| 4335 | * when they have incidentely learned an empty cookie, we simply ignore |
| 4336 | * empty cookies and mark them as invalid. |
| 4337 | */ |
| 4338 | if (delim == p3) |
| 4339 | srv = NULL; |
| 4340 | |
| 4341 | while (srv) { |
Willy Tarreau | 92f2ab1 | 2007-02-02 22:14:47 +0100 | [diff] [blame] | 4342 | if (srv->cookie && (srv->cklen == delim - p3) && |
| 4343 | !memcmp(p3, srv->cookie, delim - p3)) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4344 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4345 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4346 | txn->flags &= ~TX_CK_MASK; |
| 4347 | txn->flags |= TX_CK_VALID; |
| 4348 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4349 | t->srv = srv; |
| 4350 | break; |
| 4351 | } else { |
| 4352 | /* we found a server, but it's down */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4353 | txn->flags &= ~TX_CK_MASK; |
| 4354 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4355 | } |
| 4356 | } |
| 4357 | srv = srv->next; |
| 4358 | } |
| 4359 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4360 | if (!srv && !(txn->flags & TX_CK_DOWN)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4361 | /* no server matched this cookie */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4362 | txn->flags &= ~TX_CK_MASK; |
| 4363 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4364 | } |
| 4365 | |
| 4366 | /* depending on the cookie mode, we may have to either : |
| 4367 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 4368 | * the server never sees it ; |
| 4369 | * - remove the server id from the cookie value, and tag the cookie as an |
| 4370 | * application cookie so that it does not get accidentely removed later, |
| 4371 | * if we're in cookie prefix mode |
| 4372 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4373 | if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4374 | int delta; /* negative */ |
| 4375 | |
| 4376 | delta = buffer_replace2(req, p3, delim + 1, NULL, 0); |
| 4377 | p4 += delta; |
| 4378 | cur_end += delta; |
| 4379 | cur_next += delta; |
| 4380 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4381 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4382 | |
| 4383 | del_cookie = del_colon = NULL; |
| 4384 | app_cookies++; /* protect the header from deletion */ |
| 4385 | } |
| 4386 | else if (del_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4387 | (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] | 4388 | del_cookie = p1; |
| 4389 | del_colon = colon; |
| 4390 | } |
| 4391 | } else { |
| 4392 | /* now we know that we must keep this cookie since it's |
| 4393 | * not ours. But if we wanted to delete our cookie |
| 4394 | * earlier, we cannot remove the complete header, but we |
| 4395 | * can remove the previous block itself. |
| 4396 | */ |
| 4397 | app_cookies++; |
| 4398 | |
| 4399 | if (del_cookie != NULL) { |
| 4400 | int delta; /* negative */ |
| 4401 | |
| 4402 | delta = buffer_replace2(req, del_cookie, p1, NULL, 0); |
| 4403 | p4 += delta; |
| 4404 | cur_end += delta; |
| 4405 | cur_next += delta; |
| 4406 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4407 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4408 | del_cookie = del_colon = NULL; |
| 4409 | } |
| 4410 | } |
| 4411 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4412 | if ((t->be->appsession_name != NULL) && |
| 4413 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4414 | /* first, let's see if the cookie is our appcookie*/ |
| 4415 | |
| 4416 | /* Cool... it's the right one */ |
| 4417 | |
| 4418 | asession_temp = &local_asession; |
| 4419 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4420 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4421 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 4422 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 4423 | return; |
| 4424 | } |
| 4425 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4426 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 4427 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4428 | asession_temp->serverid = NULL; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4429 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4430 | /* only do insert, if lookup fails */ |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4431 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 4432 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4433 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4434 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4435 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4436 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 4437 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 4438 | return; |
| 4439 | } |
| 4440 | |
| 4441 | asession_temp->sessid = local_asession.sessid; |
| 4442 | asession_temp->serverid = local_asession.serverid; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4443 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4444 | } else { |
| 4445 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4446 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4447 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4448 | if (asession_temp->serverid == NULL) { |
| 4449 | Alert("Found Application Session without matching server.\n"); |
| 4450 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4451 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4452 | while (srv) { |
| 4453 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4454 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4455 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4456 | txn->flags &= ~TX_CK_MASK; |
| 4457 | txn->flags |= TX_CK_VALID; |
| 4458 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4459 | t->srv = srv; |
| 4460 | break; |
| 4461 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4462 | txn->flags &= ~TX_CK_MASK; |
| 4463 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4464 | } |
| 4465 | } |
| 4466 | srv = srv->next; |
| 4467 | }/* end while(srv) */ |
| 4468 | }/* end else if server == NULL */ |
| 4469 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 4470 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4471 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 4472 | } |
| 4473 | |
| 4474 | /* we'll have to look for another cookie ... */ |
| 4475 | p1 = p4; |
| 4476 | } /* while (p1 < cur_end) */ |
| 4477 | |
| 4478 | /* There's no more cookie on this line. |
| 4479 | * We may have marked the last one(s) for deletion. |
| 4480 | * We must do this now in two ways : |
| 4481 | * - if there is no app cookie, we simply delete the header ; |
| 4482 | * - if there are app cookies, we must delete the end of the |
| 4483 | * string properly, including the colon/semi-colon before |
| 4484 | * the cookie name. |
| 4485 | */ |
| 4486 | if (del_cookie != NULL) { |
| 4487 | int delta; |
| 4488 | if (app_cookies) { |
| 4489 | delta = buffer_replace2(req, del_colon, cur_end, NULL, 0); |
| 4490 | cur_end = del_colon; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4491 | cur_hdr->len += delta; |
| 4492 | } else { |
| 4493 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4494 | |
| 4495 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4496 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4497 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4498 | cur_hdr->len = 0; |
| 4499 | } |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 4500 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4501 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4502 | } |
| 4503 | |
| 4504 | /* keep the link from this header to next one */ |
| 4505 | old_idx = cur_idx; |
| 4506 | } /* end of cookie processing on this header */ |
| 4507 | } |
| 4508 | |
| 4509 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4510 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 4511 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 4512 | */ |
| 4513 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4514 | { |
| 4515 | char term; |
| 4516 | char *cur_ptr, *cur_end, *cur_next; |
| 4517 | int cur_idx, old_idx, last_hdr; |
| 4518 | struct http_txn *txn = &t->txn; |
| 4519 | struct hdr_idx_elem *cur_hdr; |
| 4520 | int len, delta; |
| 4521 | |
| 4522 | last_hdr = 0; |
| 4523 | |
| 4524 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4525 | old_idx = 0; |
| 4526 | |
| 4527 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4528 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4529 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4530 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4531 | (exp->action == ACT_ALLOW || |
| 4532 | exp->action == ACT_DENY)) |
| 4533 | return 0; |
| 4534 | |
| 4535 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 4536 | if (!cur_idx) |
| 4537 | break; |
| 4538 | |
| 4539 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4540 | cur_ptr = cur_next; |
| 4541 | cur_end = cur_ptr + cur_hdr->len; |
| 4542 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4543 | |
| 4544 | /* Now we have one header between cur_ptr and cur_end, |
| 4545 | * and the next header starts at cur_next. |
| 4546 | */ |
| 4547 | |
| 4548 | /* The annoying part is that pattern matching needs |
| 4549 | * that we modify the contents to null-terminate all |
| 4550 | * strings before testing them. |
| 4551 | */ |
| 4552 | |
| 4553 | term = *cur_end; |
| 4554 | *cur_end = '\0'; |
| 4555 | |
| 4556 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4557 | switch (exp->action) { |
| 4558 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4559 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4560 | last_hdr = 1; |
| 4561 | break; |
| 4562 | |
| 4563 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4564 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4565 | last_hdr = 1; |
| 4566 | break; |
| 4567 | |
| 4568 | case ACT_REPLACE: |
| 4569 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4570 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 4571 | /* FIXME: if the user adds a newline in the replacement, the |
| 4572 | * index will not be recalculated for now, and the new line |
| 4573 | * will not be counted as a new header. |
| 4574 | */ |
| 4575 | |
| 4576 | cur_end += delta; |
| 4577 | cur_next += delta; |
| 4578 | cur_hdr->len += delta; |
| 4579 | txn->rsp.eoh += delta; |
| 4580 | break; |
| 4581 | |
| 4582 | case ACT_REMOVE: |
| 4583 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 4584 | cur_next += delta; |
| 4585 | |
| 4586 | /* FIXME: this should be a separate function */ |
| 4587 | txn->rsp.eoh += delta; |
| 4588 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4589 | txn->hdr_idx.used--; |
| 4590 | cur_hdr->len = 0; |
| 4591 | cur_end = NULL; /* null-term has been rewritten */ |
| 4592 | break; |
| 4593 | |
| 4594 | } |
| 4595 | } |
| 4596 | if (cur_end) |
| 4597 | *cur_end = term; /* restore the string terminator */ |
| 4598 | |
| 4599 | /* keep the link from this header to next one in case of later |
| 4600 | * removal of next header. |
| 4601 | */ |
| 4602 | old_idx = cur_idx; |
| 4603 | } |
| 4604 | return 0; |
| 4605 | } |
| 4606 | |
| 4607 | |
| 4608 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 4609 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 4610 | * or -1 if a replacement resulted in an invalid status line. |
| 4611 | */ |
| 4612 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4613 | { |
| 4614 | char term; |
| 4615 | char *cur_ptr, *cur_end; |
| 4616 | int done; |
| 4617 | struct http_txn *txn = &t->txn; |
| 4618 | int len, delta; |
| 4619 | |
| 4620 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4621 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4622 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4623 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4624 | (exp->action == ACT_ALLOW || |
| 4625 | exp->action == ACT_DENY)) |
| 4626 | return 0; |
| 4627 | else if (exp->action == ACT_REMOVE) |
| 4628 | return 0; |
| 4629 | |
| 4630 | done = 0; |
| 4631 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4632 | 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] | 4633 | cur_end = cur_ptr + txn->rsp.sl.rq.l; |
| 4634 | |
| 4635 | /* Now we have the status line between cur_ptr and cur_end */ |
| 4636 | |
| 4637 | /* The annoying part is that pattern matching needs |
| 4638 | * that we modify the contents to null-terminate all |
| 4639 | * strings before testing them. |
| 4640 | */ |
| 4641 | |
| 4642 | term = *cur_end; |
| 4643 | *cur_end = '\0'; |
| 4644 | |
| 4645 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4646 | switch (exp->action) { |
| 4647 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4648 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4649 | done = 1; |
| 4650 | break; |
| 4651 | |
| 4652 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4653 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4654 | done = 1; |
| 4655 | break; |
| 4656 | |
| 4657 | case ACT_REPLACE: |
| 4658 | *cur_end = term; /* restore the string terminator */ |
| 4659 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4660 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 4661 | /* FIXME: if the user adds a newline in the replacement, the |
| 4662 | * index will not be recalculated for now, and the new line |
| 4663 | * will not be counted as a new header. |
| 4664 | */ |
| 4665 | |
| 4666 | txn->rsp.eoh += delta; |
| 4667 | cur_end += delta; |
| 4668 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4669 | 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] | 4670 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 4671 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4672 | cur_ptr, cur_end + 1, |
| 4673 | NULL, NULL); |
| 4674 | if (unlikely(!cur_end)) |
| 4675 | return -1; |
| 4676 | |
| 4677 | /* we have a full respnse and we know that we have either a CR |
| 4678 | * or an LF at <ptr>. |
| 4679 | */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4680 | 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] | 4681 | hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r'); |
| 4682 | /* there is no point trying this regex on headers */ |
| 4683 | return 1; |
| 4684 | } |
| 4685 | } |
| 4686 | *cur_end = term; /* restore the string terminator */ |
| 4687 | return done; |
| 4688 | } |
| 4689 | |
| 4690 | |
| 4691 | |
| 4692 | /* |
| 4693 | * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>. |
| 4694 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 4695 | * unparsable response. |
| 4696 | */ |
| 4697 | int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4698 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4699 | struct http_txn *txn = &t->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4700 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4701 | while (exp && !(txn->flags & TX_SVDENY)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4702 | int ret; |
| 4703 | |
| 4704 | /* |
| 4705 | * The interleaving of transformations and verdicts |
| 4706 | * makes it difficult to decide to continue or stop |
| 4707 | * the evaluation. |
| 4708 | */ |
| 4709 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4710 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4711 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 4712 | exp->action == ACT_PASS)) { |
| 4713 | exp = exp->next; |
| 4714 | continue; |
| 4715 | } |
| 4716 | |
| 4717 | /* Apply the filter to the status line. */ |
| 4718 | ret = apply_filter_to_sts_line(t, rtr, exp); |
| 4719 | if (unlikely(ret < 0)) |
| 4720 | return -1; |
| 4721 | |
| 4722 | if (likely(ret == 0)) { |
| 4723 | /* The filter did not match the response, it can be |
| 4724 | * iterated through all headers. |
| 4725 | */ |
| 4726 | apply_filter_to_resp_headers(t, rtr, exp); |
| 4727 | } |
| 4728 | exp = exp->next; |
| 4729 | } |
| 4730 | return 0; |
| 4731 | } |
| 4732 | |
| 4733 | |
| 4734 | |
| 4735 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 4736 | * Manage server-side cookies. It can impact performance by about 2% so it is |
| 4737 | * desirable to call it only when needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4738 | */ |
| 4739 | void manage_server_side_cookies(struct session *t, struct buffer *rtr) |
| 4740 | { |
| 4741 | struct http_txn *txn = &t->txn; |
| 4742 | char *p1, *p2, *p3, *p4; |
| 4743 | |
| 4744 | appsess *asession_temp = NULL; |
| 4745 | appsess local_asession; |
| 4746 | |
| 4747 | char *cur_ptr, *cur_end, *cur_next; |
| 4748 | int cur_idx, old_idx, delta; |
| 4749 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4750 | /* Iterate through the headers. |
| 4751 | * we start with the start line. |
| 4752 | */ |
| 4753 | old_idx = 0; |
| 4754 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4755 | |
| 4756 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 4757 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4758 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4759 | |
| 4760 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4761 | cur_ptr = cur_next; |
| 4762 | cur_end = cur_ptr + cur_hdr->len; |
| 4763 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4764 | |
| 4765 | /* We have one full header between cur_ptr and cur_end, and the |
| 4766 | * next header starts at cur_next. We're only interested in |
| 4767 | * "Cookie:" headers. |
| 4768 | */ |
| 4769 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4770 | val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10); |
| 4771 | if (!val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4772 | old_idx = cur_idx; |
| 4773 | continue; |
| 4774 | } |
| 4775 | |
| 4776 | /* 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] | 4777 | txn->flags |= TX_SCK_ANY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4778 | |
| 4779 | |
| 4780 | /* maybe we only wanted to see if there was a set-cookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4781 | if (t->be->cookie_name == NULL && |
| 4782 | t->be->appsession_name == NULL && |
| 4783 | t->be->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4784 | return; |
| 4785 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4786 | p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4787 | |
| 4788 | 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] | 4789 | if (p1 == cur_end || *p1 == ';') /* end of cookie */ |
| 4790 | break; |
| 4791 | |
| 4792 | /* p1 is at the beginning of the cookie name */ |
| 4793 | p2 = p1; |
| 4794 | |
| 4795 | while (p2 < cur_end && *p2 != '=' && *p2 != ';') |
| 4796 | p2++; |
| 4797 | |
| 4798 | if (p2 == cur_end || *p2 == ';') /* next cookie */ |
| 4799 | break; |
| 4800 | |
| 4801 | p3 = p2 + 1; /* skip the '=' sign */ |
| 4802 | if (p3 == cur_end) |
| 4803 | break; |
| 4804 | |
| 4805 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4806 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';') |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4807 | p4++; |
| 4808 | |
| 4809 | /* here, we have the cookie name between p1 and p2, |
| 4810 | * and its value between p3 and p4. |
| 4811 | * we can process it. |
| 4812 | */ |
| 4813 | |
| 4814 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4815 | if (t->be->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4816 | txn->srv_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4817 | (p4 - p1 >= t->be->capture_namelen) && |
| 4818 | memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4819 | int log_len = p4 - p1; |
| 4820 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 4821 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4822 | Alert("HTTP logging : out of memory.\n"); |
| 4823 | } |
| 4824 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4825 | if (log_len > t->be->capture_len) |
| 4826 | log_len = t->be->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4827 | memcpy(txn->srv_cookie, p1, log_len); |
| 4828 | txn->srv_cookie[log_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4829 | } |
| 4830 | |
| 4831 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4832 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 4833 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4834 | /* Cool... it's the right one */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4835 | txn->flags |= TX_SCK_SEEN; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4836 | |
| 4837 | /* If the cookie is in insert mode on a known server, we'll delete |
| 4838 | * this occurrence because we'll insert another one later. |
| 4839 | * We'll delete it too if the "indirect" option is set and we're in |
| 4840 | * a direct access. */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4841 | if (((t->srv) && (t->be->options & PR_O_COOK_INS)) || |
| 4842 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4843 | /* this header must be deleted */ |
| 4844 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 4845 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4846 | txn->hdr_idx.used--; |
| 4847 | cur_hdr->len = 0; |
| 4848 | cur_next += delta; |
| 4849 | txn->rsp.eoh += delta; |
| 4850 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4851 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4852 | } |
| 4853 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4854 | (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4855 | /* replace bytes p3->p4 with the cookie name associated |
| 4856 | * with this server since we know it. |
| 4857 | */ |
| 4858 | delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen); |
| 4859 | cur_hdr->len += delta; |
| 4860 | cur_next += delta; |
| 4861 | txn->rsp.eoh += delta; |
| 4862 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4863 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4864 | } |
| 4865 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4866 | (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4867 | /* insert the cookie name associated with this server |
| 4868 | * before existing cookie, and insert a delimitor between them.. |
| 4869 | */ |
| 4870 | delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1); |
| 4871 | cur_hdr->len += delta; |
| 4872 | cur_next += delta; |
| 4873 | txn->rsp.eoh += delta; |
| 4874 | |
| 4875 | p3[t->srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4876 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4877 | } |
| 4878 | } |
| 4879 | /* next, let's see if the cookie is our appcookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4880 | else if ((t->be->appsession_name != NULL) && |
| 4881 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4882 | |
| 4883 | /* Cool... it's the right one */ |
| 4884 | |
| 4885 | size_t server_id_len = strlen(t->srv->id) + 1; |
| 4886 | asession_temp = &local_asession; |
| 4887 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4888 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4889 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4890 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4891 | return; |
| 4892 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4893 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 4894 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4895 | asession_temp->serverid = NULL; |
| 4896 | |
| 4897 | /* only do insert, if lookup fails */ |
Ryan Warnick | 6d0b1fa | 2008-02-17 11:24:35 +0100 | [diff] [blame] | 4898 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 4899 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4900 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4901 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 4902 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 4903 | return; |
| 4904 | } |
| 4905 | asession_temp->sessid = local_asession.sessid; |
| 4906 | asession_temp->serverid = local_asession.serverid; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4907 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
| 4908 | } else { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4909 | /* free wasted memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4910 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4911 | } |
| 4912 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4913 | if (asession_temp->serverid == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4914 | if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4915 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4916 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4917 | return; |
| 4918 | } |
| 4919 | asession_temp->serverid[0] = '\0'; |
| 4920 | } |
| 4921 | |
| 4922 | if (asession_temp->serverid[0] == '\0') |
| 4923 | memcpy(asession_temp->serverid, t->srv->id, server_id_len); |
| 4924 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 4925 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4926 | |
| 4927 | #if defined(DEBUG_HASH) |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4928 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4929 | #endif |
| 4930 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 4931 | break; /* we don't want to loop again since there cannot be another cookie on the same line */ |
| 4932 | } /* we're now at the end of the cookie value */ |
| 4933 | |
| 4934 | /* keep the link from this header to next one */ |
| 4935 | old_idx = cur_idx; |
| 4936 | } /* end of cookie processing on this header */ |
| 4937 | } |
| 4938 | |
| 4939 | |
| 4940 | |
| 4941 | /* |
| 4942 | * Check if response is cacheable or not. Updates t->flags. |
| 4943 | */ |
| 4944 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 4945 | { |
| 4946 | struct http_txn *txn = &t->txn; |
| 4947 | char *p1, *p2; |
| 4948 | |
| 4949 | char *cur_ptr, *cur_end, *cur_next; |
| 4950 | int cur_idx; |
| 4951 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 4952 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4953 | return; |
| 4954 | |
| 4955 | /* Iterate through the headers. |
| 4956 | * we start with the start line. |
| 4957 | */ |
| 4958 | cur_idx = 0; |
| 4959 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4960 | |
| 4961 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 4962 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4963 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4964 | |
| 4965 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4966 | cur_ptr = cur_next; |
| 4967 | cur_end = cur_ptr + cur_hdr->len; |
| 4968 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4969 | |
| 4970 | /* We have one full header between cur_ptr and cur_end, and the |
| 4971 | * next header starts at cur_next. We're only interested in |
| 4972 | * "Cookie:" headers. |
| 4973 | */ |
| 4974 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4975 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 4976 | if (val) { |
| 4977 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 4978 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 4979 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 4980 | return; |
| 4981 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4982 | } |
| 4983 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4984 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 4985 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4986 | continue; |
| 4987 | |
| 4988 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 4989 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4990 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4991 | |
| 4992 | if (p1 >= cur_end) /* no more info */ |
| 4993 | continue; |
| 4994 | |
| 4995 | /* p1 is at the beginning of the value */ |
| 4996 | p2 = p1; |
| 4997 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4998 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4999 | p2++; |
| 5000 | |
| 5001 | /* we have a complete value between p1 and p2 */ |
| 5002 | if (p2 < cur_end && *p2 == '=') { |
| 5003 | /* we have something of the form no-cache="set-cookie" */ |
| 5004 | if ((cur_end - p1 >= 21) && |
| 5005 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 5006 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5007 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5008 | continue; |
| 5009 | } |
| 5010 | |
| 5011 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 5012 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 5013 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 5014 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 5015 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5016 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5017 | return; |
| 5018 | } |
| 5019 | |
| 5020 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5021 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5022 | continue; |
| 5023 | } |
| 5024 | } |
| 5025 | } |
| 5026 | |
| 5027 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5028 | /* |
| 5029 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 5030 | * If the server is found, it's assigned to the session. |
| 5031 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5032 | 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] | 5033 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5034 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5035 | appsess *asession_temp = NULL; |
| 5036 | appsess local_asession; |
| 5037 | char *request_line; |
| 5038 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5039 | if (t->be->appsession_name == NULL || |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 5040 | (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) || |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5041 | (request_line = memchr(begin, ';', len)) == NULL || |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5042 | ((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] | 5043 | return; |
| 5044 | |
| 5045 | /* skip ';' */ |
| 5046 | request_line++; |
| 5047 | |
| 5048 | /* look if we have a jsessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5049 | 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] | 5050 | return; |
| 5051 | |
| 5052 | /* skip jsessionid= */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5053 | request_line += t->be->appsession_name_len + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5054 | |
| 5055 | /* First try if we already have an appsession */ |
| 5056 | asession_temp = &local_asession; |
| 5057 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5058 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5059 | Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n"); |
| 5060 | send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n"); |
| 5061 | return; |
| 5062 | } |
| 5063 | |
| 5064 | /* Copy the sessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5065 | memcpy(asession_temp->sessid, request_line, t->be->appsession_len); |
| 5066 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5067 | asession_temp->serverid = NULL; |
| 5068 | |
| 5069 | /* only do insert, if lookup fails */ |
Ryan Warnick | 6d0b1fa | 2008-02-17 11:24:35 +0100 | [diff] [blame] | 5070 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 5071 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5072 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5073 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5074 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5075 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 5076 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 5077 | return; |
| 5078 | } |
| 5079 | asession_temp->sessid = local_asession.sessid; |
| 5080 | asession_temp->serverid = local_asession.serverid; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5081 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5082 | } |
| 5083 | else { |
| 5084 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5085 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5086 | } |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5087 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 5088 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5089 | asession_temp->request_count++; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5090 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5091 | #if defined(DEBUG_HASH) |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5092 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5093 | #endif |
| 5094 | if (asession_temp->serverid == NULL) { |
| 5095 | Alert("Found Application Session without matching server.\n"); |
| 5096 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5097 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5098 | while (srv) { |
| 5099 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5100 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5101 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5102 | txn->flags &= ~TX_CK_MASK; |
| 5103 | txn->flags |= TX_CK_VALID; |
| 5104 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5105 | t->srv = srv; |
| 5106 | break; |
| 5107 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5108 | txn->flags &= ~TX_CK_MASK; |
| 5109 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5110 | } |
| 5111 | } |
| 5112 | srv = srv->next; |
| 5113 | } |
| 5114 | } |
| 5115 | } |
| 5116 | |
| 5117 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5118 | /* |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5119 | * In a GET or HEAD request, check if the requested URI matches the stats uri |
| 5120 | * 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] | 5121 | * |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5122 | * 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] | 5123 | * 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] | 5124 | * produce_content() can be called to start sending data. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5125 | * |
| 5126 | * Returns 1 if the session's state changes, otherwise 0. |
| 5127 | */ |
| 5128 | int stats_check_uri_auth(struct session *t, struct proxy *backend) |
| 5129 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5130 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5131 | struct uri_auth *uri_auth = backend->uri_auth; |
| 5132 | struct user_auth *user; |
| 5133 | int authenticated, cur_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5134 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5135 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5136 | memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats)); |
| 5137 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5138 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5139 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5140 | return 0; |
| 5141 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5142 | h = t->req->data + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5143 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5144 | /* the URI is in h */ |
| 5145 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5146 | return 0; |
| 5147 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 5148 | h += uri_auth->uri_len; |
| 5149 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) { |
| 5150 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5151 | t->data_ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 5152 | break; |
| 5153 | } |
| 5154 | h++; |
| 5155 | } |
| 5156 | |
| 5157 | if (uri_auth->refresh) { |
| 5158 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 5159 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) { |
| 5160 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5161 | t->data_ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 5162 | break; |
| 5163 | } |
| 5164 | h++; |
| 5165 | } |
| 5166 | } |
| 5167 | |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 5168 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 5169 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) { |
| 5170 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5171 | t->data_ctx.stats.flags |= STAT_FMT_CSV; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 5172 | break; |
| 5173 | } |
| 5174 | h++; |
| 5175 | } |
| 5176 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5177 | t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO; |
| 5178 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5179 | /* we are in front of a interceptable URI. Let's check |
| 5180 | * if there's an authentication and if it's valid. |
| 5181 | */ |
| 5182 | user = uri_auth->users; |
| 5183 | if (!user) { |
| 5184 | /* no user auth required, it's OK */ |
| 5185 | authenticated = 1; |
| 5186 | } else { |
| 5187 | authenticated = 0; |
| 5188 | |
| 5189 | /* a user list is defined, we have to check. |
| 5190 | * skip 21 chars for "Authorization: Basic ". |
| 5191 | */ |
| 5192 | |
| 5193 | /* FIXME: this should move to an earlier place */ |
| 5194 | cur_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5195 | h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 5196 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 5197 | int len = txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5198 | if (len > 14 && |
| 5199 | !strncasecmp("Authorization:", h, 14)) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5200 | txn->auth_hdr.str = h; |
| 5201 | txn->auth_hdr.len = len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5202 | break; |
| 5203 | } |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5204 | h += len + txn->hdr_idx.v[cur_idx].cr + 1; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5205 | } |
| 5206 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5207 | if (txn->auth_hdr.len < 21 || |
| 5208 | memcmp(txn->auth_hdr.str + 14, " Basic ", 7)) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5209 | user = NULL; |
| 5210 | |
| 5211 | while (user) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5212 | if ((txn->auth_hdr.len == user->user_len + 14 + 7) |
| 5213 | && !memcmp(txn->auth_hdr.str + 14 + 7, |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5214 | user->user_pwd, user->user_len)) { |
| 5215 | authenticated = 1; |
| 5216 | break; |
| 5217 | } |
| 5218 | user = user->next; |
| 5219 | } |
| 5220 | } |
| 5221 | |
| 5222 | if (!authenticated) { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5223 | struct chunk msg; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5224 | |
| 5225 | /* no need to go further */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5226 | msg.str = trash; |
| 5227 | msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm); |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5228 | txn->status = 401; |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5229 | client_retnclose(t, &msg); |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5230 | if (!(t->flags & SN_ERR_MASK)) |
| 5231 | t->flags |= SN_ERR_PRXCOND; |
| 5232 | if (!(t->flags & SN_FINST_MASK)) |
| 5233 | t->flags |= SN_FINST_R; |
| 5234 | return 1; |
| 5235 | } |
| 5236 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5237 | /* The request is valid, the user is authenticated. Let's start sending |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5238 | * data. |
| 5239 | */ |
Willy Tarreau | 284c7b3 | 2008-06-29 16:38:43 +0200 | [diff] [blame] | 5240 | EV_FD_CLR(t->cli_fd, DIR_RD); |
| 5241 | buffer_shutr(t->req); |
| 5242 | buffer_shutr(t->rep); |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5243 | t->cli_state = CL_STSHUTR; |
| 5244 | t->req->rlim = t->req->data + BUFSIZE; /* no more rewrite needed */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 5245 | t->logs.tv_request = now; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5246 | t->data_source = DATA_SRC_STATS; |
| 5247 | t->data_state = DATA_ST_INIT; |
Willy Tarreau | 91e9993 | 2008-06-30 07:51:00 +0200 | [diff] [blame] | 5248 | t->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5249 | produce_content(t); |
| 5250 | return 1; |
| 5251 | } |
| 5252 | |
| 5253 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5254 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5255 | * Print a debug line with a header |
| 5256 | */ |
| 5257 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 5258 | { |
| 5259 | int len, max; |
| 5260 | len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id, |
| 5261 | dir, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd); |
| 5262 | max = end - start; |
| 5263 | UBOUND(max, sizeof(trash) - len - 1); |
| 5264 | len += strlcpy2(trash + len, start, max + 1); |
| 5265 | trash[len++] = '\n'; |
| 5266 | write(1, trash, len); |
| 5267 | } |
| 5268 | |
| 5269 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5270 | /************************************************************************/ |
| 5271 | /* The code below is dedicated to ACL parsing and matching */ |
| 5272 | /************************************************************************/ |
| 5273 | |
| 5274 | |
| 5275 | |
| 5276 | |
| 5277 | /* 1. Check on METHOD |
| 5278 | * We use the pre-parsed method if it is known, and store its number as an |
| 5279 | * integer. If it is unknown, we use the pointer and the length. |
| 5280 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5281 | 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] | 5282 | { |
| 5283 | int len, meth; |
| 5284 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5285 | len = strlen(*text); |
| 5286 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5287 | |
| 5288 | pattern->val.i = meth; |
| 5289 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5290 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5291 | if (!pattern->ptr.str) |
| 5292 | return 0; |
| 5293 | pattern->len = len; |
| 5294 | } |
| 5295 | return 1; |
| 5296 | } |
| 5297 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5298 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5299 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5300 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5301 | { |
| 5302 | int meth; |
| 5303 | struct http_txn *txn = l7; |
| 5304 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5305 | if (!txn) |
| 5306 | return 0; |
| 5307 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5308 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5309 | return 0; |
| 5310 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5311 | meth = txn->meth; |
| 5312 | test->i = meth; |
| 5313 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5314 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5315 | /* ensure the indexes are not affected */ |
| 5316 | return 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5317 | test->len = txn->req.sl.rq.m_l; |
| 5318 | test->ptr = txn->req.sol; |
| 5319 | } |
| 5320 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5321 | return 1; |
| 5322 | } |
| 5323 | |
| 5324 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 5325 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 5326 | int icase; |
| 5327 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5328 | if (test->i != pattern->val.i) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5329 | return ACL_PAT_FAIL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5330 | |
| 5331 | if (test->i != HTTP_METH_OTHER) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5332 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5333 | |
| 5334 | /* Other method, we must compare the strings */ |
| 5335 | if (pattern->len != test->len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5336 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 5337 | |
| 5338 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 5339 | if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) || |
| 5340 | (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5341 | return ACL_PAT_FAIL; |
| 5342 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5343 | } |
| 5344 | |
| 5345 | /* 2. Check on Request/Status Version |
| 5346 | * We simply compare strings here. |
| 5347 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5348 | 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] | 5349 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5350 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5351 | if (!pattern->ptr.str) |
| 5352 | return 0; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5353 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5354 | return 1; |
| 5355 | } |
| 5356 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5357 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5358 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5359 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5360 | { |
| 5361 | struct http_txn *txn = l7; |
| 5362 | char *ptr; |
| 5363 | int len; |
| 5364 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5365 | if (!txn) |
| 5366 | return 0; |
| 5367 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5368 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5369 | return 0; |
| 5370 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5371 | len = txn->req.sl.rq.v_l; |
| 5372 | ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som; |
| 5373 | |
| 5374 | while ((len-- > 0) && (*ptr++ != '/')); |
| 5375 | if (len <= 0) |
| 5376 | return 0; |
| 5377 | |
| 5378 | test->ptr = ptr; |
| 5379 | test->len = len; |
| 5380 | |
| 5381 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5382 | return 1; |
| 5383 | } |
| 5384 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5385 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5386 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5387 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5388 | { |
| 5389 | struct http_txn *txn = l7; |
| 5390 | char *ptr; |
| 5391 | int len; |
| 5392 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5393 | if (!txn) |
| 5394 | return 0; |
| 5395 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5396 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5397 | return 0; |
| 5398 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5399 | len = txn->rsp.sl.st.v_l; |
| 5400 | ptr = txn->rsp.sol; |
| 5401 | |
| 5402 | while ((len-- > 0) && (*ptr++ != '/')); |
| 5403 | if (len <= 0) |
| 5404 | return 0; |
| 5405 | |
| 5406 | test->ptr = ptr; |
| 5407 | test->len = len; |
| 5408 | |
| 5409 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5410 | return 1; |
| 5411 | } |
| 5412 | |
| 5413 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5414 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5415 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5416 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5417 | { |
| 5418 | struct http_txn *txn = l7; |
| 5419 | char *ptr; |
| 5420 | int len; |
| 5421 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5422 | if (!txn) |
| 5423 | return 0; |
| 5424 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5425 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5426 | return 0; |
| 5427 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5428 | len = txn->rsp.sl.st.c_l; |
| 5429 | ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som; |
| 5430 | |
| 5431 | test->i = __strl2ui(ptr, len); |
| 5432 | test->flags = ACL_TEST_F_VOL_1ST; |
| 5433 | return 1; |
| 5434 | } |
| 5435 | |
| 5436 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5437 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5438 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5439 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5440 | { |
| 5441 | struct http_txn *txn = l7; |
| 5442 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5443 | if (!txn) |
| 5444 | return 0; |
| 5445 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5446 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5447 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5448 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5449 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5450 | /* ensure the indexes are not affected */ |
| 5451 | return 0; |
| 5452 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5453 | test->len = txn->req.sl.rq.u_l; |
| 5454 | test->ptr = txn->req.sol + txn->req.sl.rq.u; |
| 5455 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 5456 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 5457 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5458 | return 1; |
| 5459 | } |
| 5460 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5461 | static int |
| 5462 | acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5463 | struct acl_expr *expr, struct acl_test *test) |
| 5464 | { |
| 5465 | struct http_txn *txn = l7; |
| 5466 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5467 | if (!txn) |
| 5468 | return 0; |
| 5469 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5470 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5471 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5472 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5473 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5474 | /* ensure the indexes are not affected */ |
| 5475 | return 0; |
| 5476 | |
| 5477 | /* Parse HTTP request */ |
| 5478 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
| 5479 | test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr; |
| 5480 | test->i = AF_INET; |
| 5481 | |
| 5482 | /* |
| 5483 | * If we are parsing url in frontend space, we prepare backend stage |
| 5484 | * to not parse again the same url ! optimization lazyness... |
| 5485 | */ |
| 5486 | if (px->options & PR_O_HTTP_PROXY) |
| 5487 | l4->flags |= SN_ADDR_SET; |
| 5488 | |
| 5489 | test->flags = ACL_TEST_F_READ_ONLY; |
| 5490 | return 1; |
| 5491 | } |
| 5492 | |
| 5493 | static int |
| 5494 | acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5495 | struct acl_expr *expr, struct acl_test *test) |
| 5496 | { |
| 5497 | struct http_txn *txn = l7; |
| 5498 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5499 | if (!txn) |
| 5500 | return 0; |
| 5501 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5502 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5503 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5504 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5505 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5506 | /* ensure the indexes are not affected */ |
| 5507 | return 0; |
| 5508 | |
| 5509 | /* Same optimization as url_ip */ |
| 5510 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
| 5511 | test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port); |
| 5512 | |
| 5513 | if (px->options & PR_O_HTTP_PROXY) |
| 5514 | l4->flags |= SN_ADDR_SET; |
| 5515 | |
| 5516 | test->flags = ACL_TEST_F_READ_ONLY; |
| 5517 | return 1; |
| 5518 | } |
| 5519 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5520 | /* 5. Check on HTTP header. A pointer to the beginning of the value is returned. |
| 5521 | * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr(). |
| 5522 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5523 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5524 | 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] | 5525 | struct acl_expr *expr, struct acl_test *test) |
| 5526 | { |
| 5527 | struct http_txn *txn = l7; |
| 5528 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5529 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5530 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5531 | if (!txn) |
| 5532 | return 0; |
| 5533 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5534 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 5535 | /* search for header from the beginning */ |
| 5536 | ctx->idx = 0; |
| 5537 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5538 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 5539 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 5540 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5541 | test->len = ctx->vlen; |
| 5542 | test->ptr = (char *)ctx->line + ctx->val; |
| 5543 | return 1; |
| 5544 | } |
| 5545 | |
| 5546 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 5547 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5548 | return 0; |
| 5549 | } |
| 5550 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5551 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5552 | acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5553 | struct acl_expr *expr, struct acl_test *test) |
| 5554 | { |
| 5555 | struct http_txn *txn = l7; |
| 5556 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5557 | if (!txn) |
| 5558 | return 0; |
| 5559 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5560 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5561 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5562 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5563 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5564 | /* ensure the indexes are not affected */ |
| 5565 | return 0; |
| 5566 | |
| 5567 | return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test); |
| 5568 | } |
| 5569 | |
| 5570 | static int |
| 5571 | acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5572 | struct acl_expr *expr, struct acl_test *test) |
| 5573 | { |
| 5574 | struct http_txn *txn = l7; |
| 5575 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5576 | if (!txn) |
| 5577 | return 0; |
| 5578 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5579 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5580 | return 0; |
| 5581 | |
| 5582 | return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test); |
| 5583 | } |
| 5584 | |
| 5585 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 5586 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 5587 | */ |
| 5588 | static int |
| 5589 | 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] | 5590 | struct acl_expr *expr, struct acl_test *test) |
| 5591 | { |
| 5592 | struct http_txn *txn = l7; |
| 5593 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5594 | struct hdr_ctx ctx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5595 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5596 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5597 | if (!txn) |
| 5598 | return 0; |
| 5599 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5600 | ctx.idx = 0; |
| 5601 | cnt = 0; |
| 5602 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx)) |
| 5603 | cnt++; |
| 5604 | |
| 5605 | test->i = cnt; |
| 5606 | test->flags = ACL_TEST_F_VOL_HDR; |
| 5607 | return 1; |
| 5608 | } |
| 5609 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5610 | static int |
| 5611 | acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5612 | struct acl_expr *expr, struct acl_test *test) |
| 5613 | { |
| 5614 | struct http_txn *txn = l7; |
| 5615 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5616 | if (!txn) |
| 5617 | return 0; |
| 5618 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5619 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5620 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5621 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5622 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5623 | /* ensure the indexes are not affected */ |
| 5624 | return 0; |
| 5625 | |
| 5626 | return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test); |
| 5627 | } |
| 5628 | |
| 5629 | static int |
| 5630 | acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5631 | struct acl_expr *expr, struct acl_test *test) |
| 5632 | { |
| 5633 | struct http_txn *txn = l7; |
| 5634 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5635 | if (!txn) |
| 5636 | return 0; |
| 5637 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5638 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5639 | return 0; |
| 5640 | |
| 5641 | return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test); |
| 5642 | } |
| 5643 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5644 | /* 7. Check on HTTP header's integer value. The integer value is returned. |
| 5645 | * FIXME: the type is 'int', it may not be appropriate for everything. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5646 | * 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] | 5647 | */ |
| 5648 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5649 | 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] | 5650 | struct acl_expr *expr, struct acl_test *test) |
| 5651 | { |
| 5652 | struct http_txn *txn = l7; |
| 5653 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5654 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5655 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5656 | if (!txn) |
| 5657 | return 0; |
| 5658 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5659 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 5660 | /* search for header from the beginning */ |
| 5661 | ctx->idx = 0; |
| 5662 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5663 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 5664 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 5665 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5666 | test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen); |
| 5667 | return 1; |
| 5668 | } |
| 5669 | |
| 5670 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 5671 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5672 | return 0; |
| 5673 | } |
| 5674 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5675 | static int |
| 5676 | acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5677 | struct acl_expr *expr, struct acl_test *test) |
| 5678 | { |
| 5679 | struct http_txn *txn = l7; |
| 5680 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5681 | if (!txn) |
| 5682 | return 0; |
| 5683 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5684 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5685 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5686 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5687 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5688 | /* ensure the indexes are not affected */ |
| 5689 | return 0; |
| 5690 | |
| 5691 | return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test); |
| 5692 | } |
| 5693 | |
| 5694 | static int |
| 5695 | acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5696 | struct acl_expr *expr, struct acl_test *test) |
| 5697 | { |
| 5698 | struct http_txn *txn = l7; |
| 5699 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5700 | if (!txn) |
| 5701 | return 0; |
| 5702 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5703 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5704 | return 0; |
| 5705 | |
| 5706 | return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test); |
| 5707 | } |
| 5708 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5709 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 5710 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 5711 | */ |
| 5712 | static int |
| 5713 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5714 | struct acl_expr *expr, struct acl_test *test) |
| 5715 | { |
| 5716 | struct http_txn *txn = l7; |
| 5717 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5718 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5719 | if (!txn) |
| 5720 | return 0; |
| 5721 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5722 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5723 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5724 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5725 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5726 | /* ensure the indexes are not affected */ |
| 5727 | return 0; |
| 5728 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5729 | end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 5730 | ptr = http_get_path(txn); |
| 5731 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5732 | return 0; |
| 5733 | |
| 5734 | /* OK, we got the '/' ! */ |
| 5735 | test->ptr = ptr; |
| 5736 | |
| 5737 | while (ptr < end && *ptr != '?') |
| 5738 | ptr++; |
| 5739 | |
| 5740 | test->len = ptr - test->ptr; |
| 5741 | |
| 5742 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 5743 | test->flags = ACL_TEST_F_VOL_1ST; |
| 5744 | return 1; |
| 5745 | } |
| 5746 | |
| 5747 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5748 | |
| 5749 | /************************************************************************/ |
| 5750 | /* All supported keywords must be declared here. */ |
| 5751 | /************************************************************************/ |
| 5752 | |
| 5753 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 5754 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5755 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT }, |
| 5756 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5757 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 5758 | { "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] | 5759 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5760 | { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5761 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 5762 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 5763 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 5764 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 5765 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 5766 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 5767 | { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE }, |
| 5768 | { "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] | 5769 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5770 | /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */ |
| 5771 | { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5772 | { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 5773 | { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 5774 | { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 5775 | { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 5776 | { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 5777 | { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 5778 | { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
| 5779 | { "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] | 5780 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5781 | { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 5782 | { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE }, |
| 5783 | { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE }, |
| 5784 | { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE }, |
| 5785 | { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE }, |
| 5786 | { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE }, |
| 5787 | { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE }, |
| 5788 | { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
| 5789 | { "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] | 5790 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5791 | { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5792 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 5793 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 5794 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 5795 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 5796 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 5797 | { "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] | 5798 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 5799 | { NULL, NULL, NULL, NULL }, |
| 5800 | |
| 5801 | #if 0 |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5802 | { "line", acl_parse_str, acl_fetch_line, acl_match_str }, |
| 5803 | { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg }, |
| 5804 | { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg }, |
| 5805 | { "line_end", acl_parse_str, acl_fetch_line, acl_match_end }, |
| 5806 | { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub }, |
| 5807 | { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir }, |
| 5808 | { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom }, |
| 5809 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5810 | { "cook", acl_parse_str, acl_fetch_cook, acl_match_str }, |
| 5811 | { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg }, |
| 5812 | { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg }, |
| 5813 | { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end }, |
| 5814 | { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub }, |
| 5815 | { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir }, |
| 5816 | { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom }, |
| 5817 | { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst }, |
| 5818 | |
| 5819 | { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str }, |
| 5820 | { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg }, |
| 5821 | { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str }, |
| 5822 | { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 }, |
| 5823 | { NULL, NULL, NULL, NULL }, |
| 5824 | #endif |
| 5825 | }}; |
| 5826 | |
| 5827 | |
| 5828 | __attribute__((constructor)) |
| 5829 | static void __http_protocol_init(void) |
| 5830 | { |
| 5831 | acl_register_keywords(&acl_kws); |
| 5832 | } |
| 5833 | |
| 5834 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5835 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5836 | * Local variables: |
| 5837 | * c-indent-level: 8 |
| 5838 | * c-basic-offset: 8 |
| 5839 | * End: |
| 5840 | */ |