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 | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 368 | static char *cli_stnames[4] = { "DAT", "SHR", "SHW", "CLS" }; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 369 | static char *srv_stnames[7] = { "IDL", "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 | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 548 | t->rep->flags |= BF_MAY_FORWARD; |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 549 | buffer_shutw_done(t->req); |
| 550 | buffer_shutr_done(t->rep); |
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 | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 679 | if (s->analysis & AN_REQ_ANY) { |
| 680 | if (s->analysis & AN_REQ_INSPECT) |
| 681 | t->expire = tick_first(t->expire, s->inspect_exp); |
| 682 | else if (s->analysis & AN_REQ_HTTP_HDR) |
| 683 | t->expire = tick_first(t->expire, s->txn.exp); |
| 684 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 685 | |
| 686 | /* restore t to its place in the task list */ |
| 687 | task_queue(t); |
| 688 | |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 689 | *next = t->expire; |
| 690 | return; /* nothing more to do */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 691 | } |
| 692 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 693 | s->fe->feconn--; |
| 694 | if (s->flags & SN_BE_ASSIGNED) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 695 | s->be->beconn--; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 696 | actconn--; |
| 697 | |
Willy Tarreau | f41d4b1 | 2007-04-28 23:26:14 +0200 | [diff] [blame] | 698 | if (unlikely((global.mode & MODE_DEBUG) && |
| 699 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 700 | int len; |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 701 | len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 702 | s->uniq_id, s->be->id, |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 703 | (unsigned short)s->cli_fd, (unsigned short)s->srv_fd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 704 | write(1, trash, len); |
| 705 | } |
| 706 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 707 | 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] | 708 | session_process_counters(s); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 709 | |
| 710 | /* let's do a final log if we need it */ |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 711 | if (s->logs.logwait && |
| 712 | !(s->flags & SN_MONITOR) && |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 713 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 714 | if (s->fe->to_log & LW_REQ) |
| 715 | http_sess_log(s); |
| 716 | else |
| 717 | tcp_sess_log(s); |
| 718 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 719 | |
| 720 | /* the task MUST not be in the run queue anymore */ |
| 721 | task_delete(t); |
| 722 | session_free(s); |
| 723 | task_free(t); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 724 | *next = TICK_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 728 | extern const char sess_term_cond[8]; |
| 729 | extern const char sess_fin_state[8]; |
| 730 | extern const char *monthname[12]; |
| 731 | const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */ |
| 732 | const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown, |
| 733 | Set-cookie seen and left unchanged (passive), Set-cookie Deleted, |
| 734 | unknown, Set-cookie Rewritten */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 735 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 736 | struct pool_head *pool2_capture; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 737 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 738 | /* |
| 739 | * send a log for the session when we have enough info about it. |
| 740 | * Will not log if the frontend has no log defined. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 741 | */ |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 742 | static void http_sess_log(struct session *s) |
| 743 | { |
| 744 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 745 | struct proxy *fe = s->fe; |
| 746 | struct proxy *be = s->be; |
| 747 | struct proxy *prx_log; |
| 748 | struct http_txn *txn = &s->txn; |
| 749 | int tolog; |
| 750 | char *uri, *h; |
| 751 | char *svid; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 752 | struct tm tm; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 753 | static char tmpline[MAX_SYSLOG_LEN]; |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 754 | int t_request; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 755 | int hdr; |
| 756 | |
| 757 | if (fe->logfac1 < 0 && fe->logfac2 < 0) |
| 758 | return; |
| 759 | prx_log = fe; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 760 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 761 | if (s->cli_addr.ss_family == AF_INET) |
| 762 | inet_ntop(AF_INET, |
| 763 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 764 | pn, sizeof(pn)); |
| 765 | else |
| 766 | inet_ntop(AF_INET6, |
| 767 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 768 | pn, sizeof(pn)); |
| 769 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 770 | get_localtime(s->logs.accept_date.tv_sec, &tm); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 771 | |
| 772 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 773 | tolog = fe->to_log; |
| 774 | |
| 775 | h = tmpline; |
| 776 | if (fe->to_log & LW_REQHDR && |
| 777 | txn->req.cap && |
| 778 | (h < tmpline + sizeof(tmpline) - 10)) { |
| 779 | *(h++) = ' '; |
| 780 | *(h++) = '{'; |
| 781 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 782 | if (hdr) |
| 783 | *(h++) = '|'; |
| 784 | if (txn->req.cap[hdr] != NULL) |
| 785 | h = encode_string(h, tmpline + sizeof(tmpline) - 7, |
| 786 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 787 | } |
| 788 | *(h++) = '}'; |
| 789 | } |
| 790 | |
| 791 | if (fe->to_log & LW_RSPHDR && |
| 792 | txn->rsp.cap && |
| 793 | (h < tmpline + sizeof(tmpline) - 7)) { |
| 794 | *(h++) = ' '; |
| 795 | *(h++) = '{'; |
| 796 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 797 | if (hdr) |
| 798 | *(h++) = '|'; |
| 799 | if (txn->rsp.cap[hdr] != NULL) |
| 800 | h = encode_string(h, tmpline + sizeof(tmpline) - 4, |
| 801 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 802 | } |
| 803 | *(h++) = '}'; |
| 804 | } |
| 805 | |
| 806 | if (h < tmpline + sizeof(tmpline) - 4) { |
| 807 | *(h++) = ' '; |
| 808 | *(h++) = '"'; |
| 809 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 810 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 811 | '#', url_encode_map, uri); |
| 812 | *(h++) = '"'; |
| 813 | } |
| 814 | *h = '\0'; |
| 815 | |
| 816 | svid = (tolog & LW_SVID) ? |
| 817 | (s->data_source != DATA_SRC_STATS) ? |
| 818 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 819 | |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 820 | t_request = -1; |
| 821 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 822 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 823 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 824 | send_log(prx_log, LOG_INFO, |
| 825 | "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]" |
| 826 | " %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] | 827 | " %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] | 828 | pn, |
| 829 | (s->cli_addr.ss_family == AF_INET) ? |
| 830 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 831 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 832 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 833 | 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] | 834 | fe->id, be->id, svid, |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 835 | t_request, |
| 836 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 837 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 838 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 839 | (tolog & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 840 | txn->status, |
Willy Tarreau | 8b3977f | 2008-01-18 11:16:32 +0100 | [diff] [blame] | 841 | (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 842 | txn->cli_cookie ? txn->cli_cookie : "-", |
| 843 | txn->srv_cookie ? txn->srv_cookie : "-", |
| 844 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 845 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 846 | (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 847 | (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-', |
| 848 | 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] | 849 | (s->flags & SN_REDISP)?"+":"", |
| 850 | (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] | 851 | s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline); |
| 852 | |
| 853 | s->logs.logwait = 0; |
| 854 | } |
| 855 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 856 | |
| 857 | /* |
| 858 | * Capture headers from message starting at <som> according to header list |
| 859 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 860 | */ |
| 861 | void capture_headers(char *som, struct hdr_idx *idx, |
| 862 | char **cap, struct cap_hdr *cap_hdr) |
| 863 | { |
| 864 | char *eol, *sol, *col, *sov; |
| 865 | int cur_idx; |
| 866 | struct cap_hdr *h; |
| 867 | int len; |
| 868 | |
| 869 | sol = som + hdr_idx_first_pos(idx); |
| 870 | cur_idx = hdr_idx_first_idx(idx); |
| 871 | |
| 872 | while (cur_idx) { |
| 873 | eol = sol + idx->v[cur_idx].len; |
| 874 | |
| 875 | col = sol; |
| 876 | while (col < eol && *col != ':') |
| 877 | col++; |
| 878 | |
| 879 | sov = col + 1; |
| 880 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 881 | sov++; |
| 882 | |
| 883 | for (h = cap_hdr; h; h = h->next) { |
| 884 | if ((h->namelen == col - sol) && |
| 885 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 886 | if (cap[h->index] == NULL) |
| 887 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 888 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 889 | |
| 890 | if (cap[h->index] == NULL) { |
| 891 | Alert("HTTP capture : out of memory.\n"); |
| 892 | continue; |
| 893 | } |
| 894 | |
| 895 | len = eol - sov; |
| 896 | if (len > h->len) |
| 897 | len = h->len; |
| 898 | |
| 899 | memcpy(cap[h->index], sov, len); |
| 900 | cap[h->index][len]=0; |
| 901 | } |
| 902 | } |
| 903 | sol = eol + idx->v[cur_idx].cr + 1; |
| 904 | cur_idx = idx->v[cur_idx].next; |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 909 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 910 | */ |
| 911 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 912 | |
| 913 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 914 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 915 | */ |
| 916 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 917 | ptr++; \ |
| 918 | if (likely(ptr < end)) \ |
| 919 | goto good; \ |
| 920 | else { \ |
| 921 | state = (st); \ |
| 922 | goto http_msg_ood; \ |
| 923 | } \ |
| 924 | } while (0) |
| 925 | |
| 926 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 927 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 928 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 929 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 930 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 931 | * will give undefined results. |
| 932 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 933 | * and that msg->sol points to the beginning of the response. |
| 934 | * If a complete line is found (which implies that at least one CR or LF is |
| 935 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 936 | * returned indicating an incomplete line (which does not mean that parts have |
| 937 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 938 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 939 | * upon next call. |
| 940 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 941 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 942 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 943 | * 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] | 944 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 945 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 946 | const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, |
| 947 | unsigned int state, const char *ptr, const char *end, |
| 948 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 949 | { |
| 950 | __label__ |
| 951 | http_msg_rpver, |
| 952 | http_msg_rpver_sp, |
| 953 | http_msg_rpcode, |
| 954 | http_msg_rpcode_sp, |
| 955 | http_msg_rpreason, |
| 956 | http_msg_rpline_eol, |
| 957 | http_msg_ood, /* out of data */ |
| 958 | http_msg_invalid; |
| 959 | |
| 960 | switch (state) { |
| 961 | http_msg_rpver: |
| 962 | case HTTP_MSG_RPVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 963 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 964 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 965 | |
| 966 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 967 | msg->sl.st.v_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 968 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 969 | } |
| 970 | goto http_msg_invalid; |
| 971 | |
| 972 | http_msg_rpver_sp: |
| 973 | case HTTP_MSG_RPVER_SP: |
| 974 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 975 | msg->sl.st.c = ptr - msg_buf; |
| 976 | goto http_msg_rpcode; |
| 977 | } |
| 978 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 979 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 980 | /* so it's a CR/LF, this is invalid */ |
| 981 | goto http_msg_invalid; |
| 982 | |
| 983 | http_msg_rpcode: |
| 984 | case HTTP_MSG_RPCODE: |
| 985 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 986 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 987 | |
| 988 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 989 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 990 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 991 | } |
| 992 | |
| 993 | /* so it's a CR/LF, so there is no reason phrase */ |
| 994 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 995 | http_msg_rsp_reason: |
| 996 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
| 997 | msg->sl.st.r = ptr - msg_buf; |
| 998 | msg->sl.st.r_l = 0; |
| 999 | goto http_msg_rpline_eol; |
| 1000 | |
| 1001 | http_msg_rpcode_sp: |
| 1002 | case HTTP_MSG_RPCODE_SP: |
| 1003 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1004 | msg->sl.st.r = ptr - msg_buf; |
| 1005 | goto http_msg_rpreason; |
| 1006 | } |
| 1007 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1008 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1009 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1010 | goto http_msg_rsp_reason; |
| 1011 | |
| 1012 | http_msg_rpreason: |
| 1013 | case HTTP_MSG_RPREASON: |
| 1014 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1015 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
| 1016 | msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r; |
| 1017 | http_msg_rpline_eol: |
| 1018 | /* We have seen the end of line. Note that we do not |
| 1019 | * necessarily have the \n yet, but at least we know that we |
| 1020 | * have EITHER \r OR \n, otherwise the response would not be |
| 1021 | * complete. We can then record the response length and return |
| 1022 | * to the caller which will be able to register it. |
| 1023 | */ |
| 1024 | msg->sl.st.l = ptr - msg->sol; |
| 1025 | return ptr; |
| 1026 | |
| 1027 | #ifdef DEBUG_FULL |
| 1028 | default: |
| 1029 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1030 | exit(1); |
| 1031 | #endif |
| 1032 | } |
| 1033 | |
| 1034 | http_msg_ood: |
| 1035 | /* out of data */ |
| 1036 | if (ret_state) |
| 1037 | *ret_state = state; |
| 1038 | if (ret_ptr) |
| 1039 | *ret_ptr = (char *)ptr; |
| 1040 | return NULL; |
| 1041 | |
| 1042 | http_msg_invalid: |
| 1043 | /* invalid message */ |
| 1044 | if (ret_state) |
| 1045 | *ret_state = HTTP_MSG_ERROR; |
| 1046 | return NULL; |
| 1047 | } |
| 1048 | |
| 1049 | |
| 1050 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1051 | * This function parses a request line between <ptr> and <end>, starting with |
| 1052 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1053 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1054 | * will give undefined results. |
| 1055 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1056 | * and that msg->sol points to the beginning of the request. |
| 1057 | * If a complete line is found (which implies that at least one CR or LF is |
| 1058 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1059 | * returned indicating an incomplete line (which does not mean that parts have |
| 1060 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1061 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1062 | * upon next call. |
| 1063 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1064 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1065 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1066 | * 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] | 1067 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1068 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1069 | const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, |
| 1070 | unsigned int state, const char *ptr, const char *end, |
| 1071 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1072 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1073 | __label__ |
| 1074 | http_msg_rqmeth, |
| 1075 | http_msg_rqmeth_sp, |
| 1076 | http_msg_rquri, |
| 1077 | http_msg_rquri_sp, |
| 1078 | http_msg_rqver, |
| 1079 | http_msg_rqline_eol, |
| 1080 | http_msg_ood, /* out of data */ |
| 1081 | http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1082 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1083 | switch (state) { |
| 1084 | http_msg_rqmeth: |
| 1085 | case HTTP_MSG_RQMETH: |
| 1086 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1087 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1088 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1089 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1090 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1091 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1092 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1093 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1094 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1095 | /* HTTP 0.9 request */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1096 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1097 | http_msg_req09_uri: |
| 1098 | msg->sl.rq.u = ptr - msg_buf; |
| 1099 | http_msg_req09_uri_e: |
| 1100 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1101 | http_msg_req09_ver: |
| 1102 | msg->sl.rq.v = ptr - msg_buf; |
| 1103 | msg->sl.rq.v_l = 0; |
| 1104 | goto http_msg_rqline_eol; |
| 1105 | } |
| 1106 | goto http_msg_invalid; |
| 1107 | |
| 1108 | http_msg_rqmeth_sp: |
| 1109 | case HTTP_MSG_RQMETH_SP: |
| 1110 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1111 | msg->sl.rq.u = ptr - msg_buf; |
| 1112 | goto http_msg_rquri; |
| 1113 | } |
| 1114 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1115 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1116 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1117 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1118 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1119 | http_msg_rquri: |
| 1120 | case HTTP_MSG_RQURI: |
| 1121 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1122 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1123 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1124 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1125 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1126 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1127 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1128 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1129 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1130 | goto http_msg_req09_uri_e; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1131 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1132 | http_msg_rquri_sp: |
| 1133 | case HTTP_MSG_RQURI_SP: |
| 1134 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1135 | msg->sl.rq.v = ptr - msg_buf; |
| 1136 | goto http_msg_rqver; |
| 1137 | } |
| 1138 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1139 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1140 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1141 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1142 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1143 | http_msg_rqver: |
| 1144 | case HTTP_MSG_RQVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1145 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1146 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1147 | |
| 1148 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1149 | msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v; |
| 1150 | http_msg_rqline_eol: |
| 1151 | /* We have seen the end of line. Note that we do not |
| 1152 | * necessarily have the \n yet, but at least we know that we |
| 1153 | * have EITHER \r OR \n, otherwise the request would not be |
| 1154 | * complete. We can then record the request length and return |
| 1155 | * to the caller which will be able to register it. |
| 1156 | */ |
| 1157 | msg->sl.rq.l = ptr - msg->sol; |
| 1158 | return ptr; |
| 1159 | } |
| 1160 | |
| 1161 | /* neither an HTTP_VER token nor a CRLF */ |
| 1162 | goto http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1163 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1164 | #ifdef DEBUG_FULL |
| 1165 | default: |
| 1166 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1167 | exit(1); |
| 1168 | #endif |
| 1169 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1170 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1171 | http_msg_ood: |
| 1172 | /* out of data */ |
| 1173 | if (ret_state) |
| 1174 | *ret_state = state; |
| 1175 | if (ret_ptr) |
| 1176 | *ret_ptr = (char *)ptr; |
| 1177 | return NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1178 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1179 | http_msg_invalid: |
| 1180 | /* invalid message */ |
| 1181 | if (ret_state) |
| 1182 | *ret_state = HTTP_MSG_ERROR; |
| 1183 | return NULL; |
| 1184 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1185 | |
| 1186 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1187 | /* |
| 1188 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1189 | * depending on the initial msg->msg_state. It can be preempted everywhere |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1190 | * when data are missing and recalled at the exact same location with no |
| 1191 | * information loss. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1192 | * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other |
| 1193 | * fields. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1194 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1195 | void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx) |
| 1196 | { |
| 1197 | __label__ |
| 1198 | http_msg_rqbefore, |
| 1199 | http_msg_rqbefore_cr, |
| 1200 | http_msg_rqmeth, |
| 1201 | http_msg_rqline_end, |
| 1202 | http_msg_hdr_first, |
| 1203 | http_msg_hdr_name, |
| 1204 | http_msg_hdr_l1_sp, |
| 1205 | http_msg_hdr_l1_lf, |
| 1206 | http_msg_hdr_l1_lws, |
| 1207 | http_msg_hdr_val, |
| 1208 | http_msg_hdr_l2_lf, |
| 1209 | http_msg_hdr_l2_lws, |
| 1210 | http_msg_complete_header, |
| 1211 | http_msg_last_lf, |
| 1212 | http_msg_ood, /* out of data */ |
| 1213 | http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1214 | |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1215 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1216 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1217 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1218 | state = msg->msg_state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1219 | ptr = buf->lr; |
| 1220 | end = buf->r; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1221 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1222 | if (unlikely(ptr >= end)) |
| 1223 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1224 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1225 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1226 | /* |
| 1227 | * First, states that are specific to the response only. |
| 1228 | * We check them first so that request and headers are |
| 1229 | * closer to each other (accessed more often). |
| 1230 | */ |
| 1231 | http_msg_rpbefore: |
| 1232 | case HTTP_MSG_RPBEFORE: |
| 1233 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1234 | if (likely(ptr == buf->data)) { |
| 1235 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1236 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1237 | } else { |
| 1238 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1239 | /* only skip empty leading lines, don't remove them */ |
| 1240 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1241 | msg->som = ptr - buf->data; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1242 | #else |
| 1243 | /* Remove empty leading lines, as recommended by |
| 1244 | * RFC2616. This takes a lot of time because we |
| 1245 | * must move all the buffer backwards, but this |
| 1246 | * is rarely needed. The method above will be |
| 1247 | * cleaner when we'll be able to start sending |
| 1248 | * the request from any place in the buffer. |
| 1249 | */ |
| 1250 | buf->lr = ptr; |
| 1251 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1252 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1253 | msg->sol = buf->data; |
| 1254 | ptr = buf->data; |
| 1255 | end = buf->r; |
| 1256 | #endif |
| 1257 | } |
| 1258 | hdr_idx_init(idx); |
| 1259 | state = HTTP_MSG_RPVER; |
| 1260 | goto http_msg_rpver; |
| 1261 | } |
| 1262 | |
| 1263 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1264 | goto http_msg_invalid; |
| 1265 | |
| 1266 | if (unlikely(*ptr == '\n')) |
| 1267 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1268 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1269 | /* stop here */ |
| 1270 | |
| 1271 | http_msg_rpbefore_cr: |
| 1272 | case HTTP_MSG_RPBEFORE_CR: |
| 1273 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1274 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1275 | /* stop here */ |
| 1276 | |
| 1277 | http_msg_rpver: |
| 1278 | case HTTP_MSG_RPVER: |
| 1279 | case HTTP_MSG_RPVER_SP: |
| 1280 | case HTTP_MSG_RPCODE: |
| 1281 | case HTTP_MSG_RPCODE_SP: |
| 1282 | case HTTP_MSG_RPREASON: |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1283 | ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1284 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1285 | if (unlikely(!ptr)) |
| 1286 | return; |
| 1287 | |
| 1288 | /* we have a full response and we know that we have either a CR |
| 1289 | * or an LF at <ptr>. |
| 1290 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1291 | //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] | 1292 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1293 | |
| 1294 | msg->sol = ptr; |
| 1295 | if (likely(*ptr == '\r')) |
| 1296 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1297 | goto http_msg_rpline_end; |
| 1298 | |
| 1299 | http_msg_rpline_end: |
| 1300 | case HTTP_MSG_RPLINE_END: |
| 1301 | /* msg->sol must point to the first of CR or LF. */ |
| 1302 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1303 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1304 | /* stop here */ |
| 1305 | |
| 1306 | /* |
| 1307 | * Second, states that are specific to the request only |
| 1308 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1309 | http_msg_rqbefore: |
| 1310 | case HTTP_MSG_RQBEFORE: |
| 1311 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1312 | if (likely(ptr == buf->data)) { |
| 1313 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1314 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1315 | } else { |
| 1316 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1317 | /* only skip empty leading lines, don't remove them */ |
| 1318 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1319 | msg->som = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1320 | #else |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1321 | /* Remove empty leading lines, as recommended by |
| 1322 | * RFC2616. This takes a lot of time because we |
| 1323 | * must move all the buffer backwards, but this |
| 1324 | * is rarely needed. The method above will be |
| 1325 | * cleaner when we'll be able to start sending |
| 1326 | * the request from any place in the buffer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1327 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1328 | buf->lr = ptr; |
| 1329 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1330 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1331 | msg->sol = buf->data; |
| 1332 | ptr = buf->data; |
| 1333 | end = buf->r; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1334 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1335 | } |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1336 | /* we will need this when keep-alive will be supported |
| 1337 | hdr_idx_init(idx); |
| 1338 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1339 | state = HTTP_MSG_RQMETH; |
| 1340 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1341 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1342 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1343 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1344 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1345 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1346 | if (unlikely(*ptr == '\n')) |
| 1347 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1348 | 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] | 1349 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1350 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1351 | http_msg_rqbefore_cr: |
| 1352 | case HTTP_MSG_RQBEFORE_CR: |
| 1353 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1354 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1355 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1356 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1357 | http_msg_rqmeth: |
| 1358 | case HTTP_MSG_RQMETH: |
| 1359 | case HTTP_MSG_RQMETH_SP: |
| 1360 | case HTTP_MSG_RQURI: |
| 1361 | case HTTP_MSG_RQURI_SP: |
| 1362 | case HTTP_MSG_RQVER: |
| 1363 | ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1364 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1365 | if (unlikely(!ptr)) |
| 1366 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1367 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1368 | /* we have a full request and we know that we have either a CR |
| 1369 | * or an LF at <ptr>. |
| 1370 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1371 | //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] | 1372 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1373 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1374 | msg->sol = ptr; |
| 1375 | if (likely(*ptr == '\r')) |
| 1376 | 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] | 1377 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1378 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1379 | http_msg_rqline_end: |
| 1380 | case HTTP_MSG_RQLINE_END: |
| 1381 | /* check for HTTP/0.9 request : no version information available. |
| 1382 | * msg->sol must point to the first of CR or LF. |
| 1383 | */ |
| 1384 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1385 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1386 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1387 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1388 | 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] | 1389 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1390 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1391 | /* |
| 1392 | * Common states below |
| 1393 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1394 | http_msg_hdr_first: |
| 1395 | case HTTP_MSG_HDR_FIRST: |
| 1396 | msg->sol = ptr; |
| 1397 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1398 | goto http_msg_hdr_name; |
| 1399 | } |
| 1400 | |
| 1401 | if (likely(*ptr == '\r')) |
| 1402 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1403 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1404 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1405 | http_msg_hdr_name: |
| 1406 | case HTTP_MSG_HDR_NAME: |
| 1407 | /* assumes msg->sol points to the first char */ |
| 1408 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1409 | 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] | 1410 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1411 | if (likely(*ptr == ':')) { |
| 1412 | msg->col = ptr - buf->data; |
| 1413 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| 1414 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1415 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1416 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1417 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1418 | http_msg_hdr_l1_sp: |
| 1419 | case HTTP_MSG_HDR_L1_SP: |
| 1420 | /* assumes msg->sol points to the first char and msg->col to the colon */ |
| 1421 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1422 | 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] | 1423 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1424 | /* header value can be basically anything except CR/LF */ |
| 1425 | msg->sov = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1426 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1427 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1428 | goto http_msg_hdr_val; |
| 1429 | } |
| 1430 | |
| 1431 | if (likely(*ptr == '\r')) |
| 1432 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1433 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1434 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1435 | http_msg_hdr_l1_lf: |
| 1436 | case HTTP_MSG_HDR_L1_LF: |
| 1437 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1438 | 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] | 1439 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1440 | http_msg_hdr_l1_lws: |
| 1441 | case HTTP_MSG_HDR_L1_LWS: |
| 1442 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1443 | /* replace HT,CR,LF with spaces */ |
| 1444 | for (; buf->data+msg->sov < ptr; msg->sov++) |
| 1445 | buf->data[msg->sov] = ' '; |
| 1446 | goto http_msg_hdr_l1_sp; |
| 1447 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1448 | /* we had a header consisting only in spaces ! */ |
| 1449 | msg->eol = buf->data + msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1450 | goto http_msg_complete_header; |
| 1451 | |
| 1452 | http_msg_hdr_val: |
| 1453 | case HTTP_MSG_HDR_VAL: |
| 1454 | /* assumes msg->sol points to the first char, msg->col to the |
| 1455 | * colon, and msg->sov points to the first character of the |
| 1456 | * value. |
| 1457 | */ |
| 1458 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1459 | 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] | 1460 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1461 | msg->eol = ptr; |
| 1462 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1463 | * real header end in case it ends with lots of LWS, but is this |
| 1464 | * really needed ? |
| 1465 | */ |
| 1466 | if (likely(*ptr == '\r')) |
| 1467 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1468 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1469 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1470 | http_msg_hdr_l2_lf: |
| 1471 | case HTTP_MSG_HDR_L2_LF: |
| 1472 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1473 | 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] | 1474 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1475 | http_msg_hdr_l2_lws: |
| 1476 | case HTTP_MSG_HDR_L2_LWS: |
| 1477 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1478 | /* LWS: replace HT,CR,LF with spaces */ |
| 1479 | for (; msg->eol < ptr; msg->eol++) |
| 1480 | *msg->eol = ' '; |
| 1481 | goto http_msg_hdr_val; |
| 1482 | } |
| 1483 | http_msg_complete_header: |
| 1484 | /* |
| 1485 | * It was a new header, so the last one is finished. |
| 1486 | * Assumes msg->sol points to the first char, msg->col to the |
| 1487 | * colon, msg->sov points to the first character of the value |
| 1488 | * and msg->eol to the first CR or LF so we know how the line |
| 1489 | * ends. We insert last header into the index. |
| 1490 | */ |
| 1491 | /* |
| 1492 | fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol); |
| 1493 | write(2, msg->sol, msg->eol-msg->sol); |
| 1494 | fprintf(stderr,"\n"); |
| 1495 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1496 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1497 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r', |
| 1498 | idx, idx->tail) < 0)) |
| 1499 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1500 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1501 | msg->sol = ptr; |
| 1502 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1503 | goto http_msg_hdr_name; |
| 1504 | } |
| 1505 | |
| 1506 | if (likely(*ptr == '\r')) |
| 1507 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1508 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1509 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1510 | http_msg_last_lf: |
| 1511 | case HTTP_MSG_LAST_LF: |
| 1512 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1513 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1514 | ptr++; |
| 1515 | buf->lr = ptr; |
| 1516 | msg->eoh = msg->sol - buf->data; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1517 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1518 | return; |
| 1519 | #ifdef DEBUG_FULL |
| 1520 | default: |
| 1521 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1522 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1523 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1524 | } |
| 1525 | http_msg_ood: |
| 1526 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1527 | msg->msg_state = state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1528 | buf->lr = ptr; |
| 1529 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1530 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1531 | http_msg_invalid: |
| 1532 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1533 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1534 | return; |
| 1535 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1536 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1537 | /* |
| 1538 | * manages the client FSM and its socket. BTW, it also tries to handle the |
| 1539 | * cookie. It returns 1 if a state has changed (and a resync may be needed), |
| 1540 | * 0 else. |
| 1541 | */ |
| 1542 | int process_cli(struct session *t) |
| 1543 | { |
| 1544 | int s = t->srv_state; |
| 1545 | int c = t->cli_state; |
| 1546 | struct buffer *req = t->req; |
| 1547 | struct buffer *rep = t->rep; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1548 | |
Willy Tarreau | 6468d92 | 2008-08-03 19:15:35 +0200 | [diff] [blame] | 1549 | 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] | 1550 | cli_stnames[c], srv_stnames[s], |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 1551 | 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] | 1552 | req->rex, rep->wex); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1553 | |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1554 | if (t->analysis & AN_REQ_INSPECT) { |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1555 | struct tcp_rule *rule; |
| 1556 | int partial; |
| 1557 | |
| 1558 | /* We will abort if we encounter a read error. In theory, |
| 1559 | * we should not abort if we get a close, it might be |
| 1560 | * valid, also very unlikely. FIXME: we'll abort for now, |
| 1561 | * this will be easier to change later. |
| 1562 | */ |
| 1563 | if (unlikely(req->flags & (BF_READ_ERROR | BF_READ_NULL))) { |
| 1564 | t->inspect_exp = TICK_ETERNITY; |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 1565 | buffer_shutr_done(req); |
| 1566 | buffer_shutw_done(rep); |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1567 | fd_delete(t->cli_fd); |
| 1568 | t->cli_state = CL_STCLOSE; |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1569 | t->analysis &= ~AN_REQ_ANY; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1570 | t->fe->failed_req++; |
| 1571 | if (!(t->flags & SN_ERR_MASK)) |
| 1572 | t->flags |= SN_ERR_CLICL; |
| 1573 | if (!(t->flags & SN_FINST_MASK)) |
| 1574 | t->flags |= SN_FINST_R; |
| 1575 | return 1; |
| 1576 | } |
| 1577 | |
| 1578 | /* Abort if client read timeout has expired */ |
| 1579 | else if (unlikely(tick_is_expired(req->rex, now_ms))) { |
| 1580 | t->inspect_exp = TICK_ETERNITY; |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 1581 | buffer_shutr_done(req); |
| 1582 | buffer_shutw_done(rep); |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1583 | fd_delete(t->cli_fd); |
| 1584 | t->cli_state = CL_STCLOSE; |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1585 | t->analysis &= ~AN_REQ_ANY; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1586 | t->fe->failed_req++; |
| 1587 | if (!(t->flags & SN_ERR_MASK)) |
| 1588 | t->flags |= SN_ERR_CLITO; |
| 1589 | if (!(t->flags & SN_FINST_MASK)) |
| 1590 | t->flags |= SN_FINST_R; |
| 1591 | return 1; |
| 1592 | } |
| 1593 | |
| 1594 | /* We don't know whether we have enough data, so must proceed |
| 1595 | * this way : |
| 1596 | * - iterate through all rules in their declaration order |
| 1597 | * - if one rule returns MISS, it means the inspect delay is |
| 1598 | * not over yet, then return immediately, otherwise consider |
| 1599 | * it as a non-match. |
| 1600 | * - if one rule returns OK, then return OK |
| 1601 | * - if one rule returns KO, then return KO |
| 1602 | */ |
| 1603 | |
| 1604 | if (tick_is_expired(t->inspect_exp, now_ms)) |
| 1605 | partial = 0; |
| 1606 | else |
| 1607 | partial = ACL_PARTIAL; |
| 1608 | |
| 1609 | list_for_each_entry(rule, &t->fe->tcp_req.inspect_rules, list) { |
| 1610 | int ret = ACL_PAT_PASS; |
| 1611 | |
| 1612 | if (rule->cond) { |
| 1613 | ret = acl_exec_cond(rule->cond, t->fe, t, NULL, ACL_DIR_REQ | partial); |
| 1614 | if (ret == ACL_PAT_MISS) { |
| 1615 | req->rex = tick_add_ifset(now_ms, t->fe->timeout.client); |
| 1616 | return 0; |
| 1617 | } |
| 1618 | ret = acl_pass(ret); |
| 1619 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 1620 | ret = !ret; |
| 1621 | } |
| 1622 | |
| 1623 | if (ret) { |
| 1624 | /* we have a matching rule. */ |
| 1625 | if (rule->action == TCP_ACT_REJECT) { |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 1626 | buffer_shutr_done(req); |
| 1627 | buffer_shutw_done(rep); |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1628 | fd_delete(t->cli_fd); |
| 1629 | t->cli_state = CL_STCLOSE; |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1630 | t->analysis &= ~AN_REQ_ANY; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1631 | t->fe->failed_req++; |
| 1632 | if (!(t->flags & SN_ERR_MASK)) |
| 1633 | t->flags |= SN_ERR_PRXCOND; |
| 1634 | if (!(t->flags & SN_FINST_MASK)) |
| 1635 | t->flags |= SN_FINST_R; |
| 1636 | t->inspect_exp = TICK_ETERNITY; |
| 1637 | return 1; |
| 1638 | } |
| 1639 | /* otherwise accept */ |
| 1640 | break; |
| 1641 | } |
| 1642 | } |
| 1643 | |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1644 | /* if we get there, it means we have no rule which matches, or |
| 1645 | * we have an explicit accept, so we apply the default accept. |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1646 | */ |
| 1647 | req->rex = tick_add_ifset(now_ms, t->fe->timeout.client); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1648 | t->analysis &= ~AN_REQ_INSPECT; |
| 1649 | if (t->analysis & AN_REQ_HTTP_HDR) |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1650 | t->txn.exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1651 | else |
Willy Tarreau | 718f0ef | 2008-08-10 16:21:32 +0200 | [diff] [blame] | 1652 | req->flags |= BF_MAY_CONNECT | BF_MAY_FORWARD; |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1653 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1654 | t->inspect_exp = TICK_ETERNITY; |
| 1655 | return 1; |
| 1656 | } |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1657 | |
| 1658 | if (t->analysis & AN_REQ_HTTP_HDR) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1659 | /* |
| 1660 | * Now parse the partial (or complete) lines. |
| 1661 | * We will check the request syntax, and also join multi-line |
| 1662 | * headers. An index of all the lines will be elaborated while |
| 1663 | * parsing. |
| 1664 | * |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1665 | * For the parsing, we use a 28 states FSM. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1666 | * |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1667 | * Here is the information we currently have : |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1668 | * req->data + req->som = beginning of request |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1669 | * req->data + req->eoh = end of processed headers / start of current one |
| 1670 | * req->data + req->eol = end of current header or line (LF or CRLF) |
| 1671 | * req->lr = first non-visited byte |
| 1672 | * req->r = end of data |
| 1673 | */ |
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 | int cur_idx; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1676 | struct http_txn *txn = &t->txn; |
| 1677 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1678 | struct proxy *cur_proxy; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1679 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1680 | if (likely(req->lr < req->r)) |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1681 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1682 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1683 | /* 1: we might have to print this header in debug mode */ |
| 1684 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1685 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1686 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1687 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1688 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1689 | sol = req->data + msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1690 | eol = sol + msg->sl.rq.l; |
| 1691 | debug_hdr("clireq", t, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 1692 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1693 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 1694 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1695 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1696 | while (cur_idx) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1697 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1698 | debug_hdr("clihdr", t, sol, eol); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1699 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 1700 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1701 | } |
| 1702 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1703 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1704 | |
| 1705 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1706 | * Now we quickly check if we have found a full valid request. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1707 | * If not so, we check the FD and buffer states before leaving. |
| 1708 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1709 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 1710 | * requests are checked first. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1711 | * |
| 1712 | */ |
| 1713 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1714 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1715 | /* |
| 1716 | * First, let's catch bad requests. |
| 1717 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1718 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1719 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1720 | |
| 1721 | /* 1: Since we are in header mode, if there's no space |
| 1722 | * left for headers, we won't be able to free more |
| 1723 | * later, so the session will never terminate. We |
| 1724 | * must terminate it now. |
| 1725 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1726 | if (unlikely(req->l >= req->rlim - req->data)) { |
| 1727 | /* FIXME: check if URI is set and return Status |
| 1728 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1729 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1730 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1731 | } |
| 1732 | |
| 1733 | /* 2: have we encountered a read error or a close ? */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1734 | else if (unlikely(req->flags & (BF_READ_ERROR | BF_READ_NULL))) { |
| 1735 | /* read error, or last read : give up. */ |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 1736 | buffer_shutr_done(req); |
| 1737 | buffer_shutw_done(rep); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1738 | fd_delete(t->cli_fd); |
| 1739 | t->cli_state = CL_STCLOSE; |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1740 | t->analysis &= ~AN_REQ_ANY; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 1741 | t->fe->failed_req++; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1742 | if (!(t->flags & SN_ERR_MASK)) |
| 1743 | t->flags |= SN_ERR_CLICL; |
| 1744 | if (!(t->flags & SN_FINST_MASK)) |
| 1745 | t->flags |= SN_FINST_R; |
| 1746 | return 1; |
| 1747 | } |
| 1748 | |
| 1749 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1750 | else if (unlikely(tick_is_expired(req->rex, now_ms) || |
| 1751 | tick_is_expired(txn->exp, now_ms))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1752 | /* read timeout : give up with an error message. */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1753 | txn->status = 408; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 1754 | client_retnclose(t, error_message(t, HTTP_ERR_408)); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1755 | t->analysis &= ~AN_REQ_ANY; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 1756 | t->fe->failed_req++; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1757 | if (!(t->flags & SN_ERR_MASK)) |
| 1758 | t->flags |= SN_ERR_CLITO; |
| 1759 | if (!(t->flags & SN_FINST_MASK)) |
| 1760 | t->flags |= SN_FINST_R; |
| 1761 | return 1; |
| 1762 | } |
| 1763 | |
| 1764 | /* 4: do we need to re-enable the read socket ? */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 1765 | else if (!tick_isset(req->rex)) { |
| 1766 | EV_FD_COND_S(t->cli_fd, DIR_RD); |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 1767 | /* fd in DIR_RD was disabled, perhaps because of a previous buffer |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1768 | * full. We cannot loop here since stream_sock_read will disable it only if |
| 1769 | * req->l == rlim-data |
| 1770 | */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 1771 | req->rex = tick_add_ifset(now_ms, t->fe->timeout.client); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1772 | } |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1773 | /* we don't need to resync if the state has not changed */ |
| 1774 | return !(t->analysis & AN_REQ_HTTP_HDR); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1775 | } |
| 1776 | |
| 1777 | |
| 1778 | /**************************************************************** |
| 1779 | * More interesting part now : we know that we have a complete * |
| 1780 | * request which at least looks like HTTP. We have an indicator * |
| 1781 | * of each header's length, so we can parse them quickly. * |
| 1782 | ****************************************************************/ |
| 1783 | |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1784 | t->analysis &= ~AN_REQ_HTTP_HDR; |
| 1785 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1786 | /* ensure we keep this pointer to the beginning of the message */ |
| 1787 | msg->sol = req->data + msg->som; |
| 1788 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1789 | /* |
| 1790 | * 1: identify the method |
| 1791 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1792 | 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] | 1793 | |
| 1794 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1795 | * 2: check if the URI matches the monitor_uri. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1796 | * We have to do this for every request which gets in, because |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1797 | * the monitor-uri is defined by the frontend. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1798 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1799 | if (unlikely((t->fe->monitor_uri_len != 0) && |
| 1800 | (t->fe->monitor_uri_len == msg->sl.rq.u_l) && |
| 1801 | !memcmp(&req->data[msg->sl.rq.u], |
| 1802 | t->fe->monitor_uri, |
| 1803 | t->fe->monitor_uri_len))) { |
| 1804 | /* |
| 1805 | * We have found the monitor URI |
| 1806 | */ |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1807 | struct acl_cond *cond; |
| 1808 | cur_proxy = t->fe; |
| 1809 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1810 | t->flags |= SN_MONITOR; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1811 | |
| 1812 | /* Check if we want to fail this monitor request or not */ |
| 1813 | list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) { |
| 1814 | 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] | 1815 | |
| 1816 | ret = acl_pass(ret); |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1817 | if (cond->pol == ACL_COND_UNLESS) |
| 1818 | ret = !ret; |
| 1819 | |
| 1820 | if (ret) { |
| 1821 | /* we fail this request, let's return 503 service unavail */ |
| 1822 | txn->status = 503; |
| 1823 | client_retnclose(t, error_message(t, HTTP_ERR_503)); |
| 1824 | goto return_prx_cond; |
| 1825 | } |
| 1826 | } |
| 1827 | |
| 1828 | /* nothing to fail, let's reply normaly */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1829 | txn->status = 200; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1830 | client_retnclose(t, &http_200_chunk); |
| 1831 | goto return_prx_cond; |
| 1832 | } |
| 1833 | |
| 1834 | /* |
| 1835 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 1836 | * Note: we cannot log anymore if the request has been |
| 1837 | * classified as invalid. |
| 1838 | */ |
| 1839 | if (unlikely(t->logs.logwait & LW_REQ)) { |
| 1840 | /* we have a complete HTTP request that we must log */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 1841 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1842 | int urilen = msg->sl.rq.l; |
| 1843 | |
| 1844 | if (urilen >= REQURI_LEN) |
| 1845 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1846 | memcpy(txn->uri, &req->data[msg->som], urilen); |
| 1847 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1848 | |
| 1849 | if (!(t->logs.logwait &= ~LW_REQ)) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1850 | http_sess_log(t); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1851 | } else { |
| 1852 | Alert("HTTP logging : out of memory.\n"); |
| 1853 | } |
| 1854 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1855 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1856 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1857 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 1858 | if (unlikely(msg->sl.rq.v_l == 0)) { |
| 1859 | int delta; |
| 1860 | char *cur_end; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1861 | msg->sol = req->data + msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1862 | cur_end = msg->sol + msg->sl.rq.l; |
| 1863 | delta = 0; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1864 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1865 | if (msg->sl.rq.u_l == 0) { |
| 1866 | /* if no URI was set, add "/" */ |
| 1867 | delta = buffer_replace2(req, cur_end, cur_end, " /", 2); |
| 1868 | cur_end += delta; |
| 1869 | msg->eoh += delta; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1870 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1871 | /* add HTTP version */ |
| 1872 | delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11); |
| 1873 | msg->eoh += delta; |
| 1874 | cur_end += delta; |
| 1875 | cur_end = (char *)http_parse_reqline(msg, req->data, |
| 1876 | HTTP_MSG_RQMETH, |
| 1877 | msg->sol, cur_end + 1, |
| 1878 | NULL, NULL); |
| 1879 | if (unlikely(!cur_end)) |
| 1880 | goto return_bad_req; |
| 1881 | |
| 1882 | /* we have a full HTTP/1.0 request now and we know that |
| 1883 | * we have either a CR or an LF at <ptr>. |
| 1884 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1885 | 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] | 1886 | } |
| 1887 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1888 | |
| 1889 | /* 5: we may need to capture headers */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1890 | if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap)) |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1891 | capture_headers(req->data + msg->som, &txn->hdr_idx, |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1892 | txn->req.cap, t->fe->req_cap); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1893 | |
| 1894 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1895 | * 6: we will have to evaluate the filters. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1896 | * As opposed to version 1.2, now they will be evaluated in the |
| 1897 | * filters order and not in the header order. This means that |
| 1898 | * each filter has to be validated among all headers. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1899 | * |
| 1900 | * We can now check whether we want to switch to another |
| 1901 | * backend, in which case we will re-check the backend's |
| 1902 | * filters and various options. In order to support 3-level |
| 1903 | * switching, here's how we should proceed : |
| 1904 | * |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1905 | * a) run be. |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1906 | * if (switch) then switch ->be to the new backend. |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1907 | * b) run be if (be != fe). |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1908 | * There cannot be any switch from there, so ->be cannot be |
| 1909 | * changed anymore. |
| 1910 | * |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1911 | * => filters always apply to ->be, then ->be may change. |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1912 | * |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1913 | * The response path will be able to apply either ->be, or |
| 1914 | * ->be then ->fe filters in order to match the reverse of |
| 1915 | * the forward sequence. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1916 | */ |
| 1917 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1918 | do { |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 1919 | struct acl_cond *cond; |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 1920 | struct redirect_rule *rule; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1921 | struct proxy *rule_set = t->be; |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1922 | cur_proxy = t->be; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1923 | |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 1924 | /* first check whether we have some ACLs set to redirect this request */ |
| 1925 | list_for_each_entry(rule, &cur_proxy->redirect_rules, list) { |
| 1926 | 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] | 1927 | |
| 1928 | ret = acl_pass(ret); |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 1929 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 1930 | ret = !ret; |
| 1931 | |
| 1932 | if (ret) { |
| 1933 | struct chunk rdr = { trash, 0 }; |
| 1934 | const char *msg_fmt; |
| 1935 | |
| 1936 | /* build redirect message */ |
| 1937 | switch(rule->code) { |
| 1938 | case 303: |
| 1939 | rdr.len = strlen(HTTP_303); |
| 1940 | msg_fmt = HTTP_303; |
| 1941 | break; |
| 1942 | case 301: |
| 1943 | rdr.len = strlen(HTTP_301); |
| 1944 | msg_fmt = HTTP_301; |
| 1945 | break; |
| 1946 | case 302: |
| 1947 | default: |
| 1948 | rdr.len = strlen(HTTP_302); |
| 1949 | msg_fmt = HTTP_302; |
| 1950 | break; |
| 1951 | } |
| 1952 | |
| 1953 | if (unlikely(rdr.len > sizeof(trash))) |
| 1954 | goto return_bad_req; |
| 1955 | memcpy(rdr.str, msg_fmt, rdr.len); |
| 1956 | |
| 1957 | switch(rule->type) { |
| 1958 | case REDIRECT_TYPE_PREFIX: { |
| 1959 | const char *path; |
| 1960 | int pathlen; |
| 1961 | |
| 1962 | path = http_get_path(txn); |
| 1963 | /* build message using path */ |
| 1964 | if (path) { |
| 1965 | pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path; |
| 1966 | } else { |
| 1967 | path = "/"; |
| 1968 | pathlen = 1; |
| 1969 | } |
| 1970 | |
| 1971 | if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4) |
| 1972 | goto return_bad_req; |
| 1973 | |
| 1974 | /* add prefix */ |
| 1975 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 1976 | rdr.len += rule->rdr_len; |
| 1977 | |
| 1978 | /* add path */ |
| 1979 | memcpy(rdr.str + rdr.len, path, pathlen); |
| 1980 | rdr.len += pathlen; |
| 1981 | break; |
| 1982 | } |
| 1983 | case REDIRECT_TYPE_LOCATION: |
| 1984 | default: |
| 1985 | if (rdr.len + rule->rdr_len > sizeof(trash) - 4) |
| 1986 | goto return_bad_req; |
| 1987 | |
| 1988 | /* add location */ |
| 1989 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 1990 | rdr.len += rule->rdr_len; |
| 1991 | break; |
| 1992 | } |
| 1993 | |
| 1994 | /* add end of headers */ |
| 1995 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 1996 | rdr.len += 4; |
| 1997 | |
| 1998 | txn->status = rule->code; |
| 1999 | /* let's log the request time */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2000 | t->logs.tv_request = now; |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2001 | client_retnclose(t, &rdr); |
| 2002 | goto return_prx_cond; |
| 2003 | } |
| 2004 | } |
| 2005 | |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2006 | /* first check whether we have some ACLs set to block this request */ |
| 2007 | list_for_each_entry(cond, &cur_proxy->block_cond, list) { |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 2008 | 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] | 2009 | |
| 2010 | ret = acl_pass(ret); |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2011 | if (cond->pol == ACL_COND_UNLESS) |
| 2012 | ret = !ret; |
| 2013 | |
| 2014 | if (ret) { |
| 2015 | txn->status = 403; |
| 2016 | /* let's log the request time */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2017 | t->logs.tv_request = now; |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2018 | client_retnclose(t, error_message(t, HTTP_ERR_403)); |
| 2019 | goto return_prx_cond; |
| 2020 | } |
| 2021 | } |
| 2022 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2023 | /* try headers filters */ |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2024 | if (rule_set->req_exp != NULL) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2025 | if (apply_filters_to_request(t, req, rule_set->req_exp) < 0) |
| 2026 | goto return_bad_req; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2027 | } |
| 2028 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2029 | if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) { |
| 2030 | /* to ensure correct connection accounting on |
| 2031 | * the backend, we count the connection for the |
| 2032 | * one managing the queue. |
| 2033 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2034 | t->be->beconn++; |
| 2035 | if (t->be->beconn > t->be->beconn_max) |
| 2036 | t->be->beconn_max = t->be->beconn; |
| 2037 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2038 | t->flags |= SN_BE_ASSIGNED; |
| 2039 | } |
| 2040 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2041 | /* has the request been denied ? */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2042 | if (txn->flags & TX_CLDENY) { |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2043 | /* no need to go further */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2044 | txn->status = 403; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2045 | /* let's log the request time */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2046 | t->logs.tv_request = now; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2047 | client_retnclose(t, error_message(t, HTTP_ERR_403)); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2048 | goto return_prx_cond; |
| 2049 | } |
| 2050 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2051 | /* We might have to check for "Connection:" */ |
Krzysztof Oledzki | 336d475 | 2007-12-25 02:40:22 +0100 | [diff] [blame] | 2052 | 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] | 2053 | !(t->flags & SN_CONN_CLOSED)) { |
| 2054 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2055 | int cur_idx, old_idx, delta, val; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2056 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2057 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2058 | 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] | 2059 | old_idx = 0; |
| 2060 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2061 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 2062 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2063 | cur_ptr = cur_next; |
| 2064 | cur_end = cur_ptr + cur_hdr->len; |
| 2065 | cur_next = cur_end + cur_hdr->cr + 1; |
| 2066 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2067 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 2068 | if (val) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2069 | /* 3 possibilities : |
| 2070 | * - we have already set Connection: close, |
| 2071 | * so we remove this line. |
| 2072 | * - we have not yet set Connection: close, |
| 2073 | * but this line indicates close. We leave |
| 2074 | * it untouched and set the flag. |
| 2075 | * - we have not yet set Connection: close, |
| 2076 | * and this line indicates non-close. We |
| 2077 | * replace it. |
| 2078 | */ |
| 2079 | if (t->flags & SN_CONN_CLOSED) { |
| 2080 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
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 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2083 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 2084 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2085 | cur_hdr->len = 0; |
| 2086 | } else { |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2087 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 2088 | delta = buffer_replace2(req, cur_ptr + val, cur_end, |
| 2089 | "close", 5); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2090 | cur_next += delta; |
| 2091 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2092 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2093 | } |
| 2094 | t->flags |= SN_CONN_CLOSED; |
| 2095 | } |
| 2096 | } |
| 2097 | old_idx = cur_idx; |
| 2098 | } |
Willy Tarreau | f2f0ee8 | 2007-03-30 12:02:43 +0200 | [diff] [blame] | 2099 | } |
| 2100 | /* add request headers from the rule sets in the same order */ |
| 2101 | for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) { |
| 2102 | if (unlikely(http_header_add_tail(req, |
| 2103 | &txn->req, |
| 2104 | &txn->hdr_idx, |
| 2105 | rule_set->req_add[cur_idx])) < 0) |
| 2106 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2107 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2108 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2109 | /* check if stats URI was requested, and if an auth is needed */ |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 2110 | if (rule_set->uri_auth != NULL && |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2111 | (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) { |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2112 | /* we have to check the URI and auth for this request */ |
| 2113 | if (stats_check_uri_auth(t, rule_set)) |
| 2114 | return 1; |
| 2115 | } |
| 2116 | |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2117 | /* now check whether we have some switching rules for this request */ |
| 2118 | if (!(t->flags & SN_BE_ASSIGNED)) { |
| 2119 | struct switching_rule *rule; |
| 2120 | |
| 2121 | list_for_each_entry(rule, &cur_proxy->switching_rules, list) { |
| 2122 | int ret; |
| 2123 | |
| 2124 | 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] | 2125 | |
| 2126 | ret = acl_pass(ret); |
Willy Tarreau | a8cfa34 | 2008-07-09 11:23:31 +0200 | [diff] [blame] | 2127 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2128 | ret = !ret; |
| 2129 | |
| 2130 | if (ret) { |
| 2131 | t->be = rule->be.backend; |
| 2132 | t->be->beconn++; |
| 2133 | if (t->be->beconn > t->be->beconn_max) |
| 2134 | t->be->beconn_max = t->be->beconn; |
| 2135 | t->be->cum_beconn++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2136 | |
| 2137 | /* assign new parameters to the session from the new backend */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2138 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 2139 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2140 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2141 | t->flags |= SN_BE_ASSIGNED; |
| 2142 | break; |
| 2143 | } |
| 2144 | } |
| 2145 | } |
| 2146 | |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 2147 | if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) { |
| 2148 | /* No backend was set, but there was a default |
| 2149 | * backend set in the frontend, so we use it and |
| 2150 | * loop again. |
| 2151 | */ |
| 2152 | t->be = cur_proxy->defbe.be; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2153 | t->be->beconn++; |
| 2154 | if (t->be->beconn > t->be->beconn_max) |
| 2155 | t->be->beconn_max = t->be->beconn; |
| 2156 | t->be->cum_beconn++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2157 | |
| 2158 | /* assign new parameters to the session from the new backend */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2159 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 2160 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2161 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 2162 | t->flags |= SN_BE_ASSIGNED; |
| 2163 | } |
| 2164 | } 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] | 2165 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2166 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2167 | if (!(t->flags & SN_BE_ASSIGNED)) { |
| 2168 | /* To ensure correct connection accounting on |
| 2169 | * the backend, we count the connection for the |
| 2170 | * one managing the queue. |
| 2171 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2172 | t->be->beconn++; |
| 2173 | if (t->be->beconn > t->be->beconn_max) |
| 2174 | t->be->beconn_max = t->be->beconn; |
| 2175 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2176 | t->flags |= SN_BE_ASSIGNED; |
| 2177 | } |
| 2178 | |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2179 | /* |
| 2180 | * Right now, we know that we have processed the entire headers |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2181 | * and that unwanted requests have been filtered out. We can do |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2182 | * whatever we want with the remaining request. Also, now we |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2183 | * may have separate values for ->fe, ->be. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2184 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2185 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 2186 | /* |
| 2187 | * If HTTP PROXY is set we simply get remote server address |
| 2188 | * parsing incoming request. |
| 2189 | */ |
| 2190 | if ((t->be->options & PR_O_HTTP_PROXY) && !(t->flags & SN_ADDR_SET)) { |
| 2191 | url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &t->srv_addr); |
| 2192 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2193 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2194 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2195 | * 7: the appsession cookie was looked up very early in 1.2, |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2196 | * so let's do the same now. |
| 2197 | */ |
| 2198 | |
| 2199 | /* It needs to look into the URI */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2200 | if (t->be->appsession_name) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2201 | 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] | 2202 | } |
| 2203 | |
| 2204 | |
| 2205 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2206 | * 8: Now we can work with the cookies. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2207 | * Note that doing so might move headers in the request, but |
| 2208 | * the fields will stay coherent and the URI will not move. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2209 | * This should only be performed in the backend. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2210 | */ |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 2211 | if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name) |
| 2212 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2213 | manage_client_side_cookies(t, req); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2214 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2215 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2216 | /* |
Willy Tarreau | bb046ac | 2007-03-03 19:17:03 +0100 | [diff] [blame] | 2217 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 2218 | * asks for it. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2219 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2220 | if ((t->fe->options | t->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2221 | if (t->cli_addr.ss_family == AF_INET) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2222 | /* Add an X-Forwarded-For header unless the source IP is |
| 2223 | * in the 'except' network range. |
| 2224 | */ |
| 2225 | if ((!t->fe->except_mask.s_addr || |
| 2226 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr) |
| 2227 | != t->fe->except_net.s_addr) && |
| 2228 | (!t->be->except_mask.s_addr || |
| 2229 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr) |
| 2230 | != t->be->except_net.s_addr)) { |
| 2231 | int len; |
| 2232 | unsigned char *pn; |
| 2233 | pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr; |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2234 | |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2235 | /* Note: we rely on the backend to get the header name to be used for |
| 2236 | * x-forwarded-for, because the header is really meant for the backends. |
| 2237 | * However, if the backend did not specify any option, we have to rely |
| 2238 | * on the frontend's header name. |
| 2239 | */ |
| 2240 | if (t->be->fwdfor_hdr_len) { |
| 2241 | len = t->be->fwdfor_hdr_len; |
| 2242 | memcpy(trash, t->be->fwdfor_hdr_name, len); |
| 2243 | } else { |
| 2244 | len = t->fe->fwdfor_hdr_len; |
| 2245 | memcpy(trash, t->fe->fwdfor_hdr_name, len); |
| 2246 | } |
| 2247 | 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] | 2248 | |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2249 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2250 | &txn->hdr_idx, trash, len)) < 0) |
| 2251 | goto return_bad_req; |
| 2252 | } |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2253 | } |
| 2254 | else if (t->cli_addr.ss_family == AF_INET6) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2255 | /* FIXME: for the sake of completeness, we should also support |
| 2256 | * 'except' here, although it is mostly useless in this case. |
| 2257 | */ |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2258 | int len; |
| 2259 | char pn[INET6_ADDRSTRLEN]; |
| 2260 | inet_ntop(AF_INET6, |
| 2261 | (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr, |
| 2262 | pn, sizeof(pn)); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2263 | |
| 2264 | /* Note: we rely on the backend to get the header name to be used for |
| 2265 | * x-forwarded-for, because the header is really meant for the backends. |
| 2266 | * However, if the backend did not specify any option, we have to rely |
| 2267 | * on the frontend's header name. |
| 2268 | */ |
| 2269 | if (t->be->fwdfor_hdr_len) { |
| 2270 | len = t->be->fwdfor_hdr_len; |
| 2271 | memcpy(trash, t->be->fwdfor_hdr_name, len); |
| 2272 | } else { |
| 2273 | len = t->fe->fwdfor_hdr_len; |
| 2274 | memcpy(trash, t->fe->fwdfor_hdr_name, len); |
| 2275 | } |
| 2276 | len += sprintf(trash + len, ": %s", pn); |
| 2277 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2278 | if (unlikely(http_header_add_tail2(req, &txn->req, |
| 2279 | &txn->hdr_idx, trash, len)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2280 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2281 | } |
| 2282 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2283 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2284 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2285 | * 10: add "Connection: close" if needed and not yet set. |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2286 | * 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] | 2287 | */ |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2288 | if (!(t->flags & SN_CONN_CLOSED) && |
Krzysztof Oledzki | 336d475 | 2007-12-25 02:40:22 +0100 | [diff] [blame] | 2289 | ((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] | 2290 | if ((unlikely(msg->sl.rq.v_l != 8) || |
| 2291 | unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) && |
| 2292 | unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx, |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2293 | "Connection: close", 17)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2294 | goto return_bad_req; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2295 | t->flags |= SN_CONN_CLOSED; |
Willy Tarreau | e15d913 | 2006-12-14 22:26:42 +0100 | [diff] [blame] | 2296 | } |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2297 | /* Before we switch to data, was assignment set in manage_client_side_cookie? |
| 2298 | * If not assigned, perhaps we are balancing on url_param, but this is a |
| 2299 | * POST; and the parameters are in the body, maybe scan there to find our server. |
| 2300 | * (unless headers overflowed the buffer?) |
| 2301 | */ |
| 2302 | if (!(t->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 2303 | t->txn.meth == HTTP_METH_POST && t->be->url_param_name != NULL && |
Willy Tarreau | fb0528b | 2008-08-11 00:21:56 +0200 | [diff] [blame] | 2304 | t->be->url_param_post_limit != 0 && req->l < BUFSIZE && |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2305 | memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) { |
| 2306 | /* are there enough bytes here? total == l || r || rlim ? |
| 2307 | * len is unsigned, but eoh is int, |
| 2308 | * how many bytes of body have we received? |
| 2309 | * eoh is the first empty line of the header |
| 2310 | */ |
| 2311 | /* 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] | 2312 | 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] | 2313 | |
| 2314 | /* If we have HTTP/1.1 and Expect: 100-continue, then abort. |
| 2315 | * We can't assume responsibility for the server's decision, |
| 2316 | * on this URI and header set. See rfc2616: 14.20, 8.2.3, |
| 2317 | * We also can't change our mind later, about which server to choose, so round robin. |
| 2318 | */ |
| 2319 | if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) { |
| 2320 | struct hdr_ctx ctx; |
| 2321 | ctx.idx = 0; |
| 2322 | /* Expect is allowed in 1.1, look for it */ |
| 2323 | http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx); |
| 2324 | if (ctx.idx != 0 && |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 2325 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2326 | /* We can't reliablly stall and wait for data, because of |
| 2327 | * .NET clients that don't conform to rfc2616; so, no need for |
| 2328 | * the next block to check length expectations. |
| 2329 | * We could send 100 status back to the client, but then we need to |
| 2330 | * re-write headers, and send the message. And this isn't the right |
| 2331 | * place for that action. |
| 2332 | * TODO: support Expect elsewhere and delete this block. |
| 2333 | */ |
| 2334 | goto end_check_maybe_wait_for_body; |
| 2335 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 2336 | |
| 2337 | if (likely(len > t->be->url_param_post_limit)) { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2338 | /* nothing to do, we got enough */ |
| 2339 | } else { |
| 2340 | /* limit implies we are supposed to need this many bytes |
| 2341 | * to find the parameter. Let's see how many bytes we can wait for. |
| 2342 | */ |
| 2343 | long long hint = len; |
| 2344 | struct hdr_ctx ctx; |
| 2345 | ctx.idx = 0; |
| 2346 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 2347 | if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 2348 | t->analysis |= AN_REQ_HTTP_BODY; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 2349 | else { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2350 | ctx.idx = 0; |
| 2351 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx); |
| 2352 | /* now if we have a length, we'll take the hint */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 2353 | if (ctx.idx) { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2354 | /* We have Content-Length */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 2355 | if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint)) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2356 | hint = 0; /* parse failure, untrusted client */ |
| 2357 | else { |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 2358 | if (hint > 0) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2359 | msg->hdr_content_len = hint; |
| 2360 | else |
| 2361 | hint = 0; /* bad client, sent negative length */ |
| 2362 | } |
| 2363 | } |
| 2364 | /* but limited to what we care about, maybe we don't expect any entity data (hint == 0) */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 2365 | if (t->be->url_param_post_limit < hint) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2366 | hint = t->be->url_param_post_limit; |
| 2367 | /* now do we really need to buffer more data? */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 2368 | if (len < hint) |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 2369 | t->analysis |= AN_REQ_HTTP_BODY; |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2370 | /* else... There are no body bytes to wait for */ |
| 2371 | } |
| 2372 | } |
| 2373 | } |
| 2374 | end_check_maybe_wait_for_body: |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2375 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2376 | /************************************************************* |
| 2377 | * OK, that's finished for the headers. We have done what we * |
| 2378 | * could. Let's switch to the DATA state. * |
| 2379 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2380 | |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 2381 | if (!(t->analysis & AN_REQ_ANY)) |
| 2382 | req->flags |= BF_MAY_CONNECT | BF_MAY_FORWARD; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2383 | |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 2384 | req->rlim = req->data + BUFSIZE; /* no more rewrite needed */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2385 | t->logs.tv_request = now; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2386 | |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 2387 | /* This is a bit tricky. We don't want the client timeout to strike |
| 2388 | * while intially waiting for the server to respond. So we rely |
| 2389 | * entirely on the server timeout to ensure that we cannot wait |
| 2390 | * indefinitely. |
| 2391 | */ |
| 2392 | if (t->fe->timeout.client || req->l >= req->rlim - req->data) |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2393 | req->rex = TICK_ETERNITY; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 2394 | else if (t->analysis || !t->be->timeout.server || rep->flags & BF_MAY_FORWARD) |
| 2395 | req->rex = tick_add(now_ms, t->fe->timeout.client); |
| 2396 | else |
| 2397 | req->rex = TICK_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2398 | |
Willy Tarreau | 1fa3126 | 2007-12-03 00:36:16 +0100 | [diff] [blame] | 2399 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 2400 | * which may be the same as the connect timeout if unspecified. |
| 2401 | * If unset, then set it to zero because we really want it to |
| 2402 | * eventually expire. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2403 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2404 | if (txn->flags & TX_CLTARPIT) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2405 | t->req->l = 0; |
| 2406 | /* flush the request so that we can drop the connection early |
| 2407 | * if the client closes first. |
| 2408 | */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2409 | req->cex = tick_add_ifset(now_ms, t->be->timeout.tarpit); |
| 2410 | if (!req->cex) |
| 2411 | req->cex = now_ms; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2412 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2413 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2414 | /* OK let's go on with the BODY now */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 2415 | goto end_of_headers; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2416 | |
| 2417 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2418 | txn->req.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2419 | txn->status = 400; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 2420 | t->analysis &= ~AN_REQ_ANY; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2421 | client_retnclose(t, error_message(t, HTTP_ERR_400)); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 2422 | t->fe->failed_req++; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2423 | return_prx_cond: |
| 2424 | if (!(t->flags & SN_ERR_MASK)) |
| 2425 | t->flags |= SN_ERR_PRXCOND; |
| 2426 | if (!(t->flags & SN_FINST_MASK)) |
| 2427 | t->flags |= SN_FINST_R; |
| 2428 | return 1; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 2429 | end_of_headers: |
| 2430 | ; // to make gcc happy |
| 2431 | } |
| 2432 | |
| 2433 | if (t->analysis & AN_REQ_HTTP_BODY) { |
| 2434 | /* We have to parse the HTTP request body to find any required data. |
| 2435 | * "balance url_param check_post" should have been the only way to get |
| 2436 | * into this. We were brought here after HTTP header analysis, so all |
| 2437 | * related structures are ready. |
| 2438 | */ |
| 2439 | struct http_msg *msg = &t->txn.req; |
| 2440 | unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1; |
| 2441 | long long limit = t->be->url_param_post_limit; |
| 2442 | struct hdr_ctx ctx; |
| 2443 | |
| 2444 | ctx.idx = 0; |
| 2445 | |
| 2446 | /* now if we have a length, we'll take the hint */ |
| 2447 | http_find_header2("Transfer-Encoding", 17, msg->sol, &t->txn.hdr_idx, &ctx); |
| 2448 | if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) { |
| 2449 | unsigned int chunk = 0; |
| 2450 | while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) { |
| 2451 | char c = msg->sol[body]; |
| 2452 | if (ishex(c)) { |
| 2453 | unsigned int hex = toupper(c) - '0'; |
| 2454 | if (hex > 9) |
| 2455 | hex -= 'A' - '9' - 1; |
| 2456 | chunk = (chunk << 4) | hex; |
| 2457 | } else |
| 2458 | break; |
| 2459 | body++; |
| 2460 | } |
| 2461 | if (body + 2 >= req->l) /* we want CRLF too */ |
| 2462 | goto http_body_incomplete; /* end of buffer? data missing! */ |
| 2463 | |
| 2464 | if (memcmp(msg->sol+body, "\r\n", 2) != 0) |
| 2465 | goto http_body_incomplete; /* chunked encoding len ends with CRLF, and we don't have it yet */ |
| 2466 | |
| 2467 | body += 2; // skip CRLF |
| 2468 | |
| 2469 | /* if we support more then one chunk here, we have to do it again when assigning server |
| 2470 | * 1. how much entity data do we have? new var |
| 2471 | * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here |
| 2472 | * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found) |
| 2473 | */ |
| 2474 | |
| 2475 | if (chunk < limit) |
| 2476 | limit = chunk; /* only reading one chunk */ |
| 2477 | } else { |
| 2478 | if (msg->hdr_content_len < limit) |
| 2479 | limit = msg->hdr_content_len; |
| 2480 | } |
| 2481 | |
| 2482 | if (req->l - body >= limit) { |
| 2483 | /* we have enough bytes ! */ |
| 2484 | t->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 2485 | t->analysis &= ~AN_REQ_HTTP_BODY; |
| 2486 | req->flags |= BF_MAY_CONNECT | BF_MAY_FORWARD; |
| 2487 | |
| 2488 | /* This is a bit tricky. We don't want the client timeout to strike |
| 2489 | * while intially waiting for the server to respond. So we rely |
| 2490 | * entirely on the server timeout to ensure that we cannot wait |
| 2491 | * indefinitely. |
| 2492 | */ |
| 2493 | if (t->fe->timeout.client || req->l >= req->rlim - req->data) |
| 2494 | req->rex = TICK_ETERNITY; |
| 2495 | else if (t->analysis || !t->be->timeout.server || rep->flags & BF_MAY_FORWARD) |
| 2496 | req->rex = tick_add(now_ms, t->fe->timeout.client); |
| 2497 | else |
| 2498 | req->rex = TICK_ETERNITY; |
| 2499 | return 1; |
| 2500 | } |
| 2501 | |
| 2502 | http_body_incomplete: |
| 2503 | if (req->l >= req->rlim - req->data || |
| 2504 | req->flags & (BF_READ_ERROR | BF_READ_NULL) || |
| 2505 | tick_is_expired(req->rex, now_ms)) { |
| 2506 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 2507 | t->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 2508 | t->analysis &= ~AN_REQ_HTTP_BODY; |
| 2509 | req->flags |= BF_MAY_CONNECT | BF_MAY_FORWARD; |
| 2510 | |
| 2511 | /* This is a bit tricky. We don't want the client timeout to strike |
| 2512 | * while intially waiting for the server to respond. So we rely |
| 2513 | * entirely on the server timeout to ensure that we cannot wait |
| 2514 | * indefinitely. |
| 2515 | */ |
| 2516 | if (t->fe->timeout.client || req->l >= req->rlim - req->data) |
| 2517 | req->rex = TICK_ETERNITY; |
| 2518 | else if (t->analysis || !t->be->timeout.server || rep->flags & BF_MAY_FORWARD) |
| 2519 | req->rex = tick_add(now_ms, t->fe->timeout.client); |
| 2520 | else |
| 2521 | req->rex = TICK_ETERNITY; |
| 2522 | return 1; |
| 2523 | } |
| 2524 | |
| 2525 | if (!tick_isset(req->rex)) { |
| 2526 | EV_FD_COND_S(t->cli_fd, DIR_RD); |
| 2527 | req->rex = tick_add_ifset(now_ms, t->fe->timeout.client); |
| 2528 | } |
| 2529 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2530 | } |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 2531 | |
| 2532 | if (c == CL_STDATA) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2533 | /* FIXME: this error handling is partly buggy because we always report |
| 2534 | * a 'DATA' phase while we don't know if the server was in IDLE, CONN |
| 2535 | * or HEADER phase. BTW, it's not logical to expire the client while |
| 2536 | * we're waiting for the server to connect. |
| 2537 | */ |
| 2538 | /* read or write error */ |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2539 | if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) { |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 2540 | buffer_shutr_done(req); |
| 2541 | buffer_shutw_done(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2542 | fd_delete(t->cli_fd); |
| 2543 | t->cli_state = CL_STCLOSE; |
| 2544 | if (!(t->flags & SN_ERR_MASK)) |
| 2545 | t->flags |= SN_ERR_CLICL; |
| 2546 | if (!(t->flags & SN_FINST_MASK)) { |
| 2547 | if (t->pend_pos) |
| 2548 | t->flags |= SN_FINST_Q; |
| 2549 | else if (s == SV_STCONN) |
| 2550 | t->flags |= SN_FINST_C; |
| 2551 | else |
| 2552 | t->flags |= SN_FINST_D; |
| 2553 | } |
| 2554 | return 1; |
| 2555 | } |
| 2556 | /* last read, or end of server write */ |
Willy Tarreau | 718f0ef | 2008-08-10 16:21:32 +0200 | [diff] [blame] | 2557 | else if (req->flags & (BF_READ_NULL | BF_SHUTW_STATUS)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2558 | EV_FD_CLR(t->cli_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2559 | buffer_shutr(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2560 | t->cli_state = CL_STSHUTR; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 2561 | /* we must allow immediate connection if not already done */ |
| 2562 | if (!req->flags & (BF_MAY_CONNECT | BF_MAY_FORWARD)) |
| 2563 | req->flags |= BF_MAY_CONNECT | BF_MAY_FORWARD; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2564 | return 1; |
| 2565 | } |
| 2566 | /* last server read and buffer empty */ |
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 | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2568 | EV_FD_CLR(t->cli_fd, DIR_WR); |
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 | shutdown(t->cli_fd, SHUT_WR); |
| 2571 | /* We must ensure that the read part is still alive when switching |
| 2572 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2573 | EV_FD_SET(t->cli_fd, DIR_RD); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2574 | req->rex = tick_add_ifset(now_ms, t->fe->timeout.client); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2575 | t->cli_state = CL_STSHUTW; |
| 2576 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 2577 | return 1; |
| 2578 | } |
| 2579 | /* read timeout */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2580 | else if (tick_is_expired(req->rex, now_ms)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2581 | EV_FD_CLR(t->cli_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2582 | buffer_shutr(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2583 | t->cli_state = CL_STSHUTR; |
| 2584 | if (!(t->flags & SN_ERR_MASK)) |
| 2585 | t->flags |= SN_ERR_CLITO; |
| 2586 | if (!(t->flags & SN_FINST_MASK)) { |
| 2587 | if (t->pend_pos) |
| 2588 | t->flags |= SN_FINST_Q; |
| 2589 | else if (s == SV_STCONN) |
| 2590 | t->flags |= SN_FINST_C; |
| 2591 | else |
| 2592 | t->flags |= SN_FINST_D; |
| 2593 | } |
| 2594 | return 1; |
| 2595 | } |
| 2596 | /* write timeout */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2597 | else if (tick_is_expired(rep->wex, now_ms)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2598 | EV_FD_CLR(t->cli_fd, DIR_WR); |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 2599 | buffer_shutw_done(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2600 | shutdown(t->cli_fd, SHUT_WR); |
| 2601 | /* We must ensure that the read part is still alive when switching |
| 2602 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2603 | EV_FD_SET(t->cli_fd, DIR_RD); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2604 | req->rex = tick_add_ifset(now_ms, t->fe->timeout.client); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2605 | |
| 2606 | t->cli_state = CL_STSHUTW; |
| 2607 | if (!(t->flags & SN_ERR_MASK)) |
| 2608 | t->flags |= SN_ERR_CLITO; |
| 2609 | if (!(t->flags & SN_FINST_MASK)) { |
| 2610 | if (t->pend_pos) |
| 2611 | t->flags |= SN_FINST_Q; |
| 2612 | else if (s == SV_STCONN) |
| 2613 | t->flags |= SN_FINST_C; |
| 2614 | else |
| 2615 | t->flags |= SN_FINST_D; |
| 2616 | } |
| 2617 | return 1; |
| 2618 | } |
| 2619 | |
| 2620 | if (req->l >= req->rlim - req->data) { |
| 2621 | /* no room to read more data */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2622 | if (EV_FD_COND_C(t->cli_fd, DIR_RD)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2623 | /* stop reading until we get some space */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2624 | req->rex = TICK_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2625 | } |
| 2626 | } else { |
| 2627 | /* there's still some space in the buffer */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 2628 | if (!tick_isset(req->rex)) { |
| 2629 | EV_FD_COND_S(t->cli_fd, DIR_RD); |
| 2630 | /* This is a bit tricky. We don't want the client timeout to strike |
| 2631 | * while intially waiting for the server to respond. So we rely |
| 2632 | * entirely on the server timeout to ensure that we cannot wait |
| 2633 | * indefinitely. |
| 2634 | */ |
| 2635 | if (t->fe->timeout.client) |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2636 | req->rex = TICK_ETERNITY; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 2637 | else if (t->analysis || !t->be->timeout.server || rep->flags & BF_MAY_FORWARD) |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2638 | req->rex = tick_add(now_ms, t->fe->timeout.client); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 2639 | else |
| 2640 | req->rex = TICK_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2641 | } |
| 2642 | } |
| 2643 | |
Willy Tarreau | 718f0ef | 2008-08-10 16:21:32 +0200 | [diff] [blame] | 2644 | /* we don't enable client write if the buffer is empty, nor if the server has to analyze it */ |
| 2645 | if ((rep->l == 0) || !(rep->flags & BF_MAY_FORWARD)) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2646 | if (EV_FD_COND_C(t->cli_fd, DIR_WR)) { |
| 2647 | /* stop writing */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2648 | rep->wex = TICK_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2649 | } |
| 2650 | } else { |
| 2651 | /* buffer not empty */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 2652 | if (!tick_isset(rep->wex)) { |
| 2653 | EV_FD_COND_S(t->cli_fd, DIR_WR); |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2654 | /* restart writing */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2655 | rep->wex = tick_add_ifset(now_ms, t->fe->timeout.client); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 2656 | if (tick_isset(rep->wex)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2657 | /* FIXME: to prevent the client from expiring read timeouts during writes, |
| 2658 | * we refresh it. */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2659 | req->rex = rep->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2660 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2661 | } |
| 2662 | } |
| 2663 | return 0; /* other cases change nothing */ |
| 2664 | } |
| 2665 | else if (c == CL_STSHUTR) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2666 | if (rep->flags & BF_WRITE_ERROR) { |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 2667 | buffer_shutw_done(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2668 | fd_delete(t->cli_fd); |
| 2669 | t->cli_state = CL_STCLOSE; |
| 2670 | if (!(t->flags & SN_ERR_MASK)) |
| 2671 | t->flags |= SN_ERR_CLICL; |
| 2672 | if (!(t->flags & SN_FINST_MASK)) { |
| 2673 | if (t->pend_pos) |
| 2674 | t->flags |= SN_FINST_Q; |
| 2675 | else if (s == SV_STCONN) |
| 2676 | t->flags |= SN_FINST_C; |
| 2677 | else |
| 2678 | t->flags |= SN_FINST_D; |
| 2679 | } |
| 2680 | return 1; |
| 2681 | } |
Willy Tarreau | 718f0ef | 2008-08-10 16:21:32 +0200 | [diff] [blame] | 2682 | else if ((rep->flags & BF_SHUTR_STATUS) && (rep->l == 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2683 | && !(t->flags & SN_SELF_GEN)) { |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 2684 | buffer_shutw_done(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2685 | fd_delete(t->cli_fd); |
| 2686 | t->cli_state = CL_STCLOSE; |
| 2687 | return 1; |
| 2688 | } |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2689 | else if (tick_is_expired(rep->wex, now_ms)) { |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 2690 | buffer_shutw_done(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2691 | fd_delete(t->cli_fd); |
| 2692 | t->cli_state = CL_STCLOSE; |
| 2693 | if (!(t->flags & SN_ERR_MASK)) |
| 2694 | t->flags |= SN_ERR_CLITO; |
| 2695 | if (!(t->flags & SN_FINST_MASK)) { |
| 2696 | if (t->pend_pos) |
| 2697 | t->flags |= SN_FINST_Q; |
| 2698 | else if (s == SV_STCONN) |
| 2699 | t->flags |= SN_FINST_C; |
| 2700 | else |
| 2701 | t->flags |= SN_FINST_D; |
| 2702 | } |
| 2703 | return 1; |
| 2704 | } |
| 2705 | |
| 2706 | if (t->flags & SN_SELF_GEN) { |
| 2707 | produce_content(t); |
| 2708 | if (rep->l == 0) { |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 2709 | buffer_shutw_done(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2710 | fd_delete(t->cli_fd); |
| 2711 | t->cli_state = CL_STCLOSE; |
| 2712 | return 1; |
| 2713 | } |
| 2714 | } |
| 2715 | |
Willy Tarreau | 718f0ef | 2008-08-10 16:21:32 +0200 | [diff] [blame] | 2716 | if ((rep->l == 0) || !(rep->flags & BF_MAY_FORWARD)) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2717 | if (EV_FD_COND_C(t->cli_fd, DIR_WR)) { |
| 2718 | /* stop writing */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2719 | rep->wex = TICK_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2720 | } |
| 2721 | } else { |
| 2722 | /* buffer not empty */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 2723 | if (!tick_isset(rep->wex)) { |
| 2724 | EV_FD_COND_S(t->cli_fd, DIR_WR); |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2725 | /* restart writing */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2726 | rep->wex = tick_add_ifset(now_ms, t->fe->timeout.client); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2727 | } |
| 2728 | } |
| 2729 | return 0; |
| 2730 | } |
| 2731 | else if (c == CL_STSHUTW) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2732 | if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 2733 | buffer_shutr_done(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2734 | fd_delete(t->cli_fd); |
| 2735 | t->cli_state = CL_STCLOSE; |
| 2736 | if (!(t->flags & SN_ERR_MASK)) |
| 2737 | t->flags |= SN_ERR_CLICL; |
| 2738 | if (!(t->flags & SN_FINST_MASK)) { |
| 2739 | if (t->pend_pos) |
| 2740 | t->flags |= SN_FINST_Q; |
| 2741 | else if (s == SV_STCONN) |
| 2742 | t->flags |= SN_FINST_C; |
| 2743 | else |
| 2744 | t->flags |= SN_FINST_D; |
| 2745 | } |
| 2746 | return 1; |
| 2747 | } |
Willy Tarreau | 718f0ef | 2008-08-10 16:21:32 +0200 | [diff] [blame] | 2748 | else if (req->flags & (BF_READ_NULL | BF_SHUTW_STATUS)) { |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 2749 | buffer_shutr_done(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2750 | fd_delete(t->cli_fd); |
| 2751 | t->cli_state = CL_STCLOSE; |
| 2752 | return 1; |
| 2753 | } |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2754 | else if (tick_is_expired(req->rex, now_ms)) { |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 2755 | buffer_shutr_done(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2756 | fd_delete(t->cli_fd); |
| 2757 | t->cli_state = CL_STCLOSE; |
| 2758 | if (!(t->flags & SN_ERR_MASK)) |
| 2759 | t->flags |= SN_ERR_CLITO; |
| 2760 | if (!(t->flags & SN_FINST_MASK)) { |
| 2761 | if (t->pend_pos) |
| 2762 | t->flags |= SN_FINST_Q; |
| 2763 | else if (s == SV_STCONN) |
| 2764 | t->flags |= SN_FINST_C; |
| 2765 | else |
| 2766 | t->flags |= SN_FINST_D; |
| 2767 | } |
| 2768 | return 1; |
| 2769 | } |
| 2770 | else if (req->l >= req->rlim - req->data) { |
| 2771 | /* no room to read more data */ |
| 2772 | |
| 2773 | /* FIXME-20050705: is it possible for a client to maintain a session |
| 2774 | * after the timeout by sending more data after it receives a close ? |
| 2775 | */ |
| 2776 | |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2777 | if (EV_FD_COND_C(t->cli_fd, DIR_RD)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2778 | /* stop reading until we get some space */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2779 | req->rex = TICK_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2780 | } |
| 2781 | } else { |
| 2782 | /* there's still some space in the buffer */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 2783 | if (!tick_isset(req->rex)) { |
| 2784 | EV_FD_COND_S(t->cli_fd, DIR_RD); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2785 | req->rex = tick_add_ifset(now_ms, t->fe->timeout.client); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2786 | } |
| 2787 | } |
| 2788 | return 0; |
| 2789 | } |
| 2790 | else { /* CL_STCLOSE: nothing to do */ |
| 2791 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 2792 | int len; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2793 | 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] | 2794 | write(1, trash, len); |
| 2795 | } |
| 2796 | return 0; |
| 2797 | } |
| 2798 | return 0; |
| 2799 | } |
| 2800 | |
| 2801 | |
| 2802 | /* |
| 2803 | * manages the server FSM and its socket. It returns 1 if a state has changed |
| 2804 | * (and a resync may be needed), 0 else. |
| 2805 | */ |
| 2806 | int process_srv(struct session *t) |
| 2807 | { |
| 2808 | int s = t->srv_state; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2809 | struct http_txn *txn = &t->txn; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2810 | struct buffer *req = t->req; |
| 2811 | struct buffer *rep = t->rep; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2812 | int conn_err; |
| 2813 | |
Willy Tarreau | 6468d92 | 2008-08-03 19:15:35 +0200 | [diff] [blame] | 2814 | 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] | 2815 | cli_stnames[t->cli_state], srv_stnames[t->srv_state], |
Willy Tarreau | 6468d92 | 2008-08-03 19:15:35 +0200 | [diff] [blame] | 2816 | EV_FD_ISSET(t->srv_fd, DIR_RD), EV_FD_ISSET(t->srv_fd, DIR_WR), |
| 2817 | rep->rex, req->wex); |
Willy Tarreau | ee99136 | 2007-05-14 14:37:50 +0200 | [diff] [blame] | 2818 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2819 | if (s == SV_STIDLE) { |
Willy Tarreau | dc0a6a0 | 2008-08-03 20:38:13 +0200 | [diff] [blame] | 2820 | if ((rep->flags & BF_SHUTW_STATUS) || |
Willy Tarreau | 6468d92 | 2008-08-03 19:15:35 +0200 | [diff] [blame] | 2821 | ((req->flags & BF_SHUTR_STATUS) && |
| 2822 | (req->l == 0 || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2823 | req->cex = TICK_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2824 | if (t->pend_pos) |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2825 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2826 | /* note that this must not return any error because it would be able to |
| 2827 | * overwrite the client_retnclose() output. |
| 2828 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2829 | if (txn->flags & TX_CLTARPIT) |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 2830 | 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] | 2831 | else |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 2832 | 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] | 2833 | |
| 2834 | return 1; |
| 2835 | } |
Willy Tarreau | dc0a6a0 | 2008-08-03 20:38:13 +0200 | [diff] [blame] | 2836 | else if (req->flags & BF_MAY_CONNECT) { |
| 2837 | /* the client allows the server to connect */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2838 | if (txn->flags & TX_CLTARPIT) { |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 2839 | /* This connection is being tarpitted. The CLIENT side has |
| 2840 | * already set the connect expiration date to the right |
| 2841 | * timeout. We just have to check that it has not expired. |
| 2842 | */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2843 | if (!tick_is_expired(req->cex, now_ms)) |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 2844 | return 0; |
| 2845 | |
| 2846 | /* We will set the queue timer to the time spent, just for |
| 2847 | * logging purposes. We fake a 500 server error, so that the |
| 2848 | * attacker will not suspect his connection has been tarpitted. |
| 2849 | * It will not cause trouble to the logs because we can exclude |
| 2850 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 2851 | */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2852 | req->cex = TICK_ETERNITY; |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2853 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 2854 | srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T, |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2855 | 500, error_message(t, HTTP_ERR_500)); |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 2856 | return 1; |
| 2857 | } |
| 2858 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2859 | /* Right now, we will need to create a connection to the server. |
| 2860 | * We might already have tried, and got a connection pending, in |
| 2861 | * which case we will not do anything till it's pending. It's up |
| 2862 | * to any other session to release it and wake us up again. |
| 2863 | */ |
| 2864 | if (t->pend_pos) { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2865 | if (!tick_is_expired(req->cex, now_ms)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2866 | return 0; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 2867 | } else { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2868 | /* we've been waiting too long here */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2869 | req->cex = TICK_ETERNITY; |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2870 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2871 | srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q, |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2872 | 503, error_message(t, HTTP_ERR_503)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2873 | if (t->srv) |
| 2874 | t->srv->failed_conns++; |
Willy Tarreau | 50fd1e1 | 2007-12-10 15:25:35 +0100 | [diff] [blame] | 2875 | t->be->failed_conns++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2876 | return 1; |
| 2877 | } |
| 2878 | } |
| 2879 | |
| 2880 | do { |
| 2881 | /* first, get a connection */ |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2882 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2883 | t->flags |= SN_REDIRECTABLE; |
| 2884 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2885 | if (srv_redispatch_connect(t)) |
| 2886 | return t->srv_state != SV_STIDLE; |
| 2887 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2888 | if ((t->flags & SN_REDIRECTABLE) && t->srv && t->srv->rdr_len) { |
| 2889 | /* Server supporting redirection and it is possible. |
| 2890 | * Invalid requests are reported as such. It concerns all |
| 2891 | * the largest ones. |
| 2892 | */ |
| 2893 | struct chunk rdr; |
| 2894 | char *path; |
| 2895 | int len; |
| 2896 | |
| 2897 | /* 1: create the response header */ |
| 2898 | rdr.len = strlen(HTTP_302); |
| 2899 | rdr.str = trash; |
| 2900 | memcpy(rdr.str, HTTP_302, rdr.len); |
| 2901 | |
| 2902 | /* 2: add the server's prefix */ |
| 2903 | if (rdr.len + t->srv->rdr_len > sizeof(trash)) |
| 2904 | goto cancel_redir; |
| 2905 | |
| 2906 | memcpy(rdr.str + rdr.len, t->srv->rdr_pfx, t->srv->rdr_len); |
| 2907 | rdr.len += t->srv->rdr_len; |
| 2908 | |
| 2909 | /* 3: add the request URI */ |
| 2910 | path = http_get_path(txn); |
| 2911 | if (!path) |
| 2912 | goto cancel_redir; |
| 2913 | len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path; |
| 2914 | if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */ |
| 2915 | goto cancel_redir; |
| 2916 | |
| 2917 | memcpy(rdr.str + rdr.len, path, len); |
| 2918 | rdr.len += len; |
| 2919 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 2920 | rdr.len += 4; |
| 2921 | |
| 2922 | srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr); |
| 2923 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
| 2924 | if (t->srv) |
| 2925 | t->srv->cum_sess++; |
| 2926 | return 1; |
| 2927 | cancel_redir: |
| 2928 | txn->status = 400; |
| 2929 | t->fe->failed_req++; |
| 2930 | srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, |
| 2931 | 400, error_message(t, HTTP_ERR_400)); |
| 2932 | return 1; |
| 2933 | } |
| 2934 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2935 | /* try to (re-)connect to the server, and fail if we expire the |
| 2936 | * number of retries. |
| 2937 | */ |
| 2938 | if (srv_retryable_connect(t)) { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2939 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2940 | return t->srv_state != SV_STIDLE; |
| 2941 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2942 | } while (1); |
| 2943 | } |
| 2944 | } |
| 2945 | else if (s == SV_STCONN) { /* connection in progress */ |
Willy Tarreau | 6468d92 | 2008-08-03 19:15:35 +0200 | [diff] [blame] | 2946 | if ((rep->flags & BF_SHUTW_STATUS) || |
| 2947 | ((req->flags & BF_SHUTR_STATUS) && |
| 2948 | ((req->l == 0 && !(req->flags & BF_WRITE_STATUS)) || |
Willy Tarreau | c9b654b | 2007-05-08 14:46:53 +0200 | [diff] [blame] | 2949 | t->be->options & PR_O_ABRT_CLOSE))) { /* give up */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2950 | req->cex = TICK_ETERNITY; |
Willy Tarreau | f899b94 | 2008-03-28 18:09:38 +0100 | [diff] [blame] | 2951 | if (!(t->flags & SN_CONN_TAR)) { |
| 2952 | /* if we are in turn-around, we have already closed the FD */ |
| 2953 | fd_delete(t->srv_fd); |
| 2954 | if (t->srv) { |
| 2955 | t->srv->cur_sess--; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 2956 | sess_change_server(t, NULL); |
Willy Tarreau | f899b94 | 2008-03-28 18:09:38 +0100 | [diff] [blame] | 2957 | } |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 2958 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2959 | |
| 2960 | /* note that this must not return any error because it would be able to |
| 2961 | * overwrite the client_retnclose() output. |
| 2962 | */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 2963 | 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] | 2964 | return 1; |
| 2965 | } |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2966 | 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] | 2967 | return 0; /* nothing changed */ |
| 2968 | } |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2969 | else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2970 | /* timeout, asynchronous connect error or first write error */ |
| 2971 | //fprintf(stderr,"2: c=%d, s=%d\n", c, s); |
| 2972 | |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 2973 | if (t->flags & SN_CONN_TAR) { |
| 2974 | /* We are doing a turn-around waiting for a new connection attempt. */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 2975 | if (!tick_is_expired(req->cex, now_ms)) |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 2976 | return 0; |
| 2977 | t->flags &= ~SN_CONN_TAR; |
| 2978 | } |
| 2979 | else { |
| 2980 | fd_delete(t->srv_fd); |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 2981 | if (t->srv) { |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 2982 | t->srv->cur_sess--; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 2983 | sess_change_server(t, NULL); |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 2984 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2985 | |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 2986 | if (!(req->flags & BF_WRITE_STATUS)) |
| 2987 | conn_err = SN_ERR_SRVTO; // it was a connect timeout. |
| 2988 | else |
| 2989 | conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error. |
| 2990 | |
| 2991 | /* ensure that we have enough retries left */ |
| 2992 | if (srv_count_retry_down(t, conn_err)) |
| 2993 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2994 | |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 2995 | if (req->flags & BF_WRITE_ERROR) { |
| 2996 | /* we encountered an immediate connection error, and we |
| 2997 | * will have to retry connecting to the same server, most |
| 2998 | * likely leading to the same result. To avoid this, we |
| 2999 | * fake a connection timeout to retry after a turn-around |
| 3000 | * time of 1 second. We will wait in the previous if block. |
| 3001 | */ |
| 3002 | t->flags |= SN_CONN_TAR; |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3003 | req->cex = tick_add(now_ms, MS_TO_TICKS(1000)); |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 3004 | return 0; |
| 3005 | } |
| 3006 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3007 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3008 | 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] | 3009 | /* We're on our last chance, and the REDISP option was specified. |
| 3010 | * We will ignore cookie and force to balance or use the dispatcher. |
| 3011 | */ |
| 3012 | /* let's try to offer this slot to anybody */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3013 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3014 | process_srv_queue(t->srv); |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 3015 | |
Krzysztof Piotr Oledzki | 5a329cf | 2008-02-22 03:50:19 +0100 | [diff] [blame] | 3016 | /* it's left to the dispatcher to choose a server */ |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 3017 | t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3018 | t->prev_srv = t->srv; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 3019 | |
| 3020 | /* first, get a connection */ |
| 3021 | if (srv_redispatch_connect(t)) |
Willy Tarreau | 00559e7 | 2008-01-06 23:46:19 +0100 | [diff] [blame] | 3022 | return t->srv_state != SV_STCONN; |
Krzysztof Piotr Oledzki | 626a19b | 2008-02-04 02:10:09 +0100 | [diff] [blame] | 3023 | } else { |
| 3024 | if (t->srv) |
| 3025 | t->srv->retries++; |
| 3026 | t->be->retries++; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 3027 | } |
| 3028 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3029 | do { |
| 3030 | /* Now we will try to either reconnect to the same server or |
| 3031 | * connect to another server. If the connection gets queued |
| 3032 | * because all servers are saturated, then we will go back to |
| 3033 | * the SV_STIDLE state. |
| 3034 | */ |
| 3035 | if (srv_retryable_connect(t)) { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 3036 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3037 | return t->srv_state != SV_STCONN; |
| 3038 | } |
| 3039 | |
| 3040 | /* we need to redispatch the connection to another server */ |
| 3041 | if (srv_redispatch_connect(t)) |
| 3042 | return t->srv_state != SV_STCONN; |
| 3043 | } while (1); |
| 3044 | } |
| 3045 | else { /* no error or write 0 */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 3046 | t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3047 | |
| 3048 | //fprintf(stderr,"3: c=%d, s=%d\n", c, s); |
| 3049 | if (req->l == 0) /* nothing to write */ { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3050 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3051 | req->wex = TICK_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3052 | } else /* need the right to write */ { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3053 | EV_FD_SET(t->srv_fd, DIR_WR); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3054 | req->wex = tick_add_ifset(now_ms, t->be->timeout.server); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 3055 | if (tick_isset(req->wex)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3056 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 3057 | * we refresh it. */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3058 | rep->rex = req->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3059 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3060 | } |
| 3061 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3062 | 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] | 3063 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3064 | rep->rex = tick_add_ifset(now_ms, t->be->timeout.server); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3065 | t->srv_state = SV_STDATA; |
Willy Tarreau | 718f0ef | 2008-08-10 16:21:32 +0200 | [diff] [blame] | 3066 | rep->flags |= BF_MAY_FORWARD; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3067 | rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */ |
| 3068 | |
| 3069 | /* if the user wants to log as soon as possible, without counting |
| 3070 | bytes from the server, then this is the right moment. */ |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 3071 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3072 | 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] | 3073 | tcp_sess_log(t); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3074 | } |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 3075 | #ifdef CONFIG_HAP_TCPSPLICE |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3076 | if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 3077 | /* TCP splicing supported by both FE and BE */ |
| 3078 | tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0); |
| 3079 | } |
| 3080 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3081 | } |
| 3082 | else { |
| 3083 | t->srv_state = SV_STHEADERS; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3084 | rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3085 | t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE; |
| 3086 | /* reset hdr_idx which was already initialized by the request. |
| 3087 | * right now, the http parser does it. |
| 3088 | * hdr_idx_init(&t->txn.hdr_idx); |
| 3089 | */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3090 | } |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3091 | req->cex = TICK_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3092 | return 1; |
| 3093 | } |
| 3094 | } |
| 3095 | else if (s == SV_STHEADERS) { /* receiving server headers */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3096 | /* |
| 3097 | * Now parse the partial (or complete) lines. |
| 3098 | * We will check the response syntax, and also join multi-line |
| 3099 | * headers. An index of all the lines will be elaborated while |
| 3100 | * parsing. |
| 3101 | * |
| 3102 | * For the parsing, we use a 28 states FSM. |
| 3103 | * |
| 3104 | * Here is the information we currently have : |
| 3105 | * rep->data + req->som = beginning of response |
| 3106 | * rep->data + req->eoh = end of processed headers / start of current one |
| 3107 | * rep->data + req->eol = end of current header or line (LF or CRLF) |
| 3108 | * rep->lr = first non-visited byte |
| 3109 | * rep->r = end of data |
| 3110 | */ |
| 3111 | |
| 3112 | int cur_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3113 | struct http_msg *msg = &txn->rsp; |
| 3114 | struct proxy *cur_proxy; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3115 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3116 | if (likely(rep->lr < rep->r)) |
| 3117 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3118 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3119 | /* 1: we might have to print this header in debug mode */ |
| 3120 | if (unlikely((global.mode & MODE_DEBUG) && |
| 3121 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
| 3122 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
| 3123 | char *eol, *sol; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3124 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3125 | sol = rep->data + msg->som; |
| 3126 | eol = sol + msg->sl.rq.l; |
| 3127 | debug_hdr("srvrep", t, sol, eol); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3128 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3129 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 3130 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3131 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3132 | while (cur_idx) { |
| 3133 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 3134 | debug_hdr("srvhdr", t, sol, eol); |
| 3135 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 3136 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 3137 | } |
| 3138 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3139 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3140 | |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 3141 | if ((rep->l < rep->rlim - rep->data) && !tick_isset(rep->rex)) { |
| 3142 | EV_FD_COND_S(t->srv_fd, DIR_RD); |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3143 | /* fd in DIR_RD was disabled, perhaps because of a previous buffer |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3144 | * full. We cannot loop here since stream_sock_read will disable it only if |
| 3145 | * rep->l == rlim-data |
| 3146 | */ |
Willy Tarreau | ce09c52 | 2008-08-11 10:35:07 +0200 | [diff] [blame] | 3147 | rep->rex = tick_add_ifset(now_ms, t->be->timeout.server); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3148 | } |
| 3149 | |
| 3150 | |
| 3151 | /* |
| 3152 | * Now we quickly check if we have found a full valid response. |
| 3153 | * If not so, we check the FD and buffer states before leaving. |
| 3154 | * A full response is indicated by the fact that we have seen |
| 3155 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 3156 | * responses are checked first. |
| 3157 | * |
| 3158 | * Depending on whether the client is still there or not, we |
| 3159 | * may send an error response back or not. Note that normally |
| 3160 | * we should only check for HTTP status there, and check I/O |
| 3161 | * errors somewhere else. |
| 3162 | */ |
| 3163 | |
| 3164 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
| 3165 | |
| 3166 | /* Invalid response, or read error or write error */ |
| 3167 | if (unlikely((msg->msg_state == HTTP_MSG_ERROR) || |
| 3168 | (req->flags & BF_WRITE_ERROR) || |
| 3169 | (rep->flags & BF_READ_ERROR))) { |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 3170 | buffer_shutr_done(rep); |
| 3171 | buffer_shutw_done(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3172 | fd_delete(t->srv_fd); |
| 3173 | if (t->srv) { |
| 3174 | t->srv->cur_sess--; |
| 3175 | t->srv->failed_resp++; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3176 | sess_change_server(t, NULL); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3177 | } |
| 3178 | t->be->failed_resp++; |
| 3179 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 3180 | rep->flags |= BF_MAY_FORWARD; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 3181 | txn->status = 502; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3182 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 3183 | if (!(t->flags & SN_ERR_MASK)) |
| 3184 | t->flags |= SN_ERR_SRVCL; |
| 3185 | if (!(t->flags & SN_FINST_MASK)) |
| 3186 | t->flags |= SN_FINST_H; |
| 3187 | /* We used to have a free connection slot. Since we'll never use it, |
| 3188 | * we have to inform the server that it may be used by another session. |
| 3189 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3190 | if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3191 | process_srv_queue(t->srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3192 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3193 | return 1; |
| 3194 | } |
| 3195 | |
| 3196 | /* end of client write or end of server read. |
| 3197 | * since we are in header mode, if there's no space left for headers, we |
| 3198 | * won't be able to free more later, so the session will never terminate. |
| 3199 | */ |
Willy Tarreau | 6468d92 | 2008-08-03 19:15:35 +0200 | [diff] [blame] | 3200 | else if (unlikely(rep->flags & (BF_READ_NULL | BF_SHUTW_STATUS) || |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3201 | rep->l >= rep->rlim - rep->data)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3202 | EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 3203 | buffer_shutr_done(rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3204 | t->srv_state = SV_STSHUTR; |
| 3205 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 3206 | return 1; |
| 3207 | } |
| 3208 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3209 | /* read timeout : return a 504 to the client. */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3210 | else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_RD) && |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3211 | tick_is_expired(rep->rex, now_ms))) { |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 3212 | buffer_shutr_done(rep); |
| 3213 | buffer_shutw_done(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3214 | fd_delete(t->srv_fd); |
| 3215 | if (t->srv) { |
| 3216 | t->srv->cur_sess--; |
| 3217 | t->srv->failed_resp++; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3218 | sess_change_server(t, NULL); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3219 | } |
| 3220 | t->be->failed_resp++; |
| 3221 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 3222 | rep->flags |= BF_MAY_FORWARD; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 3223 | txn->status = 504; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3224 | client_return(t, error_message(t, HTTP_ERR_504)); |
| 3225 | if (!(t->flags & SN_ERR_MASK)) |
| 3226 | t->flags |= SN_ERR_SRVTO; |
| 3227 | if (!(t->flags & SN_FINST_MASK)) |
| 3228 | t->flags |= SN_FINST_H; |
| 3229 | /* We used to have a free connection slot. Since we'll never use it, |
| 3230 | * we have to inform the server that it may be used by another session. |
| 3231 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3232 | if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3233 | process_srv_queue(t->srv); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3234 | return 1; |
| 3235 | } |
| 3236 | |
| 3237 | /* last client read and buffer empty */ |
| 3238 | /* FIXME!!! here, we don't want to switch to SHUTW if the |
| 3239 | * client shuts read too early, because we may still have |
| 3240 | * some work to do on the headers. |
| 3241 | * The side-effect is that if the client completely closes its |
| 3242 | * connection during SV_STHEADER, the connection to the server |
| 3243 | * is kept until a response comes back or the timeout is reached. |
| 3244 | */ |
Willy Tarreau | 6468d92 | 2008-08-03 19:15:35 +0200 | [diff] [blame] | 3245 | else if (0 && /* we don't want to switch to shutw for now */ |
| 3246 | unlikely(req->flags & BF_SHUTR_STATUS && (req->l == 0))) { |
| 3247 | |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3248 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 3249 | buffer_shutw_done(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3250 | |
| 3251 | /* We must ensure that the read part is still |
| 3252 | * alive when switching to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3253 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3254 | rep->rex = tick_add_ifset(now_ms, t->be->timeout.server); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3255 | |
| 3256 | shutdown(t->srv_fd, SHUT_WR); |
| 3257 | t->srv_state = SV_STSHUTW; |
| 3258 | return 1; |
| 3259 | } |
| 3260 | |
| 3261 | /* write timeout */ |
| 3262 | /* FIXME!!! here, we don't want to switch to SHUTW if the |
| 3263 | * client shuts read too early, because we may still have |
| 3264 | * some work to do on the headers. |
| 3265 | */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3266 | else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_WR) && |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3267 | tick_is_expired(req->wex, now_ms))) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3268 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 3269 | buffer_shutw_done(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3270 | shutdown(t->srv_fd, SHUT_WR); |
| 3271 | /* We must ensure that the read part is still alive |
| 3272 | * when switching to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3273 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3274 | rep->rex = tick_add_ifset(now_ms, t->be->timeout.server); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3275 | |
| 3276 | t->srv_state = SV_STSHUTW; |
| 3277 | if (!(t->flags & SN_ERR_MASK)) |
| 3278 | t->flags |= SN_ERR_SRVTO; |
| 3279 | if (!(t->flags & SN_FINST_MASK)) |
| 3280 | t->flags |= SN_FINST_H; |
| 3281 | return 1; |
| 3282 | } |
| 3283 | |
| 3284 | /* |
| 3285 | * And now the non-error cases. |
| 3286 | */ |
| 3287 | |
| 3288 | /* Data remaining in the request buffer. |
| 3289 | * This happens during the first pass here, and during |
| 3290 | * long posts. |
| 3291 | */ |
| 3292 | else if (likely(req->l)) { |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 3293 | if (!tick_isset(req->wex)) { |
| 3294 | EV_FD_COND_S(t->srv_fd, DIR_WR); |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3295 | /* restart writing */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3296 | req->wex = tick_add_ifset(now_ms, t->be->timeout.server); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 3297 | if (tick_isset(req->wex)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3298 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 3299 | * we refresh it. */ |
| 3300 | rep->rex = req->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3301 | } |
| 3302 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3303 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3304 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3305 | /* nothing left in the request buffer */ |
| 3306 | else { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3307 | if (EV_FD_COND_C(t->srv_fd, DIR_WR)) { |
| 3308 | /* stop writing */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3309 | req->wex = TICK_ETERNITY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3310 | } |
| 3311 | } |
| 3312 | |
| 3313 | return t->srv_state != SV_STHEADERS; |
| 3314 | } |
| 3315 | |
| 3316 | |
| 3317 | /***************************************************************** |
| 3318 | * More interesting part now : we know that we have a complete * |
| 3319 | * response which at least looks like HTTP. We have an indicator * |
| 3320 | * of each header's length, so we can parse them quickly. * |
| 3321 | ****************************************************************/ |
| 3322 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 3323 | /* ensure we keep this pointer to the beginning of the message */ |
| 3324 | msg->sol = rep->data + msg->som; |
| 3325 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3326 | /* |
| 3327 | * 1: get the status code and check for cacheability. |
| 3328 | */ |
| 3329 | |
| 3330 | t->logs.logwait &= ~LW_RESP; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 3331 | 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] | 3332 | |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 3333 | switch (txn->status) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3334 | case 200: |
| 3335 | case 203: |
| 3336 | case 206: |
| 3337 | case 300: |
| 3338 | case 301: |
| 3339 | case 410: |
| 3340 | /* RFC2616 @13.4: |
| 3341 | * "A response received with a status code of |
| 3342 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 3343 | * by a cache (...) unless a cache-control |
| 3344 | * directive prohibits caching." |
| 3345 | * |
| 3346 | * RFC2616 @9.5: POST method : |
| 3347 | * "Responses to this method are not cacheable, |
| 3348 | * unless the response includes appropriate |
| 3349 | * Cache-Control or Expires header fields." |
| 3350 | */ |
| 3351 | if (likely(txn->meth != HTTP_METH_POST) && |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3352 | (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3353 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3354 | break; |
| 3355 | default: |
| 3356 | break; |
| 3357 | } |
| 3358 | |
| 3359 | /* |
| 3360 | * 2: we may need to capture headers |
| 3361 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3362 | if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3363 | capture_headers(rep->data + msg->som, &txn->hdr_idx, |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3364 | txn->rsp.cap, t->fe->rsp_cap); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3365 | |
| 3366 | /* |
| 3367 | * 3: we will have to evaluate the filters. |
| 3368 | * As opposed to version 1.2, now they will be evaluated in the |
| 3369 | * filters order and not in the header order. This means that |
| 3370 | * each filter has to be validated among all headers. |
| 3371 | * |
| 3372 | * Filters are tried with ->be first, then with ->fe if it is |
| 3373 | * different from ->be. |
| 3374 | */ |
| 3375 | |
| 3376 | t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */ |
| 3377 | |
| 3378 | cur_proxy = t->be; |
| 3379 | while (1) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3380 | struct proxy *rule_set = cur_proxy; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3381 | |
| 3382 | /* try headers filters */ |
| 3383 | if (rule_set->rsp_exp != NULL) { |
| 3384 | if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) { |
| 3385 | return_bad_resp: |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3386 | if (t->srv) { |
| 3387 | t->srv->cur_sess--; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3388 | t->srv->failed_resp++; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3389 | sess_change_server(t, NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3390 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3391 | cur_proxy->failed_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3392 | return_srv_prx_502: |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 3393 | buffer_shutr_done(rep); |
| 3394 | buffer_shutw_done(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3395 | fd_delete(t->srv_fd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3396 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 3397 | rep->flags |= BF_MAY_FORWARD; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 3398 | txn->status = 502; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 3399 | client_return(t, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3400 | if (!(t->flags & SN_ERR_MASK)) |
| 3401 | t->flags |= SN_ERR_PRXCOND; |
| 3402 | if (!(t->flags & SN_FINST_MASK)) |
| 3403 | t->flags |= SN_FINST_H; |
| 3404 | /* We used to have a free connection slot. Since we'll never use it, |
| 3405 | * we have to inform the server that it may be used by another session. |
| 3406 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3407 | if (t->srv && may_dequeue_tasks(t->srv, cur_proxy)) |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3408 | process_srv_queue(t->srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3409 | return 1; |
| 3410 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3411 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3412 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3413 | /* has the response been denied ? */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3414 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3415 | if (t->srv) { |
| 3416 | t->srv->cur_sess--; |
| 3417 | t->srv->failed_secu++; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3418 | sess_change_server(t, NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3419 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3420 | cur_proxy->denied_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3421 | goto return_srv_prx_502; |
| 3422 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3423 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3424 | /* We might have to check for "Connection:" */ |
Krzysztof Oledzki | 336d475 | 2007-12-25 02:40:22 +0100 | [diff] [blame] | 3425 | 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] | 3426 | !(t->flags & SN_CONN_CLOSED)) { |
| 3427 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 3428 | int cur_idx, old_idx, delta, val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3429 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3430 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3431 | cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 3432 | old_idx = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3433 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3434 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 3435 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 3436 | cur_ptr = cur_next; |
| 3437 | cur_end = cur_ptr + cur_hdr->len; |
| 3438 | cur_next = cur_end + cur_hdr->cr + 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3439 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 3440 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 3441 | if (val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3442 | /* 3 possibilities : |
| 3443 | * - we have already set Connection: close, |
| 3444 | * so we remove this line. |
| 3445 | * - we have not yet set Connection: close, |
| 3446 | * but this line indicates close. We leave |
| 3447 | * it untouched and set the flag. |
| 3448 | * - we have not yet set Connection: close, |
| 3449 | * and this line indicates non-close. We |
| 3450 | * replace it. |
| 3451 | */ |
| 3452 | if (t->flags & SN_CONN_CLOSED) { |
| 3453 | delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0); |
| 3454 | txn->rsp.eoh += delta; |
| 3455 | cur_next += delta; |
| 3456 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 3457 | txn->hdr_idx.used--; |
| 3458 | cur_hdr->len = 0; |
| 3459 | } else { |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 3460 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 3461 | delta = buffer_replace2(rep, cur_ptr + val, cur_end, |
| 3462 | "close", 5); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3463 | cur_next += delta; |
| 3464 | cur_hdr->len += delta; |
| 3465 | txn->rsp.eoh += delta; |
| 3466 | } |
| 3467 | t->flags |= SN_CONN_CLOSED; |
| 3468 | } |
| 3469 | } |
| 3470 | old_idx = cur_idx; |
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 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3473 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3474 | /* add response headers from the rule sets in the same order */ |
| 3475 | for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) { |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3476 | if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, |
| 3477 | rule_set->rsp_add[cur_idx])) < 0) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3478 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3479 | } |
| 3480 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3481 | /* check whether we're already working on the frontend */ |
| 3482 | if (cur_proxy == t->fe) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3483 | break; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3484 | cur_proxy = t->fe; |
| 3485 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3486 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3487 | /* |
| 3488 | * 4: check for server cookie. |
| 3489 | */ |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 3490 | if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name |
| 3491 | || (t->be->options & PR_O_CHK_CACHE)) |
| 3492 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3493 | |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3494 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3495 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 3496 | * 5: check for cache-control or pragma headers if required. |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3497 | */ |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 3498 | if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0) |
| 3499 | check_response_for_cacheability(t, rep); |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3500 | |
| 3501 | /* |
| 3502 | * 6: add server cookie in the response if needed |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3503 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3504 | if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) && |
| 3505 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3506 | int len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3507 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3508 | /* the server is known, it's not the one the client requested, we have to |
| 3509 | * insert a set-cookie here, except if we want to insert only on POST |
| 3510 | * requests and this one isn't. Note that servers which don't have cookies |
| 3511 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3512 | */ |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3513 | len = sprintf(trash, "Set-Cookie: %s=%s; path=/", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3514 | t->be->cookie_name, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3515 | 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] | 3516 | |
Krzysztof Piotr Oledzki | 1acf217 | 2008-05-29 23:03:34 +0200 | [diff] [blame] | 3517 | if (t->be->cookie_domain) |
| 3518 | len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain); |
Krzysztof Piotr Oledzki | efe3b6f | 2008-05-23 23:49:32 +0200 | [diff] [blame] | 3519 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3520 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3521 | trash, len)) < 0) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3522 | goto return_bad_resp; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3523 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3524 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3525 | /* Here, we will tell an eventual cache on the client side that we don't |
| 3526 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 3527 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 3528 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 3529 | */ |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3530 | if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) { |
| 3531 | |
| 3532 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3533 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3534 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3535 | "Cache-control: private", 22)) < 0) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3536 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3537 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3538 | } |
| 3539 | |
| 3540 | |
| 3541 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3542 | * 7: check if result will be cacheable with a cookie. |
| 3543 | * We'll block the response if security checks have caught |
| 3544 | * nasty things such as a cacheable cookie. |
| 3545 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3546 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) == |
| 3547 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3548 | (t->be->options & PR_O_CHK_CACHE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3549 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3550 | /* we're in presence of a cacheable response containing |
| 3551 | * a set-cookie header. We'll block it as requested by |
| 3552 | * the 'checkcache' option, and send an alert. |
| 3553 | */ |
| 3554 | if (t->srv) { |
| 3555 | t->srv->cur_sess--; |
| 3556 | t->srv->failed_secu++; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3557 | sess_change_server(t, NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3558 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3559 | t->be->denied_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3560 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3561 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3562 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3563 | send_log(t->be, LOG_ALERT, |
| 3564 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3565 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3566 | goto return_srv_prx_502; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3567 | } |
| 3568 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3569 | /* |
| 3570 | * 8: add "Connection: close" if needed and not yet set. |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 3571 | * 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] | 3572 | */ |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 3573 | if (!(t->flags & SN_CONN_CLOSED) && |
Krzysztof Oledzki | 336d475 | 2007-12-25 02:40:22 +0100 | [diff] [blame] | 3574 | ((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] | 3575 | if ((unlikely(msg->sl.st.v_l != 8) || |
| 3576 | unlikely(req->data[msg->som + 7] != '0')) && |
| 3577 | unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3578 | "Connection: close", 17)) < 0) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3579 | goto return_bad_resp; |
| 3580 | t->flags |= SN_CONN_CLOSED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3581 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3582 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3583 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3584 | /************************************************************* |
| 3585 | * OK, that's finished for the headers. We have done what we * |
| 3586 | * could. Let's switch to the DATA state. * |
| 3587 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3588 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3589 | t->srv_state = SV_STDATA; |
Willy Tarreau | 718f0ef | 2008-08-10 16:21:32 +0200 | [diff] [blame] | 3590 | rep->flags |= BF_MAY_FORWARD; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3591 | rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 3592 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3593 | |
| 3594 | /* client connection already closed or option 'forceclose' required : |
| 3595 | * we close the server's outgoing connection right now. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3596 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3597 | if ((req->l == 0) && |
Willy Tarreau | 6468d92 | 2008-08-03 19:15:35 +0200 | [diff] [blame] | 3598 | (req->flags & BF_SHUTR_STATUS || t->be->options & PR_O_FORCE_CLO)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3599 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 3600 | buffer_shutw_done(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3601 | |
| 3602 | /* We must ensure that the read part is still alive when switching |
| 3603 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3604 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3605 | rep->rex = tick_add_ifset(now_ms, t->be->timeout.server); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3606 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3607 | shutdown(t->srv_fd, SHUT_WR); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3608 | t->srv_state = SV_STSHUTW; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3609 | } |
| 3610 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3611 | #ifdef CONFIG_HAP_TCPSPLICE |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3612 | if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3613 | /* TCP splicing supported by both FE and BE */ |
| 3614 | tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3615 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3616 | #endif |
| 3617 | /* 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] | 3618 | * bytes from the server, then this is the right moment. We have |
| 3619 | * to temporarily assign bytes_out to log what we currently have. |
| 3620 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3621 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 3622 | 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] | 3623 | t->logs.bytes_out = txn->rsp.eoh; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 3624 | if (t->fe->to_log & LW_REQ) |
| 3625 | http_sess_log(t); |
| 3626 | else |
| 3627 | tcp_sess_log(t); |
Krzysztof Piotr Oledzki | f1e1cb4 | 2008-01-20 23:27:02 +0100 | [diff] [blame] | 3628 | t->logs.bytes_out = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3629 | } |
| 3630 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3631 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 3632 | * otherwise we would not let the client side wake up. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3633 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3634 | |
| 3635 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3636 | } |
| 3637 | else if (s == SV_STDATA) { |
| 3638 | /* read or write error */ |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3639 | if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) { |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 3640 | buffer_shutr_done(rep); |
| 3641 | buffer_shutw_done(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3642 | fd_delete(t->srv_fd); |
| 3643 | if (t->srv) { |
| 3644 | t->srv->cur_sess--; |
| 3645 | t->srv->failed_resp++; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3646 | sess_change_server(t, NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3647 | } |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 3648 | t->be->failed_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3649 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 3650 | rep->flags |= BF_MAY_FORWARD; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3651 | if (!(t->flags & SN_ERR_MASK)) |
| 3652 | t->flags |= SN_ERR_SRVCL; |
| 3653 | if (!(t->flags & SN_FINST_MASK)) |
| 3654 | t->flags |= SN_FINST_D; |
| 3655 | /* We used to have a free connection slot. Since we'll never use it, |
| 3656 | * we have to inform the server that it may be used by another session. |
| 3657 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3658 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3659 | process_srv_queue(t->srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3660 | |
| 3661 | return 1; |
| 3662 | } |
| 3663 | /* last read, or end of client write */ |
Willy Tarreau | 6468d92 | 2008-08-03 19:15:35 +0200 | [diff] [blame] | 3664 | else if (rep->flags & (BF_READ_NULL | BF_SHUTW_STATUS)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3665 | EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3666 | buffer_shutr(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3667 | t->srv_state = SV_STSHUTR; |
| 3668 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 3669 | return 1; |
| 3670 | } |
| 3671 | /* end of client read and no more data to send */ |
Willy Tarreau | 6468d92 | 2008-08-03 19:15:35 +0200 | [diff] [blame] | 3672 | else if (req->flags & BF_SHUTR_STATUS && (req->l == 0)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3673 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 3674 | buffer_shutw_done(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3675 | shutdown(t->srv_fd, SHUT_WR); |
| 3676 | /* We must ensure that the read part is still alive when switching |
| 3677 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3678 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3679 | rep->rex = tick_add_ifset(now_ms, t->be->timeout.server); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3680 | |
| 3681 | t->srv_state = SV_STSHUTW; |
| 3682 | return 1; |
| 3683 | } |
| 3684 | /* read timeout */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3685 | else if (tick_is_expired(rep->rex, now_ms)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3686 | EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3687 | buffer_shutr(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3688 | t->srv_state = SV_STSHUTR; |
| 3689 | if (!(t->flags & SN_ERR_MASK)) |
| 3690 | t->flags |= SN_ERR_SRVTO; |
| 3691 | if (!(t->flags & SN_FINST_MASK)) |
| 3692 | t->flags |= SN_FINST_D; |
| 3693 | return 1; |
| 3694 | } |
| 3695 | /* write timeout */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3696 | else if (tick_is_expired(req->wex, now_ms)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3697 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 3698 | buffer_shutw_done(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3699 | shutdown(t->srv_fd, SHUT_WR); |
| 3700 | /* We must ensure that the read part is still alive when switching |
| 3701 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3702 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3703 | rep->cex = tick_add_ifset(now_ms, t->be->timeout.server); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3704 | t->srv_state = SV_STSHUTW; |
| 3705 | if (!(t->flags & SN_ERR_MASK)) |
| 3706 | t->flags |= SN_ERR_SRVTO; |
| 3707 | if (!(t->flags & SN_FINST_MASK)) |
| 3708 | t->flags |= SN_FINST_D; |
| 3709 | return 1; |
| 3710 | } |
| 3711 | |
| 3712 | /* recompute request time-outs */ |
| 3713 | if (req->l == 0) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3714 | if (EV_FD_COND_C(t->srv_fd, DIR_WR)) { |
| 3715 | /* stop writing */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3716 | req->wex = TICK_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3717 | } |
| 3718 | } |
| 3719 | else { /* buffer not empty, there are still data to be transferred */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 3720 | if (!tick_isset(req->wex)) { |
| 3721 | EV_FD_COND_S(t->srv_fd, DIR_WR); |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3722 | /* restart writing */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3723 | req->wex = tick_add_ifset(now_ms, t->be->timeout.server); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 3724 | if (tick_isset(req->wex)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3725 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 3726 | * we refresh it. */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3727 | rep->rex = req->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3728 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3729 | } |
| 3730 | } |
| 3731 | |
| 3732 | /* recompute response time-outs */ |
| 3733 | if (rep->l == BUFSIZE) { /* no room to read more data */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3734 | if (EV_FD_COND_C(t->srv_fd, DIR_RD)) { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3735 | rep->rex = TICK_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3736 | } |
| 3737 | } |
| 3738 | else { |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 3739 | if (!tick_isset(rep->rex)) { |
| 3740 | EV_FD_COND_S(t->srv_fd, DIR_RD); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3741 | rep->rex = tick_add_ifset(now_ms, t->be->timeout.server); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3742 | } |
| 3743 | } |
| 3744 | |
| 3745 | return 0; /* other cases change nothing */ |
| 3746 | } |
| 3747 | else if (s == SV_STSHUTR) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3748 | if (req->flags & BF_WRITE_ERROR) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3749 | //EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 3750 | buffer_shutw_done(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3751 | fd_delete(t->srv_fd); |
| 3752 | if (t->srv) { |
| 3753 | t->srv->cur_sess--; |
| 3754 | t->srv->failed_resp++; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3755 | sess_change_server(t, NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3756 | } |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 3757 | t->be->failed_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3758 | //close(t->srv_fd); |
| 3759 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 3760 | rep->flags |= BF_MAY_FORWARD; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3761 | if (!(t->flags & SN_ERR_MASK)) |
| 3762 | t->flags |= SN_ERR_SRVCL; |
| 3763 | if (!(t->flags & SN_FINST_MASK)) |
| 3764 | t->flags |= SN_FINST_D; |
| 3765 | /* We used to have a free connection slot. Since we'll never use it, |
| 3766 | * we have to inform the server that it may be used by another session. |
| 3767 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3768 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3769 | process_srv_queue(t->srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3770 | |
| 3771 | return 1; |
| 3772 | } |
Willy Tarreau | 6468d92 | 2008-08-03 19:15:35 +0200 | [diff] [blame] | 3773 | else if (req->flags & BF_SHUTR_STATUS && (req->l == 0)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3774 | //EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 3775 | buffer_shutw_done(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3776 | fd_delete(t->srv_fd); |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 3777 | if (t->srv) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3778 | t->srv->cur_sess--; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3779 | sess_change_server(t, NULL); |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 3780 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3781 | //close(t->srv_fd); |
| 3782 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 3783 | rep->flags |= BF_MAY_FORWARD; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3784 | /* We used to have a free connection slot. Since we'll never use it, |
| 3785 | * we have to inform the server that it may be used by another session. |
| 3786 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3787 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3788 | process_srv_queue(t->srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3789 | |
| 3790 | return 1; |
| 3791 | } |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3792 | else if (tick_is_expired(req->wex, now_ms)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3793 | //EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 3794 | buffer_shutw_done(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3795 | fd_delete(t->srv_fd); |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 3796 | if (t->srv) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3797 | t->srv->cur_sess--; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3798 | sess_change_server(t, NULL); |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 3799 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3800 | //close(t->srv_fd); |
| 3801 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 3802 | rep->flags |= BF_MAY_FORWARD; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3803 | if (!(t->flags & SN_ERR_MASK)) |
| 3804 | t->flags |= SN_ERR_SRVTO; |
| 3805 | if (!(t->flags & SN_FINST_MASK)) |
| 3806 | t->flags |= SN_FINST_D; |
| 3807 | /* We used to have a free connection slot. Since we'll never use it, |
| 3808 | * we have to inform the server that it may be used by another session. |
| 3809 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3810 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3811 | process_srv_queue(t->srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3812 | |
| 3813 | return 1; |
| 3814 | } |
| 3815 | else if (req->l == 0) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3816 | if (EV_FD_COND_C(t->srv_fd, DIR_WR)) { |
| 3817 | /* stop writing */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3818 | req->wex = TICK_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3819 | } |
| 3820 | } |
| 3821 | else { /* buffer not empty */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 3822 | if (!tick_isset(req->wex)) { |
| 3823 | EV_FD_COND_S(t->srv_fd, DIR_WR); |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3824 | /* restart writing */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3825 | req->wex = tick_add_ifset(now_ms, t->be->timeout.server); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3826 | } |
| 3827 | } |
| 3828 | return 0; |
| 3829 | } |
| 3830 | else if (s == SV_STSHUTW) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3831 | if (rep->flags & BF_READ_ERROR) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3832 | //EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 3833 | buffer_shutr_done(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3834 | fd_delete(t->srv_fd); |
| 3835 | if (t->srv) { |
| 3836 | t->srv->cur_sess--; |
| 3837 | t->srv->failed_resp++; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3838 | sess_change_server(t, NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3839 | } |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 3840 | t->be->failed_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3841 | //close(t->srv_fd); |
| 3842 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 3843 | rep->flags |= BF_MAY_FORWARD; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3844 | if (!(t->flags & SN_ERR_MASK)) |
| 3845 | t->flags |= SN_ERR_SRVCL; |
| 3846 | if (!(t->flags & SN_FINST_MASK)) |
| 3847 | t->flags |= SN_FINST_D; |
| 3848 | /* We used to have a free connection slot. Since we'll never use it, |
| 3849 | * we have to inform the server that it may be used by another session. |
| 3850 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3851 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3852 | process_srv_queue(t->srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3853 | |
| 3854 | return 1; |
| 3855 | } |
Willy Tarreau | 6468d92 | 2008-08-03 19:15:35 +0200 | [diff] [blame] | 3856 | else if (rep->flags & (BF_READ_NULL | BF_SHUTW_STATUS)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3857 | //EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 3858 | buffer_shutr_done(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3859 | fd_delete(t->srv_fd); |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 3860 | if (t->srv) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3861 | t->srv->cur_sess--; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3862 | sess_change_server(t, NULL); |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 3863 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3864 | //close(t->srv_fd); |
| 3865 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 3866 | rep->flags |= BF_MAY_FORWARD; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3867 | /* We used to have a free connection slot. Since we'll never use it, |
| 3868 | * we have to inform the server that it may be used by another session. |
| 3869 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3870 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3871 | process_srv_queue(t->srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3872 | |
| 3873 | return 1; |
| 3874 | } |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3875 | else if (tick_is_expired(rep->rex, now_ms)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3876 | //EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | 89edf5e | 2008-08-03 17:25:14 +0200 | [diff] [blame] | 3877 | buffer_shutr_done(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3878 | fd_delete(t->srv_fd); |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 3879 | if (t->srv) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3880 | t->srv->cur_sess--; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3881 | sess_change_server(t, NULL); |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 3882 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3883 | //close(t->srv_fd); |
| 3884 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 3885 | rep->flags |= BF_MAY_FORWARD; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3886 | if (!(t->flags & SN_ERR_MASK)) |
| 3887 | t->flags |= SN_ERR_SRVTO; |
| 3888 | if (!(t->flags & SN_FINST_MASK)) |
| 3889 | t->flags |= SN_FINST_D; |
| 3890 | /* We used to have a free connection slot. Since we'll never use it, |
| 3891 | * we have to inform the server that it may be used by another session. |
| 3892 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3893 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3894 | process_srv_queue(t->srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3895 | |
| 3896 | return 1; |
| 3897 | } |
| 3898 | else if (rep->l == BUFSIZE) { /* no room to read more data */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3899 | if (EV_FD_COND_C(t->srv_fd, DIR_RD)) { |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3900 | rep->rex = TICK_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3901 | } |
| 3902 | } |
| 3903 | else { |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame^] | 3904 | if (!tick_isset(rep->rex)) { |
| 3905 | EV_FD_COND_S(t->srv_fd, DIR_RD); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 3906 | rep->rex = tick_add_ifset(now_ms, t->be->timeout.server); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3907 | } |
| 3908 | } |
| 3909 | return 0; |
| 3910 | } |
| 3911 | else { /* SV_STCLOSE : nothing to do */ |
| 3912 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 3913 | int len; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3914 | len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3915 | 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] | 3916 | write(1, trash, len); |
| 3917 | } |
| 3918 | return 0; |
| 3919 | } |
| 3920 | return 0; |
| 3921 | } |
| 3922 | |
| 3923 | |
| 3924 | /* |
| 3925 | * Produces data for the session <s> depending on its source. Expects to be |
| 3926 | * called with s->cli_state == CL_STSHUTR. Right now, only statistics can be |
| 3927 | * produced. It stops by itself by unsetting the SN_SELF_GEN flag from the |
| 3928 | * 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] | 3929 | * 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] | 3930 | * if it changes the session state from CL_STSHUTR, otherwise 0. |
| 3931 | */ |
| 3932 | int produce_content(struct session *s) |
| 3933 | { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3934 | if (s->data_source == DATA_SRC_NONE) { |
| 3935 | s->flags &= ~SN_SELF_GEN; |
| 3936 | return 1; |
| 3937 | } |
| 3938 | else if (s->data_source == DATA_SRC_STATS) { |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3939 | /* dump server statistics */ |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 3940 | int ret = stats_dump_http(s, s->be->uri_auth); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 3941 | if (ret >= 0) |
| 3942 | return ret; |
| 3943 | /* -1 indicates an error */ |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3944 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3945 | |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 3946 | /* unknown data source or internal error */ |
| 3947 | s->txn.status = 500; |
| 3948 | client_retnclose(s, error_message(s, HTTP_ERR_500)); |
| 3949 | if (!(s->flags & SN_ERR_MASK)) |
| 3950 | s->flags |= SN_ERR_PRXCOND; |
| 3951 | if (!(s->flags & SN_FINST_MASK)) |
| 3952 | s->flags |= SN_FINST_R; |
| 3953 | s->flags &= ~SN_SELF_GEN; |
| 3954 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3955 | } |
| 3956 | |
| 3957 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3958 | /* Iterate the same filter through all request headers. |
| 3959 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3960 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 3961 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3962 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3963 | 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] | 3964 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3965 | char term; |
| 3966 | char *cur_ptr, *cur_end, *cur_next; |
| 3967 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3968 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3969 | struct hdr_idx_elem *cur_hdr; |
| 3970 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 3971 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3972 | last_hdr = 0; |
| 3973 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3974 | 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] | 3975 | old_idx = 0; |
| 3976 | |
| 3977 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3978 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3979 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3980 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3981 | (exp->action == ACT_ALLOW || |
| 3982 | exp->action == ACT_DENY || |
| 3983 | exp->action == ACT_TARPIT)) |
| 3984 | return 0; |
| 3985 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3986 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3987 | if (!cur_idx) |
| 3988 | break; |
| 3989 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3990 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3991 | cur_ptr = cur_next; |
| 3992 | cur_end = cur_ptr + cur_hdr->len; |
| 3993 | cur_next = cur_end + cur_hdr->cr + 1; |
| 3994 | |
| 3995 | /* Now we have one header between cur_ptr and cur_end, |
| 3996 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3997 | */ |
| 3998 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3999 | /* The annoying part is that pattern matching needs |
| 4000 | * that we modify the contents to null-terminate all |
| 4001 | * strings before testing them. |
| 4002 | */ |
| 4003 | |
| 4004 | term = *cur_end; |
| 4005 | *cur_end = '\0'; |
| 4006 | |
| 4007 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4008 | switch (exp->action) { |
| 4009 | case ACT_SETBE: |
| 4010 | /* It is not possible to jump a second time. |
| 4011 | * FIXME: should we return an HTTP/500 here so that |
| 4012 | * the admin knows there's a problem ? |
| 4013 | */ |
| 4014 | if (t->be != t->fe) |
| 4015 | break; |
| 4016 | |
| 4017 | /* Swithing Proxy */ |
| 4018 | t->be = (struct proxy *) exp->replace; |
| 4019 | |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4020 | /* right now, the backend switch is not overly complicated |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4021 | * because we have associated req_cap and rsp_cap to the |
| 4022 | * frontend, and the beconn will be updated later. |
| 4023 | */ |
| 4024 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 4025 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 4026 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 4027 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4028 | last_hdr = 1; |
| 4029 | break; |
| 4030 | |
| 4031 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4032 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4033 | last_hdr = 1; |
| 4034 | break; |
| 4035 | |
| 4036 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4037 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4038 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4039 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4040 | break; |
| 4041 | |
| 4042 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4043 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4044 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4045 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4046 | break; |
| 4047 | |
| 4048 | case ACT_REPLACE: |
| 4049 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4050 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 4051 | /* FIXME: if the user adds a newline in the replacement, the |
| 4052 | * index will not be recalculated for now, and the new line |
| 4053 | * will not be counted as a new header. |
| 4054 | */ |
| 4055 | |
| 4056 | cur_end += delta; |
| 4057 | cur_next += delta; |
| 4058 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4059 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4060 | break; |
| 4061 | |
| 4062 | case ACT_REMOVE: |
| 4063 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 4064 | cur_next += delta; |
| 4065 | |
| 4066 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4067 | txn->req.eoh += delta; |
| 4068 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4069 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4070 | cur_hdr->len = 0; |
| 4071 | cur_end = NULL; /* null-term has been rewritten */ |
| 4072 | break; |
| 4073 | |
| 4074 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4075 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4076 | if (cur_end) |
| 4077 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4078 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4079 | /* keep the link from this header to next one in case of later |
| 4080 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4081 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4082 | old_idx = cur_idx; |
| 4083 | } |
| 4084 | return 0; |
| 4085 | } |
| 4086 | |
| 4087 | |
| 4088 | /* Apply the filter to the request line. |
| 4089 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 4090 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4091 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 4092 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4093 | */ |
| 4094 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 4095 | { |
| 4096 | char term; |
| 4097 | char *cur_ptr, *cur_end; |
| 4098 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4099 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4100 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4101 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4102 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4103 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4104 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4105 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4106 | (exp->action == ACT_ALLOW || |
| 4107 | exp->action == ACT_DENY || |
| 4108 | exp->action == ACT_TARPIT)) |
| 4109 | return 0; |
| 4110 | else if (exp->action == ACT_REMOVE) |
| 4111 | return 0; |
| 4112 | |
| 4113 | done = 0; |
| 4114 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4115 | 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] | 4116 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4117 | |
| 4118 | /* Now we have the request line between cur_ptr and cur_end */ |
| 4119 | |
| 4120 | /* The annoying part is that pattern matching needs |
| 4121 | * that we modify the contents to null-terminate all |
| 4122 | * strings before testing them. |
| 4123 | */ |
| 4124 | |
| 4125 | term = *cur_end; |
| 4126 | *cur_end = '\0'; |
| 4127 | |
| 4128 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4129 | switch (exp->action) { |
| 4130 | case ACT_SETBE: |
| 4131 | /* It is not possible to jump a second time. |
| 4132 | * FIXME: should we return an HTTP/500 here so that |
| 4133 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4134 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4135 | if (t->be != t->fe) |
| 4136 | break; |
| 4137 | |
| 4138 | /* Swithing Proxy */ |
| 4139 | t->be = (struct proxy *) exp->replace; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4140 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4141 | /* right now, the backend switch is not too much complicated |
| 4142 | * because we have associated req_cap and rsp_cap to the |
| 4143 | * frontend, and the beconn will be updated later. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4144 | */ |
| 4145 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 4146 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 4147 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 4148 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4149 | done = 1; |
| 4150 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4151 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4152 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4153 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4154 | done = 1; |
| 4155 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4156 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4157 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4158 | txn->flags |= TX_CLDENY; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4159 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4160 | done = 1; |
| 4161 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4162 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4163 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4164 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4165 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4166 | done = 1; |
| 4167 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4168 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4169 | case ACT_REPLACE: |
| 4170 | *cur_end = term; /* restore the string terminator */ |
| 4171 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4172 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 4173 | /* FIXME: if the user adds a newline in the replacement, the |
| 4174 | * index will not be recalculated for now, and the new line |
| 4175 | * will not be counted as a new header. |
| 4176 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4177 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4178 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4179 | cur_end += delta; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4180 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4181 | 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] | 4182 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4183 | HTTP_MSG_RQMETH, |
| 4184 | cur_ptr, cur_end + 1, |
| 4185 | NULL, NULL); |
| 4186 | if (unlikely(!cur_end)) |
| 4187 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4188 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4189 | /* we have a full request and we know that we have either a CR |
| 4190 | * or an LF at <ptr>. |
| 4191 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4192 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 4193 | 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] | 4194 | /* there is no point trying this regex on headers */ |
| 4195 | return 1; |
| 4196 | } |
| 4197 | } |
| 4198 | *cur_end = term; /* restore the string terminator */ |
| 4199 | return done; |
| 4200 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 4201 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4202 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4203 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4204 | /* |
| 4205 | * Apply all the req filters <exp> to all headers in buffer <req> of session <t>. |
| 4206 | * 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] | 4207 | * unparsable request. Since it can manage the switch to another backend, it |
| 4208 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4209 | */ |
| 4210 | int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 4211 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4212 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4213 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4214 | while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4215 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4216 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4217 | /* |
| 4218 | * The interleaving of transformations and verdicts |
| 4219 | * makes it difficult to decide to continue or stop |
| 4220 | * the evaluation. |
| 4221 | */ |
| 4222 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4223 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4224 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 4225 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) { |
| 4226 | exp = exp->next; |
| 4227 | continue; |
| 4228 | } |
| 4229 | |
| 4230 | /* Apply the filter to the request line. */ |
| 4231 | ret = apply_filter_to_req_line(t, req, exp); |
| 4232 | if (unlikely(ret < 0)) |
| 4233 | return -1; |
| 4234 | |
| 4235 | if (likely(ret == 0)) { |
| 4236 | /* The filter did not match the request, it can be |
| 4237 | * iterated through all headers. |
| 4238 | */ |
| 4239 | apply_filter_to_req_headers(t, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4240 | } |
| 4241 | exp = exp->next; |
| 4242 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4243 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4244 | } |
| 4245 | |
| 4246 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4247 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4248 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 4249 | * Manage client-side cookie. It can impact performance by about 2% so it is |
| 4250 | * desirable to call it only when needed. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4251 | */ |
| 4252 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 4253 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4254 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4255 | char *p1, *p2, *p3, *p4; |
| 4256 | char *del_colon, *del_cookie, *colon; |
| 4257 | int app_cookies; |
| 4258 | |
| 4259 | appsess *asession_temp = NULL; |
| 4260 | appsess local_asession; |
| 4261 | |
| 4262 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4263 | int cur_idx, old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4264 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4265 | /* Iterate through the headers. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4266 | * we start with the start line. |
| 4267 | */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 4268 | old_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4269 | 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] | 4270 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4271 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4272 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4273 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4274 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4275 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4276 | cur_ptr = cur_next; |
| 4277 | cur_end = cur_ptr + cur_hdr->len; |
| 4278 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4279 | |
| 4280 | /* We have one full header between cur_ptr and cur_end, and the |
| 4281 | * next header starts at cur_next. We're only interested in |
| 4282 | * "Cookie:" headers. |
| 4283 | */ |
| 4284 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4285 | val = http_header_match2(cur_ptr, cur_end, "Cookie", 6); |
| 4286 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4287 | old_idx = cur_idx; |
| 4288 | continue; |
| 4289 | } |
| 4290 | |
| 4291 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 4292 | * attributes whose name begin with a '$', and associate them with |
| 4293 | * the right cookie, if we want to delete this cookie. |
| 4294 | * So there are 3 cases for each cookie read : |
| 4295 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 4296 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 4297 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 4298 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 4299 | * "special" cookie. |
| 4300 | * At the end of loop, if a "special" cookie remains, we may have to |
| 4301 | * remove it. If no application cookie persists in the header, we |
| 4302 | * *MUST* delete it |
| 4303 | */ |
| 4304 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4305 | colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4306 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4307 | /* del_cookie == NULL => nothing to be deleted */ |
| 4308 | del_colon = del_cookie = NULL; |
| 4309 | app_cookies = 0; |
| 4310 | |
| 4311 | while (p1 < cur_end) { |
| 4312 | /* skip spaces and colons, but keep an eye on these ones */ |
| 4313 | while (p1 < cur_end) { |
| 4314 | if (*p1 == ';' || *p1 == ',') |
| 4315 | colon = p1; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4316 | else if (!isspace((unsigned char)*p1)) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4317 | break; |
| 4318 | p1++; |
| 4319 | } |
| 4320 | |
| 4321 | if (p1 == cur_end) |
| 4322 | break; |
| 4323 | |
| 4324 | /* p1 is at the beginning of the cookie name */ |
| 4325 | p2 = p1; |
| 4326 | while (p2 < cur_end && *p2 != '=') |
| 4327 | p2++; |
| 4328 | |
| 4329 | if (p2 == cur_end) |
| 4330 | break; |
| 4331 | |
| 4332 | p3 = p2 + 1; /* skips the '=' sign */ |
| 4333 | if (p3 == cur_end) |
| 4334 | break; |
| 4335 | |
| 4336 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4337 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4338 | p4++; |
| 4339 | |
| 4340 | /* here, we have the cookie name between p1 and p2, |
| 4341 | * and its value between p3 and p4. |
| 4342 | * we can process it : |
| 4343 | * |
| 4344 | * Cookie: NAME=VALUE; |
| 4345 | * | || || | |
| 4346 | * | || || +--> p4 |
| 4347 | * | || |+-------> p3 |
| 4348 | * | || +--------> p2 |
| 4349 | * | |+------------> p1 |
| 4350 | * | +-------------> colon |
| 4351 | * +--------------------> cur_ptr |
| 4352 | */ |
| 4353 | |
| 4354 | if (*p1 == '$') { |
| 4355 | /* skip this one */ |
| 4356 | } |
| 4357 | else { |
| 4358 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4359 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4360 | txn->cli_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4361 | (p4 - p1 >= t->fe->capture_namelen) && |
| 4362 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4363 | int log_len = p4 - p1; |
| 4364 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 4365 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4366 | Alert("HTTP logging : out of memory.\n"); |
| 4367 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4368 | if (log_len > t->fe->capture_len) |
| 4369 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4370 | memcpy(txn->cli_cookie, p1, log_len); |
| 4371 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4372 | } |
| 4373 | } |
| 4374 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4375 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 4376 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4377 | /* Cool... it's the right one */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4378 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4379 | char *delim; |
| 4380 | |
| 4381 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 4382 | * have the server ID betweek p3 and delim, and the original cookie between |
| 4383 | * delim+1 and p4. Otherwise, delim==p4 : |
| 4384 | * |
| 4385 | * Cookie: NAME=SRV~VALUE; |
| 4386 | * | || || | | |
| 4387 | * | || || | +--> p4 |
| 4388 | * | || || +--------> delim |
| 4389 | * | || |+-----------> p3 |
| 4390 | * | || +------------> p2 |
| 4391 | * | |+----------------> p1 |
| 4392 | * | +-----------------> colon |
| 4393 | * +------------------------> cur_ptr |
| 4394 | */ |
| 4395 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4396 | if (t->be->options & PR_O_COOK_PFX) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4397 | for (delim = p3; delim < p4; delim++) |
| 4398 | if (*delim == COOKIE_DELIM) |
| 4399 | break; |
| 4400 | } |
| 4401 | else |
| 4402 | delim = p4; |
| 4403 | |
| 4404 | |
| 4405 | /* Here, we'll look for the first running server which supports the cookie. |
| 4406 | * This allows to share a same cookie between several servers, for example |
| 4407 | * to dedicate backup servers to specific servers only. |
| 4408 | * However, to prevent clients from sticking to cookie-less backup server |
| 4409 | * when they have incidentely learned an empty cookie, we simply ignore |
| 4410 | * empty cookies and mark them as invalid. |
| 4411 | */ |
| 4412 | if (delim == p3) |
| 4413 | srv = NULL; |
| 4414 | |
| 4415 | while (srv) { |
Willy Tarreau | 92f2ab1 | 2007-02-02 22:14:47 +0100 | [diff] [blame] | 4416 | if (srv->cookie && (srv->cklen == delim - p3) && |
| 4417 | !memcmp(p3, srv->cookie, delim - p3)) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4418 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4419 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4420 | txn->flags &= ~TX_CK_MASK; |
| 4421 | txn->flags |= TX_CK_VALID; |
| 4422 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4423 | t->srv = srv; |
| 4424 | break; |
| 4425 | } else { |
| 4426 | /* we found a server, but it's down */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4427 | txn->flags &= ~TX_CK_MASK; |
| 4428 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4429 | } |
| 4430 | } |
| 4431 | srv = srv->next; |
| 4432 | } |
| 4433 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4434 | if (!srv && !(txn->flags & TX_CK_DOWN)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4435 | /* no server matched this cookie */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4436 | txn->flags &= ~TX_CK_MASK; |
| 4437 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4438 | } |
| 4439 | |
| 4440 | /* depending on the cookie mode, we may have to either : |
| 4441 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 4442 | * the server never sees it ; |
| 4443 | * - remove the server id from the cookie value, and tag the cookie as an |
| 4444 | * application cookie so that it does not get accidentely removed later, |
| 4445 | * if we're in cookie prefix mode |
| 4446 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4447 | if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4448 | int delta; /* negative */ |
| 4449 | |
| 4450 | delta = buffer_replace2(req, p3, delim + 1, NULL, 0); |
| 4451 | p4 += delta; |
| 4452 | cur_end += delta; |
| 4453 | cur_next += delta; |
| 4454 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4455 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4456 | |
| 4457 | del_cookie = del_colon = NULL; |
| 4458 | app_cookies++; /* protect the header from deletion */ |
| 4459 | } |
| 4460 | else if (del_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4461 | (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] | 4462 | del_cookie = p1; |
| 4463 | del_colon = colon; |
| 4464 | } |
| 4465 | } else { |
| 4466 | /* now we know that we must keep this cookie since it's |
| 4467 | * not ours. But if we wanted to delete our cookie |
| 4468 | * earlier, we cannot remove the complete header, but we |
| 4469 | * can remove the previous block itself. |
| 4470 | */ |
| 4471 | app_cookies++; |
| 4472 | |
| 4473 | if (del_cookie != NULL) { |
| 4474 | int delta; /* negative */ |
| 4475 | |
| 4476 | delta = buffer_replace2(req, del_cookie, p1, NULL, 0); |
| 4477 | p4 += delta; |
| 4478 | cur_end += delta; |
| 4479 | cur_next += delta; |
| 4480 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4481 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4482 | del_cookie = del_colon = NULL; |
| 4483 | } |
| 4484 | } |
| 4485 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4486 | if ((t->be->appsession_name != NULL) && |
| 4487 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4488 | /* first, let's see if the cookie is our appcookie*/ |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4489 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4490 | /* Cool... it's the right one */ |
| 4491 | |
| 4492 | asession_temp = &local_asession; |
| 4493 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4494 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4495 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 4496 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 4497 | return; |
| 4498 | } |
| 4499 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4500 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 4501 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4502 | asession_temp->serverid = NULL; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4503 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4504 | /* only do insert, if lookup fails */ |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4505 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 4506 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4507 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4508 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4509 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4510 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 4511 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 4512 | return; |
| 4513 | } |
| 4514 | |
| 4515 | asession_temp->sessid = local_asession.sessid; |
| 4516 | asession_temp->serverid = local_asession.serverid; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4517 | asession_temp->request_count = 0; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4518 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4519 | } else { |
| 4520 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4521 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4522 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4523 | if (asession_temp->serverid == NULL) { |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4524 | /* TODO redispatch request */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4525 | Alert("Found Application Session without matching server.\n"); |
| 4526 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4527 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4528 | while (srv) { |
| 4529 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4530 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4531 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4532 | txn->flags &= ~TX_CK_MASK; |
| 4533 | txn->flags |= TX_CK_VALID; |
| 4534 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4535 | t->srv = srv; |
| 4536 | break; |
| 4537 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4538 | txn->flags &= ~TX_CK_MASK; |
| 4539 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4540 | } |
| 4541 | } |
| 4542 | srv = srv->next; |
| 4543 | }/* end while(srv) */ |
| 4544 | }/* end else if server == NULL */ |
| 4545 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 4546 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4547 | asession_temp->request_count++; |
| 4548 | #if defined(DEBUG_HASH) |
| 4549 | Alert("manage_client_side_cookies\n"); |
| 4550 | appsession_hash_dump(&(t->be->htbl_proxy)); |
| 4551 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4552 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 4553 | } |
| 4554 | |
| 4555 | /* we'll have to look for another cookie ... */ |
| 4556 | p1 = p4; |
| 4557 | } /* while (p1 < cur_end) */ |
| 4558 | |
| 4559 | /* There's no more cookie on this line. |
| 4560 | * We may have marked the last one(s) for deletion. |
| 4561 | * We must do this now in two ways : |
| 4562 | * - if there is no app cookie, we simply delete the header ; |
| 4563 | * - if there are app cookies, we must delete the end of the |
| 4564 | * string properly, including the colon/semi-colon before |
| 4565 | * the cookie name. |
| 4566 | */ |
| 4567 | if (del_cookie != NULL) { |
| 4568 | int delta; |
| 4569 | if (app_cookies) { |
| 4570 | delta = buffer_replace2(req, del_colon, cur_end, NULL, 0); |
| 4571 | cur_end = del_colon; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4572 | cur_hdr->len += delta; |
| 4573 | } else { |
| 4574 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4575 | |
| 4576 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4577 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4578 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4579 | cur_hdr->len = 0; |
| 4580 | } |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 4581 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4582 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4583 | } |
| 4584 | |
| 4585 | /* keep the link from this header to next one */ |
| 4586 | old_idx = cur_idx; |
| 4587 | } /* end of cookie processing on this header */ |
| 4588 | } |
| 4589 | |
| 4590 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4591 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 4592 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 4593 | */ |
| 4594 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4595 | { |
| 4596 | char term; |
| 4597 | char *cur_ptr, *cur_end, *cur_next; |
| 4598 | int cur_idx, old_idx, last_hdr; |
| 4599 | struct http_txn *txn = &t->txn; |
| 4600 | struct hdr_idx_elem *cur_hdr; |
| 4601 | int len, delta; |
| 4602 | |
| 4603 | last_hdr = 0; |
| 4604 | |
| 4605 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4606 | old_idx = 0; |
| 4607 | |
| 4608 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4609 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4610 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4611 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4612 | (exp->action == ACT_ALLOW || |
| 4613 | exp->action == ACT_DENY)) |
| 4614 | return 0; |
| 4615 | |
| 4616 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 4617 | if (!cur_idx) |
| 4618 | break; |
| 4619 | |
| 4620 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4621 | cur_ptr = cur_next; |
| 4622 | cur_end = cur_ptr + cur_hdr->len; |
| 4623 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4624 | |
| 4625 | /* Now we have one header between cur_ptr and cur_end, |
| 4626 | * and the next header starts at cur_next. |
| 4627 | */ |
| 4628 | |
| 4629 | /* The annoying part is that pattern matching needs |
| 4630 | * that we modify the contents to null-terminate all |
| 4631 | * strings before testing them. |
| 4632 | */ |
| 4633 | |
| 4634 | term = *cur_end; |
| 4635 | *cur_end = '\0'; |
| 4636 | |
| 4637 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4638 | switch (exp->action) { |
| 4639 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4640 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4641 | last_hdr = 1; |
| 4642 | break; |
| 4643 | |
| 4644 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4645 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4646 | last_hdr = 1; |
| 4647 | break; |
| 4648 | |
| 4649 | case ACT_REPLACE: |
| 4650 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4651 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 4652 | /* FIXME: if the user adds a newline in the replacement, the |
| 4653 | * index will not be recalculated for now, and the new line |
| 4654 | * will not be counted as a new header. |
| 4655 | */ |
| 4656 | |
| 4657 | cur_end += delta; |
| 4658 | cur_next += delta; |
| 4659 | cur_hdr->len += delta; |
| 4660 | txn->rsp.eoh += delta; |
| 4661 | break; |
| 4662 | |
| 4663 | case ACT_REMOVE: |
| 4664 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 4665 | cur_next += delta; |
| 4666 | |
| 4667 | /* FIXME: this should be a separate function */ |
| 4668 | txn->rsp.eoh += delta; |
| 4669 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4670 | txn->hdr_idx.used--; |
| 4671 | cur_hdr->len = 0; |
| 4672 | cur_end = NULL; /* null-term has been rewritten */ |
| 4673 | break; |
| 4674 | |
| 4675 | } |
| 4676 | } |
| 4677 | if (cur_end) |
| 4678 | *cur_end = term; /* restore the string terminator */ |
| 4679 | |
| 4680 | /* keep the link from this header to next one in case of later |
| 4681 | * removal of next header. |
| 4682 | */ |
| 4683 | old_idx = cur_idx; |
| 4684 | } |
| 4685 | return 0; |
| 4686 | } |
| 4687 | |
| 4688 | |
| 4689 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 4690 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 4691 | * or -1 if a replacement resulted in an invalid status line. |
| 4692 | */ |
| 4693 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4694 | { |
| 4695 | char term; |
| 4696 | char *cur_ptr, *cur_end; |
| 4697 | int done; |
| 4698 | struct http_txn *txn = &t->txn; |
| 4699 | int len, delta; |
| 4700 | |
| 4701 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4702 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4703 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4704 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4705 | (exp->action == ACT_ALLOW || |
| 4706 | exp->action == ACT_DENY)) |
| 4707 | return 0; |
| 4708 | else if (exp->action == ACT_REMOVE) |
| 4709 | return 0; |
| 4710 | |
| 4711 | done = 0; |
| 4712 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4713 | 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] | 4714 | cur_end = cur_ptr + txn->rsp.sl.rq.l; |
| 4715 | |
| 4716 | /* Now we have the status line between cur_ptr and cur_end */ |
| 4717 | |
| 4718 | /* The annoying part is that pattern matching needs |
| 4719 | * that we modify the contents to null-terminate all |
| 4720 | * strings before testing them. |
| 4721 | */ |
| 4722 | |
| 4723 | term = *cur_end; |
| 4724 | *cur_end = '\0'; |
| 4725 | |
| 4726 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4727 | switch (exp->action) { |
| 4728 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4729 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4730 | done = 1; |
| 4731 | break; |
| 4732 | |
| 4733 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4734 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4735 | done = 1; |
| 4736 | break; |
| 4737 | |
| 4738 | case ACT_REPLACE: |
| 4739 | *cur_end = term; /* restore the string terminator */ |
| 4740 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4741 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 4742 | /* FIXME: if the user adds a newline in the replacement, the |
| 4743 | * index will not be recalculated for now, and the new line |
| 4744 | * will not be counted as a new header. |
| 4745 | */ |
| 4746 | |
| 4747 | txn->rsp.eoh += delta; |
| 4748 | cur_end += delta; |
| 4749 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4750 | 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] | 4751 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 4752 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4753 | cur_ptr, cur_end + 1, |
| 4754 | NULL, NULL); |
| 4755 | if (unlikely(!cur_end)) |
| 4756 | return -1; |
| 4757 | |
| 4758 | /* we have a full respnse and we know that we have either a CR |
| 4759 | * or an LF at <ptr>. |
| 4760 | */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4761 | 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] | 4762 | hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r'); |
| 4763 | /* there is no point trying this regex on headers */ |
| 4764 | return 1; |
| 4765 | } |
| 4766 | } |
| 4767 | *cur_end = term; /* restore the string terminator */ |
| 4768 | return done; |
| 4769 | } |
| 4770 | |
| 4771 | |
| 4772 | |
| 4773 | /* |
| 4774 | * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>. |
| 4775 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 4776 | * unparsable response. |
| 4777 | */ |
| 4778 | int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4779 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4780 | struct http_txn *txn = &t->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4781 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4782 | while (exp && !(txn->flags & TX_SVDENY)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4783 | int ret; |
| 4784 | |
| 4785 | /* |
| 4786 | * The interleaving of transformations and verdicts |
| 4787 | * makes it difficult to decide to continue or stop |
| 4788 | * the evaluation. |
| 4789 | */ |
| 4790 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4791 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4792 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 4793 | exp->action == ACT_PASS)) { |
| 4794 | exp = exp->next; |
| 4795 | continue; |
| 4796 | } |
| 4797 | |
| 4798 | /* Apply the filter to the status line. */ |
| 4799 | ret = apply_filter_to_sts_line(t, rtr, exp); |
| 4800 | if (unlikely(ret < 0)) |
| 4801 | return -1; |
| 4802 | |
| 4803 | if (likely(ret == 0)) { |
| 4804 | /* The filter did not match the response, it can be |
| 4805 | * iterated through all headers. |
| 4806 | */ |
| 4807 | apply_filter_to_resp_headers(t, rtr, exp); |
| 4808 | } |
| 4809 | exp = exp->next; |
| 4810 | } |
| 4811 | return 0; |
| 4812 | } |
| 4813 | |
| 4814 | |
| 4815 | |
| 4816 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 4817 | * Manage server-side cookies. It can impact performance by about 2% so it is |
| 4818 | * desirable to call it only when needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4819 | */ |
| 4820 | void manage_server_side_cookies(struct session *t, struct buffer *rtr) |
| 4821 | { |
| 4822 | struct http_txn *txn = &t->txn; |
| 4823 | char *p1, *p2, *p3, *p4; |
| 4824 | |
| 4825 | appsess *asession_temp = NULL; |
| 4826 | appsess local_asession; |
| 4827 | |
| 4828 | char *cur_ptr, *cur_end, *cur_next; |
| 4829 | int cur_idx, old_idx, delta; |
| 4830 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4831 | /* Iterate through the headers. |
| 4832 | * we start with the start line. |
| 4833 | */ |
| 4834 | old_idx = 0; |
| 4835 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4836 | |
| 4837 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 4838 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4839 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4840 | |
| 4841 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4842 | cur_ptr = cur_next; |
| 4843 | cur_end = cur_ptr + cur_hdr->len; |
| 4844 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4845 | |
| 4846 | /* We have one full header between cur_ptr and cur_end, and the |
| 4847 | * next header starts at cur_next. We're only interested in |
| 4848 | * "Cookie:" headers. |
| 4849 | */ |
| 4850 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4851 | val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10); |
| 4852 | if (!val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4853 | old_idx = cur_idx; |
| 4854 | continue; |
| 4855 | } |
| 4856 | |
| 4857 | /* 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] | 4858 | txn->flags |= TX_SCK_ANY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4859 | |
| 4860 | |
| 4861 | /* maybe we only wanted to see if there was a set-cookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4862 | if (t->be->cookie_name == NULL && |
| 4863 | t->be->appsession_name == NULL && |
| 4864 | t->be->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4865 | return; |
| 4866 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4867 | p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4868 | |
| 4869 | 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] | 4870 | if (p1 == cur_end || *p1 == ';') /* end of cookie */ |
| 4871 | break; |
| 4872 | |
| 4873 | /* p1 is at the beginning of the cookie name */ |
| 4874 | p2 = p1; |
| 4875 | |
| 4876 | while (p2 < cur_end && *p2 != '=' && *p2 != ';') |
| 4877 | p2++; |
| 4878 | |
| 4879 | if (p2 == cur_end || *p2 == ';') /* next cookie */ |
| 4880 | break; |
| 4881 | |
| 4882 | p3 = p2 + 1; /* skip the '=' sign */ |
| 4883 | if (p3 == cur_end) |
| 4884 | break; |
| 4885 | |
| 4886 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4887 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';') |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4888 | p4++; |
| 4889 | |
| 4890 | /* here, we have the cookie name between p1 and p2, |
| 4891 | * and its value between p3 and p4. |
| 4892 | * we can process it. |
| 4893 | */ |
| 4894 | |
| 4895 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4896 | if (t->be->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4897 | txn->srv_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4898 | (p4 - p1 >= t->be->capture_namelen) && |
| 4899 | memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4900 | int log_len = p4 - p1; |
| 4901 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 4902 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4903 | Alert("HTTP logging : out of memory.\n"); |
| 4904 | } |
| 4905 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4906 | if (log_len > t->be->capture_len) |
| 4907 | log_len = t->be->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4908 | memcpy(txn->srv_cookie, p1, log_len); |
| 4909 | txn->srv_cookie[log_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4910 | } |
| 4911 | |
| 4912 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4913 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 4914 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4915 | /* Cool... it's the right one */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4916 | txn->flags |= TX_SCK_SEEN; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4917 | |
| 4918 | /* If the cookie is in insert mode on a known server, we'll delete |
| 4919 | * this occurrence because we'll insert another one later. |
| 4920 | * We'll delete it too if the "indirect" option is set and we're in |
| 4921 | * a direct access. */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4922 | if (((t->srv) && (t->be->options & PR_O_COOK_INS)) || |
| 4923 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4924 | /* this header must be deleted */ |
| 4925 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 4926 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4927 | txn->hdr_idx.used--; |
| 4928 | cur_hdr->len = 0; |
| 4929 | cur_next += delta; |
| 4930 | txn->rsp.eoh += delta; |
| 4931 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4932 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4933 | } |
| 4934 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4935 | (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4936 | /* replace bytes p3->p4 with the cookie name associated |
| 4937 | * with this server since we know it. |
| 4938 | */ |
| 4939 | delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen); |
| 4940 | cur_hdr->len += delta; |
| 4941 | cur_next += delta; |
| 4942 | txn->rsp.eoh += delta; |
| 4943 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4944 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4945 | } |
| 4946 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4947 | (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4948 | /* insert the cookie name associated with this server |
| 4949 | * before existing cookie, and insert a delimitor between them.. |
| 4950 | */ |
| 4951 | delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1); |
| 4952 | cur_hdr->len += delta; |
| 4953 | cur_next += delta; |
| 4954 | txn->rsp.eoh += delta; |
| 4955 | |
| 4956 | p3[t->srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4957 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4958 | } |
| 4959 | } |
| 4960 | /* next, let's see if the cookie is our appcookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4961 | else if ((t->be->appsession_name != NULL) && |
| 4962 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4963 | |
| 4964 | /* Cool... it's the right one */ |
| 4965 | |
| 4966 | size_t server_id_len = strlen(t->srv->id) + 1; |
| 4967 | asession_temp = &local_asession; |
| 4968 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4969 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4970 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4971 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4972 | return; |
| 4973 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4974 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 4975 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4976 | asession_temp->serverid = NULL; |
| 4977 | |
| 4978 | /* only do insert, if lookup fails */ |
Ryan Warnick | 6d0b1fa | 2008-02-17 11:24:35 +0100 | [diff] [blame] | 4979 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 4980 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4981 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4982 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 4983 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 4984 | return; |
| 4985 | } |
| 4986 | asession_temp->sessid = local_asession.sessid; |
| 4987 | asession_temp->serverid = local_asession.serverid; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4988 | asession_temp->request_count = 0; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4989 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
| 4990 | } else { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4991 | /* free wasted memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4992 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4993 | } |
| 4994 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4995 | if (asession_temp->serverid == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4996 | if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4997 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4998 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4999 | return; |
| 5000 | } |
| 5001 | asession_temp->serverid[0] = '\0'; |
| 5002 | } |
| 5003 | |
| 5004 | if (asession_temp->serverid[0] == '\0') |
| 5005 | memcpy(asession_temp->serverid, t->srv->id, server_id_len); |
| 5006 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 5007 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5008 | asession_temp->request_count++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5009 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5010 | Alert("manage_server_side_cookies\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5011 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5012 | #endif |
| 5013 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 5014 | break; /* we don't want to loop again since there cannot be another cookie on the same line */ |
| 5015 | } /* we're now at the end of the cookie value */ |
| 5016 | |
| 5017 | /* keep the link from this header to next one */ |
| 5018 | old_idx = cur_idx; |
| 5019 | } /* end of cookie processing on this header */ |
| 5020 | } |
| 5021 | |
| 5022 | |
| 5023 | |
| 5024 | /* |
| 5025 | * Check if response is cacheable or not. Updates t->flags. |
| 5026 | */ |
| 5027 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 5028 | { |
| 5029 | struct http_txn *txn = &t->txn; |
| 5030 | char *p1, *p2; |
| 5031 | |
| 5032 | char *cur_ptr, *cur_end, *cur_next; |
| 5033 | int cur_idx; |
| 5034 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 5035 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5036 | return; |
| 5037 | |
| 5038 | /* Iterate through the headers. |
| 5039 | * we start with the start line. |
| 5040 | */ |
| 5041 | cur_idx = 0; |
| 5042 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 5043 | |
| 5044 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 5045 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5046 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5047 | |
| 5048 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 5049 | cur_ptr = cur_next; |
| 5050 | cur_end = cur_ptr + cur_hdr->len; |
| 5051 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5052 | |
| 5053 | /* We have one full header between cur_ptr and cur_end, and the |
| 5054 | * next header starts at cur_next. We're only interested in |
| 5055 | * "Cookie:" headers. |
| 5056 | */ |
| 5057 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5058 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 5059 | if (val) { |
| 5060 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 5061 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 5062 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 5063 | return; |
| 5064 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5065 | } |
| 5066 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5067 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 5068 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5069 | continue; |
| 5070 | |
| 5071 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 5072 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5073 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5074 | |
| 5075 | if (p1 >= cur_end) /* no more info */ |
| 5076 | continue; |
| 5077 | |
| 5078 | /* p1 is at the beginning of the value */ |
| 5079 | p2 = p1; |
| 5080 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 5081 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5082 | p2++; |
| 5083 | |
| 5084 | /* we have a complete value between p1 and p2 */ |
| 5085 | if (p2 < cur_end && *p2 == '=') { |
| 5086 | /* we have something of the form no-cache="set-cookie" */ |
| 5087 | if ((cur_end - p1 >= 21) && |
| 5088 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 5089 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5090 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5091 | continue; |
| 5092 | } |
| 5093 | |
| 5094 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 5095 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 5096 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 5097 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 5098 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5099 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5100 | return; |
| 5101 | } |
| 5102 | |
| 5103 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5104 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5105 | continue; |
| 5106 | } |
| 5107 | } |
| 5108 | } |
| 5109 | |
| 5110 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5111 | /* |
| 5112 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 5113 | * If the server is found, it's assigned to the session. |
| 5114 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5115 | 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] | 5116 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5117 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5118 | appsess *asession_temp = NULL; |
| 5119 | appsess local_asession; |
| 5120 | char *request_line; |
| 5121 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5122 | if (t->be->appsession_name == NULL || |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 5123 | (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) || |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5124 | (request_line = memchr(begin, ';', len)) == NULL || |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5125 | ((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] | 5126 | return; |
| 5127 | |
| 5128 | /* skip ';' */ |
| 5129 | request_line++; |
| 5130 | |
| 5131 | /* look if we have a jsessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5132 | 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] | 5133 | return; |
| 5134 | |
| 5135 | /* skip jsessionid= */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5136 | request_line += t->be->appsession_name_len + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5137 | |
| 5138 | /* First try if we already have an appsession */ |
| 5139 | asession_temp = &local_asession; |
| 5140 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5141 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5142 | Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n"); |
| 5143 | send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n"); |
| 5144 | return; |
| 5145 | } |
| 5146 | |
| 5147 | /* Copy the sessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5148 | memcpy(asession_temp->sessid, request_line, t->be->appsession_len); |
| 5149 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5150 | asession_temp->serverid = NULL; |
| 5151 | |
| 5152 | /* only do insert, if lookup fails */ |
Ryan Warnick | 6d0b1fa | 2008-02-17 11:24:35 +0100 | [diff] [blame] | 5153 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 5154 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5155 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5156 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5157 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5158 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 5159 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 5160 | return; |
| 5161 | } |
| 5162 | asession_temp->sessid = local_asession.sessid; |
| 5163 | asession_temp->serverid = local_asession.serverid; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5164 | asession_temp->request_count=0; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5165 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5166 | } |
| 5167 | else { |
| 5168 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5169 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5170 | } |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5171 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 5172 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5173 | asession_temp->request_count++; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5174 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5175 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5176 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5177 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5178 | #endif |
| 5179 | if (asession_temp->serverid == NULL) { |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5180 | /* TODO redispatch request */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5181 | Alert("Found Application Session without matching server.\n"); |
| 5182 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5183 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5184 | while (srv) { |
| 5185 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5186 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5187 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5188 | txn->flags &= ~TX_CK_MASK; |
| 5189 | txn->flags |= TX_CK_VALID; |
| 5190 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5191 | t->srv = srv; |
| 5192 | break; |
| 5193 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5194 | txn->flags &= ~TX_CK_MASK; |
| 5195 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5196 | } |
| 5197 | } |
| 5198 | srv = srv->next; |
| 5199 | } |
| 5200 | } |
| 5201 | } |
| 5202 | |
| 5203 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5204 | /* |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5205 | * In a GET or HEAD request, check if the requested URI matches the stats uri |
| 5206 | * 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] | 5207 | * |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5208 | * 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] | 5209 | * 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] | 5210 | * produce_content() can be called to start sending data. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5211 | * |
| 5212 | * Returns 1 if the session's state changes, otherwise 0. |
| 5213 | */ |
| 5214 | int stats_check_uri_auth(struct session *t, struct proxy *backend) |
| 5215 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5216 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5217 | struct uri_auth *uri_auth = backend->uri_auth; |
| 5218 | struct user_auth *user; |
| 5219 | int authenticated, cur_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5220 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5221 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5222 | memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats)); |
| 5223 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5224 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5225 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5226 | return 0; |
| 5227 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5228 | h = t->req->data + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5229 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5230 | /* the URI is in h */ |
| 5231 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5232 | return 0; |
| 5233 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 5234 | h += uri_auth->uri_len; |
| 5235 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) { |
| 5236 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5237 | t->data_ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 5238 | break; |
| 5239 | } |
| 5240 | h++; |
| 5241 | } |
| 5242 | |
| 5243 | if (uri_auth->refresh) { |
| 5244 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 5245 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) { |
| 5246 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5247 | t->data_ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 5248 | break; |
| 5249 | } |
| 5250 | h++; |
| 5251 | } |
| 5252 | } |
| 5253 | |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 5254 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 5255 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) { |
| 5256 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5257 | t->data_ctx.stats.flags |= STAT_FMT_CSV; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 5258 | break; |
| 5259 | } |
| 5260 | h++; |
| 5261 | } |
| 5262 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5263 | t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO; |
| 5264 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5265 | /* we are in front of a interceptable URI. Let's check |
| 5266 | * if there's an authentication and if it's valid. |
| 5267 | */ |
| 5268 | user = uri_auth->users; |
| 5269 | if (!user) { |
| 5270 | /* no user auth required, it's OK */ |
| 5271 | authenticated = 1; |
| 5272 | } else { |
| 5273 | authenticated = 0; |
| 5274 | |
| 5275 | /* a user list is defined, we have to check. |
| 5276 | * skip 21 chars for "Authorization: Basic ". |
| 5277 | */ |
| 5278 | |
| 5279 | /* FIXME: this should move to an earlier place */ |
| 5280 | cur_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5281 | h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 5282 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 5283 | int len = txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5284 | if (len > 14 && |
| 5285 | !strncasecmp("Authorization:", h, 14)) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5286 | txn->auth_hdr.str = h; |
| 5287 | txn->auth_hdr.len = len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5288 | break; |
| 5289 | } |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5290 | h += len + txn->hdr_idx.v[cur_idx].cr + 1; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5291 | } |
| 5292 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5293 | if (txn->auth_hdr.len < 21 || |
| 5294 | memcmp(txn->auth_hdr.str + 14, " Basic ", 7)) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5295 | user = NULL; |
| 5296 | |
| 5297 | while (user) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5298 | if ((txn->auth_hdr.len == user->user_len + 14 + 7) |
| 5299 | && !memcmp(txn->auth_hdr.str + 14 + 7, |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5300 | user->user_pwd, user->user_len)) { |
| 5301 | authenticated = 1; |
| 5302 | break; |
| 5303 | } |
| 5304 | user = user->next; |
| 5305 | } |
| 5306 | } |
| 5307 | |
| 5308 | if (!authenticated) { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5309 | struct chunk msg; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5310 | |
| 5311 | /* no need to go further */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5312 | msg.str = trash; |
| 5313 | msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm); |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5314 | txn->status = 401; |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5315 | client_retnclose(t, &msg); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 5316 | t->analysis &= ~AN_REQ_ANY; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5317 | if (!(t->flags & SN_ERR_MASK)) |
| 5318 | t->flags |= SN_ERR_PRXCOND; |
| 5319 | if (!(t->flags & SN_FINST_MASK)) |
| 5320 | t->flags |= SN_FINST_R; |
| 5321 | return 1; |
| 5322 | } |
| 5323 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5324 | /* The request is valid, the user is authenticated. Let's start sending |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5325 | * data. |
| 5326 | */ |
Willy Tarreau | 284c7b3 | 2008-06-29 16:38:43 +0200 | [diff] [blame] | 5327 | EV_FD_CLR(t->cli_fd, DIR_RD); |
| 5328 | buffer_shutr(t->req); |
| 5329 | buffer_shutr(t->rep); |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5330 | t->cli_state = CL_STSHUTR; |
| 5331 | t->req->rlim = t->req->data + BUFSIZE; /* no more rewrite needed */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 5332 | t->logs.tv_request = now; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5333 | t->data_source = DATA_SRC_STATS; |
| 5334 | t->data_state = DATA_ST_INIT; |
Willy Tarreau | 91e9993 | 2008-06-30 07:51:00 +0200 | [diff] [blame] | 5335 | t->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5336 | produce_content(t); |
| 5337 | return 1; |
| 5338 | } |
| 5339 | |
| 5340 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5341 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5342 | * Print a debug line with a header |
| 5343 | */ |
| 5344 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 5345 | { |
| 5346 | int len, max; |
| 5347 | len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id, |
| 5348 | dir, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd); |
| 5349 | max = end - start; |
| 5350 | UBOUND(max, sizeof(trash) - len - 1); |
| 5351 | len += strlcpy2(trash + len, start, max + 1); |
| 5352 | trash[len++] = '\n'; |
| 5353 | write(1, trash, len); |
| 5354 | } |
| 5355 | |
| 5356 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5357 | /************************************************************************/ |
| 5358 | /* The code below is dedicated to ACL parsing and matching */ |
| 5359 | /************************************************************************/ |
| 5360 | |
| 5361 | |
| 5362 | |
| 5363 | |
| 5364 | /* 1. Check on METHOD |
| 5365 | * We use the pre-parsed method if it is known, and store its number as an |
| 5366 | * integer. If it is unknown, we use the pointer and the length. |
| 5367 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5368 | 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] | 5369 | { |
| 5370 | int len, meth; |
| 5371 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5372 | len = strlen(*text); |
| 5373 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5374 | |
| 5375 | pattern->val.i = meth; |
| 5376 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5377 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5378 | if (!pattern->ptr.str) |
| 5379 | return 0; |
| 5380 | pattern->len = len; |
| 5381 | } |
| 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_meth(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 | int meth; |
| 5390 | struct http_txn *txn = l7; |
| 5391 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5392 | if (!txn) |
| 5393 | return 0; |
| 5394 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5395 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5396 | return 0; |
| 5397 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5398 | meth = txn->meth; |
| 5399 | test->i = meth; |
| 5400 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5401 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5402 | /* ensure the indexes are not affected */ |
| 5403 | return 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5404 | test->len = txn->req.sl.rq.m_l; |
| 5405 | test->ptr = txn->req.sol; |
| 5406 | } |
| 5407 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5408 | return 1; |
| 5409 | } |
| 5410 | |
| 5411 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 5412 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 5413 | int icase; |
| 5414 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5415 | if (test->i != pattern->val.i) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5416 | return ACL_PAT_FAIL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5417 | |
| 5418 | if (test->i != HTTP_METH_OTHER) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5419 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5420 | |
| 5421 | /* Other method, we must compare the strings */ |
| 5422 | if (pattern->len != test->len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5423 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 5424 | |
| 5425 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 5426 | if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) || |
| 5427 | (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5428 | return ACL_PAT_FAIL; |
| 5429 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5430 | } |
| 5431 | |
| 5432 | /* 2. Check on Request/Status Version |
| 5433 | * We simply compare strings here. |
| 5434 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5435 | 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] | 5436 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5437 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5438 | if (!pattern->ptr.str) |
| 5439 | return 0; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5440 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5441 | return 1; |
| 5442 | } |
| 5443 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5444 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5445 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5446 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5447 | { |
| 5448 | struct http_txn *txn = l7; |
| 5449 | char *ptr; |
| 5450 | int len; |
| 5451 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5452 | if (!txn) |
| 5453 | return 0; |
| 5454 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5455 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5456 | return 0; |
| 5457 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5458 | len = txn->req.sl.rq.v_l; |
| 5459 | ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som; |
| 5460 | |
| 5461 | while ((len-- > 0) && (*ptr++ != '/')); |
| 5462 | if (len <= 0) |
| 5463 | return 0; |
| 5464 | |
| 5465 | test->ptr = ptr; |
| 5466 | test->len = len; |
| 5467 | |
| 5468 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5469 | return 1; |
| 5470 | } |
| 5471 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5472 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5473 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5474 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5475 | { |
| 5476 | struct http_txn *txn = l7; |
| 5477 | char *ptr; |
| 5478 | int len; |
| 5479 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5480 | if (!txn) |
| 5481 | return 0; |
| 5482 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5483 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5484 | return 0; |
| 5485 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5486 | len = txn->rsp.sl.st.v_l; |
| 5487 | ptr = txn->rsp.sol; |
| 5488 | |
| 5489 | while ((len-- > 0) && (*ptr++ != '/')); |
| 5490 | if (len <= 0) |
| 5491 | return 0; |
| 5492 | |
| 5493 | test->ptr = ptr; |
| 5494 | test->len = len; |
| 5495 | |
| 5496 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5497 | return 1; |
| 5498 | } |
| 5499 | |
| 5500 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5501 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5502 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5503 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5504 | { |
| 5505 | struct http_txn *txn = l7; |
| 5506 | char *ptr; |
| 5507 | int len; |
| 5508 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5509 | if (!txn) |
| 5510 | return 0; |
| 5511 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5512 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5513 | return 0; |
| 5514 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5515 | len = txn->rsp.sl.st.c_l; |
| 5516 | ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som; |
| 5517 | |
| 5518 | test->i = __strl2ui(ptr, len); |
| 5519 | test->flags = ACL_TEST_F_VOL_1ST; |
| 5520 | return 1; |
| 5521 | } |
| 5522 | |
| 5523 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5524 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5525 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5526 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5527 | { |
| 5528 | struct http_txn *txn = l7; |
| 5529 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5530 | if (!txn) |
| 5531 | return 0; |
| 5532 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5533 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5534 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5535 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5536 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5537 | /* ensure the indexes are not affected */ |
| 5538 | return 0; |
| 5539 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5540 | test->len = txn->req.sl.rq.u_l; |
| 5541 | test->ptr = txn->req.sol + txn->req.sl.rq.u; |
| 5542 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 5543 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 5544 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5545 | return 1; |
| 5546 | } |
| 5547 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5548 | static int |
| 5549 | acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5550 | struct acl_expr *expr, struct acl_test *test) |
| 5551 | { |
| 5552 | struct http_txn *txn = l7; |
| 5553 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5554 | if (!txn) |
| 5555 | return 0; |
| 5556 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5557 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5558 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5559 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5560 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5561 | /* ensure the indexes are not affected */ |
| 5562 | return 0; |
| 5563 | |
| 5564 | /* Parse HTTP request */ |
| 5565 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
| 5566 | test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr; |
| 5567 | test->i = AF_INET; |
| 5568 | |
| 5569 | /* |
| 5570 | * If we are parsing url in frontend space, we prepare backend stage |
| 5571 | * to not parse again the same url ! optimization lazyness... |
| 5572 | */ |
| 5573 | if (px->options & PR_O_HTTP_PROXY) |
| 5574 | l4->flags |= SN_ADDR_SET; |
| 5575 | |
| 5576 | test->flags = ACL_TEST_F_READ_ONLY; |
| 5577 | return 1; |
| 5578 | } |
| 5579 | |
| 5580 | static int |
| 5581 | acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5582 | struct acl_expr *expr, struct acl_test *test) |
| 5583 | { |
| 5584 | struct http_txn *txn = l7; |
| 5585 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5586 | if (!txn) |
| 5587 | return 0; |
| 5588 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5589 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5590 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5591 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5592 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5593 | /* ensure the indexes are not affected */ |
| 5594 | return 0; |
| 5595 | |
| 5596 | /* Same optimization as url_ip */ |
| 5597 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
| 5598 | test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port); |
| 5599 | |
| 5600 | if (px->options & PR_O_HTTP_PROXY) |
| 5601 | l4->flags |= SN_ADDR_SET; |
| 5602 | |
| 5603 | test->flags = ACL_TEST_F_READ_ONLY; |
| 5604 | return 1; |
| 5605 | } |
| 5606 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5607 | /* 5. Check on HTTP header. A pointer to the beginning of the value is returned. |
| 5608 | * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr(). |
| 5609 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5610 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5611 | 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] | 5612 | struct acl_expr *expr, struct acl_test *test) |
| 5613 | { |
| 5614 | struct http_txn *txn = l7; |
| 5615 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5616 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5617 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5618 | if (!txn) |
| 5619 | return 0; |
| 5620 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5621 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 5622 | /* search for header from the beginning */ |
| 5623 | ctx->idx = 0; |
| 5624 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5625 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 5626 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 5627 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5628 | test->len = ctx->vlen; |
| 5629 | test->ptr = (char *)ctx->line + ctx->val; |
| 5630 | return 1; |
| 5631 | } |
| 5632 | |
| 5633 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 5634 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5635 | return 0; |
| 5636 | } |
| 5637 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5638 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5639 | acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5640 | struct acl_expr *expr, struct acl_test *test) |
| 5641 | { |
| 5642 | struct http_txn *txn = l7; |
| 5643 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5644 | if (!txn) |
| 5645 | return 0; |
| 5646 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5647 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5648 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5649 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5650 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5651 | /* ensure the indexes are not affected */ |
| 5652 | return 0; |
| 5653 | |
| 5654 | return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test); |
| 5655 | } |
| 5656 | |
| 5657 | static int |
| 5658 | acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5659 | struct acl_expr *expr, struct acl_test *test) |
| 5660 | { |
| 5661 | struct http_txn *txn = l7; |
| 5662 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5663 | if (!txn) |
| 5664 | return 0; |
| 5665 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5666 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5667 | return 0; |
| 5668 | |
| 5669 | return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test); |
| 5670 | } |
| 5671 | |
| 5672 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 5673 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 5674 | */ |
| 5675 | static int |
| 5676 | 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] | 5677 | struct acl_expr *expr, struct acl_test *test) |
| 5678 | { |
| 5679 | struct http_txn *txn = l7; |
| 5680 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5681 | struct hdr_ctx ctx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5682 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5683 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5684 | if (!txn) |
| 5685 | return 0; |
| 5686 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5687 | ctx.idx = 0; |
| 5688 | cnt = 0; |
| 5689 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx)) |
| 5690 | cnt++; |
| 5691 | |
| 5692 | test->i = cnt; |
| 5693 | test->flags = ACL_TEST_F_VOL_HDR; |
| 5694 | return 1; |
| 5695 | } |
| 5696 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5697 | static int |
| 5698 | acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5699 | struct acl_expr *expr, struct acl_test *test) |
| 5700 | { |
| 5701 | struct http_txn *txn = l7; |
| 5702 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5703 | if (!txn) |
| 5704 | return 0; |
| 5705 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5706 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5707 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5708 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5709 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5710 | /* ensure the indexes are not affected */ |
| 5711 | return 0; |
| 5712 | |
| 5713 | return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test); |
| 5714 | } |
| 5715 | |
| 5716 | static int |
| 5717 | acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5718 | struct acl_expr *expr, struct acl_test *test) |
| 5719 | { |
| 5720 | struct http_txn *txn = l7; |
| 5721 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5722 | if (!txn) |
| 5723 | return 0; |
| 5724 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5725 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5726 | return 0; |
| 5727 | |
| 5728 | return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test); |
| 5729 | } |
| 5730 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5731 | /* 7. Check on HTTP header's integer value. The integer value is returned. |
| 5732 | * FIXME: the type is 'int', it may not be appropriate for everything. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5733 | * 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] | 5734 | */ |
| 5735 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5736 | 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] | 5737 | struct acl_expr *expr, struct acl_test *test) |
| 5738 | { |
| 5739 | struct http_txn *txn = l7; |
| 5740 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5741 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5742 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5743 | if (!txn) |
| 5744 | return 0; |
| 5745 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5746 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 5747 | /* search for header from the beginning */ |
| 5748 | ctx->idx = 0; |
| 5749 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5750 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 5751 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 5752 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5753 | test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen); |
| 5754 | return 1; |
| 5755 | } |
| 5756 | |
| 5757 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 5758 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5759 | return 0; |
| 5760 | } |
| 5761 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5762 | static int |
| 5763 | acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5764 | struct acl_expr *expr, struct acl_test *test) |
| 5765 | { |
| 5766 | struct http_txn *txn = l7; |
| 5767 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5768 | if (!txn) |
| 5769 | return 0; |
| 5770 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5771 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5772 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5773 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5774 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5775 | /* ensure the indexes are not affected */ |
| 5776 | return 0; |
| 5777 | |
| 5778 | return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test); |
| 5779 | } |
| 5780 | |
| 5781 | static int |
| 5782 | acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5783 | struct acl_expr *expr, struct acl_test *test) |
| 5784 | { |
| 5785 | struct http_txn *txn = l7; |
| 5786 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5787 | if (!txn) |
| 5788 | return 0; |
| 5789 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5790 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5791 | return 0; |
| 5792 | |
| 5793 | return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test); |
| 5794 | } |
| 5795 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5796 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 5797 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 5798 | */ |
| 5799 | static int |
| 5800 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5801 | struct acl_expr *expr, struct acl_test *test) |
| 5802 | { |
| 5803 | struct http_txn *txn = l7; |
| 5804 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5805 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5806 | if (!txn) |
| 5807 | return 0; |
| 5808 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5809 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5810 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5811 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5812 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5813 | /* ensure the indexes are not affected */ |
| 5814 | return 0; |
| 5815 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5816 | end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 5817 | ptr = http_get_path(txn); |
| 5818 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5819 | return 0; |
| 5820 | |
| 5821 | /* OK, we got the '/' ! */ |
| 5822 | test->ptr = ptr; |
| 5823 | |
| 5824 | while (ptr < end && *ptr != '?') |
| 5825 | ptr++; |
| 5826 | |
| 5827 | test->len = ptr - test->ptr; |
| 5828 | |
| 5829 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 5830 | test->flags = ACL_TEST_F_VOL_1ST; |
| 5831 | return 1; |
| 5832 | } |
| 5833 | |
| 5834 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5835 | |
| 5836 | /************************************************************************/ |
| 5837 | /* All supported keywords must be declared here. */ |
| 5838 | /************************************************************************/ |
| 5839 | |
| 5840 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 5841 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5842 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT }, |
| 5843 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5844 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 5845 | { "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] | 5846 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5847 | { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5848 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 5849 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 5850 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 5851 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 5852 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 5853 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 5854 | { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE }, |
| 5855 | { "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] | 5856 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5857 | /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */ |
| 5858 | { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5859 | { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 5860 | { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 5861 | { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 5862 | { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 5863 | { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 5864 | { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 5865 | { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
| 5866 | { "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] | 5867 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5868 | { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 5869 | { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE }, |
| 5870 | { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE }, |
| 5871 | { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE }, |
| 5872 | { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE }, |
| 5873 | { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE }, |
| 5874 | { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE }, |
| 5875 | { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
| 5876 | { "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] | 5877 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5878 | { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5879 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 5880 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 5881 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 5882 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 5883 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 5884 | { "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] | 5885 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 5886 | { NULL, NULL, NULL, NULL }, |
| 5887 | |
| 5888 | #if 0 |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5889 | { "line", acl_parse_str, acl_fetch_line, acl_match_str }, |
| 5890 | { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg }, |
| 5891 | { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg }, |
| 5892 | { "line_end", acl_parse_str, acl_fetch_line, acl_match_end }, |
| 5893 | { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub }, |
| 5894 | { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir }, |
| 5895 | { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom }, |
| 5896 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5897 | { "cook", acl_parse_str, acl_fetch_cook, acl_match_str }, |
| 5898 | { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg }, |
| 5899 | { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg }, |
| 5900 | { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end }, |
| 5901 | { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub }, |
| 5902 | { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir }, |
| 5903 | { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom }, |
| 5904 | { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst }, |
| 5905 | |
| 5906 | { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str }, |
| 5907 | { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg }, |
| 5908 | { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str }, |
| 5909 | { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 }, |
| 5910 | { NULL, NULL, NULL, NULL }, |
| 5911 | #endif |
| 5912 | }}; |
| 5913 | |
| 5914 | |
| 5915 | __attribute__((constructor)) |
| 5916 | static void __http_protocol_init(void) |
| 5917 | { |
| 5918 | acl_register_keywords(&acl_kws); |
| 5919 | } |
| 5920 | |
| 5921 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5922 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5923 | * Local variables: |
| 5924 | * c-indent-level: 8 |
| 5925 | * c-basic-offset: 8 |
| 5926 | * End: |
| 5927 | */ |