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 | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 369 | static char *srv_stnames[6] = { "IDL", "CON", "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 | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 549 | buffer_shutw(t->req); |
| 550 | buffer_shutr(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 | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 648 | /* Processes the client, server, request and response jobs of a session task, |
| 649 | * then puts it back to the wait queue in a clean state, or cleans up its |
| 650 | * resources if it must be deleted. Returns in <next> the date the task wants |
| 651 | * to be woken up, or TICK_ETERNITY. In order not to call all functions for |
| 652 | * nothing too many times, the request and response buffers flags are monitored |
| 653 | * and each function is called only if at least another function has changed at |
| 654 | * least one flag. If one of the functions called returns non-zero, then it |
| 655 | * will be called once again after all other functions. This permits explicit |
| 656 | * external loops which may be useful for complex state machines. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 657 | */ |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 658 | #define PROCESS_CLI 0x1 |
| 659 | #define PROCESS_SRV 0x2 |
| 660 | #define PROCESS_REQ 0x4 |
| 661 | #define PROCESS_RTR 0x8 |
| 662 | #define PROCESS_ALL (PROCESS_CLI|PROCESS_SRV|PROCESS_REQ|PROCESS_RTR) |
| 663 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 664 | void process_session(struct task *t, int *next) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 665 | { |
| 666 | struct session *s = t->context; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 667 | unsigned resync = PROCESS_ALL; |
| 668 | unsigned int rqf; |
| 669 | unsigned int rpf; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 670 | |
Willy Tarreau | 507385d | 2008-08-17 13:04:25 +0200 | [diff] [blame^] | 671 | /* check timeout expiration only once and adjust buffer flags |
| 672 | * accordingly. |
| 673 | */ |
| 674 | if (unlikely(tick_is_expired(t->expire, now_ms))) { |
| 675 | if (tick_is_expired(s->req->rex, now_ms)) |
| 676 | s->req->flags |= BF_READ_TIMEOUT; |
| 677 | |
| 678 | if (tick_is_expired(s->req->wex, now_ms)) |
| 679 | s->req->flags |= BF_WRITE_TIMEOUT; |
| 680 | |
| 681 | if (tick_is_expired(s->rep->rex, now_ms)) |
| 682 | s->rep->flags |= BF_READ_TIMEOUT; |
| 683 | |
| 684 | if (tick_is_expired(s->rep->wex, now_ms)) |
| 685 | s->rep->flags |= BF_WRITE_TIMEOUT; |
| 686 | } |
| 687 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 688 | do { |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 689 | if (resync & PROCESS_REQ) { |
| 690 | resync &= ~PROCESS_REQ; |
| 691 | if (s->analysis & AN_REQ_ANY) { |
| 692 | rqf = s->req->flags; |
| 693 | rpf = s->rep->flags; |
| 694 | |
| 695 | if (process_request(s)) |
| 696 | resync |= PROCESS_REQ; |
| 697 | |
| 698 | if (!(s->analysis & AN_REQ_ANY) && !(s->req->flags & BF_MAY_FORWARD)) |
| 699 | s->req->flags |= BF_MAY_FORWARD; |
| 700 | |
| 701 | if (rqf != s->req->flags || rpf != s->rep->flags) |
| 702 | resync |= PROCESS_ALL & ~PROCESS_REQ; |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | if (resync & PROCESS_RTR) { |
| 707 | resync &= ~PROCESS_RTR; |
| 708 | if (s->analysis & AN_RTR_ANY) { |
| 709 | rqf = s->req->flags; |
| 710 | rpf = s->rep->flags; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 711 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 712 | if (process_response(s)) |
| 713 | resync |= PROCESS_RTR; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 714 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 715 | if (!(s->analysis & AN_RTR_ANY) && !(s->rep->flags & BF_MAY_FORWARD)) |
| 716 | s->rep->flags |= BF_MAY_FORWARD; |
| 717 | |
| 718 | if (rqf != s->req->flags || rpf != s->rep->flags) |
| 719 | resync |= PROCESS_ALL & ~PROCESS_RTR; |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | if (resync & PROCESS_CLI) { |
| 724 | rqf = s->req->flags; |
| 725 | rpf = s->rep->flags; |
| 726 | |
| 727 | resync &= ~PROCESS_CLI; |
| 728 | if (process_cli(s)) |
| 729 | resync |= PROCESS_CLI; |
| 730 | |
| 731 | if (rqf != s->req->flags || rpf != s->rep->flags) |
| 732 | resync |= PROCESS_ALL & ~PROCESS_CLI; |
| 733 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 734 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 735 | if (resync & PROCESS_SRV) { |
| 736 | rqf = s->req->flags; |
| 737 | rpf = s->rep->flags; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 738 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 739 | resync &= ~PROCESS_SRV; |
| 740 | if (process_srv(s)) |
| 741 | resync |= PROCESS_SRV; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 742 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 743 | if (rqf != s->req->flags || rpf != s->rep->flags) |
| 744 | resync |= PROCESS_ALL & ~PROCESS_SRV; |
| 745 | } |
| 746 | } while (resync); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 747 | |
Willy Tarreau | f41d4b1 | 2007-04-28 23:26:14 +0200 | [diff] [blame] | 748 | 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] | 749 | |
| 750 | if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED)) |
| 751 | session_process_counters(s); |
| 752 | |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 753 | s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE; |
| 754 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 755 | |
Willy Tarreau | 7f875f6 | 2008-08-11 17:35:01 +0200 | [diff] [blame] | 756 | /* Trick: if a request is being waiting for the server to respond, |
| 757 | * and if we know the server can timeout, we don't want the timeout |
| 758 | * to expire on the client side first, but we're still interested |
| 759 | * in passing data from the client to the server (eg: POST). Thus, |
| 760 | * we can cancel the client's request timeout if the server's |
| 761 | * request timeout is set and the server has not yet sent a response. |
| 762 | */ |
| 763 | |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 764 | if ((s->rep->flags & (BF_MAY_FORWARD|BF_SHUTR)) == 0 && |
Willy Tarreau | 26ed74d | 2008-08-17 12:11:14 +0200 | [diff] [blame] | 765 | (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) |
Willy Tarreau | 7f875f6 | 2008-08-11 17:35:01 +0200 | [diff] [blame] | 766 | s->req->rex = TICK_ETERNITY; |
| 767 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 768 | t->expire = tick_first(tick_first(s->req->rex, s->req->wex), |
| 769 | tick_first(s->rep->rex, s->rep->wex)); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 770 | if (s->analysis & AN_REQ_ANY) { |
| 771 | if (s->analysis & AN_REQ_INSPECT) |
| 772 | t->expire = tick_first(t->expire, s->inspect_exp); |
| 773 | else if (s->analysis & AN_REQ_HTTP_HDR) |
| 774 | t->expire = tick_first(t->expire, s->txn.exp); |
| 775 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 776 | |
| 777 | /* restore t to its place in the task list */ |
| 778 | task_queue(t); |
| 779 | |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 780 | #ifdef DEBUG_DEV |
| 781 | /* this may only happen when no timeout is set or in case of an FSM bug */ |
| 782 | if (!t->expire) |
| 783 | ABORT_NOW(); |
| 784 | #endif |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 785 | *next = t->expire; |
| 786 | return; /* nothing more to do */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 787 | } |
| 788 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 789 | s->fe->feconn--; |
| 790 | if (s->flags & SN_BE_ASSIGNED) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 791 | s->be->beconn--; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 792 | actconn--; |
| 793 | |
Willy Tarreau | f41d4b1 | 2007-04-28 23:26:14 +0200 | [diff] [blame] | 794 | if (unlikely((global.mode & MODE_DEBUG) && |
| 795 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 796 | int len; |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 797 | len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 798 | s->uniq_id, s->be->id, |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 799 | (unsigned short)s->cli_fd, (unsigned short)s->srv_fd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 800 | write(1, trash, len); |
| 801 | } |
| 802 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 803 | 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] | 804 | session_process_counters(s); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 805 | |
| 806 | /* let's do a final log if we need it */ |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 807 | if (s->logs.logwait && |
| 808 | !(s->flags & SN_MONITOR) && |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 809 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 810 | if (s->fe->to_log & LW_REQ) |
| 811 | http_sess_log(s); |
| 812 | else |
| 813 | tcp_sess_log(s); |
| 814 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 815 | |
| 816 | /* the task MUST not be in the run queue anymore */ |
| 817 | task_delete(t); |
| 818 | session_free(s); |
| 819 | task_free(t); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 820 | *next = TICK_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 824 | extern const char sess_term_cond[8]; |
| 825 | extern const char sess_fin_state[8]; |
| 826 | extern const char *monthname[12]; |
| 827 | const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */ |
| 828 | const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown, |
| 829 | Set-cookie seen and left unchanged (passive), Set-cookie Deleted, |
| 830 | unknown, Set-cookie Rewritten */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 831 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 832 | struct pool_head *pool2_capture; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 833 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 834 | /* |
| 835 | * send a log for the session when we have enough info about it. |
| 836 | * Will not log if the frontend has no log defined. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 837 | */ |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 838 | static void http_sess_log(struct session *s) |
| 839 | { |
| 840 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 841 | struct proxy *fe = s->fe; |
| 842 | struct proxy *be = s->be; |
| 843 | struct proxy *prx_log; |
| 844 | struct http_txn *txn = &s->txn; |
| 845 | int tolog; |
| 846 | char *uri, *h; |
| 847 | char *svid; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 848 | struct tm tm; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 849 | static char tmpline[MAX_SYSLOG_LEN]; |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 850 | int t_request; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 851 | int hdr; |
| 852 | |
| 853 | if (fe->logfac1 < 0 && fe->logfac2 < 0) |
| 854 | return; |
| 855 | prx_log = fe; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 856 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 857 | if (s->cli_addr.ss_family == AF_INET) |
| 858 | inet_ntop(AF_INET, |
| 859 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 860 | pn, sizeof(pn)); |
| 861 | else |
| 862 | inet_ntop(AF_INET6, |
| 863 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 864 | pn, sizeof(pn)); |
| 865 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 866 | get_localtime(s->logs.accept_date.tv_sec, &tm); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 867 | |
| 868 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 869 | tolog = fe->to_log; |
| 870 | |
| 871 | h = tmpline; |
| 872 | if (fe->to_log & LW_REQHDR && |
| 873 | txn->req.cap && |
| 874 | (h < tmpline + sizeof(tmpline) - 10)) { |
| 875 | *(h++) = ' '; |
| 876 | *(h++) = '{'; |
| 877 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 878 | if (hdr) |
| 879 | *(h++) = '|'; |
| 880 | if (txn->req.cap[hdr] != NULL) |
| 881 | h = encode_string(h, tmpline + sizeof(tmpline) - 7, |
| 882 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 883 | } |
| 884 | *(h++) = '}'; |
| 885 | } |
| 886 | |
| 887 | if (fe->to_log & LW_RSPHDR && |
| 888 | txn->rsp.cap && |
| 889 | (h < tmpline + sizeof(tmpline) - 7)) { |
| 890 | *(h++) = ' '; |
| 891 | *(h++) = '{'; |
| 892 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 893 | if (hdr) |
| 894 | *(h++) = '|'; |
| 895 | if (txn->rsp.cap[hdr] != NULL) |
| 896 | h = encode_string(h, tmpline + sizeof(tmpline) - 4, |
| 897 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 898 | } |
| 899 | *(h++) = '}'; |
| 900 | } |
| 901 | |
| 902 | if (h < tmpline + sizeof(tmpline) - 4) { |
| 903 | *(h++) = ' '; |
| 904 | *(h++) = '"'; |
| 905 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 906 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 907 | '#', url_encode_map, uri); |
| 908 | *(h++) = '"'; |
| 909 | } |
| 910 | *h = '\0'; |
| 911 | |
| 912 | svid = (tolog & LW_SVID) ? |
| 913 | (s->data_source != DATA_SRC_STATS) ? |
| 914 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 915 | |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 916 | t_request = -1; |
| 917 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 918 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 919 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 920 | send_log(prx_log, LOG_INFO, |
| 921 | "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]" |
| 922 | " %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] | 923 | " %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] | 924 | pn, |
| 925 | (s->cli_addr.ss_family == AF_INET) ? |
| 926 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 927 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 928 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 929 | 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] | 930 | fe->id, be->id, svid, |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 931 | t_request, |
| 932 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 933 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 934 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 935 | (tolog & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 936 | txn->status, |
Willy Tarreau | 8b3977f | 2008-01-18 11:16:32 +0100 | [diff] [blame] | 937 | (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 938 | txn->cli_cookie ? txn->cli_cookie : "-", |
| 939 | txn->srv_cookie ? txn->srv_cookie : "-", |
| 940 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 941 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 942 | (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 943 | (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-', |
| 944 | 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] | 945 | (s->flags & SN_REDISP)?"+":"", |
| 946 | (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] | 947 | s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline); |
| 948 | |
| 949 | s->logs.logwait = 0; |
| 950 | } |
| 951 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 952 | |
| 953 | /* |
| 954 | * Capture headers from message starting at <som> according to header list |
| 955 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 956 | */ |
| 957 | void capture_headers(char *som, struct hdr_idx *idx, |
| 958 | char **cap, struct cap_hdr *cap_hdr) |
| 959 | { |
| 960 | char *eol, *sol, *col, *sov; |
| 961 | int cur_idx; |
| 962 | struct cap_hdr *h; |
| 963 | int len; |
| 964 | |
| 965 | sol = som + hdr_idx_first_pos(idx); |
| 966 | cur_idx = hdr_idx_first_idx(idx); |
| 967 | |
| 968 | while (cur_idx) { |
| 969 | eol = sol + idx->v[cur_idx].len; |
| 970 | |
| 971 | col = sol; |
| 972 | while (col < eol && *col != ':') |
| 973 | col++; |
| 974 | |
| 975 | sov = col + 1; |
| 976 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 977 | sov++; |
| 978 | |
| 979 | for (h = cap_hdr; h; h = h->next) { |
| 980 | if ((h->namelen == col - sol) && |
| 981 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 982 | if (cap[h->index] == NULL) |
| 983 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 984 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 985 | |
| 986 | if (cap[h->index] == NULL) { |
| 987 | Alert("HTTP capture : out of memory.\n"); |
| 988 | continue; |
| 989 | } |
| 990 | |
| 991 | len = eol - sov; |
| 992 | if (len > h->len) |
| 993 | len = h->len; |
| 994 | |
| 995 | memcpy(cap[h->index], sov, len); |
| 996 | cap[h->index][len]=0; |
| 997 | } |
| 998 | } |
| 999 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1000 | cur_idx = idx->v[cur_idx].next; |
| 1001 | } |
| 1002 | } |
| 1003 | |
| 1004 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1005 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1006 | */ |
| 1007 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1008 | |
| 1009 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1010 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1011 | */ |
| 1012 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1013 | ptr++; \ |
| 1014 | if (likely(ptr < end)) \ |
| 1015 | goto good; \ |
| 1016 | else { \ |
| 1017 | state = (st); \ |
| 1018 | goto http_msg_ood; \ |
| 1019 | } \ |
| 1020 | } while (0) |
| 1021 | |
| 1022 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1023 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1024 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1025 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1026 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1027 | * will give undefined results. |
| 1028 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1029 | * and that msg->sol points to the beginning of the response. |
| 1030 | * If a complete line is found (which implies that at least one CR or LF is |
| 1031 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1032 | * returned indicating an incomplete line (which does not mean that parts have |
| 1033 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1034 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1035 | * upon next call. |
| 1036 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1037 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1038 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1039 | * 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] | 1040 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1041 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1042 | const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, |
| 1043 | unsigned int state, const char *ptr, const char *end, |
| 1044 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1045 | { |
| 1046 | __label__ |
| 1047 | http_msg_rpver, |
| 1048 | http_msg_rpver_sp, |
| 1049 | http_msg_rpcode, |
| 1050 | http_msg_rpcode_sp, |
| 1051 | http_msg_rpreason, |
| 1052 | http_msg_rpline_eol, |
| 1053 | http_msg_ood, /* out of data */ |
| 1054 | http_msg_invalid; |
| 1055 | |
| 1056 | switch (state) { |
| 1057 | http_msg_rpver: |
| 1058 | case HTTP_MSG_RPVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1059 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1060 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1061 | |
| 1062 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1063 | msg->sl.st.v_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1064 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1065 | } |
| 1066 | goto http_msg_invalid; |
| 1067 | |
| 1068 | http_msg_rpver_sp: |
| 1069 | case HTTP_MSG_RPVER_SP: |
| 1070 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1071 | msg->sl.st.c = ptr - msg_buf; |
| 1072 | goto http_msg_rpcode; |
| 1073 | } |
| 1074 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1075 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1076 | /* so it's a CR/LF, this is invalid */ |
| 1077 | goto http_msg_invalid; |
| 1078 | |
| 1079 | http_msg_rpcode: |
| 1080 | case HTTP_MSG_RPCODE: |
| 1081 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1082 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1083 | |
| 1084 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1085 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 1086 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1087 | } |
| 1088 | |
| 1089 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1090 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 1091 | http_msg_rsp_reason: |
| 1092 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
| 1093 | msg->sl.st.r = ptr - msg_buf; |
| 1094 | msg->sl.st.r_l = 0; |
| 1095 | goto http_msg_rpline_eol; |
| 1096 | |
| 1097 | http_msg_rpcode_sp: |
| 1098 | case HTTP_MSG_RPCODE_SP: |
| 1099 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1100 | msg->sl.st.r = ptr - msg_buf; |
| 1101 | goto http_msg_rpreason; |
| 1102 | } |
| 1103 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1104 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1105 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1106 | goto http_msg_rsp_reason; |
| 1107 | |
| 1108 | http_msg_rpreason: |
| 1109 | case HTTP_MSG_RPREASON: |
| 1110 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1111 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
| 1112 | msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r; |
| 1113 | http_msg_rpline_eol: |
| 1114 | /* We have seen the end of line. Note that we do not |
| 1115 | * necessarily have the \n yet, but at least we know that we |
| 1116 | * have EITHER \r OR \n, otherwise the response would not be |
| 1117 | * complete. We can then record the response length and return |
| 1118 | * to the caller which will be able to register it. |
| 1119 | */ |
| 1120 | msg->sl.st.l = ptr - msg->sol; |
| 1121 | return ptr; |
| 1122 | |
| 1123 | #ifdef DEBUG_FULL |
| 1124 | default: |
| 1125 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1126 | exit(1); |
| 1127 | #endif |
| 1128 | } |
| 1129 | |
| 1130 | http_msg_ood: |
| 1131 | /* out of data */ |
| 1132 | if (ret_state) |
| 1133 | *ret_state = state; |
| 1134 | if (ret_ptr) |
| 1135 | *ret_ptr = (char *)ptr; |
| 1136 | return NULL; |
| 1137 | |
| 1138 | http_msg_invalid: |
| 1139 | /* invalid message */ |
| 1140 | if (ret_state) |
| 1141 | *ret_state = HTTP_MSG_ERROR; |
| 1142 | return NULL; |
| 1143 | } |
| 1144 | |
| 1145 | |
| 1146 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1147 | * This function parses a request line between <ptr> and <end>, starting with |
| 1148 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1149 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1150 | * will give undefined results. |
| 1151 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1152 | * and that msg->sol points to the beginning of the request. |
| 1153 | * If a complete line is found (which implies that at least one CR or LF is |
| 1154 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1155 | * returned indicating an incomplete line (which does not mean that parts have |
| 1156 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1157 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1158 | * upon next call. |
| 1159 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1160 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1161 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1162 | * 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] | 1163 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1164 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1165 | const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, |
| 1166 | unsigned int state, const char *ptr, const char *end, |
| 1167 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1168 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1169 | __label__ |
| 1170 | http_msg_rqmeth, |
| 1171 | http_msg_rqmeth_sp, |
| 1172 | http_msg_rquri, |
| 1173 | http_msg_rquri_sp, |
| 1174 | http_msg_rqver, |
| 1175 | http_msg_rqline_eol, |
| 1176 | http_msg_ood, /* out of data */ |
| 1177 | http_msg_invalid; |
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 | switch (state) { |
| 1180 | http_msg_rqmeth: |
| 1181 | case HTTP_MSG_RQMETH: |
| 1182 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1183 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1184 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1185 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1186 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1187 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1188 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1189 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1190 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1191 | /* HTTP 0.9 request */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1192 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1193 | http_msg_req09_uri: |
| 1194 | msg->sl.rq.u = ptr - msg_buf; |
| 1195 | http_msg_req09_uri_e: |
| 1196 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1197 | http_msg_req09_ver: |
| 1198 | msg->sl.rq.v = ptr - msg_buf; |
| 1199 | msg->sl.rq.v_l = 0; |
| 1200 | goto http_msg_rqline_eol; |
| 1201 | } |
| 1202 | goto http_msg_invalid; |
| 1203 | |
| 1204 | http_msg_rqmeth_sp: |
| 1205 | case HTTP_MSG_RQMETH_SP: |
| 1206 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1207 | msg->sl.rq.u = ptr - msg_buf; |
| 1208 | goto http_msg_rquri; |
| 1209 | } |
| 1210 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1211 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1212 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1213 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1214 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1215 | http_msg_rquri: |
| 1216 | case HTTP_MSG_RQURI: |
| 1217 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1218 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1219 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1220 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1221 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1222 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1223 | } |
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 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1226 | goto http_msg_req09_uri_e; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1227 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1228 | http_msg_rquri_sp: |
| 1229 | case HTTP_MSG_RQURI_SP: |
| 1230 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1231 | msg->sl.rq.v = ptr - msg_buf; |
| 1232 | goto http_msg_rqver; |
| 1233 | } |
| 1234 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1235 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1236 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1237 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1238 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1239 | http_msg_rqver: |
| 1240 | case HTTP_MSG_RQVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1241 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1242 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1243 | |
| 1244 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1245 | msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v; |
| 1246 | http_msg_rqline_eol: |
| 1247 | /* We have seen the end of line. Note that we do not |
| 1248 | * necessarily have the \n yet, but at least we know that we |
| 1249 | * have EITHER \r OR \n, otherwise the request would not be |
| 1250 | * complete. We can then record the request length and return |
| 1251 | * to the caller which will be able to register it. |
| 1252 | */ |
| 1253 | msg->sl.rq.l = ptr - msg->sol; |
| 1254 | return ptr; |
| 1255 | } |
| 1256 | |
| 1257 | /* neither an HTTP_VER token nor a CRLF */ |
| 1258 | goto http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1259 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1260 | #ifdef DEBUG_FULL |
| 1261 | default: |
| 1262 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1263 | exit(1); |
| 1264 | #endif |
| 1265 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1266 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1267 | http_msg_ood: |
| 1268 | /* out of data */ |
| 1269 | if (ret_state) |
| 1270 | *ret_state = state; |
| 1271 | if (ret_ptr) |
| 1272 | *ret_ptr = (char *)ptr; |
| 1273 | return NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1274 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1275 | http_msg_invalid: |
| 1276 | /* invalid message */ |
| 1277 | if (ret_state) |
| 1278 | *ret_state = HTTP_MSG_ERROR; |
| 1279 | return NULL; |
| 1280 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1281 | |
| 1282 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1283 | /* |
| 1284 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1285 | * depending on the initial msg->msg_state. It can be preempted everywhere |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1286 | * when data are missing and recalled at the exact same location with no |
| 1287 | * information loss. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1288 | * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other |
| 1289 | * fields. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1290 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1291 | void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx) |
| 1292 | { |
| 1293 | __label__ |
| 1294 | http_msg_rqbefore, |
| 1295 | http_msg_rqbefore_cr, |
| 1296 | http_msg_rqmeth, |
| 1297 | http_msg_rqline_end, |
| 1298 | http_msg_hdr_first, |
| 1299 | http_msg_hdr_name, |
| 1300 | http_msg_hdr_l1_sp, |
| 1301 | http_msg_hdr_l1_lf, |
| 1302 | http_msg_hdr_l1_lws, |
| 1303 | http_msg_hdr_val, |
| 1304 | http_msg_hdr_l2_lf, |
| 1305 | http_msg_hdr_l2_lws, |
| 1306 | http_msg_complete_header, |
| 1307 | http_msg_last_lf, |
| 1308 | http_msg_ood, /* out of data */ |
| 1309 | http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1310 | |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1311 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1312 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1313 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1314 | state = msg->msg_state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1315 | ptr = buf->lr; |
| 1316 | end = buf->r; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1317 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1318 | if (unlikely(ptr >= end)) |
| 1319 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1320 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1321 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1322 | /* |
| 1323 | * First, states that are specific to the response only. |
| 1324 | * We check them first so that request and headers are |
| 1325 | * closer to each other (accessed more often). |
| 1326 | */ |
| 1327 | http_msg_rpbefore: |
| 1328 | case HTTP_MSG_RPBEFORE: |
| 1329 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1330 | if (likely(ptr == buf->data)) { |
| 1331 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1332 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1333 | } else { |
| 1334 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1335 | /* only skip empty leading lines, don't remove them */ |
| 1336 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1337 | msg->som = ptr - buf->data; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1338 | #else |
| 1339 | /* Remove empty leading lines, as recommended by |
| 1340 | * RFC2616. This takes a lot of time because we |
| 1341 | * must move all the buffer backwards, but this |
| 1342 | * is rarely needed. The method above will be |
| 1343 | * cleaner when we'll be able to start sending |
| 1344 | * the request from any place in the buffer. |
| 1345 | */ |
| 1346 | buf->lr = ptr; |
| 1347 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1348 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1349 | msg->sol = buf->data; |
| 1350 | ptr = buf->data; |
| 1351 | end = buf->r; |
| 1352 | #endif |
| 1353 | } |
| 1354 | hdr_idx_init(idx); |
| 1355 | state = HTTP_MSG_RPVER; |
| 1356 | goto http_msg_rpver; |
| 1357 | } |
| 1358 | |
| 1359 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1360 | goto http_msg_invalid; |
| 1361 | |
| 1362 | if (unlikely(*ptr == '\n')) |
| 1363 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1364 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1365 | /* stop here */ |
| 1366 | |
| 1367 | http_msg_rpbefore_cr: |
| 1368 | case HTTP_MSG_RPBEFORE_CR: |
| 1369 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1370 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1371 | /* stop here */ |
| 1372 | |
| 1373 | http_msg_rpver: |
| 1374 | case HTTP_MSG_RPVER: |
| 1375 | case HTTP_MSG_RPVER_SP: |
| 1376 | case HTTP_MSG_RPCODE: |
| 1377 | case HTTP_MSG_RPCODE_SP: |
| 1378 | case HTTP_MSG_RPREASON: |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1379 | ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1380 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1381 | if (unlikely(!ptr)) |
| 1382 | return; |
| 1383 | |
| 1384 | /* we have a full response and we know that we have either a CR |
| 1385 | * or an LF at <ptr>. |
| 1386 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1387 | //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] | 1388 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1389 | |
| 1390 | msg->sol = ptr; |
| 1391 | if (likely(*ptr == '\r')) |
| 1392 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1393 | goto http_msg_rpline_end; |
| 1394 | |
| 1395 | http_msg_rpline_end: |
| 1396 | case HTTP_MSG_RPLINE_END: |
| 1397 | /* msg->sol must point to the first of CR or LF. */ |
| 1398 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1399 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1400 | /* stop here */ |
| 1401 | |
| 1402 | /* |
| 1403 | * Second, states that are specific to the request only |
| 1404 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1405 | http_msg_rqbefore: |
| 1406 | case HTTP_MSG_RQBEFORE: |
| 1407 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1408 | if (likely(ptr == buf->data)) { |
| 1409 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1410 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1411 | } else { |
| 1412 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1413 | /* only skip empty leading lines, don't remove them */ |
| 1414 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1415 | msg->som = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1416 | #else |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1417 | /* Remove empty leading lines, as recommended by |
| 1418 | * RFC2616. This takes a lot of time because we |
| 1419 | * must move all the buffer backwards, but this |
| 1420 | * is rarely needed. The method above will be |
| 1421 | * cleaner when we'll be able to start sending |
| 1422 | * the request from any place in the buffer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1423 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1424 | buf->lr = ptr; |
| 1425 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1426 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1427 | msg->sol = buf->data; |
| 1428 | ptr = buf->data; |
| 1429 | end = buf->r; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1430 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1431 | } |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1432 | /* we will need this when keep-alive will be supported |
| 1433 | hdr_idx_init(idx); |
| 1434 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1435 | state = HTTP_MSG_RQMETH; |
| 1436 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1437 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1438 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1439 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1440 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1441 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1442 | if (unlikely(*ptr == '\n')) |
| 1443 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1444 | 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] | 1445 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1446 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1447 | http_msg_rqbefore_cr: |
| 1448 | case HTTP_MSG_RQBEFORE_CR: |
| 1449 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1450 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1451 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1452 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1453 | http_msg_rqmeth: |
| 1454 | case HTTP_MSG_RQMETH: |
| 1455 | case HTTP_MSG_RQMETH_SP: |
| 1456 | case HTTP_MSG_RQURI: |
| 1457 | case HTTP_MSG_RQURI_SP: |
| 1458 | case HTTP_MSG_RQVER: |
| 1459 | ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1460 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1461 | if (unlikely(!ptr)) |
| 1462 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1463 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1464 | /* we have a full request and we know that we have either a CR |
| 1465 | * or an LF at <ptr>. |
| 1466 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1467 | //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] | 1468 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
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 | msg->sol = ptr; |
| 1471 | if (likely(*ptr == '\r')) |
| 1472 | 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] | 1473 | goto http_msg_rqline_end; |
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_rqline_end: |
| 1476 | case HTTP_MSG_RQLINE_END: |
| 1477 | /* check for HTTP/0.9 request : no version information available. |
| 1478 | * msg->sol must point to the first of CR or LF. |
| 1479 | */ |
| 1480 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1481 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1482 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1483 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1484 | 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] | 1485 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1486 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1487 | /* |
| 1488 | * Common states below |
| 1489 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1490 | http_msg_hdr_first: |
| 1491 | case HTTP_MSG_HDR_FIRST: |
| 1492 | msg->sol = ptr; |
| 1493 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1494 | goto http_msg_hdr_name; |
| 1495 | } |
| 1496 | |
| 1497 | if (likely(*ptr == '\r')) |
| 1498 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1499 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1500 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1501 | http_msg_hdr_name: |
| 1502 | case HTTP_MSG_HDR_NAME: |
| 1503 | /* assumes msg->sol points to the first char */ |
| 1504 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1505 | 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] | 1506 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1507 | if (likely(*ptr == ':')) { |
| 1508 | msg->col = ptr - buf->data; |
| 1509 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| 1510 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1511 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1512 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1513 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1514 | http_msg_hdr_l1_sp: |
| 1515 | case HTTP_MSG_HDR_L1_SP: |
| 1516 | /* assumes msg->sol points to the first char and msg->col to the colon */ |
| 1517 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1518 | 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] | 1519 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1520 | /* header value can be basically anything except CR/LF */ |
| 1521 | msg->sov = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1522 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1523 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1524 | goto http_msg_hdr_val; |
| 1525 | } |
| 1526 | |
| 1527 | if (likely(*ptr == '\r')) |
| 1528 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1529 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1530 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1531 | http_msg_hdr_l1_lf: |
| 1532 | case HTTP_MSG_HDR_L1_LF: |
| 1533 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1534 | 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] | 1535 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1536 | http_msg_hdr_l1_lws: |
| 1537 | case HTTP_MSG_HDR_L1_LWS: |
| 1538 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1539 | /* replace HT,CR,LF with spaces */ |
| 1540 | for (; buf->data+msg->sov < ptr; msg->sov++) |
| 1541 | buf->data[msg->sov] = ' '; |
| 1542 | goto http_msg_hdr_l1_sp; |
| 1543 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1544 | /* we had a header consisting only in spaces ! */ |
| 1545 | msg->eol = buf->data + msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1546 | goto http_msg_complete_header; |
| 1547 | |
| 1548 | http_msg_hdr_val: |
| 1549 | case HTTP_MSG_HDR_VAL: |
| 1550 | /* assumes msg->sol points to the first char, msg->col to the |
| 1551 | * colon, and msg->sov points to the first character of the |
| 1552 | * value. |
| 1553 | */ |
| 1554 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1555 | 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] | 1556 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1557 | msg->eol = ptr; |
| 1558 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1559 | * real header end in case it ends with lots of LWS, but is this |
| 1560 | * really needed ? |
| 1561 | */ |
| 1562 | if (likely(*ptr == '\r')) |
| 1563 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1564 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1565 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1566 | http_msg_hdr_l2_lf: |
| 1567 | case HTTP_MSG_HDR_L2_LF: |
| 1568 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1569 | 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] | 1570 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1571 | http_msg_hdr_l2_lws: |
| 1572 | case HTTP_MSG_HDR_L2_LWS: |
| 1573 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1574 | /* LWS: replace HT,CR,LF with spaces */ |
| 1575 | for (; msg->eol < ptr; msg->eol++) |
| 1576 | *msg->eol = ' '; |
| 1577 | goto http_msg_hdr_val; |
| 1578 | } |
| 1579 | http_msg_complete_header: |
| 1580 | /* |
| 1581 | * It was a new header, so the last one is finished. |
| 1582 | * Assumes msg->sol points to the first char, msg->col to the |
| 1583 | * colon, msg->sov points to the first character of the value |
| 1584 | * and msg->eol to the first CR or LF so we know how the line |
| 1585 | * ends. We insert last header into the index. |
| 1586 | */ |
| 1587 | /* |
| 1588 | fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol); |
| 1589 | write(2, msg->sol, msg->eol-msg->sol); |
| 1590 | fprintf(stderr,"\n"); |
| 1591 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1592 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1593 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r', |
| 1594 | idx, idx->tail) < 0)) |
| 1595 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1596 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1597 | msg->sol = ptr; |
| 1598 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1599 | goto http_msg_hdr_name; |
| 1600 | } |
| 1601 | |
| 1602 | if (likely(*ptr == '\r')) |
| 1603 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1604 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1605 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1606 | http_msg_last_lf: |
| 1607 | case HTTP_MSG_LAST_LF: |
| 1608 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1609 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1610 | ptr++; |
| 1611 | buf->lr = ptr; |
| 1612 | msg->eoh = msg->sol - buf->data; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1613 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1614 | return; |
| 1615 | #ifdef DEBUG_FULL |
| 1616 | default: |
| 1617 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1618 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1619 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1620 | } |
| 1621 | http_msg_ood: |
| 1622 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1623 | msg->msg_state = state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1624 | buf->lr = ptr; |
| 1625 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1626 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1627 | http_msg_invalid: |
| 1628 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1629 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1630 | return; |
| 1631 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1632 | |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1633 | /* This function performs all the processing enabled for the current request. |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1634 | * It normally returns zero, but may return 1 if it absolutely needs to be |
| 1635 | * called again after other functions. It relies on buffers flags, and updates |
| 1636 | * t->analysis. It might make sense to explode it into several other functions. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1637 | */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1638 | int process_request(struct session *t) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1639 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1640 | struct buffer *req = t->req; |
| 1641 | struct buffer *rep = t->rep; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1642 | |
Willy Tarreau | e46ab55 | 2008-08-13 19:19:37 +0200 | [diff] [blame] | 1643 | DPRINTF(stderr,"[%u] process_req: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x analysis=%02x\n", |
| 1644 | now_ms, |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1645 | cli_stnames[t->cli_state], srv_stnames[t->srv_state], |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 1646 | t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_RD) : 0, |
| 1647 | t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_WR) : 0, |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1648 | req->rex, rep->wex, req->flags, rep->flags, t->analysis); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1649 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1650 | update_state: |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1651 | if (t->analysis & AN_REQ_INSPECT) { |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1652 | struct tcp_rule *rule; |
| 1653 | int partial; |
| 1654 | |
| 1655 | /* We will abort if we encounter a read error. In theory, |
| 1656 | * we should not abort if we get a close, it might be |
| 1657 | * valid, also very unlikely. FIXME: we'll abort for now, |
| 1658 | * this will be easier to change later. |
| 1659 | */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1660 | if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1661 | t->inspect_exp = TICK_ETERNITY; |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1662 | t->analysis &= ~AN_REQ_ANY; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1663 | t->fe->failed_req++; |
| 1664 | if (!(t->flags & SN_ERR_MASK)) |
| 1665 | t->flags |= SN_ERR_CLICL; |
| 1666 | if (!(t->flags & SN_FINST_MASK)) |
| 1667 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1668 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1669 | } |
| 1670 | |
| 1671 | /* Abort if client read timeout has expired */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1672 | else if (req->flags & BF_READ_TIMEOUT) { |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1673 | t->inspect_exp = TICK_ETERNITY; |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1674 | t->analysis &= ~AN_REQ_ANY; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1675 | t->fe->failed_req++; |
| 1676 | if (!(t->flags & SN_ERR_MASK)) |
| 1677 | t->flags |= SN_ERR_CLITO; |
| 1678 | if (!(t->flags & SN_FINST_MASK)) |
| 1679 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1680 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1681 | } |
| 1682 | |
| 1683 | /* We don't know whether we have enough data, so must proceed |
| 1684 | * this way : |
| 1685 | * - iterate through all rules in their declaration order |
| 1686 | * - if one rule returns MISS, it means the inspect delay is |
| 1687 | * not over yet, then return immediately, otherwise consider |
| 1688 | * it as a non-match. |
| 1689 | * - if one rule returns OK, then return OK |
| 1690 | * - if one rule returns KO, then return KO |
| 1691 | */ |
| 1692 | |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 1693 | if (req->flags & (BF_READ_NULL | BF_SHUTR) || |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1694 | tick_is_expired(t->inspect_exp, now_ms)) |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1695 | partial = 0; |
| 1696 | else |
| 1697 | partial = ACL_PARTIAL; |
| 1698 | |
| 1699 | list_for_each_entry(rule, &t->fe->tcp_req.inspect_rules, list) { |
| 1700 | int ret = ACL_PAT_PASS; |
| 1701 | |
| 1702 | if (rule->cond) { |
| 1703 | ret = acl_exec_cond(rule->cond, t->fe, t, NULL, ACL_DIR_REQ | partial); |
| 1704 | if (ret == ACL_PAT_MISS) { |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1705 | /* just set the request timeout once at the beginning of the request */ |
| 1706 | if (!tick_isset(t->inspect_exp)) |
| 1707 | t->inspect_exp = tick_add_ifset(now_ms, t->fe->tcp_req.inspect_delay); |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1708 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1709 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1710 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1711 | ret = acl_pass(ret); |
| 1712 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 1713 | ret = !ret; |
| 1714 | } |
| 1715 | |
| 1716 | if (ret) { |
| 1717 | /* we have a matching rule. */ |
| 1718 | if (rule->action == TCP_ACT_REJECT) { |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 1719 | buffer_shutr(req); |
| 1720 | buffer_shutw(rep); |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1721 | fd_delete(t->cli_fd); |
| 1722 | t->cli_state = CL_STCLOSE; |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1723 | t->analysis &= ~AN_REQ_ANY; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1724 | t->fe->failed_req++; |
| 1725 | if (!(t->flags & SN_ERR_MASK)) |
| 1726 | t->flags |= SN_ERR_PRXCOND; |
| 1727 | if (!(t->flags & SN_FINST_MASK)) |
| 1728 | t->flags |= SN_FINST_R; |
| 1729 | t->inspect_exp = TICK_ETERNITY; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1730 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1731 | } |
| 1732 | /* otherwise accept */ |
| 1733 | break; |
| 1734 | } |
| 1735 | } |
| 1736 | |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1737 | /* if we get there, it means we have no rule which matches, or |
| 1738 | * we have an explicit accept, so we apply the default accept. |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1739 | */ |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1740 | t->analysis &= ~AN_REQ_INSPECT; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1741 | t->inspect_exp = TICK_ETERNITY; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1742 | } |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1743 | |
| 1744 | if (t->analysis & AN_REQ_HTTP_HDR) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1745 | /* |
| 1746 | * Now parse the partial (or complete) lines. |
| 1747 | * We will check the request syntax, and also join multi-line |
| 1748 | * headers. An index of all the lines will be elaborated while |
| 1749 | * parsing. |
| 1750 | * |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1751 | * For the parsing, we use a 28 states FSM. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1752 | * |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1753 | * Here is the information we currently have : |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1754 | * req->data + req->som = beginning of request |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1755 | * req->data + req->eoh = end of processed headers / start of current one |
| 1756 | * req->data + req->eol = end of current header or line (LF or CRLF) |
| 1757 | * req->lr = first non-visited byte |
| 1758 | * req->r = end of data |
| 1759 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1760 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1761 | int cur_idx; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1762 | struct http_txn *txn = &t->txn; |
| 1763 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1764 | struct proxy *cur_proxy; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1765 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1766 | if (likely(req->lr < req->r)) |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1767 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1768 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1769 | /* 1: we might have to print this header in debug mode */ |
| 1770 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1771 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1772 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1773 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1774 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1775 | sol = req->data + msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1776 | eol = sol + msg->sl.rq.l; |
| 1777 | debug_hdr("clireq", t, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 1778 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1779 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 1780 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1781 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1782 | while (cur_idx) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1783 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1784 | debug_hdr("clihdr", t, sol, eol); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1785 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 1786 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1787 | } |
| 1788 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1789 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1790 | |
| 1791 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1792 | * Now we quickly check if we have found a full valid request. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1793 | * If not so, we check the FD and buffer states before leaving. |
| 1794 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1795 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 1796 | * requests are checked first. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1797 | * |
| 1798 | */ |
| 1799 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1800 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1801 | /* |
| 1802 | * First, let's catch bad requests. |
| 1803 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1804 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1805 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1806 | |
| 1807 | /* 1: Since we are in header mode, if there's no space |
| 1808 | * left for headers, we won't be able to free more |
| 1809 | * later, so the session will never terminate. We |
| 1810 | * must terminate it now. |
| 1811 | */ |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 1812 | if (unlikely(req->flags & BF_FULL)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1813 | /* FIXME: check if URI is set and return Status |
| 1814 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1815 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1816 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1817 | } |
| 1818 | |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1819 | /* 2: have we encountered a close ? */ |
| 1820 | else if (req->flags & BF_READ_NULL) { |
| 1821 | txn->status = 400; |
| 1822 | client_retnclose(t, error_message(t, HTTP_ERR_400)); |
| 1823 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1824 | t->analysis &= ~AN_REQ_ANY; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 1825 | t->fe->failed_req++; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1826 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1827 | if (!(t->flags & SN_ERR_MASK)) |
| 1828 | t->flags |= SN_ERR_CLICL; |
| 1829 | if (!(t->flags & SN_FINST_MASK)) |
| 1830 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1831 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1832 | } |
| 1833 | |
| 1834 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1835 | else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(txn->exp, now_ms)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1836 | /* read timeout : give up with an error message. */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1837 | txn->status = 408; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 1838 | client_retnclose(t, error_message(t, HTTP_ERR_408)); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1839 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1840 | t->analysis &= ~AN_REQ_ANY; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 1841 | t->fe->failed_req++; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1842 | if (!(t->flags & SN_ERR_MASK)) |
| 1843 | t->flags |= SN_ERR_CLITO; |
| 1844 | if (!(t->flags & SN_FINST_MASK)) |
| 1845 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1846 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1847 | } |
| 1848 | |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1849 | /* 4: have we encountered a read error or did we have to shutdown ? */ |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 1850 | else if (req->flags & (BF_READ_ERROR | BF_SHUTR)) { |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1851 | /* we cannot return any message on error */ |
| 1852 | msg->msg_state = HTTP_MSG_ERROR; |
| 1853 | t->analysis &= ~AN_REQ_ANY; |
| 1854 | t->fe->failed_req++; |
| 1855 | if (!(t->flags & SN_ERR_MASK)) |
| 1856 | t->flags |= SN_ERR_CLICL; |
| 1857 | if (!(t->flags & SN_FINST_MASK)) |
| 1858 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1859 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1860 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1861 | |
| 1862 | /* just set the request timeout once at the beginning of the request */ |
| 1863 | if (!tick_isset(t->txn.exp)) |
| 1864 | t->txn.exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq); |
| 1865 | |
| 1866 | /* we're not ready yet */ |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1867 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1868 | } |
| 1869 | |
| 1870 | |
| 1871 | /**************************************************************** |
| 1872 | * More interesting part now : we know that we have a complete * |
| 1873 | * request which at least looks like HTTP. We have an indicator * |
| 1874 | * of each header's length, so we can parse them quickly. * |
| 1875 | ****************************************************************/ |
| 1876 | |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1877 | t->analysis &= ~AN_REQ_HTTP_HDR; |
| 1878 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1879 | /* ensure we keep this pointer to the beginning of the message */ |
| 1880 | msg->sol = req->data + msg->som; |
| 1881 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1882 | /* |
| 1883 | * 1: identify the method |
| 1884 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1885 | 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] | 1886 | |
| 1887 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1888 | * 2: check if the URI matches the monitor_uri. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1889 | * We have to do this for every request which gets in, because |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1890 | * the monitor-uri is defined by the frontend. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1891 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1892 | if (unlikely((t->fe->monitor_uri_len != 0) && |
| 1893 | (t->fe->monitor_uri_len == msg->sl.rq.u_l) && |
| 1894 | !memcmp(&req->data[msg->sl.rq.u], |
| 1895 | t->fe->monitor_uri, |
| 1896 | t->fe->monitor_uri_len))) { |
| 1897 | /* |
| 1898 | * We have found the monitor URI |
| 1899 | */ |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1900 | struct acl_cond *cond; |
| 1901 | cur_proxy = t->fe; |
| 1902 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1903 | t->flags |= SN_MONITOR; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1904 | |
| 1905 | /* Check if we want to fail this monitor request or not */ |
| 1906 | list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) { |
| 1907 | 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] | 1908 | |
| 1909 | ret = acl_pass(ret); |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1910 | if (cond->pol == ACL_COND_UNLESS) |
| 1911 | ret = !ret; |
| 1912 | |
| 1913 | if (ret) { |
| 1914 | /* we fail this request, let's return 503 service unavail */ |
| 1915 | txn->status = 503; |
| 1916 | client_retnclose(t, error_message(t, HTTP_ERR_503)); |
| 1917 | goto return_prx_cond; |
| 1918 | } |
| 1919 | } |
| 1920 | |
| 1921 | /* nothing to fail, let's reply normaly */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1922 | txn->status = 200; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1923 | client_retnclose(t, &http_200_chunk); |
| 1924 | goto return_prx_cond; |
| 1925 | } |
| 1926 | |
| 1927 | /* |
| 1928 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 1929 | * Note: we cannot log anymore if the request has been |
| 1930 | * classified as invalid. |
| 1931 | */ |
| 1932 | if (unlikely(t->logs.logwait & LW_REQ)) { |
| 1933 | /* we have a complete HTTP request that we must log */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 1934 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1935 | int urilen = msg->sl.rq.l; |
| 1936 | |
| 1937 | if (urilen >= REQURI_LEN) |
| 1938 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1939 | memcpy(txn->uri, &req->data[msg->som], urilen); |
| 1940 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1941 | |
| 1942 | if (!(t->logs.logwait &= ~LW_REQ)) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1943 | http_sess_log(t); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1944 | } else { |
| 1945 | Alert("HTTP logging : out of memory.\n"); |
| 1946 | } |
| 1947 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1948 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1949 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1950 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 1951 | if (unlikely(msg->sl.rq.v_l == 0)) { |
| 1952 | int delta; |
| 1953 | char *cur_end; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1954 | msg->sol = req->data + msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1955 | cur_end = msg->sol + msg->sl.rq.l; |
| 1956 | delta = 0; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1957 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1958 | if (msg->sl.rq.u_l == 0) { |
| 1959 | /* if no URI was set, add "/" */ |
| 1960 | delta = buffer_replace2(req, cur_end, cur_end, " /", 2); |
| 1961 | cur_end += delta; |
| 1962 | msg->eoh += delta; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1963 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1964 | /* add HTTP version */ |
| 1965 | delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11); |
| 1966 | msg->eoh += delta; |
| 1967 | cur_end += delta; |
| 1968 | cur_end = (char *)http_parse_reqline(msg, req->data, |
| 1969 | HTTP_MSG_RQMETH, |
| 1970 | msg->sol, cur_end + 1, |
| 1971 | NULL, NULL); |
| 1972 | if (unlikely(!cur_end)) |
| 1973 | goto return_bad_req; |
| 1974 | |
| 1975 | /* we have a full HTTP/1.0 request now and we know that |
| 1976 | * we have either a CR or an LF at <ptr>. |
| 1977 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1978 | 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] | 1979 | } |
| 1980 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1981 | |
| 1982 | /* 5: we may need to capture headers */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1983 | if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap)) |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1984 | capture_headers(req->data + msg->som, &txn->hdr_idx, |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1985 | txn->req.cap, t->fe->req_cap); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1986 | |
| 1987 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1988 | * 6: we will have to evaluate the filters. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1989 | * As opposed to version 1.2, now they will be evaluated in the |
| 1990 | * filters order and not in the header order. This means that |
| 1991 | * each filter has to be validated among all headers. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1992 | * |
| 1993 | * We can now check whether we want to switch to another |
| 1994 | * backend, in which case we will re-check the backend's |
| 1995 | * filters and various options. In order to support 3-level |
| 1996 | * switching, here's how we should proceed : |
| 1997 | * |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1998 | * a) run be. |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1999 | * if (switch) then switch ->be to the new backend. |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2000 | * b) run be if (be != fe). |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2001 | * There cannot be any switch from there, so ->be cannot be |
| 2002 | * changed anymore. |
| 2003 | * |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2004 | * => filters always apply to ->be, then ->be may change. |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2005 | * |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2006 | * The response path will be able to apply either ->be, or |
| 2007 | * ->be then ->fe filters in order to match the reverse of |
| 2008 | * the forward sequence. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2009 | */ |
| 2010 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2011 | do { |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2012 | struct acl_cond *cond; |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2013 | struct redirect_rule *rule; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2014 | struct proxy *rule_set = t->be; |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2015 | cur_proxy = t->be; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2016 | |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2017 | /* first check whether we have some ACLs set to redirect this request */ |
| 2018 | list_for_each_entry(rule, &cur_proxy->redirect_rules, list) { |
| 2019 | 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] | 2020 | |
| 2021 | ret = acl_pass(ret); |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2022 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2023 | ret = !ret; |
| 2024 | |
| 2025 | if (ret) { |
| 2026 | struct chunk rdr = { trash, 0 }; |
| 2027 | const char *msg_fmt; |
| 2028 | |
| 2029 | /* build redirect message */ |
| 2030 | switch(rule->code) { |
| 2031 | case 303: |
| 2032 | rdr.len = strlen(HTTP_303); |
| 2033 | msg_fmt = HTTP_303; |
| 2034 | break; |
| 2035 | case 301: |
| 2036 | rdr.len = strlen(HTTP_301); |
| 2037 | msg_fmt = HTTP_301; |
| 2038 | break; |
| 2039 | case 302: |
| 2040 | default: |
| 2041 | rdr.len = strlen(HTTP_302); |
| 2042 | msg_fmt = HTTP_302; |
| 2043 | break; |
| 2044 | } |
| 2045 | |
| 2046 | if (unlikely(rdr.len > sizeof(trash))) |
| 2047 | goto return_bad_req; |
| 2048 | memcpy(rdr.str, msg_fmt, rdr.len); |
| 2049 | |
| 2050 | switch(rule->type) { |
| 2051 | case REDIRECT_TYPE_PREFIX: { |
| 2052 | const char *path; |
| 2053 | int pathlen; |
| 2054 | |
| 2055 | path = http_get_path(txn); |
| 2056 | /* build message using path */ |
| 2057 | if (path) { |
| 2058 | pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path; |
| 2059 | } else { |
| 2060 | path = "/"; |
| 2061 | pathlen = 1; |
| 2062 | } |
| 2063 | |
| 2064 | if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4) |
| 2065 | goto return_bad_req; |
| 2066 | |
| 2067 | /* add prefix */ |
| 2068 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 2069 | rdr.len += rule->rdr_len; |
| 2070 | |
| 2071 | /* add path */ |
| 2072 | memcpy(rdr.str + rdr.len, path, pathlen); |
| 2073 | rdr.len += pathlen; |
| 2074 | break; |
| 2075 | } |
| 2076 | case REDIRECT_TYPE_LOCATION: |
| 2077 | default: |
| 2078 | if (rdr.len + rule->rdr_len > sizeof(trash) - 4) |
| 2079 | goto return_bad_req; |
| 2080 | |
| 2081 | /* add location */ |
| 2082 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 2083 | rdr.len += rule->rdr_len; |
| 2084 | break; |
| 2085 | } |
| 2086 | |
| 2087 | /* add end of headers */ |
| 2088 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 2089 | rdr.len += 4; |
| 2090 | |
| 2091 | txn->status = rule->code; |
| 2092 | /* let's log the request time */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2093 | t->logs.tv_request = now; |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2094 | client_retnclose(t, &rdr); |
| 2095 | goto return_prx_cond; |
| 2096 | } |
| 2097 | } |
| 2098 | |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2099 | /* first check whether we have some ACLs set to block this request */ |
| 2100 | list_for_each_entry(cond, &cur_proxy->block_cond, list) { |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 2101 | 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] | 2102 | |
| 2103 | ret = acl_pass(ret); |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2104 | if (cond->pol == ACL_COND_UNLESS) |
| 2105 | ret = !ret; |
| 2106 | |
| 2107 | if (ret) { |
| 2108 | txn->status = 403; |
| 2109 | /* let's log the request time */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2110 | t->logs.tv_request = now; |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2111 | client_retnclose(t, error_message(t, HTTP_ERR_403)); |
| 2112 | goto return_prx_cond; |
| 2113 | } |
| 2114 | } |
| 2115 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2116 | /* try headers filters */ |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2117 | if (rule_set->req_exp != NULL) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2118 | if (apply_filters_to_request(t, req, rule_set->req_exp) < 0) |
| 2119 | goto return_bad_req; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2120 | } |
| 2121 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2122 | if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) { |
| 2123 | /* to ensure correct connection accounting on |
| 2124 | * the backend, we count the connection for the |
| 2125 | * one managing the queue. |
| 2126 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2127 | t->be->beconn++; |
| 2128 | if (t->be->beconn > t->be->beconn_max) |
| 2129 | t->be->beconn_max = t->be->beconn; |
| 2130 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2131 | t->flags |= SN_BE_ASSIGNED; |
| 2132 | } |
| 2133 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2134 | /* has the request been denied ? */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2135 | if (txn->flags & TX_CLDENY) { |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2136 | /* no need to go further */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2137 | txn->status = 403; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2138 | /* let's log the request time */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2139 | t->logs.tv_request = now; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2140 | client_retnclose(t, error_message(t, HTTP_ERR_403)); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2141 | goto return_prx_cond; |
| 2142 | } |
| 2143 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2144 | /* We might have to check for "Connection:" */ |
Krzysztof Oledzki | 336d475 | 2007-12-25 02:40:22 +0100 | [diff] [blame] | 2145 | 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] | 2146 | !(t->flags & SN_CONN_CLOSED)) { |
| 2147 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2148 | int cur_idx, old_idx, delta, val; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2149 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2150 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2151 | 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] | 2152 | old_idx = 0; |
| 2153 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2154 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 2155 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2156 | cur_ptr = cur_next; |
| 2157 | cur_end = cur_ptr + cur_hdr->len; |
| 2158 | cur_next = cur_end + cur_hdr->cr + 1; |
| 2159 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2160 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 2161 | if (val) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2162 | /* 3 possibilities : |
| 2163 | * - we have already set Connection: close, |
| 2164 | * so we remove this line. |
| 2165 | * - we have not yet set Connection: close, |
| 2166 | * but this line indicates close. We leave |
| 2167 | * it untouched and set the flag. |
| 2168 | * - we have not yet set Connection: close, |
| 2169 | * and this line indicates non-close. We |
| 2170 | * replace it. |
| 2171 | */ |
| 2172 | if (t->flags & SN_CONN_CLOSED) { |
| 2173 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2174 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2175 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2176 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 2177 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2178 | cur_hdr->len = 0; |
| 2179 | } else { |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2180 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 2181 | delta = buffer_replace2(req, cur_ptr + val, cur_end, |
| 2182 | "close", 5); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2183 | cur_next += delta; |
| 2184 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2185 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2186 | } |
| 2187 | t->flags |= SN_CONN_CLOSED; |
| 2188 | } |
| 2189 | } |
| 2190 | old_idx = cur_idx; |
| 2191 | } |
Willy Tarreau | f2f0ee8 | 2007-03-30 12:02:43 +0200 | [diff] [blame] | 2192 | } |
| 2193 | /* add request headers from the rule sets in the same order */ |
| 2194 | for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) { |
| 2195 | if (unlikely(http_header_add_tail(req, |
| 2196 | &txn->req, |
| 2197 | &txn->hdr_idx, |
| 2198 | rule_set->req_add[cur_idx])) < 0) |
| 2199 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2200 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2201 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2202 | /* check if stats URI was requested, and if an auth is needed */ |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 2203 | if (rule_set->uri_auth != NULL && |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2204 | (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) { |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2205 | /* we have to check the URI and auth for this request. |
| 2206 | * FIXME!!! that one is rather dangerous, we want to |
| 2207 | * make it follow standard rules (eg: clear t->analysis). |
| 2208 | */ |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2209 | if (stats_check_uri_auth(t, rule_set)) |
| 2210 | return 1; |
| 2211 | } |
| 2212 | |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2213 | /* now check whether we have some switching rules for this request */ |
| 2214 | if (!(t->flags & SN_BE_ASSIGNED)) { |
| 2215 | struct switching_rule *rule; |
| 2216 | |
| 2217 | list_for_each_entry(rule, &cur_proxy->switching_rules, list) { |
| 2218 | int ret; |
| 2219 | |
| 2220 | 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] | 2221 | |
| 2222 | ret = acl_pass(ret); |
Willy Tarreau | a8cfa34 | 2008-07-09 11:23:31 +0200 | [diff] [blame] | 2223 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2224 | ret = !ret; |
| 2225 | |
| 2226 | if (ret) { |
| 2227 | t->be = rule->be.backend; |
| 2228 | t->be->beconn++; |
| 2229 | if (t->be->beconn > t->be->beconn_max) |
| 2230 | t->be->beconn_max = t->be->beconn; |
| 2231 | t->be->cum_beconn++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2232 | |
| 2233 | /* assign new parameters to the session from the new backend */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2234 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 2235 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2236 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2237 | t->flags |= SN_BE_ASSIGNED; |
| 2238 | break; |
| 2239 | } |
| 2240 | } |
| 2241 | } |
| 2242 | |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 2243 | if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) { |
| 2244 | /* No backend was set, but there was a default |
| 2245 | * backend set in the frontend, so we use it and |
| 2246 | * loop again. |
| 2247 | */ |
| 2248 | t->be = cur_proxy->defbe.be; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2249 | t->be->beconn++; |
| 2250 | if (t->be->beconn > t->be->beconn_max) |
| 2251 | t->be->beconn_max = t->be->beconn; |
| 2252 | t->be->cum_beconn++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2253 | |
| 2254 | /* assign new parameters to the session from the new backend */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2255 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 2256 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2257 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 2258 | t->flags |= SN_BE_ASSIGNED; |
| 2259 | } |
| 2260 | } 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] | 2261 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2262 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2263 | if (!(t->flags & SN_BE_ASSIGNED)) { |
| 2264 | /* To ensure correct connection accounting on |
| 2265 | * the backend, we count the connection for the |
| 2266 | * one managing the queue. |
| 2267 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2268 | t->be->beconn++; |
| 2269 | if (t->be->beconn > t->be->beconn_max) |
| 2270 | t->be->beconn_max = t->be->beconn; |
| 2271 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2272 | t->flags |= SN_BE_ASSIGNED; |
| 2273 | } |
| 2274 | |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2275 | /* |
| 2276 | * Right now, we know that we have processed the entire headers |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2277 | * and that unwanted requests have been filtered out. We can do |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2278 | * whatever we want with the remaining request. Also, now we |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2279 | * may have separate values for ->fe, ->be. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2280 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2281 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 2282 | /* |
| 2283 | * If HTTP PROXY is set we simply get remote server address |
| 2284 | * parsing incoming request. |
| 2285 | */ |
| 2286 | if ((t->be->options & PR_O_HTTP_PROXY) && !(t->flags & SN_ADDR_SET)) { |
| 2287 | url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &t->srv_addr); |
| 2288 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2289 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2290 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2291 | * 7: the appsession cookie was looked up very early in 1.2, |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2292 | * so let's do the same now. |
| 2293 | */ |
| 2294 | |
| 2295 | /* It needs to look into the URI */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2296 | if (t->be->appsession_name) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2297 | 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] | 2298 | } |
| 2299 | |
| 2300 | |
| 2301 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2302 | * 8: Now we can work with the cookies. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2303 | * Note that doing so might move headers in the request, but |
| 2304 | * the fields will stay coherent and the URI will not move. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2305 | * This should only be performed in the backend. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2306 | */ |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 2307 | if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name) |
| 2308 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2309 | manage_client_side_cookies(t, req); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2310 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2311 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2312 | /* |
Willy Tarreau | bb046ac | 2007-03-03 19:17:03 +0100 | [diff] [blame] | 2313 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 2314 | * asks for it. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2315 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2316 | if ((t->fe->options | t->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2317 | if (t->cli_addr.ss_family == AF_INET) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2318 | /* Add an X-Forwarded-For header unless the source IP is |
| 2319 | * in the 'except' network range. |
| 2320 | */ |
| 2321 | if ((!t->fe->except_mask.s_addr || |
| 2322 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr) |
| 2323 | != t->fe->except_net.s_addr) && |
| 2324 | (!t->be->except_mask.s_addr || |
| 2325 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr) |
| 2326 | != t->be->except_net.s_addr)) { |
| 2327 | int len; |
| 2328 | unsigned char *pn; |
| 2329 | pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr; |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2330 | |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2331 | /* Note: we rely on the backend to get the header name to be used for |
| 2332 | * x-forwarded-for, because the header is really meant for the backends. |
| 2333 | * However, if the backend did not specify any option, we have to rely |
| 2334 | * on the frontend's header name. |
| 2335 | */ |
| 2336 | if (t->be->fwdfor_hdr_len) { |
| 2337 | len = t->be->fwdfor_hdr_len; |
| 2338 | memcpy(trash, t->be->fwdfor_hdr_name, len); |
| 2339 | } else { |
| 2340 | len = t->fe->fwdfor_hdr_len; |
| 2341 | memcpy(trash, t->fe->fwdfor_hdr_name, len); |
| 2342 | } |
| 2343 | 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] | 2344 | |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2345 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2346 | &txn->hdr_idx, trash, len)) < 0) |
| 2347 | goto return_bad_req; |
| 2348 | } |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2349 | } |
| 2350 | else if (t->cli_addr.ss_family == AF_INET6) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2351 | /* FIXME: for the sake of completeness, we should also support |
| 2352 | * 'except' here, although it is mostly useless in this case. |
| 2353 | */ |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2354 | int len; |
| 2355 | char pn[INET6_ADDRSTRLEN]; |
| 2356 | inet_ntop(AF_INET6, |
| 2357 | (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr, |
| 2358 | pn, sizeof(pn)); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2359 | |
| 2360 | /* Note: we rely on the backend to get the header name to be used for |
| 2361 | * x-forwarded-for, because the header is really meant for the backends. |
| 2362 | * However, if the backend did not specify any option, we have to rely |
| 2363 | * on the frontend's header name. |
| 2364 | */ |
| 2365 | if (t->be->fwdfor_hdr_len) { |
| 2366 | len = t->be->fwdfor_hdr_len; |
| 2367 | memcpy(trash, t->be->fwdfor_hdr_name, len); |
| 2368 | } else { |
| 2369 | len = t->fe->fwdfor_hdr_len; |
| 2370 | memcpy(trash, t->fe->fwdfor_hdr_name, len); |
| 2371 | } |
| 2372 | len += sprintf(trash + len, ": %s", pn); |
| 2373 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2374 | if (unlikely(http_header_add_tail2(req, &txn->req, |
| 2375 | &txn->hdr_idx, trash, len)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2376 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2377 | } |
| 2378 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2379 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2380 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2381 | * 10: add "Connection: close" if needed and not yet set. |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2382 | * 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] | 2383 | */ |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2384 | if (!(t->flags & SN_CONN_CLOSED) && |
Krzysztof Oledzki | 336d475 | 2007-12-25 02:40:22 +0100 | [diff] [blame] | 2385 | ((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] | 2386 | if ((unlikely(msg->sl.rq.v_l != 8) || |
| 2387 | unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) && |
| 2388 | unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx, |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2389 | "Connection: close", 17)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2390 | goto return_bad_req; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2391 | t->flags |= SN_CONN_CLOSED; |
Willy Tarreau | e15d913 | 2006-12-14 22:26:42 +0100 | [diff] [blame] | 2392 | } |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2393 | /* Before we switch to data, was assignment set in manage_client_side_cookie? |
| 2394 | * If not assigned, perhaps we are balancing on url_param, but this is a |
| 2395 | * POST; and the parameters are in the body, maybe scan there to find our server. |
| 2396 | * (unless headers overflowed the buffer?) |
| 2397 | */ |
| 2398 | if (!(t->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 2399 | t->txn.meth == HTTP_METH_POST && t->be->url_param_name != NULL && |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2400 | t->be->url_param_post_limit != 0 && !(req->flags & BF_FULL) && |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2401 | memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) { |
| 2402 | /* are there enough bytes here? total == l || r || rlim ? |
| 2403 | * len is unsigned, but eoh is int, |
| 2404 | * how many bytes of body have we received? |
| 2405 | * eoh is the first empty line of the header |
| 2406 | */ |
| 2407 | /* 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] | 2408 | 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] | 2409 | |
| 2410 | /* If we have HTTP/1.1 and Expect: 100-continue, then abort. |
| 2411 | * We can't assume responsibility for the server's decision, |
| 2412 | * on this URI and header set. See rfc2616: 14.20, 8.2.3, |
| 2413 | * We also can't change our mind later, about which server to choose, so round robin. |
| 2414 | */ |
| 2415 | if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) { |
| 2416 | struct hdr_ctx ctx; |
| 2417 | ctx.idx = 0; |
| 2418 | /* Expect is allowed in 1.1, look for it */ |
| 2419 | http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx); |
| 2420 | if (ctx.idx != 0 && |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2421 | 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] | 2422 | /* We can't reliablly stall and wait for data, because of |
| 2423 | * .NET clients that don't conform to rfc2616; so, no need for |
| 2424 | * the next block to check length expectations. |
| 2425 | * We could send 100 status back to the client, but then we need to |
| 2426 | * re-write headers, and send the message. And this isn't the right |
| 2427 | * place for that action. |
| 2428 | * TODO: support Expect elsewhere and delete this block. |
| 2429 | */ |
| 2430 | goto end_check_maybe_wait_for_body; |
| 2431 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2432 | |
| 2433 | if (likely(len > t->be->url_param_post_limit)) { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2434 | /* nothing to do, we got enough */ |
| 2435 | } else { |
| 2436 | /* limit implies we are supposed to need this many bytes |
| 2437 | * to find the parameter. Let's see how many bytes we can wait for. |
| 2438 | */ |
| 2439 | long long hint = len; |
| 2440 | struct hdr_ctx ctx; |
| 2441 | ctx.idx = 0; |
| 2442 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2443 | 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] | 2444 | t->analysis |= AN_REQ_HTTP_BODY; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2445 | else { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2446 | ctx.idx = 0; |
| 2447 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx); |
| 2448 | /* now if we have a length, we'll take the hint */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2449 | if (ctx.idx) { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2450 | /* We have Content-Length */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2451 | if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint)) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2452 | hint = 0; /* parse failure, untrusted client */ |
| 2453 | else { |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2454 | if (hint > 0) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2455 | msg->hdr_content_len = hint; |
| 2456 | else |
| 2457 | hint = 0; /* bad client, sent negative length */ |
| 2458 | } |
| 2459 | } |
| 2460 | /* 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] | 2461 | if (t->be->url_param_post_limit < hint) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2462 | hint = t->be->url_param_post_limit; |
| 2463 | /* now do we really need to buffer more data? */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2464 | if (len < hint) |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 2465 | t->analysis |= AN_REQ_HTTP_BODY; |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2466 | /* else... There are no body bytes to wait for */ |
| 2467 | } |
| 2468 | } |
| 2469 | } |
| 2470 | end_check_maybe_wait_for_body: |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2471 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2472 | /************************************************************* |
| 2473 | * OK, that's finished for the headers. We have done what we * |
| 2474 | * could. Let's switch to the DATA state. * |
| 2475 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2476 | |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2477 | buffer_set_rlim(req, BUFSIZE); /* no more rewrite needed */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2478 | t->logs.tv_request = now; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2479 | |
Willy Tarreau | 1fa3126 | 2007-12-03 00:36:16 +0100 | [diff] [blame] | 2480 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 2481 | * which may be the same as the connect timeout if unspecified. |
| 2482 | * If unset, then set it to zero because we really want it to |
| 2483 | * eventually expire. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2484 | * FIXME: this part should be moved elsewhere (eg: on the server side) |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2485 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2486 | if (txn->flags & TX_CLTARPIT) { |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2487 | buffer_flush(t->req); |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2488 | /* flush the request so that we can drop the connection early |
| 2489 | * if the client closes first. |
| 2490 | */ |
Willy Tarreau | 26ed74d | 2008-08-17 12:11:14 +0200 | [diff] [blame] | 2491 | req->wex = tick_add_ifset(now_ms, t->be->timeout.tarpit); |
| 2492 | if (!req->wex) |
| 2493 | req->wex = now_ms; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2494 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2495 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2496 | /* OK let's go on with the BODY now */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2497 | goto end_of_headers; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2498 | |
| 2499 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2500 | txn->req.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2501 | txn->status = 400; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2502 | t->analysis &= ~AN_REQ_ANY; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2503 | client_retnclose(t, error_message(t, HTTP_ERR_400)); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 2504 | t->fe->failed_req++; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2505 | return_prx_cond: |
| 2506 | if (!(t->flags & SN_ERR_MASK)) |
| 2507 | t->flags |= SN_ERR_PRXCOND; |
| 2508 | if (!(t->flags & SN_FINST_MASK)) |
| 2509 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2510 | return 0; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2511 | end_of_headers: |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2512 | ; // to keep gcc happy |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2513 | } |
| 2514 | |
| 2515 | if (t->analysis & AN_REQ_HTTP_BODY) { |
| 2516 | /* We have to parse the HTTP request body to find any required data. |
| 2517 | * "balance url_param check_post" should have been the only way to get |
| 2518 | * into this. We were brought here after HTTP header analysis, so all |
| 2519 | * related structures are ready. |
| 2520 | */ |
| 2521 | struct http_msg *msg = &t->txn.req; |
| 2522 | unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1; |
| 2523 | long long limit = t->be->url_param_post_limit; |
| 2524 | struct hdr_ctx ctx; |
| 2525 | |
| 2526 | ctx.idx = 0; |
| 2527 | |
| 2528 | /* now if we have a length, we'll take the hint */ |
| 2529 | http_find_header2("Transfer-Encoding", 17, msg->sol, &t->txn.hdr_idx, &ctx); |
| 2530 | if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) { |
| 2531 | unsigned int chunk = 0; |
| 2532 | while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) { |
| 2533 | char c = msg->sol[body]; |
| 2534 | if (ishex(c)) { |
| 2535 | unsigned int hex = toupper(c) - '0'; |
| 2536 | if (hex > 9) |
| 2537 | hex -= 'A' - '9' - 1; |
| 2538 | chunk = (chunk << 4) | hex; |
| 2539 | } else |
| 2540 | break; |
| 2541 | body++; |
| 2542 | } |
| 2543 | if (body + 2 >= req->l) /* we want CRLF too */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2544 | goto http_body_end; /* end of buffer? data missing! */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2545 | |
| 2546 | if (memcmp(msg->sol+body, "\r\n", 2) != 0) |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2547 | goto http_body_end; /* chunked encoding len ends with CRLF, and we don't have it yet */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2548 | |
| 2549 | body += 2; // skip CRLF |
| 2550 | |
| 2551 | /* if we support more then one chunk here, we have to do it again when assigning server |
| 2552 | * 1. how much entity data do we have? new var |
| 2553 | * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here |
| 2554 | * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found) |
| 2555 | */ |
| 2556 | |
| 2557 | if (chunk < limit) |
| 2558 | limit = chunk; /* only reading one chunk */ |
| 2559 | } else { |
| 2560 | if (msg->hdr_content_len < limit) |
| 2561 | limit = msg->hdr_content_len; |
| 2562 | } |
| 2563 | |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2564 | http_body_end: |
| 2565 | /* we leave once we know we have nothing left to do. This means that we have |
| 2566 | * enough bytes, or that we know we'll not get any more (buffer full, read |
| 2567 | * buffer closed). |
| 2568 | */ |
| 2569 | if (req->l - body >= limit || /* enough bytes! */ |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2570 | req->flags & (BF_FULL | BF_READ_ERROR | BF_READ_NULL | BF_READ_TIMEOUT)) { |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2571 | /* The situation will not evolve, so let's give up on the analysis. */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2572 | t->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 2573 | t->analysis &= ~AN_REQ_HTTP_BODY; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2574 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2575 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2576 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2577 | /* we want to leave with that clean */ |
| 2578 | if (unlikely(t->analysis & AN_REQ_ANY)) |
| 2579 | goto update_state; |
| 2580 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2581 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2582 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2583 | /* This function performs all the processing enabled for the current response. |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2584 | * It normally returns zero, but may return 1 if it absolutely needs to be |
| 2585 | * called again after other functions. It relies on buffers flags, and updates |
| 2586 | * t->analysis. It might make sense to explode it into several other functions. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2587 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2588 | int process_response(struct session *t) |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2589 | { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2590 | struct http_txn *txn = &t->txn; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2591 | struct buffer *req = t->req; |
| 2592 | struct buffer *rep = t->rep; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2593 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2594 | DPRINTF(stderr,"[%u] process_rep: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x analysis=%02x\n", |
Willy Tarreau | e46ab55 | 2008-08-13 19:19:37 +0200 | [diff] [blame] | 2595 | now_ms, |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2596 | cli_stnames[t->cli_state], srv_stnames[t->srv_state], |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 2597 | t->srv_fd >= 0 && fdtab[t->srv_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->srv_fd, DIR_RD) : 0, |
| 2598 | t->srv_fd >= 0 && fdtab[t->srv_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->srv_fd, DIR_WR) : 0, |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2599 | req->rex, rep->wex, req->flags, rep->flags, t->analysis); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 2600 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2601 | update_state: |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2602 | if (t->analysis & AN_RTR_HTTP_HDR) { /* receiving server headers */ |
| 2603 | /* |
| 2604 | * Now parse the partial (or complete) lines. |
| 2605 | * We will check the response syntax, and also join multi-line |
| 2606 | * headers. An index of all the lines will be elaborated while |
| 2607 | * parsing. |
| 2608 | * |
| 2609 | * For the parsing, we use a 28 states FSM. |
| 2610 | * |
| 2611 | * Here is the information we currently have : |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2612 | * rep->data + rep->som = beginning of response |
| 2613 | * rep->data + rep->eoh = end of processed headers / start of current one |
| 2614 | * rep->data + rep->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2615 | * rep->lr = first non-visited byte |
| 2616 | * rep->r = end of data |
| 2617 | */ |
Willy Tarreau | 7f875f6 | 2008-08-11 17:35:01 +0200 | [diff] [blame] | 2618 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2619 | int cur_idx; |
| 2620 | struct http_msg *msg = &txn->rsp; |
| 2621 | struct proxy *cur_proxy; |
| 2622 | |
| 2623 | if (likely(rep->lr < rep->r)) |
| 2624 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
| 2625 | |
| 2626 | /* 1: we might have to print this header in debug mode */ |
| 2627 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2628 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
| 2629 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
| 2630 | char *eol, *sol; |
| 2631 | |
| 2632 | sol = rep->data + msg->som; |
| 2633 | eol = sol + msg->sl.rq.l; |
| 2634 | debug_hdr("srvrep", t, sol, eol); |
| 2635 | |
| 2636 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2637 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
| 2638 | |
| 2639 | while (cur_idx) { |
| 2640 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2641 | debug_hdr("srvhdr", t, sol, eol); |
| 2642 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2643 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2644 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2645 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2646 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2647 | /* |
| 2648 | * Now we quickly check if we have found a full valid response. |
| 2649 | * If not so, we check the FD and buffer states before leaving. |
| 2650 | * A full response is indicated by the fact that we have seen |
| 2651 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 2652 | * responses are checked first. |
| 2653 | * |
| 2654 | * Depending on whether the client is still there or not, we |
| 2655 | * may send an error response back or not. Note that normally |
| 2656 | * we should only check for HTTP status there, and check I/O |
| 2657 | * errors somewhere else. |
| 2658 | */ |
| 2659 | |
| 2660 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2661 | /* Invalid response */ |
| 2662 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 2663 | hdr_response_bad: |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 2664 | buffer_shutr(rep); |
| 2665 | buffer_shutw(req); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2666 | fd_delete(t->srv_fd); |
| 2667 | if (t->srv) { |
| 2668 | t->srv->cur_sess--; |
| 2669 | t->srv->failed_resp++; |
| 2670 | sess_change_server(t, NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2671 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2672 | t->be->failed_resp++; |
| 2673 | t->srv_state = SV_STCLOSE; |
| 2674 | t->analysis &= ~AN_RTR_ANY; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2675 | txn->status = 502; |
| 2676 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 2677 | if (!(t->flags & SN_ERR_MASK)) |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2678 | t->flags |= SN_ERR_PRXCOND; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2679 | if (!(t->flags & SN_FINST_MASK)) |
| 2680 | t->flags |= SN_FINST_H; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 2681 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2682 | if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 2683 | process_srv_queue(t->srv); |
| 2684 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2685 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2686 | } |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2687 | /* write error to client, read error or close from server */ |
| 2688 | if (req->flags & BF_WRITE_ERROR || |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 2689 | rep->flags & (BF_READ_ERROR | BF_READ_NULL | BF_SHUTW)) { |
| 2690 | buffer_shutr(rep); |
| 2691 | buffer_shutw(req); |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2692 | fd_delete(t->srv_fd); |
| 2693 | if (t->srv) { |
| 2694 | t->srv->cur_sess--; |
| 2695 | t->srv->failed_resp++; |
| 2696 | sess_change_server(t, NULL); |
| 2697 | } |
| 2698 | t->be->failed_resp++; |
| 2699 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2700 | t->analysis &= ~AN_RTR_ANY; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2701 | txn->status = 502; |
| 2702 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 2703 | if (!(t->flags & SN_ERR_MASK)) |
| 2704 | t->flags |= SN_ERR_SRVCL; |
| 2705 | if (!(t->flags & SN_FINST_MASK)) |
| 2706 | t->flags |= SN_FINST_H; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 2707 | |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2708 | if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 2709 | process_srv_queue(t->srv); |
| 2710 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2711 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2712 | } |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2713 | /* too large response does not fit in buffer. */ |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2714 | else if (rep->flags & BF_FULL) { |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2715 | goto hdr_response_bad; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 2716 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2717 | /* read timeout : return a 504 to the client. */ |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2718 | else if (rep->flags & BF_READ_TIMEOUT) { |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 2719 | buffer_shutr(rep); |
| 2720 | buffer_shutw(req); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2721 | fd_delete(t->srv_fd); |
| 2722 | if (t->srv) { |
| 2723 | t->srv->cur_sess--; |
| 2724 | t->srv->failed_resp++; |
| 2725 | sess_change_server(t, NULL); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2726 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2727 | t->be->failed_resp++; |
| 2728 | t->srv_state = SV_STCLOSE; |
| 2729 | t->analysis &= ~AN_RTR_ANY; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2730 | txn->status = 504; |
| 2731 | client_return(t, error_message(t, HTTP_ERR_504)); |
| 2732 | if (!(t->flags & SN_ERR_MASK)) |
| 2733 | t->flags |= SN_ERR_SRVTO; |
| 2734 | if (!(t->flags & SN_FINST_MASK)) |
| 2735 | t->flags |= SN_FINST_H; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 2736 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2737 | if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 2738 | process_srv_queue(t->srv); |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2739 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2740 | } |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2741 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2742 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2743 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2744 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2745 | /***************************************************************** |
| 2746 | * More interesting part now : we know that we have a complete * |
| 2747 | * response which at least looks like HTTP. We have an indicator * |
| 2748 | * of each header's length, so we can parse them quickly. * |
| 2749 | ****************************************************************/ |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2750 | |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 2751 | t->analysis &= ~AN_RTR_HTTP_HDR; |
| 2752 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2753 | /* ensure we keep this pointer to the beginning of the message */ |
| 2754 | msg->sol = rep->data + msg->som; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2755 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2756 | /* |
| 2757 | * 1: get the status code and check for cacheability. |
| 2758 | */ |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2759 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2760 | t->logs.logwait &= ~LW_RESP; |
| 2761 | txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2762 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2763 | switch (txn->status) { |
| 2764 | case 200: |
| 2765 | case 203: |
| 2766 | case 206: |
| 2767 | case 300: |
| 2768 | case 301: |
| 2769 | case 410: |
| 2770 | /* RFC2616 @13.4: |
| 2771 | * "A response received with a status code of |
| 2772 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 2773 | * by a cache (...) unless a cache-control |
| 2774 | * directive prohibits caching." |
| 2775 | * |
| 2776 | * RFC2616 @9.5: POST method : |
| 2777 | * "Responses to this method are not cacheable, |
| 2778 | * unless the response includes appropriate |
| 2779 | * Cache-Control or Expires header fields." |
| 2780 | */ |
| 2781 | if (likely(txn->meth != HTTP_METH_POST) && |
| 2782 | (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
| 2783 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 2784 | break; |
| 2785 | default: |
| 2786 | break; |
| 2787 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2788 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2789 | /* |
| 2790 | * 2: we may need to capture headers |
| 2791 | */ |
| 2792 | if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap)) |
| 2793 | capture_headers(rep->data + msg->som, &txn->hdr_idx, |
| 2794 | txn->rsp.cap, t->fe->rsp_cap); |
| 2795 | |
| 2796 | /* |
| 2797 | * 3: we will have to evaluate the filters. |
| 2798 | * As opposed to version 1.2, now they will be evaluated in the |
| 2799 | * filters order and not in the header order. This means that |
| 2800 | * each filter has to be validated among all headers. |
| 2801 | * |
| 2802 | * Filters are tried with ->be first, then with ->fe if it is |
| 2803 | * different from ->be. |
| 2804 | */ |
| 2805 | |
| 2806 | t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */ |
| 2807 | |
| 2808 | cur_proxy = t->be; |
| 2809 | while (1) { |
| 2810 | struct proxy *rule_set = cur_proxy; |
| 2811 | |
| 2812 | /* try headers filters */ |
| 2813 | if (rule_set->rsp_exp != NULL) { |
| 2814 | if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) { |
| 2815 | return_bad_resp: |
| 2816 | if (t->srv) { |
| 2817 | t->srv->cur_sess--; |
| 2818 | t->srv->failed_resp++; |
| 2819 | sess_change_server(t, NULL); |
| 2820 | } |
| 2821 | cur_proxy->failed_resp++; |
| 2822 | return_srv_prx_502: |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 2823 | buffer_shutr(rep); |
| 2824 | buffer_shutw(req); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2825 | fd_delete(t->srv_fd); |
| 2826 | t->srv_state = SV_STCLOSE; |
| 2827 | t->analysis &= ~AN_RTR_ANY; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2828 | txn->status = 502; |
| 2829 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 2830 | if (!(t->flags & SN_ERR_MASK)) |
| 2831 | t->flags |= SN_ERR_PRXCOND; |
| 2832 | if (!(t->flags & SN_FINST_MASK)) |
| 2833 | t->flags |= SN_FINST_H; |
| 2834 | /* We used to have a free connection slot. Since we'll never use it, |
| 2835 | * we have to inform the server that it may be used by another session. |
| 2836 | */ |
| 2837 | if (t->srv && may_dequeue_tasks(t->srv, cur_proxy)) |
| 2838 | process_srv_queue(t->srv); |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2839 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2840 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2841 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 2842 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2843 | /* has the response been denied ? */ |
| 2844 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | f899b94 | 2008-03-28 18:09:38 +0100 | [diff] [blame] | 2845 | if (t->srv) { |
| 2846 | t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2847 | t->srv->failed_secu++; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 2848 | sess_change_server(t, NULL); |
Willy Tarreau | f899b94 | 2008-03-28 18:09:38 +0100 | [diff] [blame] | 2849 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2850 | cur_proxy->denied_resp++; |
| 2851 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 2852 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2853 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2854 | /* We might have to check for "Connection:" */ |
| 2855 | if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) && |
| 2856 | !(t->flags & SN_CONN_CLOSED)) { |
| 2857 | char *cur_ptr, *cur_end, *cur_next; |
| 2858 | int cur_idx, old_idx, delta, val; |
| 2859 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2860 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2861 | cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 2862 | old_idx = 0; |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 2863 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2864 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 2865 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 2866 | cur_ptr = cur_next; |
| 2867 | cur_end = cur_ptr + cur_hdr->len; |
| 2868 | cur_next = cur_end + cur_hdr->cr + 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2869 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2870 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 2871 | if (val) { |
| 2872 | /* 3 possibilities : |
| 2873 | * - we have already set Connection: close, |
| 2874 | * so we remove this line. |
| 2875 | * - we have not yet set Connection: close, |
| 2876 | * but this line indicates close. We leave |
| 2877 | * it untouched and set the flag. |
| 2878 | * - we have not yet set Connection: close, |
| 2879 | * and this line indicates non-close. We |
| 2880 | * replace it. |
| 2881 | */ |
| 2882 | if (t->flags & SN_CONN_CLOSED) { |
| 2883 | delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0); |
| 2884 | txn->rsp.eoh += delta; |
| 2885 | cur_next += delta; |
| 2886 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 2887 | txn->hdr_idx.used--; |
| 2888 | cur_hdr->len = 0; |
| 2889 | } else { |
| 2890 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 2891 | delta = buffer_replace2(rep, cur_ptr + val, cur_end, |
| 2892 | "close", 5); |
| 2893 | cur_next += delta; |
| 2894 | cur_hdr->len += delta; |
| 2895 | txn->rsp.eoh += delta; |
| 2896 | } |
| 2897 | t->flags |= SN_CONN_CLOSED; |
| 2898 | } |
| 2899 | } |
| 2900 | old_idx = cur_idx; |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 2901 | } |
| 2902 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2903 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2904 | /* add response headers from the rule sets in the same order */ |
| 2905 | for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) { |
| 2906 | if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, |
| 2907 | rule_set->rsp_add[cur_idx])) < 0) |
| 2908 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 2909 | } |
| 2910 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2911 | /* check whether we're already working on the frontend */ |
| 2912 | if (cur_proxy == t->fe) |
| 2913 | break; |
| 2914 | cur_proxy = t->fe; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2915 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2916 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2917 | /* |
| 2918 | * 4: check for server cookie. |
| 2919 | */ |
| 2920 | if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name |
| 2921 | || (t->be->options & PR_O_CHK_CACHE)) |
| 2922 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2923 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2924 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2925 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2926 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2927 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2928 | if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0) |
| 2929 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2930 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2931 | /* |
| 2932 | * 6: add server cookie in the response if needed |
| 2933 | */ |
| 2934 | if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) && |
| 2935 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) { |
| 2936 | int len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2937 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2938 | /* the server is known, it's not the one the client requested, we have to |
| 2939 | * insert a set-cookie here, except if we want to insert only on POST |
| 2940 | * requests and this one isn't. Note that servers which don't have cookies |
| 2941 | * (eg: some backup servers) will return a full cookie removal request. |
| 2942 | */ |
| 2943 | len = sprintf(trash, "Set-Cookie: %s=%s; path=/", |
| 2944 | t->be->cookie_name, |
| 2945 | 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] | 2946 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2947 | if (t->be->cookie_domain) |
| 2948 | len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2949 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2950 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 2951 | trash, len)) < 0) |
| 2952 | goto return_bad_resp; |
| 2953 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2954 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2955 | /* Here, we will tell an eventual cache on the client side that we don't |
| 2956 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 2957 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 2958 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 2959 | */ |
| 2960 | if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2961 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2962 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 2963 | |
| 2964 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 2965 | "Cache-control: private", 22)) < 0) |
| 2966 | goto return_bad_resp; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2967 | } |
| 2968 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2969 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2970 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2971 | /* |
| 2972 | * 7: check if result will be cacheable with a cookie. |
| 2973 | * We'll block the response if security checks have caught |
| 2974 | * nasty things such as a cacheable cookie. |
| 2975 | */ |
| 2976 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) == |
| 2977 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) && |
| 2978 | (t->be->options & PR_O_CHK_CACHE)) { |
| 2979 | |
| 2980 | /* we're in presence of a cacheable response containing |
| 2981 | * a set-cookie header. We'll block it as requested by |
| 2982 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2983 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2984 | if (t->srv) { |
| 2985 | t->srv->cur_sess--; |
| 2986 | t->srv->failed_secu++; |
| 2987 | sess_change_server(t, NULL); |
| 2988 | } |
| 2989 | t->be->denied_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2990 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2991 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 2992 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 2993 | send_log(t->be, LOG_ALERT, |
| 2994 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 2995 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 2996 | goto return_srv_prx_502; |
| 2997 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2998 | |
| 2999 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3000 | * 8: add "Connection: close" if needed and not yet set. |
| 3001 | * 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] | 3002 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3003 | if (!(t->flags & SN_CONN_CLOSED) && |
| 3004 | ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) { |
| 3005 | if ((unlikely(msg->sl.st.v_l != 8) || |
| 3006 | unlikely(req->data[msg->som + 7] != '0')) && |
| 3007 | unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3008 | "Connection: close", 17)) < 0) |
| 3009 | goto return_bad_resp; |
| 3010 | t->flags |= SN_CONN_CLOSED; |
| 3011 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3012 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3013 | /************************************************************* |
| 3014 | * OK, that's finished for the headers. We have done what we * |
| 3015 | * could. Let's switch to the DATA state. * |
| 3016 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3017 | |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 3018 | buffer_set_rlim(rep, BUFSIZE); /* no more rewrite needed */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3019 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3020 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3021 | #ifdef CONFIG_HAP_TCPSPLICE |
| 3022 | if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
| 3023 | /* TCP splicing supported by both FE and BE */ |
| 3024 | tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0); |
| 3025 | } |
| 3026 | #endif |
| 3027 | /* if the user wants to log as soon as possible, without counting |
| 3028 | * bytes from the server, then this is the right moment. We have |
| 3029 | * to temporarily assign bytes_out to log what we currently have. |
| 3030 | */ |
| 3031 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 3032 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 3033 | t->logs.bytes_out = txn->rsp.eoh; |
| 3034 | if (t->fe->to_log & LW_REQ) |
| 3035 | http_sess_log(t); |
| 3036 | else |
| 3037 | tcp_sess_log(t); |
| 3038 | t->logs.bytes_out = 0; |
| 3039 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3040 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3041 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 3042 | * otherwise we would not let the client side wake up. |
| 3043 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3044 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3045 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3046 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3047 | |
| 3048 | /* we want to leave with that clean */ |
| 3049 | if (unlikely(t->analysis & AN_RTR_ANY)) |
| 3050 | goto update_state; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3051 | return 0; |
| 3052 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3053 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3054 | /* |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3055 | * Manages the client FSM and its socket. It normally returns zero, but may |
| 3056 | * return 1 if it absolutely wants to be called again. |
| 3057 | * |
| 3058 | * Note: process_cli is the ONLY function allowed to set cli_state to anything |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 3059 | * but CL_STCLOSE. |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3060 | */ |
| 3061 | int process_cli(struct session *t) |
| 3062 | { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3063 | struct buffer *req = t->req; |
| 3064 | struct buffer *rep = t->rep; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3065 | |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 3066 | DPRINTF(stderr,"[%u] process_cli: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n", |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3067 | now_ms, |
| 3068 | cli_stnames[t->cli_state], srv_stnames[t->srv_state], |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 3069 | t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_RD) : 0, |
| 3070 | t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_WR) : 0, |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3071 | req->rex, rep->wex, |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 3072 | req->flags, rep->flags, |
| 3073 | req->l, rep->l); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3074 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3075 | update_state: |
Willy Tarreau | 1ae3a05 | 2008-08-16 10:56:30 +0200 | [diff] [blame] | 3076 | /* FIXME: we still have to check for CL_STSHUTR because client_retnclose |
| 3077 | * still set this state (and will do until unix sockets are converted). |
| 3078 | */ |
| 3079 | if (t->cli_state == CL_STDATA || t->cli_state == CL_STSHUTR) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3080 | /* read or write error */ |
| 3081 | if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) { |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3082 | buffer_shutr(req); |
| 3083 | buffer_shutw(rep); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3084 | fd_delete(t->cli_fd); |
| 3085 | t->cli_state = CL_STCLOSE; |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3086 | trace_term(t, TT_HTTP_CLI_1); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3087 | if (!(t->flags & SN_ERR_MASK)) |
| 3088 | t->flags |= SN_ERR_CLICL; |
| 3089 | if (!(t->flags & SN_FINST_MASK)) { |
| 3090 | if (t->analysis & AN_REQ_ANY) |
| 3091 | t->flags |= SN_FINST_R; |
| 3092 | else if (t->pend_pos) |
| 3093 | t->flags |= SN_FINST_Q; |
Willy Tarreau | 1ae3a05 | 2008-08-16 10:56:30 +0200 | [diff] [blame] | 3094 | else if (t->srv_state == SV_STCONN) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3095 | t->flags |= SN_FINST_C; |
| 3096 | else |
| 3097 | t->flags |= SN_FINST_D; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3098 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3099 | goto update_state; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3100 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3101 | /* last read, or end of server write */ |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3102 | else if (!(req->flags & BF_SHUTR) && /* already done */ |
| 3103 | req->flags & (BF_READ_NULL | BF_SHUTW)) { |
| 3104 | buffer_shutr(req); |
| 3105 | if (!(rep->flags & BF_SHUTW)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3106 | EV_FD_CLR(t->cli_fd, DIR_RD); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3107 | trace_term(t, TT_HTTP_CLI_2); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3108 | } else { |
| 3109 | /* output was already closed */ |
| 3110 | fd_delete(t->cli_fd); |
| 3111 | t->cli_state = CL_STCLOSE; |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3112 | trace_term(t, TT_HTTP_CLI_3); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3113 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3114 | goto update_state; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3115 | } |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 3116 | /* last server read and buffer empty : we only check them when we're |
| 3117 | * allowed to forward the data. |
| 3118 | */ |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3119 | else if (!(rep->flags & BF_SHUTW) && /* already done */ |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 3120 | rep->flags & BF_EMPTY && rep->flags & BF_MAY_FORWARD && |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3121 | rep->flags & BF_SHUTR && !(t->flags & SN_SELF_GEN)) { |
| 3122 | buffer_shutw(rep); |
| 3123 | if (!(req->flags & BF_SHUTR)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3124 | EV_FD_CLR(t->cli_fd, DIR_WR); |
| 3125 | shutdown(t->cli_fd, SHUT_WR); |
| 3126 | /* We must ensure that the read part is still alive when switching to shutw */ |
| 3127 | /* FIXME: is this still true ? */ |
| 3128 | EV_FD_SET(t->cli_fd, DIR_RD); |
| 3129 | req->rex = tick_add_ifset(now_ms, t->fe->timeout.client); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3130 | trace_term(t, TT_HTTP_CLI_4); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3131 | } else { |
| 3132 | fd_delete(t->cli_fd); |
| 3133 | t->cli_state = CL_STCLOSE; |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3134 | trace_term(t, TT_HTTP_CLI_5); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3135 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3136 | goto update_state; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3137 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3138 | /* read timeout */ |
Willy Tarreau | 507385d | 2008-08-17 13:04:25 +0200 | [diff] [blame^] | 3139 | else if (req->flags & BF_READ_TIMEOUT) { |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3140 | buffer_shutr(req); |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3141 | if (!(rep->flags & BF_SHUTW)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3142 | EV_FD_CLR(t->cli_fd, DIR_RD); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3143 | trace_term(t, TT_HTTP_CLI_6); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3144 | } else { |
| 3145 | /* output was already closed */ |
| 3146 | fd_delete(t->cli_fd); |
| 3147 | t->cli_state = CL_STCLOSE; |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3148 | trace_term(t, TT_HTTP_CLI_7); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3149 | } |
| 3150 | if (!(t->flags & SN_ERR_MASK)) |
| 3151 | t->flags |= SN_ERR_CLITO; |
| 3152 | if (!(t->flags & SN_FINST_MASK)) { |
| 3153 | if (t->analysis & AN_REQ_ANY) |
| 3154 | t->flags |= SN_FINST_R; |
| 3155 | else if (t->pend_pos) |
| 3156 | t->flags |= SN_FINST_Q; |
Willy Tarreau | 1ae3a05 | 2008-08-16 10:56:30 +0200 | [diff] [blame] | 3157 | else if (t->srv_state == SV_STCONN) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3158 | t->flags |= SN_FINST_C; |
| 3159 | else |
| 3160 | t->flags |= SN_FINST_D; |
| 3161 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3162 | goto update_state; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3163 | } |
| 3164 | /* write timeout */ |
Willy Tarreau | 507385d | 2008-08-17 13:04:25 +0200 | [diff] [blame^] | 3165 | else if (rep->flags & BF_WRITE_TIMEOUT) { |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3166 | buffer_shutw(rep); |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3167 | if (!(req->flags & BF_SHUTR)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3168 | EV_FD_CLR(t->cli_fd, DIR_WR); |
| 3169 | shutdown(t->cli_fd, SHUT_WR); |
| 3170 | /* We must ensure that the read part is still alive when switching to shutw */ |
| 3171 | /* FIXME: is this still true ? */ |
| 3172 | EV_FD_SET(t->cli_fd, DIR_RD); |
| 3173 | req->rex = tick_add_ifset(now_ms, t->fe->timeout.client); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3174 | trace_term(t, TT_HTTP_CLI_8); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3175 | } else { |
| 3176 | fd_delete(t->cli_fd); |
| 3177 | t->cli_state = CL_STCLOSE; |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3178 | trace_term(t, TT_HTTP_CLI_9); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3179 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3180 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3181 | if (!(t->flags & SN_ERR_MASK)) |
| 3182 | t->flags |= SN_ERR_CLITO; |
| 3183 | if (!(t->flags & SN_FINST_MASK)) { |
| 3184 | if (t->analysis & AN_REQ_ANY) |
| 3185 | t->flags |= SN_FINST_R; |
| 3186 | else if (t->pend_pos) |
| 3187 | t->flags |= SN_FINST_Q; |
Willy Tarreau | 1ae3a05 | 2008-08-16 10:56:30 +0200 | [diff] [blame] | 3188 | else if (t->srv_state == SV_STCONN) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3189 | t->flags |= SN_FINST_C; |
| 3190 | else |
| 3191 | t->flags |= SN_FINST_D; |
| 3192 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3193 | goto update_state; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3194 | } |
| 3195 | |
| 3196 | /* manage read timeout */ |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3197 | if (!(req->flags & BF_SHUTR)) { |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 3198 | if (req->flags & BF_FULL) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3199 | /* no room to read more data */ |
| 3200 | if (EV_FD_COND_C(t->cli_fd, DIR_RD)) { |
| 3201 | /* stop reading until we get some space */ |
| 3202 | req->rex = TICK_ETERNITY; |
| 3203 | } |
| 3204 | } else { |
| 3205 | EV_FD_COND_S(t->cli_fd, DIR_RD); |
| 3206 | req->rex = tick_add_ifset(now_ms, t->fe->timeout.client); |
| 3207 | } |
| 3208 | } |
| 3209 | |
| 3210 | /* manage write timeout */ |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3211 | if (!(rep->flags & BF_SHUTW)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3212 | /* first, we may have to produce data (eg: stats). |
| 3213 | * right now, this is limited to the SHUTR state. |
| 3214 | */ |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3215 | if (req->flags & BF_SHUTR && t->flags & SN_SELF_GEN) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3216 | produce_content(t); |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 3217 | if (rep->flags & BF_EMPTY) { |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3218 | buffer_shutw(rep); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3219 | fd_delete(t->cli_fd); |
| 3220 | t->cli_state = CL_STCLOSE; |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3221 | trace_term(t, TT_HTTP_CLI_10); |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3222 | goto update_state; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3223 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3224 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3225 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3226 | /* we don't enable client write if the buffer is empty, nor if the server has to analyze it */ |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 3227 | if ((rep->flags & BF_EMPTY) || !(rep->flags & BF_MAY_FORWARD)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3228 | if (EV_FD_COND_C(t->cli_fd, DIR_WR)) { |
| 3229 | /* stop writing */ |
| 3230 | rep->wex = TICK_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3231 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3232 | } else { |
| 3233 | /* buffer not empty */ |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 3234 | EV_FD_COND_S(t->cli_fd, DIR_WR); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3235 | if (!tick_isset(rep->wex)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3236 | /* restart writing */ |
| 3237 | rep->wex = tick_add_ifset(now_ms, t->fe->timeout.client); |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3238 | if (!(req->flags & BF_SHUTR) && tick_isset(rep->wex) && tick_isset(req->rex)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3239 | /* FIXME: to prevent the client from expiring read timeouts during writes, |
| 3240 | * we refresh it, except if it was already infinite. */ |
| 3241 | req->rex = rep->wex; |
| 3242 | } |
| 3243 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3244 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3245 | } |
| 3246 | return 0; /* other cases change nothing */ |
| 3247 | } |
Willy Tarreau | 1ae3a05 | 2008-08-16 10:56:30 +0200 | [diff] [blame] | 3248 | else if (t->cli_state == CL_STCLOSE) { /* CL_STCLOSE: nothing to do */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3249 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 3250 | int len; |
| 3251 | 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); |
| 3252 | write(1, trash, len); |
| 3253 | } |
| 3254 | return 0; |
| 3255 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3256 | #ifdef DEBUG_DEV |
| 3257 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, t->cli_state); |
| 3258 | ABORT_NOW(); |
Willy Tarreau | 1ae3a05 | 2008-08-16 10:56:30 +0200 | [diff] [blame] | 3259 | #endif |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3260 | return 0; |
| 3261 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3262 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3263 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3264 | /* |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3265 | * Manages the server FSM and its socket. It normally returns zero, but may |
| 3266 | * return 1 if it absolutely wants to be called again. |
| 3267 | * |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 3268 | * Note: process_srv is the ONLY function allowed to set srv_state to anything |
| 3269 | * but SV_STCLOSE. The only exception is for functions called from this |
| 3270 | * one (eg: those in backend.c). |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3271 | */ |
| 3272 | int process_srv(struct session *t) |
| 3273 | { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3274 | struct http_txn *txn = &t->txn; |
| 3275 | struct buffer *req = t->req; |
| 3276 | struct buffer *rep = t->rep; |
| 3277 | int conn_err; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3278 | |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 3279 | DPRINTF(stderr,"[%u] process_srv: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n", |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3280 | now_ms, |
| 3281 | cli_stnames[t->cli_state], srv_stnames[t->srv_state], |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 3282 | t->srv_fd >= 0 && fdtab[t->srv_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->srv_fd, DIR_RD) : 0, |
| 3283 | t->srv_fd >= 0 && fdtab[t->srv_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->srv_fd, DIR_WR) : 0, |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3284 | rep->rex, req->wex, |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 3285 | req->flags, rep->flags, |
| 3286 | req->l, rep->l); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3287 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3288 | update_state: |
Willy Tarreau | 1ae3a05 | 2008-08-16 10:56:30 +0200 | [diff] [blame] | 3289 | if (t->srv_state == SV_STIDLE) { |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3290 | if ((rep->flags & BF_SHUTW) || |
| 3291 | ((req->flags & BF_SHUTR) && |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 3292 | (req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */ |
Willy Tarreau | 26ed74d | 2008-08-17 12:11:14 +0200 | [diff] [blame] | 3293 | req->wex = TICK_ETERNITY; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3294 | if (t->pend_pos) |
| 3295 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 3296 | /* note that this must not return any error because it would be able to |
| 3297 | * overwrite the client_retnclose() output. |
| 3298 | */ |
| 3299 | if (txn->flags & TX_CLTARPIT) |
| 3300 | srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_T, 0, NULL); |
| 3301 | else |
| 3302 | srv_close_with_err(t, SN_ERR_CLICL, t->pend_pos ? SN_FINST_Q : SN_FINST_C, 0, NULL); |
| 3303 | |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3304 | trace_term(t, TT_HTTP_SRV_1); |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3305 | goto update_state; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3306 | } |
Willy Tarreau | d9f4836 | 2008-08-16 16:39:26 +0200 | [diff] [blame] | 3307 | else if (req->flags & BF_MAY_FORWARD) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3308 | /* the client allows the server to connect */ |
| 3309 | if (txn->flags & TX_CLTARPIT) { |
| 3310 | /* This connection is being tarpitted. The CLIENT side has |
| 3311 | * already set the connect expiration date to the right |
| 3312 | * timeout. We just have to check that it has not expired. |
| 3313 | */ |
Willy Tarreau | 507385d | 2008-08-17 13:04:25 +0200 | [diff] [blame^] | 3314 | if (!(req->flags & BF_WRITE_TIMEOUT)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3315 | return 0; |
| 3316 | |
| 3317 | /* We will set the queue timer to the time spent, just for |
| 3318 | * logging purposes. We fake a 500 server error, so that the |
| 3319 | * attacker will not suspect his connection has been tarpitted. |
| 3320 | * It will not cause trouble to the logs because we can exclude |
| 3321 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 3322 | */ |
Willy Tarreau | 26ed74d | 2008-08-17 12:11:14 +0200 | [diff] [blame] | 3323 | req->wex = TICK_ETERNITY; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3324 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 3325 | srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T, |
| 3326 | 500, error_message(t, HTTP_ERR_500)); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3327 | trace_term(t, TT_HTTP_SRV_2); |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3328 | goto update_state; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3329 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3330 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3331 | /* Right now, we will need to create a connection to the server. |
| 3332 | * We might already have tried, and got a connection pending, in |
| 3333 | * which case we will not do anything till it's pending. It's up |
| 3334 | * to any other session to release it and wake us up again. |
| 3335 | */ |
| 3336 | if (t->pend_pos) { |
Willy Tarreau | 507385d | 2008-08-17 13:04:25 +0200 | [diff] [blame^] | 3337 | if (!(req->flags & BF_WRITE_TIMEOUT)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3338 | return 0; |
| 3339 | } else { |
| 3340 | /* we've been waiting too long here */ |
Willy Tarreau | 26ed74d | 2008-08-17 12:11:14 +0200 | [diff] [blame] | 3341 | req->wex = TICK_ETERNITY; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3342 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 3343 | srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q, |
| 3344 | 503, error_message(t, HTTP_ERR_503)); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3345 | trace_term(t, TT_HTTP_SRV_3); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3346 | if (t->srv) |
| 3347 | t->srv->failed_conns++; |
| 3348 | t->be->failed_conns++; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3349 | goto update_state; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3350 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3351 | } |
| 3352 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3353 | do { |
| 3354 | /* first, get a connection */ |
| 3355 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 3356 | t->flags |= SN_REDIRECTABLE; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3357 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3358 | if (srv_redispatch_connect(t)) { |
| 3359 | if (t->srv_state == SV_STIDLE) |
| 3360 | return 0; |
| 3361 | goto update_state; |
| 3362 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3363 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3364 | if ((t->flags & SN_REDIRECTABLE) && t->srv && t->srv->rdr_len) { |
| 3365 | /* Server supporting redirection and it is possible. |
| 3366 | * Invalid requests are reported as such. It concerns all |
| 3367 | * the largest ones. |
| 3368 | */ |
| 3369 | struct chunk rdr; |
| 3370 | char *path; |
| 3371 | int len; |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3372 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3373 | /* 1: create the response header */ |
| 3374 | rdr.len = strlen(HTTP_302); |
| 3375 | rdr.str = trash; |
| 3376 | memcpy(rdr.str, HTTP_302, rdr.len); |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3377 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3378 | /* 2: add the server's prefix */ |
| 3379 | if (rdr.len + t->srv->rdr_len > sizeof(trash)) |
| 3380 | goto cancel_redir; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3381 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3382 | memcpy(rdr.str + rdr.len, t->srv->rdr_pfx, t->srv->rdr_len); |
| 3383 | rdr.len += t->srv->rdr_len; |
| 3384 | |
| 3385 | /* 3: add the request URI */ |
| 3386 | path = http_get_path(txn); |
| 3387 | if (!path) |
| 3388 | goto cancel_redir; |
| 3389 | len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path; |
| 3390 | if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */ |
| 3391 | goto cancel_redir; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3392 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3393 | memcpy(rdr.str + rdr.len, path, len); |
| 3394 | rdr.len += len; |
| 3395 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 3396 | rdr.len += 4; |
Krzysztof Piotr Oledzki | efe3b6f | 2008-05-23 23:49:32 +0200 | [diff] [blame] | 3397 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3398 | srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3399 | trace_term(t, TT_HTTP_SRV_3); |
| 3400 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3401 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
| 3402 | if (t->srv) |
| 3403 | t->srv->cum_sess++; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3404 | goto update_state; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3405 | cancel_redir: |
| 3406 | txn->status = 400; |
| 3407 | t->fe->failed_req++; |
| 3408 | srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, |
| 3409 | 400, error_message(t, HTTP_ERR_400)); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3410 | trace_term(t, TT_HTTP_SRV_4); |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3411 | goto update_state; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3412 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3413 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3414 | /* try to (re-)connect to the server, and fail if we expire the |
| 3415 | * number of retries. |
| 3416 | */ |
| 3417 | if (srv_retryable_connect(t)) { |
| 3418 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3419 | if (t->srv_state == SV_STIDLE) |
| 3420 | return 0; |
| 3421 | goto update_state; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3422 | } |
| 3423 | } while (1); |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3424 | } /* end if may_forward */ |
| 3425 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3426 | } |
Willy Tarreau | 1ae3a05 | 2008-08-16 10:56:30 +0200 | [diff] [blame] | 3427 | else if (t->srv_state == SV_STCONN) { /* connection in progress */ |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3428 | if ((rep->flags & BF_SHUTW) || |
| 3429 | ((req->flags & BF_SHUTR) && |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 3430 | ((req->flags & BF_EMPTY && !(req->flags & BF_WRITE_STATUS)) || |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3431 | t->be->options & PR_O_ABRT_CLOSE))) { /* give up */ |
Willy Tarreau | 26ed74d | 2008-08-17 12:11:14 +0200 | [diff] [blame] | 3432 | req->wex = TICK_ETERNITY; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3433 | if (!(t->flags & SN_CONN_TAR)) { |
| 3434 | /* if we are in turn-around, we have already closed the FD */ |
| 3435 | fd_delete(t->srv_fd); |
| 3436 | if (t->srv) { |
| 3437 | t->srv->cur_sess--; |
| 3438 | sess_change_server(t, NULL); |
| 3439 | } |
| 3440 | } |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3441 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3442 | /* note that this must not return any error because it would be able to |
| 3443 | * overwrite the client_retnclose() output. |
| 3444 | */ |
| 3445 | srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3446 | trace_term(t, TT_HTTP_SRV_5); |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3447 | goto update_state; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3448 | } |
Willy Tarreau | 507385d | 2008-08-17 13:04:25 +0200 | [diff] [blame^] | 3449 | if (!(req->flags & (BF_WRITE_STATUS | BF_WRITE_TIMEOUT))) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3450 | return 0; /* nothing changed */ |
| 3451 | } |
| 3452 | else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) { |
| 3453 | /* timeout, asynchronous connect error or first write error */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3454 | if (t->flags & SN_CONN_TAR) { |
| 3455 | /* We are doing a turn-around waiting for a new connection attempt. */ |
Willy Tarreau | 507385d | 2008-08-17 13:04:25 +0200 | [diff] [blame^] | 3456 | if (!(req->flags & BF_WRITE_TIMEOUT)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3457 | return 0; |
| 3458 | t->flags &= ~SN_CONN_TAR; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3459 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3460 | else { |
| 3461 | fd_delete(t->srv_fd); |
| 3462 | if (t->srv) { |
| 3463 | t->srv->cur_sess--; |
| 3464 | sess_change_server(t, NULL); |
| 3465 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3466 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3467 | if (!(req->flags & BF_WRITE_STATUS)) |
| 3468 | conn_err = SN_ERR_SRVTO; // it was a connect timeout. |
| 3469 | else |
| 3470 | conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3471 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3472 | /* ensure that we have enough retries left */ |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3473 | if (srv_count_retry_down(t, conn_err)) { |
| 3474 | goto update_state; |
| 3475 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3476 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3477 | if (req->flags & BF_WRITE_ERROR) { |
| 3478 | /* we encountered an immediate connection error, and we |
| 3479 | * will have to retry connecting to the same server, most |
| 3480 | * likely leading to the same result. To avoid this, we |
| 3481 | * fake a connection timeout to retry after a turn-around |
| 3482 | * time of 1 second. We will wait in the previous if block. |
| 3483 | */ |
| 3484 | t->flags |= SN_CONN_TAR; |
Willy Tarreau | 26ed74d | 2008-08-17 12:11:14 +0200 | [diff] [blame] | 3485 | req->wex = tick_add(now_ms, MS_TO_TICKS(1000)); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3486 | return 0; |
| 3487 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3488 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3489 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3490 | if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) { |
| 3491 | /* We're on our last chance, and the REDISP option was specified. |
| 3492 | * We will ignore cookie and force to balance or use the dispatcher. |
| 3493 | */ |
| 3494 | /* let's try to offer this slot to anybody */ |
| 3495 | if (may_dequeue_tasks(t->srv, t->be)) |
| 3496 | process_srv_queue(t->srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3497 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3498 | /* it's left to the dispatcher to choose a server */ |
| 3499 | t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
| 3500 | t->prev_srv = t->srv; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3501 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3502 | /* first, get a connection */ |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3503 | if (srv_redispatch_connect(t)) { |
| 3504 | if (t->srv_state == SV_STCONN) |
| 3505 | return 0; |
| 3506 | goto update_state; |
| 3507 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3508 | } else { |
| 3509 | if (t->srv) |
| 3510 | t->srv->retries++; |
| 3511 | t->be->retries++; |
| 3512 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3513 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3514 | do { |
| 3515 | /* Now we will try to either reconnect to the same server or |
| 3516 | * connect to another server. If the connection gets queued |
| 3517 | * because all servers are saturated, then we will go back to |
| 3518 | * the SV_STIDLE state. |
| 3519 | */ |
| 3520 | if (srv_retryable_connect(t)) { |
| 3521 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3522 | if (t->srv_state == SV_STCONN) |
| 3523 | return 0; |
| 3524 | goto update_state; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3525 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3526 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3527 | /* we need to redispatch the connection to another server */ |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3528 | if (srv_redispatch_connect(t)) { |
| 3529 | if (t->srv_state == SV_STCONN) |
| 3530 | return 0; |
| 3531 | goto update_state; |
| 3532 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3533 | } while (1); |
| 3534 | } |
| 3535 | else { /* no error or write 0 */ |
| 3536 | t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3537 | |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 3538 | if (req->flags & BF_EMPTY) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3539 | EV_FD_CLR(t->srv_fd, DIR_WR); |
| 3540 | req->wex = TICK_ETERNITY; |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 3541 | } else { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3542 | EV_FD_SET(t->srv_fd, DIR_WR); |
| 3543 | req->wex = tick_add_ifset(now_ms, t->be->timeout.server); |
| 3544 | if (tick_isset(req->wex)) { |
| 3545 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 3546 | * we refresh it. */ |
| 3547 | rep->rex = req->wex; |
| 3548 | } |
| 3549 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3550 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3551 | if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */ |
| 3552 | EV_FD_SET(t->srv_fd, DIR_RD); |
| 3553 | rep->rex = tick_add_ifset(now_ms, t->be->timeout.server); |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 3554 | buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3555 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3556 | /* if the user wants to log as soon as possible, without counting |
| 3557 | bytes from the server, then this is the right moment. */ |
| 3558 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 3559 | t->logs.t_close = t->logs.t_connect; /* to get a valid end date */ |
| 3560 | tcp_sess_log(t); |
| 3561 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3562 | #ifdef CONFIG_HAP_TCPSPLICE |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3563 | if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
| 3564 | /* TCP splicing supported by both FE and BE */ |
| 3565 | tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0); |
| 3566 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3567 | #endif |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3568 | } |
| 3569 | else { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3570 | t->analysis |= AN_RTR_HTTP_HDR; |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 3571 | buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3572 | t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE; |
| 3573 | /* reset hdr_idx which was already initialized by the request. |
| 3574 | * right now, the http parser does it. |
| 3575 | * hdr_idx_init(&t->txn.hdr_idx); |
| 3576 | */ |
| 3577 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3578 | |
| 3579 | t->srv_state = SV_STDATA; |
| 3580 | if (!(t->analysis & AN_RTR_ANY)) |
| 3581 | t->rep->flags |= BF_MAY_FORWARD; |
Willy Tarreau | 26ed74d | 2008-08-17 12:11:14 +0200 | [diff] [blame] | 3582 | req->wex = TICK_ETERNITY; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3583 | goto update_state; |
| 3584 | } /* else no error or write 0 */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3585 | } |
Willy Tarreau | 1ae3a05 | 2008-08-16 10:56:30 +0200 | [diff] [blame] | 3586 | else if (t->srv_state == SV_STDATA) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3587 | /* read or write error */ |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 3588 | /* FIXME: what happens when we have to deal with HTTP ??? */ |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3589 | if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) { |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3590 | buffer_shutr(rep); |
| 3591 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3592 | fd_delete(t->srv_fd); |
| 3593 | if (t->srv) { |
| 3594 | t->srv->cur_sess--; |
| 3595 | t->srv->failed_resp++; |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3596 | sess_change_server(t, NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3597 | } |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 3598 | t->be->failed_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3599 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3600 | trace_term(t, TT_HTTP_SRV_6); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3601 | if (!(t->flags & SN_ERR_MASK)) |
| 3602 | t->flags |= SN_ERR_SRVCL; |
| 3603 | if (!(t->flags & SN_FINST_MASK)) |
| 3604 | t->flags |= SN_FINST_D; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 3605 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3606 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 3607 | process_srv_queue(t->srv); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3608 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3609 | goto update_state; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3610 | } |
| 3611 | /* last read, or end of client write */ |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3612 | else if (!(rep->flags & BF_SHUTR) && /* not already done */ |
| 3613 | rep->flags & (BF_READ_NULL | BF_SHUTW)) { |
| 3614 | buffer_shutr(rep); |
| 3615 | if (!(req->flags & BF_SHUTW)) { |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 3616 | EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3617 | trace_term(t, TT_HTTP_SRV_7); |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 3618 | } else { |
| 3619 | /* output was already closed */ |
| 3620 | fd_delete(t->srv_fd); |
| 3621 | if (t->srv) { |
| 3622 | t->srv->cur_sess--; |
| 3623 | sess_change_server(t, NULL); |
| 3624 | } |
| 3625 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3626 | trace_term(t, TT_HTTP_SRV_8); |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 3627 | |
| 3628 | if (may_dequeue_tasks(t->srv, t->be)) |
| 3629 | process_srv_queue(t->srv); |
| 3630 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3631 | goto update_state; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3632 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 3633 | /* end of client read and no more data to send. We can forward |
| 3634 | * the close when we're allowed to forward data (anytime right |
| 3635 | * now). If we're using option forceclose, then we may also |
| 3636 | * shutdown the outgoing write channel once the response starts |
| 3637 | * coming from the server. |
| 3638 | */ |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3639 | else if (!(req->flags & BF_SHUTW) && /* not already done */ |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 3640 | req->flags & BF_EMPTY && req->flags & BF_MAY_FORWARD && |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3641 | (req->flags & BF_SHUTR || |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 3642 | (t->be->options & PR_O_FORCE_CLO && rep->flags & BF_READ_STATUS))) { |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3643 | buffer_shutw(req); |
| 3644 | if (!(rep->flags & BF_SHUTR)) { |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 3645 | EV_FD_CLR(t->srv_fd, DIR_WR); |
| 3646 | shutdown(t->srv_fd, SHUT_WR); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3647 | trace_term(t, TT_HTTP_SRV_9); |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 3648 | /* We must ensure that the read part is still alive when switching to shutw */ |
| 3649 | /* FIXME: is this still true ? */ |
| 3650 | EV_FD_SET(t->srv_fd, DIR_RD); |
| 3651 | rep->rex = tick_add_ifset(now_ms, t->be->timeout.server); |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 3652 | } else { |
| 3653 | fd_delete(t->srv_fd); |
| 3654 | if (t->srv) { |
| 3655 | t->srv->cur_sess--; |
| 3656 | sess_change_server(t, NULL); |
| 3657 | } |
| 3658 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3659 | trace_term(t, TT_HTTP_SRV_10); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3660 | |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 3661 | if (may_dequeue_tasks(t->srv, t->be)) |
| 3662 | process_srv_queue(t->srv); |
| 3663 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3664 | goto update_state; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3665 | } |
| 3666 | /* read timeout */ |
Willy Tarreau | 507385d | 2008-08-17 13:04:25 +0200 | [diff] [blame^] | 3667 | else if (rep->flags & BF_READ_TIMEOUT) { |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3668 | buffer_shutr(rep); |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3669 | if (!(req->flags & BF_SHUTW)) { |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 3670 | EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3671 | trace_term(t, TT_HTTP_SRV_11); |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 3672 | } else { |
| 3673 | fd_delete(t->srv_fd); |
| 3674 | if (t->srv) { |
| 3675 | t->srv->cur_sess--; |
| 3676 | sess_change_server(t, NULL); |
| 3677 | } |
| 3678 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3679 | trace_term(t, TT_HTTP_SRV_12); |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 3680 | |
| 3681 | if (may_dequeue_tasks(t->srv, t->be)) |
| 3682 | process_srv_queue(t->srv); |
| 3683 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3684 | if (!(t->flags & SN_ERR_MASK)) |
| 3685 | t->flags |= SN_ERR_SRVTO; |
| 3686 | if (!(t->flags & SN_FINST_MASK)) |
| 3687 | t->flags |= SN_FINST_D; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3688 | |
| 3689 | goto update_state; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3690 | } |
| 3691 | /* write timeout */ |
Willy Tarreau | 507385d | 2008-08-17 13:04:25 +0200 | [diff] [blame^] | 3692 | else if (req->flags & BF_WRITE_TIMEOUT) { |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3693 | buffer_shutw(req); |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3694 | if (!(rep->flags & BF_SHUTR)) { |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 3695 | EV_FD_CLR(t->srv_fd, DIR_WR); |
| 3696 | shutdown(t->srv_fd, SHUT_WR); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3697 | trace_term(t, TT_HTTP_SRV_13); |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 3698 | /* We must ensure that the read part is still alive when switching to shutw */ |
| 3699 | /* FIXME: is this still needed ? */ |
| 3700 | EV_FD_SET(t->srv_fd, DIR_RD); |
| 3701 | rep->rex = tick_add_ifset(now_ms, t->be->timeout.server); |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 3702 | } else { |
| 3703 | fd_delete(t->srv_fd); |
| 3704 | if (t->srv) { |
| 3705 | t->srv->cur_sess--; |
| 3706 | sess_change_server(t, NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3707 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 3708 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3709 | trace_term(t, TT_HTTP_SRV_14); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3710 | |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 3711 | if (may_dequeue_tasks(t->srv, t->be)) |
| 3712 | process_srv_queue(t->srv); |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 3713 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3714 | if (!(t->flags & SN_ERR_MASK)) |
| 3715 | t->flags |= SN_ERR_SRVTO; |
| 3716 | if (!(t->flags & SN_FINST_MASK)) |
| 3717 | t->flags |= SN_FINST_D; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3718 | |
| 3719 | goto update_state; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3720 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3721 | |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 3722 | /* manage read timeout */ |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3723 | if (!(rep->flags & BF_SHUTR)) { |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 3724 | if (rep->flags & BF_FULL) { |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 3725 | if (EV_FD_COND_C(t->srv_fd, DIR_RD)) |
| 3726 | rep->rex = TICK_ETERNITY; |
| 3727 | } else { |
| 3728 | EV_FD_COND_S(t->srv_fd, DIR_RD); |
| 3729 | rep->rex = tick_add_ifset(now_ms, t->be->timeout.server); |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 3730 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3731 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3732 | |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 3733 | /* manage write timeout */ |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3734 | if (!(req->flags & BF_SHUTW)) { |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 3735 | if (req->flags & BF_EMPTY || !(req->flags & BF_MAY_FORWARD)) { |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 3736 | /* stop writing */ |
| 3737 | if (EV_FD_COND_C(t->srv_fd, DIR_WR)) |
| 3738 | req->wex = TICK_ETERNITY; |
| 3739 | } else { |
| 3740 | /* buffer not empty, there are still data to be transferred */ |
| 3741 | EV_FD_COND_S(t->srv_fd, DIR_WR); |
| 3742 | if (!tick_isset(req->wex)) { |
| 3743 | /* restart writing */ |
| 3744 | req->wex = tick_add_ifset(now_ms, t->be->timeout.server); |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 3745 | if (!(rep->flags & BF_SHUTR) && tick_isset(req->wex) && tick_isset(rep->rex)) { |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 3746 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 3747 | * we refresh it, except if it was already infinite. |
| 3748 | */ |
| 3749 | rep->rex = req->wex; |
| 3750 | } |
| 3751 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3752 | } |
| 3753 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 3754 | return 0; /* other cases change nothing */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3755 | } |
Willy Tarreau | 1ae3a05 | 2008-08-16 10:56:30 +0200 | [diff] [blame] | 3756 | else if (t->srv_state == SV_STCLOSE) { /* SV_STCLOSE : nothing to do */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3757 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 3758 | int len; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3759 | len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3760 | 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] | 3761 | write(1, trash, len); |
| 3762 | } |
| 3763 | return 0; |
| 3764 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3765 | #ifdef DEBUG_DEV |
| 3766 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, t->srv_state); |
| 3767 | ABORT_NOW(); |
Willy Tarreau | 1ae3a05 | 2008-08-16 10:56:30 +0200 | [diff] [blame] | 3768 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3769 | return 0; |
| 3770 | } |
| 3771 | |
| 3772 | |
| 3773 | /* |
| 3774 | * Produces data for the session <s> depending on its source. Expects to be |
Willy Tarreau | 1ae3a05 | 2008-08-16 10:56:30 +0200 | [diff] [blame] | 3775 | * called with client socket shut down on input. Right now, only statistics can |
| 3776 | * be produced. It stops by itself by unsetting the SN_SELF_GEN flag from the |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3777 | * 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] | 3778 | * the buffer, or simply by letting an empty buffer upon return. It returns 1 |
Willy Tarreau | 1ae3a05 | 2008-08-16 10:56:30 +0200 | [diff] [blame] | 3779 | * when it wants to stop sending data, otherwise 0. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3780 | */ |
| 3781 | int produce_content(struct session *s) |
| 3782 | { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3783 | if (s->data_source == DATA_SRC_NONE) { |
| 3784 | s->flags &= ~SN_SELF_GEN; |
| 3785 | return 1; |
| 3786 | } |
| 3787 | else if (s->data_source == DATA_SRC_STATS) { |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3788 | /* dump server statistics */ |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 3789 | int ret = stats_dump_http(s, s->be->uri_auth); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 3790 | if (ret >= 0) |
| 3791 | return ret; |
| 3792 | /* -1 indicates an error */ |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3793 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3794 | |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 3795 | /* unknown data source or internal error */ |
| 3796 | s->txn.status = 500; |
| 3797 | client_retnclose(s, error_message(s, HTTP_ERR_500)); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3798 | trace_term(s, TT_HTTP_CNT_1); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 3799 | if (!(s->flags & SN_ERR_MASK)) |
| 3800 | s->flags |= SN_ERR_PRXCOND; |
| 3801 | if (!(s->flags & SN_FINST_MASK)) |
| 3802 | s->flags |= SN_FINST_R; |
| 3803 | s->flags &= ~SN_SELF_GEN; |
| 3804 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3805 | } |
| 3806 | |
| 3807 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3808 | /* Iterate the same filter through all request headers. |
| 3809 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3810 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 3811 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3812 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3813 | 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] | 3814 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3815 | char term; |
| 3816 | char *cur_ptr, *cur_end, *cur_next; |
| 3817 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3818 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3819 | struct hdr_idx_elem *cur_hdr; |
| 3820 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 3821 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3822 | last_hdr = 0; |
| 3823 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3824 | 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] | 3825 | old_idx = 0; |
| 3826 | |
| 3827 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3828 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3829 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3830 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3831 | (exp->action == ACT_ALLOW || |
| 3832 | exp->action == ACT_DENY || |
| 3833 | exp->action == ACT_TARPIT)) |
| 3834 | return 0; |
| 3835 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3836 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3837 | if (!cur_idx) |
| 3838 | break; |
| 3839 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3840 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3841 | cur_ptr = cur_next; |
| 3842 | cur_end = cur_ptr + cur_hdr->len; |
| 3843 | cur_next = cur_end + cur_hdr->cr + 1; |
| 3844 | |
| 3845 | /* Now we have one header between cur_ptr and cur_end, |
| 3846 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3847 | */ |
| 3848 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3849 | /* The annoying part is that pattern matching needs |
| 3850 | * that we modify the contents to null-terminate all |
| 3851 | * strings before testing them. |
| 3852 | */ |
| 3853 | |
| 3854 | term = *cur_end; |
| 3855 | *cur_end = '\0'; |
| 3856 | |
| 3857 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 3858 | switch (exp->action) { |
| 3859 | case ACT_SETBE: |
| 3860 | /* It is not possible to jump a second time. |
| 3861 | * FIXME: should we return an HTTP/500 here so that |
| 3862 | * the admin knows there's a problem ? |
| 3863 | */ |
| 3864 | if (t->be != t->fe) |
| 3865 | break; |
| 3866 | |
| 3867 | /* Swithing Proxy */ |
| 3868 | t->be = (struct proxy *) exp->replace; |
| 3869 | |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3870 | /* right now, the backend switch is not overly complicated |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3871 | * because we have associated req_cap and rsp_cap to the |
| 3872 | * frontend, and the beconn will be updated later. |
| 3873 | */ |
| 3874 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 3875 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 3876 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3877 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3878 | last_hdr = 1; |
| 3879 | break; |
| 3880 | |
| 3881 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3882 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3883 | last_hdr = 1; |
| 3884 | break; |
| 3885 | |
| 3886 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3887 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3888 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3889 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3890 | break; |
| 3891 | |
| 3892 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3893 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3894 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3895 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3896 | break; |
| 3897 | |
| 3898 | case ACT_REPLACE: |
| 3899 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 3900 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 3901 | /* FIXME: if the user adds a newline in the replacement, the |
| 3902 | * index will not be recalculated for now, and the new line |
| 3903 | * will not be counted as a new header. |
| 3904 | */ |
| 3905 | |
| 3906 | cur_end += delta; |
| 3907 | cur_next += delta; |
| 3908 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3909 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3910 | break; |
| 3911 | |
| 3912 | case ACT_REMOVE: |
| 3913 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 3914 | cur_next += delta; |
| 3915 | |
| 3916 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3917 | txn->req.eoh += delta; |
| 3918 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 3919 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3920 | cur_hdr->len = 0; |
| 3921 | cur_end = NULL; /* null-term has been rewritten */ |
| 3922 | break; |
| 3923 | |
| 3924 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3925 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3926 | if (cur_end) |
| 3927 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3928 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3929 | /* keep the link from this header to next one in case of later |
| 3930 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3931 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3932 | old_idx = cur_idx; |
| 3933 | } |
| 3934 | return 0; |
| 3935 | } |
| 3936 | |
| 3937 | |
| 3938 | /* Apply the filter to the request line. |
| 3939 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 3940 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3941 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 3942 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3943 | */ |
| 3944 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 3945 | { |
| 3946 | char term; |
| 3947 | char *cur_ptr, *cur_end; |
| 3948 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3949 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3950 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3951 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3952 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3953 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3954 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3955 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3956 | (exp->action == ACT_ALLOW || |
| 3957 | exp->action == ACT_DENY || |
| 3958 | exp->action == ACT_TARPIT)) |
| 3959 | return 0; |
| 3960 | else if (exp->action == ACT_REMOVE) |
| 3961 | return 0; |
| 3962 | |
| 3963 | done = 0; |
| 3964 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 3965 | 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] | 3966 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3967 | |
| 3968 | /* Now we have the request line between cur_ptr and cur_end */ |
| 3969 | |
| 3970 | /* The annoying part is that pattern matching needs |
| 3971 | * that we modify the contents to null-terminate all |
| 3972 | * strings before testing them. |
| 3973 | */ |
| 3974 | |
| 3975 | term = *cur_end; |
| 3976 | *cur_end = '\0'; |
| 3977 | |
| 3978 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 3979 | switch (exp->action) { |
| 3980 | case ACT_SETBE: |
| 3981 | /* It is not possible to jump a second time. |
| 3982 | * FIXME: should we return an HTTP/500 here so that |
| 3983 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3984 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3985 | if (t->be != t->fe) |
| 3986 | break; |
| 3987 | |
| 3988 | /* Swithing Proxy */ |
| 3989 | t->be = (struct proxy *) exp->replace; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3990 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3991 | /* right now, the backend switch is not too much complicated |
| 3992 | * because we have associated req_cap and rsp_cap to the |
| 3993 | * frontend, and the beconn will be updated later. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3994 | */ |
| 3995 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 3996 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 3997 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3998 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3999 | done = 1; |
| 4000 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4001 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4002 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4003 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4004 | done = 1; |
| 4005 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4006 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4007 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4008 | txn->flags |= TX_CLDENY; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4009 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4010 | done = 1; |
| 4011 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4012 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4013 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4014 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4015 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4016 | done = 1; |
| 4017 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4018 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4019 | case ACT_REPLACE: |
| 4020 | *cur_end = term; /* restore the string terminator */ |
| 4021 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4022 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 4023 | /* FIXME: if the user adds a newline in the replacement, the |
| 4024 | * index will not be recalculated for now, and the new line |
| 4025 | * will not be counted as a new header. |
| 4026 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4027 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4028 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4029 | cur_end += delta; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4030 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4031 | 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] | 4032 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4033 | HTTP_MSG_RQMETH, |
| 4034 | cur_ptr, cur_end + 1, |
| 4035 | NULL, NULL); |
| 4036 | if (unlikely(!cur_end)) |
| 4037 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4038 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4039 | /* we have a full request and we know that we have either a CR |
| 4040 | * or an LF at <ptr>. |
| 4041 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4042 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 4043 | 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] | 4044 | /* there is no point trying this regex on headers */ |
| 4045 | return 1; |
| 4046 | } |
| 4047 | } |
| 4048 | *cur_end = term; /* restore the string terminator */ |
| 4049 | return done; |
| 4050 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 4051 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4052 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4053 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4054 | /* |
| 4055 | * Apply all the req filters <exp> to all headers in buffer <req> of session <t>. |
| 4056 | * 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] | 4057 | * unparsable request. Since it can manage the switch to another backend, it |
| 4058 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4059 | */ |
| 4060 | int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 4061 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4062 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4063 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4064 | while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4065 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4066 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4067 | /* |
| 4068 | * The interleaving of transformations and verdicts |
| 4069 | * makes it difficult to decide to continue or stop |
| 4070 | * the evaluation. |
| 4071 | */ |
| 4072 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4073 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4074 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 4075 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) { |
| 4076 | exp = exp->next; |
| 4077 | continue; |
| 4078 | } |
| 4079 | |
| 4080 | /* Apply the filter to the request line. */ |
| 4081 | ret = apply_filter_to_req_line(t, req, exp); |
| 4082 | if (unlikely(ret < 0)) |
| 4083 | return -1; |
| 4084 | |
| 4085 | if (likely(ret == 0)) { |
| 4086 | /* The filter did not match the request, it can be |
| 4087 | * iterated through all headers. |
| 4088 | */ |
| 4089 | apply_filter_to_req_headers(t, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4090 | } |
| 4091 | exp = exp->next; |
| 4092 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4093 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4094 | } |
| 4095 | |
| 4096 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4097 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4098 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 4099 | * Manage client-side cookie. It can impact performance by about 2% so it is |
| 4100 | * desirable to call it only when needed. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4101 | */ |
| 4102 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 4103 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4104 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4105 | char *p1, *p2, *p3, *p4; |
| 4106 | char *del_colon, *del_cookie, *colon; |
| 4107 | int app_cookies; |
| 4108 | |
| 4109 | appsess *asession_temp = NULL; |
| 4110 | appsess local_asession; |
| 4111 | |
| 4112 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4113 | int cur_idx, old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4114 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4115 | /* Iterate through the headers. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4116 | * we start with the start line. |
| 4117 | */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 4118 | old_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4119 | 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] | 4120 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4121 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4122 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4123 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4124 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4125 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4126 | cur_ptr = cur_next; |
| 4127 | cur_end = cur_ptr + cur_hdr->len; |
| 4128 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4129 | |
| 4130 | /* We have one full header between cur_ptr and cur_end, and the |
| 4131 | * next header starts at cur_next. We're only interested in |
| 4132 | * "Cookie:" headers. |
| 4133 | */ |
| 4134 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4135 | val = http_header_match2(cur_ptr, cur_end, "Cookie", 6); |
| 4136 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4137 | old_idx = cur_idx; |
| 4138 | continue; |
| 4139 | } |
| 4140 | |
| 4141 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 4142 | * attributes whose name begin with a '$', and associate them with |
| 4143 | * the right cookie, if we want to delete this cookie. |
| 4144 | * So there are 3 cases for each cookie read : |
| 4145 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 4146 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 4147 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 4148 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 4149 | * "special" cookie. |
| 4150 | * At the end of loop, if a "special" cookie remains, we may have to |
| 4151 | * remove it. If no application cookie persists in the header, we |
| 4152 | * *MUST* delete it |
| 4153 | */ |
| 4154 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4155 | colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4156 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4157 | /* del_cookie == NULL => nothing to be deleted */ |
| 4158 | del_colon = del_cookie = NULL; |
| 4159 | app_cookies = 0; |
| 4160 | |
| 4161 | while (p1 < cur_end) { |
| 4162 | /* skip spaces and colons, but keep an eye on these ones */ |
| 4163 | while (p1 < cur_end) { |
| 4164 | if (*p1 == ';' || *p1 == ',') |
| 4165 | colon = p1; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4166 | else if (!isspace((unsigned char)*p1)) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4167 | break; |
| 4168 | p1++; |
| 4169 | } |
| 4170 | |
| 4171 | if (p1 == cur_end) |
| 4172 | break; |
| 4173 | |
| 4174 | /* p1 is at the beginning of the cookie name */ |
| 4175 | p2 = p1; |
| 4176 | while (p2 < cur_end && *p2 != '=') |
| 4177 | p2++; |
| 4178 | |
| 4179 | if (p2 == cur_end) |
| 4180 | break; |
| 4181 | |
| 4182 | p3 = p2 + 1; /* skips the '=' sign */ |
| 4183 | if (p3 == cur_end) |
| 4184 | break; |
| 4185 | |
| 4186 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4187 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4188 | p4++; |
| 4189 | |
| 4190 | /* here, we have the cookie name between p1 and p2, |
| 4191 | * and its value between p3 and p4. |
| 4192 | * we can process it : |
| 4193 | * |
| 4194 | * Cookie: NAME=VALUE; |
| 4195 | * | || || | |
| 4196 | * | || || +--> p4 |
| 4197 | * | || |+-------> p3 |
| 4198 | * | || +--------> p2 |
| 4199 | * | |+------------> p1 |
| 4200 | * | +-------------> colon |
| 4201 | * +--------------------> cur_ptr |
| 4202 | */ |
| 4203 | |
| 4204 | if (*p1 == '$') { |
| 4205 | /* skip this one */ |
| 4206 | } |
| 4207 | else { |
| 4208 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4209 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4210 | txn->cli_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4211 | (p4 - p1 >= t->fe->capture_namelen) && |
| 4212 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4213 | int log_len = p4 - p1; |
| 4214 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 4215 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4216 | Alert("HTTP logging : out of memory.\n"); |
| 4217 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4218 | if (log_len > t->fe->capture_len) |
| 4219 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4220 | memcpy(txn->cli_cookie, p1, log_len); |
| 4221 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4222 | } |
| 4223 | } |
| 4224 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4225 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 4226 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4227 | /* Cool... it's the right one */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4228 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4229 | char *delim; |
| 4230 | |
| 4231 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 4232 | * have the server ID betweek p3 and delim, and the original cookie between |
| 4233 | * delim+1 and p4. Otherwise, delim==p4 : |
| 4234 | * |
| 4235 | * Cookie: NAME=SRV~VALUE; |
| 4236 | * | || || | | |
| 4237 | * | || || | +--> p4 |
| 4238 | * | || || +--------> delim |
| 4239 | * | || |+-----------> p3 |
| 4240 | * | || +------------> p2 |
| 4241 | * | |+----------------> p1 |
| 4242 | * | +-----------------> colon |
| 4243 | * +------------------------> cur_ptr |
| 4244 | */ |
| 4245 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4246 | if (t->be->options & PR_O_COOK_PFX) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4247 | for (delim = p3; delim < p4; delim++) |
| 4248 | if (*delim == COOKIE_DELIM) |
| 4249 | break; |
| 4250 | } |
| 4251 | else |
| 4252 | delim = p4; |
| 4253 | |
| 4254 | |
| 4255 | /* Here, we'll look for the first running server which supports the cookie. |
| 4256 | * This allows to share a same cookie between several servers, for example |
| 4257 | * to dedicate backup servers to specific servers only. |
| 4258 | * However, to prevent clients from sticking to cookie-less backup server |
| 4259 | * when they have incidentely learned an empty cookie, we simply ignore |
| 4260 | * empty cookies and mark them as invalid. |
| 4261 | */ |
| 4262 | if (delim == p3) |
| 4263 | srv = NULL; |
| 4264 | |
| 4265 | while (srv) { |
Willy Tarreau | 92f2ab1 | 2007-02-02 22:14:47 +0100 | [diff] [blame] | 4266 | if (srv->cookie && (srv->cklen == delim - p3) && |
| 4267 | !memcmp(p3, srv->cookie, delim - p3)) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4268 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4269 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4270 | txn->flags &= ~TX_CK_MASK; |
| 4271 | txn->flags |= TX_CK_VALID; |
| 4272 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4273 | t->srv = srv; |
| 4274 | break; |
| 4275 | } else { |
| 4276 | /* we found a server, but it's down */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4277 | txn->flags &= ~TX_CK_MASK; |
| 4278 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4279 | } |
| 4280 | } |
| 4281 | srv = srv->next; |
| 4282 | } |
| 4283 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4284 | if (!srv && !(txn->flags & TX_CK_DOWN)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4285 | /* no server matched this cookie */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4286 | txn->flags &= ~TX_CK_MASK; |
| 4287 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4288 | } |
| 4289 | |
| 4290 | /* depending on the cookie mode, we may have to either : |
| 4291 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 4292 | * the server never sees it ; |
| 4293 | * - remove the server id from the cookie value, and tag the cookie as an |
| 4294 | * application cookie so that it does not get accidentely removed later, |
| 4295 | * if we're in cookie prefix mode |
| 4296 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4297 | if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4298 | int delta; /* negative */ |
| 4299 | |
| 4300 | delta = buffer_replace2(req, p3, delim + 1, NULL, 0); |
| 4301 | p4 += delta; |
| 4302 | cur_end += delta; |
| 4303 | cur_next += delta; |
| 4304 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4305 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4306 | |
| 4307 | del_cookie = del_colon = NULL; |
| 4308 | app_cookies++; /* protect the header from deletion */ |
| 4309 | } |
| 4310 | else if (del_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4311 | (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] | 4312 | del_cookie = p1; |
| 4313 | del_colon = colon; |
| 4314 | } |
| 4315 | } else { |
| 4316 | /* now we know that we must keep this cookie since it's |
| 4317 | * not ours. But if we wanted to delete our cookie |
| 4318 | * earlier, we cannot remove the complete header, but we |
| 4319 | * can remove the previous block itself. |
| 4320 | */ |
| 4321 | app_cookies++; |
| 4322 | |
| 4323 | if (del_cookie != NULL) { |
| 4324 | int delta; /* negative */ |
| 4325 | |
| 4326 | delta = buffer_replace2(req, del_cookie, p1, NULL, 0); |
| 4327 | p4 += delta; |
| 4328 | cur_end += delta; |
| 4329 | cur_next += delta; |
| 4330 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4331 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4332 | del_cookie = del_colon = NULL; |
| 4333 | } |
| 4334 | } |
| 4335 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4336 | if ((t->be->appsession_name != NULL) && |
| 4337 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4338 | /* first, let's see if the cookie is our appcookie*/ |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4339 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4340 | /* Cool... it's the right one */ |
| 4341 | |
| 4342 | asession_temp = &local_asession; |
| 4343 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4344 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4345 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 4346 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 4347 | return; |
| 4348 | } |
| 4349 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4350 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 4351 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4352 | asession_temp->serverid = NULL; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4353 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4354 | /* only do insert, if lookup fails */ |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4355 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 4356 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4357 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4358 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4359 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4360 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 4361 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 4362 | return; |
| 4363 | } |
| 4364 | |
| 4365 | asession_temp->sessid = local_asession.sessid; |
| 4366 | asession_temp->serverid = local_asession.serverid; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4367 | asession_temp->request_count = 0; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4368 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4369 | } else { |
| 4370 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4371 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4372 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4373 | if (asession_temp->serverid == NULL) { |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4374 | /* TODO redispatch request */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4375 | Alert("Found Application Session without matching server.\n"); |
| 4376 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4377 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4378 | while (srv) { |
| 4379 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4380 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4381 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4382 | txn->flags &= ~TX_CK_MASK; |
| 4383 | txn->flags |= TX_CK_VALID; |
| 4384 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4385 | t->srv = srv; |
| 4386 | break; |
| 4387 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4388 | txn->flags &= ~TX_CK_MASK; |
| 4389 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4390 | } |
| 4391 | } |
| 4392 | srv = srv->next; |
| 4393 | }/* end while(srv) */ |
| 4394 | }/* end else if server == NULL */ |
| 4395 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 4396 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4397 | asession_temp->request_count++; |
| 4398 | #if defined(DEBUG_HASH) |
| 4399 | Alert("manage_client_side_cookies\n"); |
| 4400 | appsession_hash_dump(&(t->be->htbl_proxy)); |
| 4401 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4402 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 4403 | } |
| 4404 | |
| 4405 | /* we'll have to look for another cookie ... */ |
| 4406 | p1 = p4; |
| 4407 | } /* while (p1 < cur_end) */ |
| 4408 | |
| 4409 | /* There's no more cookie on this line. |
| 4410 | * We may have marked the last one(s) for deletion. |
| 4411 | * We must do this now in two ways : |
| 4412 | * - if there is no app cookie, we simply delete the header ; |
| 4413 | * - if there are app cookies, we must delete the end of the |
| 4414 | * string properly, including the colon/semi-colon before |
| 4415 | * the cookie name. |
| 4416 | */ |
| 4417 | if (del_cookie != NULL) { |
| 4418 | int delta; |
| 4419 | if (app_cookies) { |
| 4420 | delta = buffer_replace2(req, del_colon, cur_end, NULL, 0); |
| 4421 | cur_end = del_colon; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4422 | cur_hdr->len += delta; |
| 4423 | } else { |
| 4424 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4425 | |
| 4426 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4427 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4428 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4429 | cur_hdr->len = 0; |
| 4430 | } |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 4431 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4432 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4433 | } |
| 4434 | |
| 4435 | /* keep the link from this header to next one */ |
| 4436 | old_idx = cur_idx; |
| 4437 | } /* end of cookie processing on this header */ |
| 4438 | } |
| 4439 | |
| 4440 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4441 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 4442 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 4443 | */ |
| 4444 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4445 | { |
| 4446 | char term; |
| 4447 | char *cur_ptr, *cur_end, *cur_next; |
| 4448 | int cur_idx, old_idx, last_hdr; |
| 4449 | struct http_txn *txn = &t->txn; |
| 4450 | struct hdr_idx_elem *cur_hdr; |
| 4451 | int len, delta; |
| 4452 | |
| 4453 | last_hdr = 0; |
| 4454 | |
| 4455 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4456 | old_idx = 0; |
| 4457 | |
| 4458 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4459 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4460 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4461 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4462 | (exp->action == ACT_ALLOW || |
| 4463 | exp->action == ACT_DENY)) |
| 4464 | return 0; |
| 4465 | |
| 4466 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 4467 | if (!cur_idx) |
| 4468 | break; |
| 4469 | |
| 4470 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4471 | cur_ptr = cur_next; |
| 4472 | cur_end = cur_ptr + cur_hdr->len; |
| 4473 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4474 | |
| 4475 | /* Now we have one header between cur_ptr and cur_end, |
| 4476 | * and the next header starts at cur_next. |
| 4477 | */ |
| 4478 | |
| 4479 | /* The annoying part is that pattern matching needs |
| 4480 | * that we modify the contents to null-terminate all |
| 4481 | * strings before testing them. |
| 4482 | */ |
| 4483 | |
| 4484 | term = *cur_end; |
| 4485 | *cur_end = '\0'; |
| 4486 | |
| 4487 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4488 | switch (exp->action) { |
| 4489 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4490 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4491 | last_hdr = 1; |
| 4492 | break; |
| 4493 | |
| 4494 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4495 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4496 | last_hdr = 1; |
| 4497 | break; |
| 4498 | |
| 4499 | case ACT_REPLACE: |
| 4500 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4501 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 4502 | /* FIXME: if the user adds a newline in the replacement, the |
| 4503 | * index will not be recalculated for now, and the new line |
| 4504 | * will not be counted as a new header. |
| 4505 | */ |
| 4506 | |
| 4507 | cur_end += delta; |
| 4508 | cur_next += delta; |
| 4509 | cur_hdr->len += delta; |
| 4510 | txn->rsp.eoh += delta; |
| 4511 | break; |
| 4512 | |
| 4513 | case ACT_REMOVE: |
| 4514 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 4515 | cur_next += delta; |
| 4516 | |
| 4517 | /* FIXME: this should be a separate function */ |
| 4518 | txn->rsp.eoh += delta; |
| 4519 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4520 | txn->hdr_idx.used--; |
| 4521 | cur_hdr->len = 0; |
| 4522 | cur_end = NULL; /* null-term has been rewritten */ |
| 4523 | break; |
| 4524 | |
| 4525 | } |
| 4526 | } |
| 4527 | if (cur_end) |
| 4528 | *cur_end = term; /* restore the string terminator */ |
| 4529 | |
| 4530 | /* keep the link from this header to next one in case of later |
| 4531 | * removal of next header. |
| 4532 | */ |
| 4533 | old_idx = cur_idx; |
| 4534 | } |
| 4535 | return 0; |
| 4536 | } |
| 4537 | |
| 4538 | |
| 4539 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 4540 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 4541 | * or -1 if a replacement resulted in an invalid status line. |
| 4542 | */ |
| 4543 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4544 | { |
| 4545 | char term; |
| 4546 | char *cur_ptr, *cur_end; |
| 4547 | int done; |
| 4548 | struct http_txn *txn = &t->txn; |
| 4549 | int len, delta; |
| 4550 | |
| 4551 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4552 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4553 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4554 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4555 | (exp->action == ACT_ALLOW || |
| 4556 | exp->action == ACT_DENY)) |
| 4557 | return 0; |
| 4558 | else if (exp->action == ACT_REMOVE) |
| 4559 | return 0; |
| 4560 | |
| 4561 | done = 0; |
| 4562 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4563 | 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] | 4564 | cur_end = cur_ptr + txn->rsp.sl.rq.l; |
| 4565 | |
| 4566 | /* Now we have the status line between cur_ptr and cur_end */ |
| 4567 | |
| 4568 | /* The annoying part is that pattern matching needs |
| 4569 | * that we modify the contents to null-terminate all |
| 4570 | * strings before testing them. |
| 4571 | */ |
| 4572 | |
| 4573 | term = *cur_end; |
| 4574 | *cur_end = '\0'; |
| 4575 | |
| 4576 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4577 | switch (exp->action) { |
| 4578 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4579 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4580 | done = 1; |
| 4581 | break; |
| 4582 | |
| 4583 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4584 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4585 | done = 1; |
| 4586 | break; |
| 4587 | |
| 4588 | case ACT_REPLACE: |
| 4589 | *cur_end = term; /* restore the string terminator */ |
| 4590 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4591 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 4592 | /* FIXME: if the user adds a newline in the replacement, the |
| 4593 | * index will not be recalculated for now, and the new line |
| 4594 | * will not be counted as a new header. |
| 4595 | */ |
| 4596 | |
| 4597 | txn->rsp.eoh += delta; |
| 4598 | cur_end += delta; |
| 4599 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4600 | 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] | 4601 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 4602 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4603 | cur_ptr, cur_end + 1, |
| 4604 | NULL, NULL); |
| 4605 | if (unlikely(!cur_end)) |
| 4606 | return -1; |
| 4607 | |
| 4608 | /* we have a full respnse and we know that we have either a CR |
| 4609 | * or an LF at <ptr>. |
| 4610 | */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4611 | 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] | 4612 | hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r'); |
| 4613 | /* there is no point trying this regex on headers */ |
| 4614 | return 1; |
| 4615 | } |
| 4616 | } |
| 4617 | *cur_end = term; /* restore the string terminator */ |
| 4618 | return done; |
| 4619 | } |
| 4620 | |
| 4621 | |
| 4622 | |
| 4623 | /* |
| 4624 | * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>. |
| 4625 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 4626 | * unparsable response. |
| 4627 | */ |
| 4628 | int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4629 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4630 | struct http_txn *txn = &t->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4631 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4632 | while (exp && !(txn->flags & TX_SVDENY)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4633 | int ret; |
| 4634 | |
| 4635 | /* |
| 4636 | * The interleaving of transformations and verdicts |
| 4637 | * makes it difficult to decide to continue or stop |
| 4638 | * the evaluation. |
| 4639 | */ |
| 4640 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4641 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4642 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 4643 | exp->action == ACT_PASS)) { |
| 4644 | exp = exp->next; |
| 4645 | continue; |
| 4646 | } |
| 4647 | |
| 4648 | /* Apply the filter to the status line. */ |
| 4649 | ret = apply_filter_to_sts_line(t, rtr, exp); |
| 4650 | if (unlikely(ret < 0)) |
| 4651 | return -1; |
| 4652 | |
| 4653 | if (likely(ret == 0)) { |
| 4654 | /* The filter did not match the response, it can be |
| 4655 | * iterated through all headers. |
| 4656 | */ |
| 4657 | apply_filter_to_resp_headers(t, rtr, exp); |
| 4658 | } |
| 4659 | exp = exp->next; |
| 4660 | } |
| 4661 | return 0; |
| 4662 | } |
| 4663 | |
| 4664 | |
| 4665 | |
| 4666 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 4667 | * Manage server-side cookies. It can impact performance by about 2% so it is |
| 4668 | * desirable to call it only when needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4669 | */ |
| 4670 | void manage_server_side_cookies(struct session *t, struct buffer *rtr) |
| 4671 | { |
| 4672 | struct http_txn *txn = &t->txn; |
| 4673 | char *p1, *p2, *p3, *p4; |
| 4674 | |
| 4675 | appsess *asession_temp = NULL; |
| 4676 | appsess local_asession; |
| 4677 | |
| 4678 | char *cur_ptr, *cur_end, *cur_next; |
| 4679 | int cur_idx, old_idx, delta; |
| 4680 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4681 | /* Iterate through the headers. |
| 4682 | * we start with the start line. |
| 4683 | */ |
| 4684 | old_idx = 0; |
| 4685 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4686 | |
| 4687 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 4688 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4689 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4690 | |
| 4691 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4692 | cur_ptr = cur_next; |
| 4693 | cur_end = cur_ptr + cur_hdr->len; |
| 4694 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4695 | |
| 4696 | /* We have one full header between cur_ptr and cur_end, and the |
| 4697 | * next header starts at cur_next. We're only interested in |
| 4698 | * "Cookie:" headers. |
| 4699 | */ |
| 4700 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4701 | val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10); |
| 4702 | if (!val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4703 | old_idx = cur_idx; |
| 4704 | continue; |
| 4705 | } |
| 4706 | |
| 4707 | /* 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] | 4708 | txn->flags |= TX_SCK_ANY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4709 | |
| 4710 | |
| 4711 | /* maybe we only wanted to see if there was a set-cookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4712 | if (t->be->cookie_name == NULL && |
| 4713 | t->be->appsession_name == NULL && |
| 4714 | t->be->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4715 | return; |
| 4716 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4717 | p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4718 | |
| 4719 | 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] | 4720 | if (p1 == cur_end || *p1 == ';') /* end of cookie */ |
| 4721 | break; |
| 4722 | |
| 4723 | /* p1 is at the beginning of the cookie name */ |
| 4724 | p2 = p1; |
| 4725 | |
| 4726 | while (p2 < cur_end && *p2 != '=' && *p2 != ';') |
| 4727 | p2++; |
| 4728 | |
| 4729 | if (p2 == cur_end || *p2 == ';') /* next cookie */ |
| 4730 | break; |
| 4731 | |
| 4732 | p3 = p2 + 1; /* skip the '=' sign */ |
| 4733 | if (p3 == cur_end) |
| 4734 | break; |
| 4735 | |
| 4736 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4737 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';') |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4738 | p4++; |
| 4739 | |
| 4740 | /* here, we have the cookie name between p1 and p2, |
| 4741 | * and its value between p3 and p4. |
| 4742 | * we can process it. |
| 4743 | */ |
| 4744 | |
| 4745 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4746 | if (t->be->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4747 | txn->srv_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4748 | (p4 - p1 >= t->be->capture_namelen) && |
| 4749 | memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4750 | int log_len = p4 - p1; |
| 4751 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 4752 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4753 | Alert("HTTP logging : out of memory.\n"); |
| 4754 | } |
| 4755 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4756 | if (log_len > t->be->capture_len) |
| 4757 | log_len = t->be->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4758 | memcpy(txn->srv_cookie, p1, log_len); |
| 4759 | txn->srv_cookie[log_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4760 | } |
| 4761 | |
| 4762 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4763 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 4764 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4765 | /* Cool... it's the right one */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4766 | txn->flags |= TX_SCK_SEEN; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4767 | |
| 4768 | /* If the cookie is in insert mode on a known server, we'll delete |
| 4769 | * this occurrence because we'll insert another one later. |
| 4770 | * We'll delete it too if the "indirect" option is set and we're in |
| 4771 | * a direct access. */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4772 | if (((t->srv) && (t->be->options & PR_O_COOK_INS)) || |
| 4773 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4774 | /* this header must be deleted */ |
| 4775 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 4776 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4777 | txn->hdr_idx.used--; |
| 4778 | cur_hdr->len = 0; |
| 4779 | cur_next += delta; |
| 4780 | txn->rsp.eoh += delta; |
| 4781 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4782 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4783 | } |
| 4784 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4785 | (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4786 | /* replace bytes p3->p4 with the cookie name associated |
| 4787 | * with this server since we know it. |
| 4788 | */ |
| 4789 | delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen); |
| 4790 | cur_hdr->len += delta; |
| 4791 | cur_next += delta; |
| 4792 | txn->rsp.eoh += delta; |
| 4793 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4794 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4795 | } |
| 4796 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4797 | (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4798 | /* insert the cookie name associated with this server |
| 4799 | * before existing cookie, and insert a delimitor between them.. |
| 4800 | */ |
| 4801 | delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1); |
| 4802 | cur_hdr->len += delta; |
| 4803 | cur_next += delta; |
| 4804 | txn->rsp.eoh += delta; |
| 4805 | |
| 4806 | p3[t->srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4807 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4808 | } |
| 4809 | } |
| 4810 | /* next, let's see if the cookie is our appcookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4811 | else if ((t->be->appsession_name != NULL) && |
| 4812 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4813 | |
| 4814 | /* Cool... it's the right one */ |
| 4815 | |
| 4816 | size_t server_id_len = strlen(t->srv->id) + 1; |
| 4817 | asession_temp = &local_asession; |
| 4818 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4819 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4820 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4821 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4822 | return; |
| 4823 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4824 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 4825 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4826 | asession_temp->serverid = NULL; |
| 4827 | |
| 4828 | /* only do insert, if lookup fails */ |
Ryan Warnick | 6d0b1fa | 2008-02-17 11:24:35 +0100 | [diff] [blame] | 4829 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 4830 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4831 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4832 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 4833 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 4834 | return; |
| 4835 | } |
| 4836 | asession_temp->sessid = local_asession.sessid; |
| 4837 | asession_temp->serverid = local_asession.serverid; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4838 | asession_temp->request_count = 0; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4839 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
| 4840 | } else { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4841 | /* free wasted memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4842 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4843 | } |
| 4844 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4845 | if (asession_temp->serverid == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4846 | if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4847 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4848 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4849 | return; |
| 4850 | } |
| 4851 | asession_temp->serverid[0] = '\0'; |
| 4852 | } |
| 4853 | |
| 4854 | if (asession_temp->serverid[0] == '\0') |
| 4855 | memcpy(asession_temp->serverid, t->srv->id, server_id_len); |
| 4856 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 4857 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4858 | asession_temp->request_count++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4859 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4860 | Alert("manage_server_side_cookies\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4861 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4862 | #endif |
| 4863 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 4864 | break; /* we don't want to loop again since there cannot be another cookie on the same line */ |
| 4865 | } /* we're now at the end of the cookie value */ |
| 4866 | |
| 4867 | /* keep the link from this header to next one */ |
| 4868 | old_idx = cur_idx; |
| 4869 | } /* end of cookie processing on this header */ |
| 4870 | } |
| 4871 | |
| 4872 | |
| 4873 | |
| 4874 | /* |
| 4875 | * Check if response is cacheable or not. Updates t->flags. |
| 4876 | */ |
| 4877 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 4878 | { |
| 4879 | struct http_txn *txn = &t->txn; |
| 4880 | char *p1, *p2; |
| 4881 | |
| 4882 | char *cur_ptr, *cur_end, *cur_next; |
| 4883 | int cur_idx; |
| 4884 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 4885 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4886 | return; |
| 4887 | |
| 4888 | /* Iterate through the headers. |
| 4889 | * we start with the start line. |
| 4890 | */ |
| 4891 | cur_idx = 0; |
| 4892 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4893 | |
| 4894 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 4895 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4896 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4897 | |
| 4898 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4899 | cur_ptr = cur_next; |
| 4900 | cur_end = cur_ptr + cur_hdr->len; |
| 4901 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4902 | |
| 4903 | /* We have one full header between cur_ptr and cur_end, and the |
| 4904 | * next header starts at cur_next. We're only interested in |
| 4905 | * "Cookie:" headers. |
| 4906 | */ |
| 4907 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4908 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 4909 | if (val) { |
| 4910 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 4911 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 4912 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 4913 | return; |
| 4914 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4915 | } |
| 4916 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4917 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 4918 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4919 | continue; |
| 4920 | |
| 4921 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 4922 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4923 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4924 | |
| 4925 | if (p1 >= cur_end) /* no more info */ |
| 4926 | continue; |
| 4927 | |
| 4928 | /* p1 is at the beginning of the value */ |
| 4929 | p2 = p1; |
| 4930 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4931 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4932 | p2++; |
| 4933 | |
| 4934 | /* we have a complete value between p1 and p2 */ |
| 4935 | if (p2 < cur_end && *p2 == '=') { |
| 4936 | /* we have something of the form no-cache="set-cookie" */ |
| 4937 | if ((cur_end - p1 >= 21) && |
| 4938 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 4939 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4940 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4941 | continue; |
| 4942 | } |
| 4943 | |
| 4944 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 4945 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 4946 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 4947 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 4948 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4949 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4950 | return; |
| 4951 | } |
| 4952 | |
| 4953 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4954 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4955 | continue; |
| 4956 | } |
| 4957 | } |
| 4958 | } |
| 4959 | |
| 4960 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4961 | /* |
| 4962 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 4963 | * If the server is found, it's assigned to the session. |
| 4964 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4965 | 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] | 4966 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4967 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4968 | appsess *asession_temp = NULL; |
| 4969 | appsess local_asession; |
| 4970 | char *request_line; |
| 4971 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4972 | if (t->be->appsession_name == NULL || |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 4973 | (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) || |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4974 | (request_line = memchr(begin, ';', len)) == NULL || |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4975 | ((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] | 4976 | return; |
| 4977 | |
| 4978 | /* skip ';' */ |
| 4979 | request_line++; |
| 4980 | |
| 4981 | /* look if we have a jsessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4982 | 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] | 4983 | return; |
| 4984 | |
| 4985 | /* skip jsessionid= */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4986 | request_line += t->be->appsession_name_len + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4987 | |
| 4988 | /* First try if we already have an appsession */ |
| 4989 | asession_temp = &local_asession; |
| 4990 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4991 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4992 | Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n"); |
| 4993 | send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n"); |
| 4994 | return; |
| 4995 | } |
| 4996 | |
| 4997 | /* Copy the sessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4998 | memcpy(asession_temp->sessid, request_line, t->be->appsession_len); |
| 4999 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5000 | asession_temp->serverid = NULL; |
| 5001 | |
| 5002 | /* only do insert, if lookup fails */ |
Ryan Warnick | 6d0b1fa | 2008-02-17 11:24:35 +0100 | [diff] [blame] | 5003 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 5004 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5005 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5006 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5007 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5008 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 5009 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 5010 | return; |
| 5011 | } |
| 5012 | asession_temp->sessid = local_asession.sessid; |
| 5013 | asession_temp->serverid = local_asession.serverid; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5014 | asession_temp->request_count=0; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5015 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5016 | } |
| 5017 | else { |
| 5018 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5019 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5020 | } |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5021 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 5022 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5023 | asession_temp->request_count++; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5024 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5025 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5026 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5027 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5028 | #endif |
| 5029 | if (asession_temp->serverid == NULL) { |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5030 | /* TODO redispatch request */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5031 | Alert("Found Application Session without matching server.\n"); |
| 5032 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5033 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5034 | while (srv) { |
| 5035 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5036 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5037 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5038 | txn->flags &= ~TX_CK_MASK; |
| 5039 | txn->flags |= TX_CK_VALID; |
| 5040 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5041 | t->srv = srv; |
| 5042 | break; |
| 5043 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5044 | txn->flags &= ~TX_CK_MASK; |
| 5045 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5046 | } |
| 5047 | } |
| 5048 | srv = srv->next; |
| 5049 | } |
| 5050 | } |
| 5051 | } |
| 5052 | |
| 5053 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5054 | /* |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5055 | * In a GET or HEAD request, check if the requested URI matches the stats uri |
| 5056 | * 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] | 5057 | * |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5058 | * 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] | 5059 | * 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] | 5060 | * produce_content() can be called to start sending data. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5061 | * |
| 5062 | * Returns 1 if the session's state changes, otherwise 0. |
| 5063 | */ |
| 5064 | int stats_check_uri_auth(struct session *t, struct proxy *backend) |
| 5065 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5066 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5067 | struct uri_auth *uri_auth = backend->uri_auth; |
| 5068 | struct user_auth *user; |
| 5069 | int authenticated, cur_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5070 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5071 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5072 | memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats)); |
| 5073 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5074 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5075 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5076 | return 0; |
| 5077 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5078 | h = t->req->data + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5079 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5080 | /* the URI is in h */ |
| 5081 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5082 | return 0; |
| 5083 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 5084 | h += uri_auth->uri_len; |
| 5085 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) { |
| 5086 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5087 | t->data_ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 5088 | break; |
| 5089 | } |
| 5090 | h++; |
| 5091 | } |
| 5092 | |
| 5093 | if (uri_auth->refresh) { |
| 5094 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 5095 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) { |
| 5096 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5097 | t->data_ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 5098 | break; |
| 5099 | } |
| 5100 | h++; |
| 5101 | } |
| 5102 | } |
| 5103 | |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 5104 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 5105 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) { |
| 5106 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5107 | t->data_ctx.stats.flags |= STAT_FMT_CSV; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 5108 | break; |
| 5109 | } |
| 5110 | h++; |
| 5111 | } |
| 5112 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5113 | t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO; |
| 5114 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5115 | /* we are in front of a interceptable URI. Let's check |
| 5116 | * if there's an authentication and if it's valid. |
| 5117 | */ |
| 5118 | user = uri_auth->users; |
| 5119 | if (!user) { |
| 5120 | /* no user auth required, it's OK */ |
| 5121 | authenticated = 1; |
| 5122 | } else { |
| 5123 | authenticated = 0; |
| 5124 | |
| 5125 | /* a user list is defined, we have to check. |
| 5126 | * skip 21 chars for "Authorization: Basic ". |
| 5127 | */ |
| 5128 | |
| 5129 | /* FIXME: this should move to an earlier place */ |
| 5130 | cur_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5131 | h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 5132 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 5133 | int len = txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5134 | if (len > 14 && |
| 5135 | !strncasecmp("Authorization:", h, 14)) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5136 | txn->auth_hdr.str = h; |
| 5137 | txn->auth_hdr.len = len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5138 | break; |
| 5139 | } |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5140 | h += len + txn->hdr_idx.v[cur_idx].cr + 1; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5141 | } |
| 5142 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5143 | if (txn->auth_hdr.len < 21 || |
| 5144 | memcmp(txn->auth_hdr.str + 14, " Basic ", 7)) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5145 | user = NULL; |
| 5146 | |
| 5147 | while (user) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5148 | if ((txn->auth_hdr.len == user->user_len + 14 + 7) |
| 5149 | && !memcmp(txn->auth_hdr.str + 14 + 7, |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5150 | user->user_pwd, user->user_len)) { |
| 5151 | authenticated = 1; |
| 5152 | break; |
| 5153 | } |
| 5154 | user = user->next; |
| 5155 | } |
| 5156 | } |
| 5157 | |
| 5158 | if (!authenticated) { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5159 | struct chunk msg; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5160 | |
| 5161 | /* no need to go further */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5162 | msg.str = trash; |
| 5163 | msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm); |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5164 | txn->status = 401; |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5165 | client_retnclose(t, &msg); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 5166 | trace_term(t, TT_HTTP_URI_1); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 5167 | t->analysis &= ~AN_REQ_ANY; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5168 | if (!(t->flags & SN_ERR_MASK)) |
| 5169 | t->flags |= SN_ERR_PRXCOND; |
| 5170 | if (!(t->flags & SN_FINST_MASK)) |
| 5171 | t->flags |= SN_FINST_R; |
| 5172 | return 1; |
| 5173 | } |
| 5174 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5175 | /* The request is valid, the user is authenticated. Let's start sending |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5176 | * data. |
| 5177 | */ |
Willy Tarreau | 284c7b3 | 2008-06-29 16:38:43 +0200 | [diff] [blame] | 5178 | EV_FD_CLR(t->cli_fd, DIR_RD); |
| 5179 | buffer_shutr(t->req); |
| 5180 | buffer_shutr(t->rep); |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 5181 | buffer_set_rlim(t->req, BUFSIZE); /* no more rewrite needed */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 5182 | t->logs.tv_request = now; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5183 | t->data_source = DATA_SRC_STATS; |
| 5184 | t->data_state = DATA_ST_INIT; |
Willy Tarreau | 91e9993 | 2008-06-30 07:51:00 +0200 | [diff] [blame] | 5185 | t->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5186 | produce_content(t); |
| 5187 | return 1; |
| 5188 | } |
| 5189 | |
| 5190 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5191 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5192 | * Print a debug line with a header |
| 5193 | */ |
| 5194 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 5195 | { |
| 5196 | int len, max; |
| 5197 | len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id, |
| 5198 | dir, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd); |
| 5199 | max = end - start; |
| 5200 | UBOUND(max, sizeof(trash) - len - 1); |
| 5201 | len += strlcpy2(trash + len, start, max + 1); |
| 5202 | trash[len++] = '\n'; |
| 5203 | write(1, trash, len); |
| 5204 | } |
| 5205 | |
| 5206 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5207 | /************************************************************************/ |
| 5208 | /* The code below is dedicated to ACL parsing and matching */ |
| 5209 | /************************************************************************/ |
| 5210 | |
| 5211 | |
| 5212 | |
| 5213 | |
| 5214 | /* 1. Check on METHOD |
| 5215 | * We use the pre-parsed method if it is known, and store its number as an |
| 5216 | * integer. If it is unknown, we use the pointer and the length. |
| 5217 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5218 | 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] | 5219 | { |
| 5220 | int len, meth; |
| 5221 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5222 | len = strlen(*text); |
| 5223 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5224 | |
| 5225 | pattern->val.i = meth; |
| 5226 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5227 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5228 | if (!pattern->ptr.str) |
| 5229 | return 0; |
| 5230 | pattern->len = len; |
| 5231 | } |
| 5232 | return 1; |
| 5233 | } |
| 5234 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5235 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5236 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5237 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5238 | { |
| 5239 | int meth; |
| 5240 | struct http_txn *txn = l7; |
| 5241 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5242 | if (!txn) |
| 5243 | return 0; |
| 5244 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5245 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5246 | return 0; |
| 5247 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5248 | meth = txn->meth; |
| 5249 | test->i = meth; |
| 5250 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5251 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5252 | /* ensure the indexes are not affected */ |
| 5253 | return 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5254 | test->len = txn->req.sl.rq.m_l; |
| 5255 | test->ptr = txn->req.sol; |
| 5256 | } |
| 5257 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5258 | return 1; |
| 5259 | } |
| 5260 | |
| 5261 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 5262 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 5263 | int icase; |
| 5264 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5265 | if (test->i != pattern->val.i) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5266 | return ACL_PAT_FAIL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5267 | |
| 5268 | if (test->i != HTTP_METH_OTHER) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5269 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5270 | |
| 5271 | /* Other method, we must compare the strings */ |
| 5272 | if (pattern->len != test->len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5273 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 5274 | |
| 5275 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 5276 | if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) || |
| 5277 | (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5278 | return ACL_PAT_FAIL; |
| 5279 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5280 | } |
| 5281 | |
| 5282 | /* 2. Check on Request/Status Version |
| 5283 | * We simply compare strings here. |
| 5284 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5285 | 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] | 5286 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5287 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5288 | if (!pattern->ptr.str) |
| 5289 | return 0; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5290 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5291 | return 1; |
| 5292 | } |
| 5293 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5294 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5295 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5296 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5297 | { |
| 5298 | struct http_txn *txn = l7; |
| 5299 | char *ptr; |
| 5300 | int len; |
| 5301 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5302 | if (!txn) |
| 5303 | return 0; |
| 5304 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5305 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5306 | return 0; |
| 5307 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5308 | len = txn->req.sl.rq.v_l; |
| 5309 | ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som; |
| 5310 | |
| 5311 | while ((len-- > 0) && (*ptr++ != '/')); |
| 5312 | if (len <= 0) |
| 5313 | return 0; |
| 5314 | |
| 5315 | test->ptr = ptr; |
| 5316 | test->len = len; |
| 5317 | |
| 5318 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5319 | return 1; |
| 5320 | } |
| 5321 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5322 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5323 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5324 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5325 | { |
| 5326 | struct http_txn *txn = l7; |
| 5327 | char *ptr; |
| 5328 | int len; |
| 5329 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5330 | if (!txn) |
| 5331 | return 0; |
| 5332 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5333 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5334 | return 0; |
| 5335 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5336 | len = txn->rsp.sl.st.v_l; |
| 5337 | ptr = txn->rsp.sol; |
| 5338 | |
| 5339 | while ((len-- > 0) && (*ptr++ != '/')); |
| 5340 | if (len <= 0) |
| 5341 | return 0; |
| 5342 | |
| 5343 | test->ptr = ptr; |
| 5344 | test->len = len; |
| 5345 | |
| 5346 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5347 | return 1; |
| 5348 | } |
| 5349 | |
| 5350 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5351 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5352 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5353 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5354 | { |
| 5355 | struct http_txn *txn = l7; |
| 5356 | char *ptr; |
| 5357 | int len; |
| 5358 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5359 | if (!txn) |
| 5360 | return 0; |
| 5361 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5362 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5363 | return 0; |
| 5364 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5365 | len = txn->rsp.sl.st.c_l; |
| 5366 | ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som; |
| 5367 | |
| 5368 | test->i = __strl2ui(ptr, len); |
| 5369 | test->flags = ACL_TEST_F_VOL_1ST; |
| 5370 | return 1; |
| 5371 | } |
| 5372 | |
| 5373 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5374 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5375 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5376 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5377 | { |
| 5378 | struct http_txn *txn = l7; |
| 5379 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5380 | if (!txn) |
| 5381 | return 0; |
| 5382 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5383 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5384 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5385 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5386 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5387 | /* ensure the indexes are not affected */ |
| 5388 | return 0; |
| 5389 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5390 | test->len = txn->req.sl.rq.u_l; |
| 5391 | test->ptr = txn->req.sol + txn->req.sl.rq.u; |
| 5392 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 5393 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 5394 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5395 | return 1; |
| 5396 | } |
| 5397 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5398 | static int |
| 5399 | acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5400 | struct acl_expr *expr, struct acl_test *test) |
| 5401 | { |
| 5402 | struct http_txn *txn = l7; |
| 5403 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5404 | if (!txn) |
| 5405 | return 0; |
| 5406 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5407 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5408 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5409 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5410 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5411 | /* ensure the indexes are not affected */ |
| 5412 | return 0; |
| 5413 | |
| 5414 | /* Parse HTTP request */ |
| 5415 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
| 5416 | test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr; |
| 5417 | test->i = AF_INET; |
| 5418 | |
| 5419 | /* |
| 5420 | * If we are parsing url in frontend space, we prepare backend stage |
| 5421 | * to not parse again the same url ! optimization lazyness... |
| 5422 | */ |
| 5423 | if (px->options & PR_O_HTTP_PROXY) |
| 5424 | l4->flags |= SN_ADDR_SET; |
| 5425 | |
| 5426 | test->flags = ACL_TEST_F_READ_ONLY; |
| 5427 | return 1; |
| 5428 | } |
| 5429 | |
| 5430 | static int |
| 5431 | acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5432 | struct acl_expr *expr, struct acl_test *test) |
| 5433 | { |
| 5434 | struct http_txn *txn = l7; |
| 5435 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5436 | if (!txn) |
| 5437 | return 0; |
| 5438 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5439 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5440 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5441 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5442 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5443 | /* ensure the indexes are not affected */ |
| 5444 | return 0; |
| 5445 | |
| 5446 | /* Same optimization as url_ip */ |
| 5447 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
| 5448 | test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port); |
| 5449 | |
| 5450 | if (px->options & PR_O_HTTP_PROXY) |
| 5451 | l4->flags |= SN_ADDR_SET; |
| 5452 | |
| 5453 | test->flags = ACL_TEST_F_READ_ONLY; |
| 5454 | return 1; |
| 5455 | } |
| 5456 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5457 | /* 5. Check on HTTP header. A pointer to the beginning of the value is returned. |
| 5458 | * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr(). |
| 5459 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5460 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5461 | 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] | 5462 | struct acl_expr *expr, struct acl_test *test) |
| 5463 | { |
| 5464 | struct http_txn *txn = l7; |
| 5465 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5466 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5467 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5468 | if (!txn) |
| 5469 | return 0; |
| 5470 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5471 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 5472 | /* search for header from the beginning */ |
| 5473 | ctx->idx = 0; |
| 5474 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5475 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 5476 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 5477 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5478 | test->len = ctx->vlen; |
| 5479 | test->ptr = (char *)ctx->line + ctx->val; |
| 5480 | return 1; |
| 5481 | } |
| 5482 | |
| 5483 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 5484 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5485 | return 0; |
| 5486 | } |
| 5487 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5488 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5489 | acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5490 | struct acl_expr *expr, struct acl_test *test) |
| 5491 | { |
| 5492 | struct http_txn *txn = l7; |
| 5493 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5494 | if (!txn) |
| 5495 | return 0; |
| 5496 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5497 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5498 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5499 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5500 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5501 | /* ensure the indexes are not affected */ |
| 5502 | return 0; |
| 5503 | |
| 5504 | return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test); |
| 5505 | } |
| 5506 | |
| 5507 | static int |
| 5508 | acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5509 | struct acl_expr *expr, struct acl_test *test) |
| 5510 | { |
| 5511 | struct http_txn *txn = l7; |
| 5512 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5513 | if (!txn) |
| 5514 | return 0; |
| 5515 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5516 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5517 | return 0; |
| 5518 | |
| 5519 | return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test); |
| 5520 | } |
| 5521 | |
| 5522 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 5523 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 5524 | */ |
| 5525 | static int |
| 5526 | 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] | 5527 | struct acl_expr *expr, struct acl_test *test) |
| 5528 | { |
| 5529 | struct http_txn *txn = l7; |
| 5530 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5531 | struct hdr_ctx ctx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5532 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5533 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5534 | if (!txn) |
| 5535 | return 0; |
| 5536 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5537 | ctx.idx = 0; |
| 5538 | cnt = 0; |
| 5539 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx)) |
| 5540 | cnt++; |
| 5541 | |
| 5542 | test->i = cnt; |
| 5543 | test->flags = ACL_TEST_F_VOL_HDR; |
| 5544 | return 1; |
| 5545 | } |
| 5546 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5547 | static int |
| 5548 | acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5549 | struct acl_expr *expr, struct acl_test *test) |
| 5550 | { |
| 5551 | struct http_txn *txn = l7; |
| 5552 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5553 | if (!txn) |
| 5554 | return 0; |
| 5555 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5556 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5557 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5558 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5559 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5560 | /* ensure the indexes are not affected */ |
| 5561 | return 0; |
| 5562 | |
| 5563 | return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test); |
| 5564 | } |
| 5565 | |
| 5566 | static int |
| 5567 | acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5568 | struct acl_expr *expr, struct acl_test *test) |
| 5569 | { |
| 5570 | struct http_txn *txn = l7; |
| 5571 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5572 | if (!txn) |
| 5573 | return 0; |
| 5574 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5575 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5576 | return 0; |
| 5577 | |
| 5578 | return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test); |
| 5579 | } |
| 5580 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5581 | /* 7. Check on HTTP header's integer value. The integer value is returned. |
| 5582 | * FIXME: the type is 'int', it may not be appropriate for everything. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5583 | * 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] | 5584 | */ |
| 5585 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5586 | 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] | 5587 | struct acl_expr *expr, struct acl_test *test) |
| 5588 | { |
| 5589 | struct http_txn *txn = l7; |
| 5590 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5591 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5592 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5593 | if (!txn) |
| 5594 | return 0; |
| 5595 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5596 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 5597 | /* search for header from the beginning */ |
| 5598 | ctx->idx = 0; |
| 5599 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5600 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 5601 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 5602 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5603 | test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen); |
| 5604 | return 1; |
| 5605 | } |
| 5606 | |
| 5607 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 5608 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5609 | return 0; |
| 5610 | } |
| 5611 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5612 | static int |
| 5613 | acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5614 | struct acl_expr *expr, struct acl_test *test) |
| 5615 | { |
| 5616 | struct http_txn *txn = l7; |
| 5617 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5618 | if (!txn) |
| 5619 | return 0; |
| 5620 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5621 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5622 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5623 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5624 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5625 | /* ensure the indexes are not affected */ |
| 5626 | return 0; |
| 5627 | |
| 5628 | return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test); |
| 5629 | } |
| 5630 | |
| 5631 | static int |
| 5632 | acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5633 | struct acl_expr *expr, struct acl_test *test) |
| 5634 | { |
| 5635 | struct http_txn *txn = l7; |
| 5636 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5637 | if (!txn) |
| 5638 | return 0; |
| 5639 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5640 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5641 | return 0; |
| 5642 | |
| 5643 | return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test); |
| 5644 | } |
| 5645 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5646 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 5647 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 5648 | */ |
| 5649 | static int |
| 5650 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5651 | struct acl_expr *expr, struct acl_test *test) |
| 5652 | { |
| 5653 | struct http_txn *txn = l7; |
| 5654 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5655 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5656 | if (!txn) |
| 5657 | return 0; |
| 5658 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5659 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5660 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5661 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5662 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5663 | /* ensure the indexes are not affected */ |
| 5664 | return 0; |
| 5665 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5666 | end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 5667 | ptr = http_get_path(txn); |
| 5668 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5669 | return 0; |
| 5670 | |
| 5671 | /* OK, we got the '/' ! */ |
| 5672 | test->ptr = ptr; |
| 5673 | |
| 5674 | while (ptr < end && *ptr != '?') |
| 5675 | ptr++; |
| 5676 | |
| 5677 | test->len = ptr - test->ptr; |
| 5678 | |
| 5679 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 5680 | test->flags = ACL_TEST_F_VOL_1ST; |
| 5681 | return 1; |
| 5682 | } |
| 5683 | |
| 5684 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5685 | |
| 5686 | /************************************************************************/ |
| 5687 | /* All supported keywords must be declared here. */ |
| 5688 | /************************************************************************/ |
| 5689 | |
| 5690 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 5691 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5692 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT }, |
| 5693 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5694 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 5695 | { "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] | 5696 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5697 | { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5698 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 5699 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 5700 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 5701 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 5702 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 5703 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 5704 | { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE }, |
| 5705 | { "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] | 5706 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5707 | /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */ |
| 5708 | { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5709 | { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 5710 | { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 5711 | { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 5712 | { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 5713 | { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 5714 | { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 5715 | { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
| 5716 | { "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] | 5717 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5718 | { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 5719 | { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE }, |
| 5720 | { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE }, |
| 5721 | { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE }, |
| 5722 | { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE }, |
| 5723 | { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE }, |
| 5724 | { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE }, |
| 5725 | { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
| 5726 | { "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] | 5727 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5728 | { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5729 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 5730 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 5731 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 5732 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 5733 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 5734 | { "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] | 5735 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 5736 | { NULL, NULL, NULL, NULL }, |
| 5737 | |
| 5738 | #if 0 |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5739 | { "line", acl_parse_str, acl_fetch_line, acl_match_str }, |
| 5740 | { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg }, |
| 5741 | { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg }, |
| 5742 | { "line_end", acl_parse_str, acl_fetch_line, acl_match_end }, |
| 5743 | { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub }, |
| 5744 | { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir }, |
| 5745 | { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom }, |
| 5746 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5747 | { "cook", acl_parse_str, acl_fetch_cook, acl_match_str }, |
| 5748 | { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg }, |
| 5749 | { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg }, |
| 5750 | { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end }, |
| 5751 | { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub }, |
| 5752 | { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir }, |
| 5753 | { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom }, |
| 5754 | { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst }, |
| 5755 | |
| 5756 | { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str }, |
| 5757 | { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg }, |
| 5758 | { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str }, |
| 5759 | { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 }, |
| 5760 | { NULL, NULL, NULL, NULL }, |
| 5761 | #endif |
| 5762 | }}; |
| 5763 | |
| 5764 | |
| 5765 | __attribute__((constructor)) |
| 5766 | static void __http_protocol_init(void) |
| 5767 | { |
| 5768 | acl_register_keywords(&acl_kws); |
| 5769 | } |
| 5770 | |
| 5771 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5772 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5773 | * Local variables: |
| 5774 | * c-indent-level: 8 |
| 5775 | * c-basic-offset: 8 |
| 5776 | * End: |
| 5777 | */ |