Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP protocol analyzer |
| 3 | * |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 4 | * Copyright 2000-2008 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <ctype.h> |
| 14 | #include <errno.h> |
| 15 | #include <fcntl.h> |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <string.h> |
| 19 | #include <syslog.h> |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 20 | #include <time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | |
| 22 | #include <sys/socket.h> |
| 23 | #include <sys/stat.h> |
| 24 | #include <sys/types.h> |
| 25 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 26 | #include <common/appsession.h> |
| 27 | #include <common/compat.h> |
| 28 | #include <common/config.h> |
Willy Tarreau | a4cd1f5 | 2006-12-16 19:57:26 +0100 | [diff] [blame] | 29 | #include <common/debug.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 30 | #include <common/memory.h> |
| 31 | #include <common/mini-clist.h> |
| 32 | #include <common/standard.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 33 | #include <common/ticks.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 34 | #include <common/time.h> |
| 35 | #include <common/uri_auth.h> |
| 36 | #include <common/version.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 37 | |
| 38 | #include <types/capture.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 39 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 40 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 41 | #include <proto/acl.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 42 | #include <proto/backend.h> |
| 43 | #include <proto/buffers.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 44 | #include <proto/dumpstats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 45 | #include <proto/fd.h> |
| 46 | #include <proto/log.h> |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 47 | #include <proto/hdr_idx.h> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 48 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 49 | #include <proto/proto_http.h> |
| 50 | #include <proto/queue.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 51 | #include <proto/senddata.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 52 | #include <proto/session.h> |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 53 | #include <proto/stream_sock.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 54 | #include <proto/task.h> |
| 55 | |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 56 | #ifdef CONFIG_HAP_TCPSPLICE |
| 57 | #include <libtcpsplice.h> |
| 58 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 59 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 60 | #define DEBUG_PARSE_NO_SPEEDUP |
| 61 | #undef DEBUG_PARSE_NO_SPEEDUP |
| 62 | |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 63 | /* This is used to perform a quick jump as an alternative to a break/continue |
| 64 | * instruction. The first argument is the label for normal operation, and the |
| 65 | * second one is the break/continue instruction in the no_speedup mode. |
| 66 | */ |
| 67 | |
| 68 | #ifdef DEBUG_PARSE_NO_SPEEDUP |
| 69 | #define QUICK_JUMP(x,y) y |
| 70 | #else |
| 71 | #define QUICK_JUMP(x,y) goto x |
| 72 | #endif |
| 73 | |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 74 | /* This is used by remote monitoring */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 75 | const char HTTP_200[] = |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 76 | "HTTP/1.0 200 OK\r\n" |
| 77 | "Cache-Control: no-cache\r\n" |
| 78 | "Connection: close\r\n" |
| 79 | "Content-Type: text/html\r\n" |
| 80 | "\r\n" |
| 81 | "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n"; |
| 82 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 83 | const struct chunk http_200_chunk = { |
| 84 | .str = (char *)&HTTP_200, |
| 85 | .len = sizeof(HTTP_200)-1 |
| 86 | }; |
| 87 | |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 88 | const char *HTTP_301 = |
| 89 | "HTTP/1.0 301 Moved Permantenly\r\n" |
| 90 | "Cache-Control: no-cache\r\n" |
| 91 | "Connection: close\r\n" |
| 92 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 93 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 94 | const char *HTTP_302 = |
| 95 | "HTTP/1.0 302 Found\r\n" |
| 96 | "Cache-Control: no-cache\r\n" |
| 97 | "Connection: close\r\n" |
| 98 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 99 | |
| 100 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 101 | const char *HTTP_303 = |
| 102 | "HTTP/1.0 303 See Other\r\n" |
| 103 | "Cache-Control: no-cache\r\n" |
| 104 | "Connection: close\r\n" |
| 105 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 106 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 107 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 108 | const char *HTTP_401_fmt = |
| 109 | "HTTP/1.0 401 Unauthorized\r\n" |
| 110 | "Cache-Control: no-cache\r\n" |
| 111 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 112 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 113 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 114 | "\r\n" |
| 115 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 116 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 117 | |
| 118 | const int http_err_codes[HTTP_ERR_SIZE] = { |
| 119 | [HTTP_ERR_400] = 400, |
| 120 | [HTTP_ERR_403] = 403, |
| 121 | [HTTP_ERR_408] = 408, |
| 122 | [HTTP_ERR_500] = 500, |
| 123 | [HTTP_ERR_502] = 502, |
| 124 | [HTTP_ERR_503] = 503, |
| 125 | [HTTP_ERR_504] = 504, |
| 126 | }; |
| 127 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 128 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 129 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 130 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 131 | "Cache-Control: no-cache\r\n" |
| 132 | "Connection: close\r\n" |
| 133 | "Content-Type: text/html\r\n" |
| 134 | "\r\n" |
| 135 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 136 | |
| 137 | [HTTP_ERR_403] = |
| 138 | "HTTP/1.0 403 Forbidden\r\n" |
| 139 | "Cache-Control: no-cache\r\n" |
| 140 | "Connection: close\r\n" |
| 141 | "Content-Type: text/html\r\n" |
| 142 | "\r\n" |
| 143 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 144 | |
| 145 | [HTTP_ERR_408] = |
| 146 | "HTTP/1.0 408 Request Time-out\r\n" |
| 147 | "Cache-Control: no-cache\r\n" |
| 148 | "Connection: close\r\n" |
| 149 | "Content-Type: text/html\r\n" |
| 150 | "\r\n" |
| 151 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 152 | |
| 153 | [HTTP_ERR_500] = |
| 154 | "HTTP/1.0 500 Server Error\r\n" |
| 155 | "Cache-Control: no-cache\r\n" |
| 156 | "Connection: close\r\n" |
| 157 | "Content-Type: text/html\r\n" |
| 158 | "\r\n" |
| 159 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 160 | |
| 161 | [HTTP_ERR_502] = |
| 162 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 163 | "Cache-Control: no-cache\r\n" |
| 164 | "Connection: close\r\n" |
| 165 | "Content-Type: text/html\r\n" |
| 166 | "\r\n" |
| 167 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 168 | |
| 169 | [HTTP_ERR_503] = |
| 170 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 171 | "Cache-Control: no-cache\r\n" |
| 172 | "Connection: close\r\n" |
| 173 | "Content-Type: text/html\r\n" |
| 174 | "\r\n" |
| 175 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 176 | |
| 177 | [HTTP_ERR_504] = |
| 178 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 179 | "Cache-Control: no-cache\r\n" |
| 180 | "Connection: close\r\n" |
| 181 | "Content-Type: text/html\r\n" |
| 182 | "\r\n" |
| 183 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 184 | |
| 185 | }; |
| 186 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 187 | /* We must put the messages here since GCC cannot initialize consts depending |
| 188 | * on strlen(). |
| 189 | */ |
| 190 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 191 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 192 | #define FD_SETS_ARE_BITFIELDS |
| 193 | #ifdef FD_SETS_ARE_BITFIELDS |
| 194 | /* |
| 195 | * This map is used with all the FD_* macros to check whether a particular bit |
| 196 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 197 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 198 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 199 | * exclusively to the macros. |
| 200 | */ |
| 201 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 202 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 203 | |
| 204 | #else |
| 205 | #error "Check if your OS uses bitfields for fd_sets" |
| 206 | #endif |
| 207 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 208 | void init_proto_http() |
| 209 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 210 | int i; |
| 211 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 212 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 213 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 214 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 215 | if (!http_err_msgs[msg]) { |
| 216 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 217 | abort(); |
| 218 | } |
| 219 | |
| 220 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 221 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 222 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 223 | |
| 224 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 225 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 226 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 227 | * printable characters above. |
| 228 | */ |
| 229 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 230 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 231 | for (i = 0; i < 32; i++) { |
| 232 | FD_SET(i, hdr_encode_map); |
| 233 | FD_SET(i, url_encode_map); |
| 234 | } |
| 235 | for (i = 127; i < 256; i++) { |
| 236 | FD_SET(i, hdr_encode_map); |
| 237 | FD_SET(i, url_encode_map); |
| 238 | } |
| 239 | |
| 240 | tmp = "\"#{|}"; |
| 241 | while (*tmp) { |
| 242 | FD_SET(*tmp, hdr_encode_map); |
| 243 | tmp++; |
| 244 | } |
| 245 | |
| 246 | tmp = "\"#"; |
| 247 | while (*tmp) { |
| 248 | FD_SET(*tmp, url_encode_map); |
| 249 | tmp++; |
| 250 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 251 | |
| 252 | /* memory allocations */ |
| 253 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 254 | pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 255 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 256 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 257 | /* |
| 258 | * We have 26 list of methods (1 per first letter), each of which can have |
| 259 | * up to 3 entries (2 valid, 1 null). |
| 260 | */ |
| 261 | struct http_method_desc { |
| 262 | http_meth_t meth; |
| 263 | int len; |
| 264 | const char text[8]; |
| 265 | }; |
| 266 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 267 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 268 | ['C' - 'A'] = { |
| 269 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 270 | }, |
| 271 | ['D' - 'A'] = { |
| 272 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 273 | }, |
| 274 | ['G' - 'A'] = { |
| 275 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 276 | }, |
| 277 | ['H' - 'A'] = { |
| 278 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 279 | }, |
| 280 | ['P' - 'A'] = { |
| 281 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 282 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 283 | }, |
| 284 | ['T' - 'A'] = { |
| 285 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 286 | }, |
| 287 | /* rest is empty like this : |
| 288 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 289 | */ |
| 290 | }; |
| 291 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 292 | /* It is about twice as fast on recent architectures to lookup a byte in a |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 293 | * table than to perform a boolean AND or OR between two tests. Refer to |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 294 | * RFC2616 for those chars. |
| 295 | */ |
| 296 | |
| 297 | const char http_is_spht[256] = { |
| 298 | [' '] = 1, ['\t'] = 1, |
| 299 | }; |
| 300 | |
| 301 | const char http_is_crlf[256] = { |
| 302 | ['\r'] = 1, ['\n'] = 1, |
| 303 | }; |
| 304 | |
| 305 | const char http_is_lws[256] = { |
| 306 | [' '] = 1, ['\t'] = 1, |
| 307 | ['\r'] = 1, ['\n'] = 1, |
| 308 | }; |
| 309 | |
| 310 | const char http_is_sep[256] = { |
| 311 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 312 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 313 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 314 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 315 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 316 | }; |
| 317 | |
| 318 | const char http_is_ctl[256] = { |
| 319 | [0 ... 31] = 1, |
| 320 | [127] = 1, |
| 321 | }; |
| 322 | |
| 323 | /* |
| 324 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 325 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 326 | * them correctly. Instead, define every non-CTL char's status. |
| 327 | */ |
| 328 | const char http_is_token[256] = { |
| 329 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 330 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 331 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 332 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 333 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 334 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 335 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 336 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 337 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 338 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 339 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 340 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 341 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 342 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 343 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 344 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 345 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 346 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 347 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 348 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 349 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 350 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 351 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 352 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 353 | }; |
| 354 | |
| 355 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 356 | /* |
| 357 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 358 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 359 | */ |
| 360 | const char http_is_ver_token[256] = { |
| 361 | ['.'] = 1, ['/'] = 1, |
| 362 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 363 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| 364 | ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| 365 | }; |
| 366 | |
| 367 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 368 | #ifdef DEBUG_FULL |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 369 | static char *cli_stnames[4] = { "DAT", "SHR", "SHW", "CLS" }; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 370 | #endif |
| 371 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 372 | static void http_sess_log(struct session *s); |
| 373 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 374 | /* |
| 375 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 376 | * CRLF. Text length is measured first, so it cannot be NULL. |
| 377 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 378 | * of headers is automatically adjusted. The number of bytes added is returned |
| 379 | * on success, otherwise <0 is returned indicating an error. |
| 380 | */ |
| 381 | int http_header_add_tail(struct buffer *b, struct http_msg *msg, |
| 382 | struct hdr_idx *hdr_idx, const char *text) |
| 383 | { |
| 384 | int bytes, len; |
| 385 | |
| 386 | len = strlen(text); |
| 387 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 388 | if (!bytes) |
| 389 | return -1; |
| 390 | msg->eoh += bytes; |
| 391 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 392 | } |
| 393 | |
| 394 | /* |
| 395 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 396 | * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then |
| 397 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 398 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 399 | * of headers is automatically adjusted. The number of bytes added is returned |
| 400 | * on success, otherwise <0 is returned indicating an error. |
| 401 | */ |
| 402 | int http_header_add_tail2(struct buffer *b, struct http_msg *msg, |
| 403 | struct hdr_idx *hdr_idx, const char *text, int len) |
| 404 | { |
| 405 | int bytes; |
| 406 | |
| 407 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 408 | if (!bytes) |
| 409 | return -1; |
| 410 | msg->eoh += bytes; |
| 411 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 412 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 413 | |
| 414 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 415 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 416 | * If so, returns the position of the first non-space character relative to |
| 417 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 418 | * to return a pointer to the place after the first space. Returns 0 if the |
| 419 | * header name does not match. Checks are case-insensitive. |
| 420 | */ |
| 421 | int http_header_match2(const char *hdr, const char *end, |
| 422 | const char *name, int len) |
| 423 | { |
| 424 | const char *val; |
| 425 | |
| 426 | if (hdr + len >= end) |
| 427 | return 0; |
| 428 | if (hdr[len] != ':') |
| 429 | return 0; |
| 430 | if (strncasecmp(hdr, name, len) != 0) |
| 431 | return 0; |
| 432 | val = hdr + len + 1; |
| 433 | while (val < end && HTTP_IS_SPHT(*val)) |
| 434 | val++; |
| 435 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 436 | return len + 2; /* we may replace starting from second space */ |
| 437 | return val - hdr; |
| 438 | } |
| 439 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 440 | /* Find the end of the header value contained between <s> and <e>. |
| 441 | * See RFC2616, par 2.2 for more information. Note that it requires |
| 442 | * a valid header to return a valid result. |
| 443 | */ |
| 444 | const char *find_hdr_value_end(const char *s, const char *e) |
| 445 | { |
| 446 | int quoted, qdpair; |
| 447 | |
| 448 | quoted = qdpair = 0; |
| 449 | for (; s < e; s++) { |
| 450 | if (qdpair) qdpair = 0; |
| 451 | else if (quoted && *s == '\\') qdpair = 1; |
| 452 | else if (quoted && *s == '"') quoted = 0; |
| 453 | else if (*s == '"') quoted = 1; |
| 454 | else if (*s == ',') return s; |
| 455 | } |
| 456 | return s; |
| 457 | } |
| 458 | |
| 459 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 460 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 461 | * structure holds everything necessary to use the header and find next |
| 462 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 463 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 464 | * 1 when it finds a value, and 0 when there is no more. |
| 465 | */ |
| 466 | int http_find_header2(const char *name, int len, |
| 467 | const char *sol, struct hdr_idx *idx, |
| 468 | struct hdr_ctx *ctx) |
| 469 | { |
| 470 | __label__ return_hdr, next_hdr; |
| 471 | const char *eol, *sov; |
| 472 | int cur_idx; |
| 473 | |
| 474 | if (ctx->idx) { |
| 475 | /* We have previously returned a value, let's search |
| 476 | * another one on the same line. |
| 477 | */ |
| 478 | cur_idx = ctx->idx; |
| 479 | sol = ctx->line; |
| 480 | sov = sol + ctx->val + ctx->vlen; |
| 481 | eol = sol + idx->v[cur_idx].len; |
| 482 | |
| 483 | if (sov >= eol) |
| 484 | /* no more values in this header */ |
| 485 | goto next_hdr; |
| 486 | |
| 487 | /* values remaining for this header, skip the comma */ |
| 488 | sov++; |
| 489 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 490 | sov++; |
| 491 | |
| 492 | goto return_hdr; |
| 493 | } |
| 494 | |
| 495 | /* first request for this header */ |
| 496 | sol += hdr_idx_first_pos(idx); |
| 497 | cur_idx = hdr_idx_first_idx(idx); |
| 498 | |
| 499 | while (cur_idx) { |
| 500 | eol = sol + idx->v[cur_idx].len; |
| 501 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 502 | if (len == 0) { |
| 503 | /* No argument was passed, we want any header. |
| 504 | * To achieve this, we simply build a fake request. */ |
| 505 | while (sol + len < eol && sol[len] != ':') |
| 506 | len++; |
| 507 | name = sol; |
| 508 | } |
| 509 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 510 | if ((len < eol - sol) && |
| 511 | (sol[len] == ':') && |
| 512 | (strncasecmp(sol, name, len) == 0)) { |
| 513 | |
| 514 | sov = sol + len + 1; |
| 515 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 516 | sov++; |
| 517 | return_hdr: |
| 518 | ctx->line = sol; |
| 519 | ctx->idx = cur_idx; |
| 520 | ctx->val = sov - sol; |
| 521 | |
| 522 | eol = find_hdr_value_end(sov, eol); |
| 523 | ctx->vlen = eol - sov; |
| 524 | return 1; |
| 525 | } |
| 526 | next_hdr: |
| 527 | sol = eol + idx->v[cur_idx].cr + 1; |
| 528 | cur_idx = idx->v[cur_idx].next; |
| 529 | } |
| 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | int http_find_header(const char *name, |
| 534 | const char *sol, struct hdr_idx *idx, |
| 535 | struct hdr_ctx *ctx) |
| 536 | { |
| 537 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 538 | } |
| 539 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 540 | /* This function shuts down the buffers on the server side, and sets indicators |
| 541 | * accordingly. The server's fd is supposed to already be closed. Note that if |
| 542 | * <status> is 0, or if the message pointer is NULL, then no message is returned. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 543 | */ |
| 544 | void srv_close_with_err(struct session *t, int err, int finst, |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 545 | int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 546 | { |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 547 | buffer_write_ena(t->rep); |
Willy Tarreau | ba392ce | 2008-08-16 21:13:23 +0200 | [diff] [blame] | 548 | buffer_shutw(t->req); |
| 549 | buffer_shutr(t->rep); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 550 | if (status > 0 && msg) { |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 551 | t->txn.status = status; |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 552 | if (t->fe->mode == PR_MODE_HTTP) |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 553 | client_return(t, msg); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 554 | } |
| 555 | if (!(t->flags & SN_ERR_MASK)) |
| 556 | t->flags |= err; |
| 557 | if (!(t->flags & SN_FINST_MASK)) |
| 558 | t->flags |= finst; |
| 559 | } |
| 560 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 561 | /* This function returns the appropriate error location for the given session |
| 562 | * and message. |
| 563 | */ |
| 564 | |
| 565 | struct chunk *error_message(struct session *s, int msgnum) |
| 566 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 567 | if (s->be->errmsg[msgnum].str) |
| 568 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 569 | else if (s->fe->errmsg[msgnum].str) |
| 570 | return &s->fe->errmsg[msgnum]; |
| 571 | else |
| 572 | return &http_err_chunks[msgnum]; |
| 573 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 574 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 575 | /* |
| 576 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 577 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 578 | */ |
| 579 | static http_meth_t find_http_meth(const char *str, const int len) |
| 580 | { |
| 581 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 582 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 583 | |
| 584 | m = ((unsigned)*str - 'A'); |
| 585 | |
| 586 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 587 | for (h = http_methods[m]; h->len > 0; h++) { |
| 588 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 589 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 590 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 591 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 592 | }; |
| 593 | return HTTP_METH_OTHER; |
| 594 | } |
| 595 | return HTTP_METH_NONE; |
| 596 | |
| 597 | } |
| 598 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 599 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 600 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 601 | * It is returned otherwise. |
| 602 | */ |
| 603 | static char * |
| 604 | http_get_path(struct http_txn *txn) |
| 605 | { |
| 606 | char *ptr, *end; |
| 607 | |
| 608 | ptr = txn->req.sol + txn->req.sl.rq.u; |
| 609 | end = ptr + txn->req.sl.rq.u_l; |
| 610 | |
| 611 | if (ptr >= end) |
| 612 | return NULL; |
| 613 | |
| 614 | /* RFC2616, par. 5.1.2 : |
| 615 | * Request-URI = "*" | absuri | abspath | authority |
| 616 | */ |
| 617 | |
| 618 | if (*ptr == '*') |
| 619 | return NULL; |
| 620 | |
| 621 | if (isalpha((unsigned char)*ptr)) { |
| 622 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 623 | ptr++; |
| 624 | while (ptr < end && |
| 625 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 626 | ptr++; |
| 627 | /* skip '://' */ |
| 628 | if (ptr == end || *ptr++ != ':') |
| 629 | return NULL; |
| 630 | if (ptr == end || *ptr++ != '/') |
| 631 | return NULL; |
| 632 | if (ptr == end || *ptr++ != '/') |
| 633 | return NULL; |
| 634 | } |
| 635 | /* skip [user[:passwd]@]host[:[port]] */ |
| 636 | |
| 637 | while (ptr < end && *ptr != '/') |
| 638 | ptr++; |
| 639 | |
| 640 | if (ptr == end) |
| 641 | return NULL; |
| 642 | |
| 643 | /* OK, we got the '/' ! */ |
| 644 | return ptr; |
| 645 | } |
| 646 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 647 | /* Processes the client, server, request and response jobs of a session task, |
| 648 | * then puts it back to the wait queue in a clean state, or cleans up its |
| 649 | * resources if it must be deleted. Returns in <next> the date the task wants |
| 650 | * to be woken up, or TICK_ETERNITY. In order not to call all functions for |
| 651 | * nothing too many times, the request and response buffers flags are monitored |
| 652 | * and each function is called only if at least another function has changed at |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 653 | * least one flag it is interested in. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 654 | */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 655 | void process_session(struct task *t, int *next) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 656 | { |
| 657 | struct session *s = t->context; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 658 | int resync; |
| 659 | unsigned int rqf_cli, rpf_cli; |
| 660 | unsigned int rqf_srv, rpf_srv; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 661 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 662 | /* Check timeouts only during data phase for now */ |
| 663 | if (unlikely(t->state & TASK_WOKEN_TIMER)) { |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame] | 664 | if (s->rep->cons->state == SI_ST_EST) { |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 665 | stream_sock_data_check_timeouts(s->rep->cons->fd); |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame] | 666 | if (tick_is_expired(s->rep->analyse_exp, now_ms)) |
| 667 | s->rep->flags |= BF_ANA_TIMEOUT; |
| 668 | } |
| 669 | if (s->req->cons->state == SI_ST_EST) { |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 670 | stream_sock_data_check_timeouts(s->req->cons->fd); |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame] | 671 | if (tick_is_expired(s->req->analyse_exp, now_ms)) |
| 672 | s->req->flags |= BF_ANA_TIMEOUT; |
| 673 | } |
Willy Tarreau | 3537467 | 2008-09-03 18:11:02 +0200 | [diff] [blame] | 674 | /* Note that we don't check nor indicate if we wake up because |
| 675 | * of a timeout on a stream interface. |
| 676 | */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 677 | } |
| 678 | |
Willy Tarreau | 48adac5 | 2008-08-30 04:58:38 +0200 | [diff] [blame] | 679 | /* Check if we need to close the write side. This can only happen |
| 680 | * when either SHUTR or EMPTY appears, because WRITE_ENA cannot appear |
| 681 | * from low level, and neither HIJACK nor SHUTW can disappear from low |
| 682 | * level. |
| 683 | */ |
| 684 | if (unlikely((s->req->flags & (BF_SHUTW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) == (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR))) { |
| 685 | buffer_shutw(s->req); |
| 686 | s->req->cons->shutw(s->req->cons); |
| 687 | } |
| 688 | |
| 689 | if (unlikely((s->rep->flags & (BF_SHUTW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) == (BF_EMPTY|BF_WRITE_ENA|BF_SHUTR))) { |
| 690 | buffer_shutw(s->rep); |
| 691 | s->rep->cons->shutw(s->rep->cons); |
| 692 | } |
| 693 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 694 | /* When a server-side connection is released, we have to |
| 695 | * count it and check for pending connections on this server. |
| 696 | */ |
| 697 | if (unlikely(s->req->cons->state == SI_ST_CLO && |
| 698 | s->req->cons->prev_state == SI_ST_EST)) { |
| 699 | /* Count server-side errors (but not timeouts). */ |
| 700 | if (s->req->flags & BF_WRITE_ERROR) { |
| 701 | s->be->failed_resp++; |
| 702 | if (s->srv) |
| 703 | s->srv->failed_resp++; |
| 704 | } |
| 705 | |
| 706 | if (s->srv) { |
| 707 | s->srv->cur_sess--; |
| 708 | sess_change_server(s, NULL); |
| 709 | if (may_dequeue_tasks(s->srv, s->be)) |
| 710 | process_srv_queue(s->srv); |
| 711 | } |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 712 | } |
| 713 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 714 | /* Dirty trick: force one first pass everywhere */ |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame] | 715 | rqf_cli = rqf_srv = ~s->req->flags; |
| 716 | rpf_cli = rpf_srv = ~s->rep->flags; |
Willy Tarreau | 507385d | 2008-08-17 13:04:25 +0200 | [diff] [blame] | 717 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 718 | /* well, the ST_CONN state is already handled properly */ |
| 719 | if (s->req->prod->state == SI_ST_EST) { |
| 720 | rqf_cli = s->req->flags; |
| 721 | rpf_cli = s->rep->flags; |
| 722 | } |
| 723 | |
| 724 | if (s->req->cons->state == SI_ST_EST) { |
| 725 | rqf_srv = s->req->flags; |
| 726 | rpf_srv = s->rep->flags; |
| 727 | } |
| 728 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 729 | do { |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 730 | DPRINTF(stderr,"[%u] %s: task=%p rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rql=%d rpl=%d cs=%d ss=%d\n", |
| 731 | now_ms, __FUNCTION__, |
| 732 | t, |
| 733 | s->req, s->rep, |
| 734 | s->req->rex, s->rep->wex, |
| 735 | s->req->flags, s->rep->flags, |
| 736 | s->req->l, s->rep->l, s->rep->cons->state, s->req->cons->state); |
| 737 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 738 | resync = 0; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 739 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 740 | /* Maybe resync client FD state */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 741 | if (s->rep->cons->state != SI_ST_CLO) { |
| 742 | if (((rqf_cli ^ s->req->flags) & BF_MASK_INTERFACE_I) || |
| 743 | ((rpf_cli ^ s->rep->flags) & BF_MASK_INTERFACE_O)) { |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 744 | stream_sock_data_update(s->rep->cons->fd); |
| 745 | rqf_cli = s->req->flags; |
| 746 | rpf_cli = s->rep->flags; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 747 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 748 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 749 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 750 | /* Maybe resync server FD state */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 751 | if (s->req->cons->state != SI_ST_CLO) { |
| 752 | if (((rpf_srv ^ s->rep->flags) & BF_MASK_INTERFACE_I) || |
| 753 | ((rqf_srv ^ s->req->flags) & BF_MASK_INTERFACE_O)) { |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame] | 754 | if (s->req->cons->state < SI_ST_EST && s->req->flags & BF_WRITE_ENA) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 755 | process_srv_conn(s); |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame] | 756 | resync = 1; /* we might have to resync */ |
| 757 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 758 | |
| 759 | if (s->req->cons->state == SI_ST_EST) { |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 760 | if ((s->req->flags & (BF_SHUTW|BF_EMPTY|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA) && |
Willy Tarreau | 376580a | 2008-08-27 18:52:22 +0200 | [diff] [blame] | 761 | s->be->options & PR_O_FORCE_CLO && |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 762 | s->rep->flags & BF_READ_ACTIVITY) { |
Willy Tarreau | 376580a | 2008-08-27 18:52:22 +0200 | [diff] [blame] | 763 | /* We want to force the connection to the server to close, |
| 764 | * and the server has begun to respond. That's the right |
| 765 | * time. |
| 766 | */ |
| 767 | buffer_shutw_now(s->req); |
| 768 | } |
| 769 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 770 | stream_sock_data_update(s->req->cons->fd); |
Willy Tarreau | 376580a | 2008-08-27 18:52:22 +0200 | [diff] [blame] | 771 | |
| 772 | /* When a server-side connection is released, we have to |
| 773 | * count it and check for pending connections on this server. |
| 774 | */ |
| 775 | if (s->req->cons->state == SI_ST_CLO) { |
| 776 | if (s->srv) { |
| 777 | s->srv->cur_sess--; |
| 778 | sess_change_server(s, NULL); |
| 779 | if (may_dequeue_tasks(s->srv, s->be)) |
| 780 | process_srv_queue(s->srv); |
| 781 | } |
| 782 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 783 | } |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 784 | rqf_srv = s->req->flags; |
| 785 | rpf_srv = s->rep->flags; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 786 | } |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | /* we may have to resync because of pending connections */ |
| 790 | if (resync) |
| 791 | continue; |
| 792 | |
| 793 | /**** Process layer 7 below ****/ |
| 794 | |
| 795 | /* Analyse request */ |
| 796 | if (s->req->flags & BF_MASK_ANALYSER) { |
| 797 | unsigned int flags = s->req->flags; |
| 798 | |
| 799 | if (s->req->prod->state >= SI_ST_EST) { |
| 800 | /* it's up to the analysers to reset write_ena */ |
| 801 | buffer_write_ena(s->req); |
| 802 | if (s->req->analysers) |
| 803 | process_request(s); |
| 804 | } |
| 805 | s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 806 | flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 807 | if (s->req->flags != flags) |
| 808 | resync = 1; |
| 809 | } |
| 810 | |
| 811 | /* Analyse response */ |
| 812 | if (unlikely(s->rep->flags & BF_HIJACK)) { |
| 813 | /* In inject mode, we wake up everytime something has |
| 814 | * happened on the write side of the buffer. |
| 815 | */ |
| 816 | unsigned int flags = s->rep->flags; |
| 817 | |
| 818 | if ((s->rep->flags & (BF_WRITE_PARTIAL|BF_WRITE_ERROR|BF_SHUTW)) && |
| 819 | !(s->rep->flags & BF_FULL)) { |
| 820 | produce_content(s); |
| 821 | } |
| 822 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 823 | flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 824 | if (s->rep->flags != flags) |
| 825 | resync = 1; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 826 | } |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame] | 827 | else if (s->rep->flags & BF_MASK_ANALYSER) { |
| 828 | unsigned int flags = s->rep->flags; |
| 829 | |
| 830 | if (s->rep->prod->state >= SI_ST_EST) { |
| 831 | /* it's up to the analysers to reset write_ena */ |
| 832 | buffer_write_ena(s->rep); |
| 833 | if (s->rep->analysers) |
| 834 | process_response(s); |
| 835 | } |
| 836 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 837 | flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 838 | if (s->rep->flags != flags) |
| 839 | resync = 1; |
| 840 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 841 | |
Willy Tarreau | 4ffd51a | 2008-08-30 13:36:43 +0200 | [diff] [blame] | 842 | /* For the moment, we need to clean the client and server flags that |
| 843 | * have vanished. This is just a temporary measure though. |
| 844 | */ |
| 845 | rqf_cli &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 846 | rqf_srv &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 847 | rpf_cli &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 848 | rpf_srv &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 849 | } while (resync); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 850 | |
Willy Tarreau | a37095b | 2008-09-03 11:37:47 +0200 | [diff] [blame] | 851 | /* This is needed only when debugging is enabled, to indicate |
| 852 | * client-side or server-side close. Please note that in the unlikely |
| 853 | * event where both sides would close at once, the sequence is reported |
| 854 | * on the server side first. |
| 855 | */ |
| 856 | if (unlikely((global.mode & MODE_DEBUG) && |
| 857 | (!(global.mode & MODE_QUIET) || |
| 858 | (global.mode & MODE_VERBOSE)))) { |
| 859 | int len; |
| 860 | |
| 861 | if (s->si[1].state == SI_ST_CLO && |
| 862 | s->si[1].prev_state == SI_ST_EST) { |
| 863 | len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n", |
| 864 | s->uniq_id, s->be->id, |
| 865 | (unsigned short)s->si[0].fd, |
| 866 | (unsigned short)s->si[1].fd); |
| 867 | write(1, trash, len); |
| 868 | } |
| 869 | |
| 870 | if (s->si[0].state == SI_ST_CLO && |
| 871 | s->si[0].prev_state == SI_ST_EST) { |
| 872 | len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", |
| 873 | s->uniq_id, s->be->id, |
| 874 | (unsigned short)s->si[0].fd, |
| 875 | (unsigned short)s->si[1].fd); |
| 876 | write(1, trash, len); |
| 877 | } |
| 878 | } |
| 879 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 880 | if (likely((s->rep->cons->state != SI_ST_CLO) || |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 881 | (s->req->cons->state != SI_ST_CLO && s->req->cons->state != SI_ST_INI))) { |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 882 | |
| 883 | if ((s->fe->options & PR_O_CONTSTATS) && (s->flags & SN_BE_ASSIGNED)) |
| 884 | session_process_counters(s); |
| 885 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 886 | if (s->rep->cons->state == SI_ST_EST) |
| 887 | stream_sock_data_finish(s->rep->cons->fd); |
| 888 | |
| 889 | if (s->req->cons->state == SI_ST_EST) |
| 890 | stream_sock_data_finish(s->req->cons->fd); |
| 891 | |
Willy Tarreau | 9a2d154 | 2008-08-30 12:31:07 +0200 | [diff] [blame] | 892 | s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
| 893 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE & BF_CLEAR_TIMEOUT; |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 894 | s->si[0].prev_state = s->si[0].state; |
| 895 | s->si[1].prev_state = s->si[1].state; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 896 | |
Willy Tarreau | 7f875f6 | 2008-08-11 17:35:01 +0200 | [diff] [blame] | 897 | /* Trick: if a request is being waiting for the server to respond, |
| 898 | * and if we know the server can timeout, we don't want the timeout |
| 899 | * to expire on the client side first, but we're still interested |
| 900 | * in passing data from the client to the server (eg: POST). Thus, |
| 901 | * we can cancel the client's request timeout if the server's |
| 902 | * request timeout is set and the server has not yet sent a response. |
| 903 | */ |
| 904 | |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 905 | if ((s->rep->flags & (BF_WRITE_ENA|BF_SHUTR)) == 0 && |
Willy Tarreau | 26ed74d | 2008-08-17 12:11:14 +0200 | [diff] [blame] | 906 | (tick_isset(s->req->wex) || tick_isset(s->rep->rex))) |
Willy Tarreau | 7f875f6 | 2008-08-11 17:35:01 +0200 | [diff] [blame] | 907 | s->req->rex = TICK_ETERNITY; |
| 908 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 909 | t->expire = tick_first(tick_first(s->req->rex, s->req->wex), |
| 910 | tick_first(s->rep->rex, s->rep->wex)); |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 911 | if (s->req->analysers) |
| 912 | t->expire = tick_first(t->expire, s->req->analyse_exp); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 913 | |
Willy Tarreau | 3537467 | 2008-09-03 18:11:02 +0200 | [diff] [blame] | 914 | if (s->si[0].exp) |
| 915 | t->expire = tick_first(t->expire, s->si[0].exp); |
| 916 | |
| 917 | if (s->si[1].exp) |
| 918 | t->expire = tick_first(t->expire, s->si[1].exp); |
| 919 | |
Willy Tarreau | cb65125 | 2008-08-29 13:57:30 +0200 | [diff] [blame] | 920 | #ifdef DEBUG_FULL |
| 921 | fprintf(stderr, "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u rep->rex=%u rep->wex=%u\n", |
| 922 | now_ms, t->expire, s->req->rex, s->req->wex, s->req->analyse_exp, s->rep->rex, s->rep->wex); |
| 923 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 924 | /* restore t to its place in the task list */ |
| 925 | task_queue(t); |
| 926 | |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 927 | #ifdef DEBUG_DEV |
| 928 | /* this may only happen when no timeout is set or in case of an FSM bug */ |
| 929 | if (!t->expire) |
| 930 | ABORT_NOW(); |
| 931 | #endif |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 932 | *next = t->expire; |
| 933 | return; /* nothing more to do */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 934 | } |
| 935 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 936 | s->fe->feconn--; |
| 937 | if (s->flags & SN_BE_ASSIGNED) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 938 | s->be->beconn--; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 939 | actconn--; |
| 940 | |
Willy Tarreau | f41d4b1 | 2007-04-28 23:26:14 +0200 | [diff] [blame] | 941 | if (unlikely((global.mode & MODE_DEBUG) && |
| 942 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 943 | int len; |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 944 | len = sprintf(trash, "%08x:%s.closed[%04x:%04x] (term_trace=0x%08x)\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 945 | s->uniq_id, s->be->id, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 946 | (unsigned short)s->req->prod->fd, (unsigned short)s->req->cons->fd, |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 947 | s->term_trace); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 948 | write(1, trash, len); |
| 949 | } |
| 950 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 951 | 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] | 952 | session_process_counters(s); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 953 | |
| 954 | /* let's do a final log if we need it */ |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 955 | if (s->logs.logwait && |
| 956 | !(s->flags & SN_MONITOR) && |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 957 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 958 | if (s->fe->to_log & LW_REQ) |
| 959 | http_sess_log(s); |
| 960 | else |
| 961 | tcp_sess_log(s); |
| 962 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 963 | |
| 964 | /* the task MUST not be in the run queue anymore */ |
| 965 | task_delete(t); |
| 966 | session_free(s); |
| 967 | task_free(t); |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 968 | *next = TICK_ETERNITY; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 972 | extern const char sess_term_cond[8]; |
| 973 | extern const char sess_fin_state[8]; |
| 974 | extern const char *monthname[12]; |
| 975 | const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */ |
| 976 | const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown, |
| 977 | Set-cookie seen and left unchanged (passive), Set-cookie Deleted, |
| 978 | unknown, Set-cookie Rewritten */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 979 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 980 | struct pool_head *pool2_capture; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 981 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 982 | /* |
| 983 | * send a log for the session when we have enough info about it. |
| 984 | * Will not log if the frontend has no log defined. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 985 | */ |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 986 | static void http_sess_log(struct session *s) |
| 987 | { |
| 988 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 989 | struct proxy *fe = s->fe; |
| 990 | struct proxy *be = s->be; |
| 991 | struct proxy *prx_log; |
| 992 | struct http_txn *txn = &s->txn; |
| 993 | int tolog; |
| 994 | char *uri, *h; |
| 995 | char *svid; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 996 | struct tm tm; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 997 | static char tmpline[MAX_SYSLOG_LEN]; |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 998 | int t_request; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 999 | int hdr; |
| 1000 | |
| 1001 | if (fe->logfac1 < 0 && fe->logfac2 < 0) |
| 1002 | return; |
| 1003 | prx_log = fe; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1004 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1005 | if (s->cli_addr.ss_family == AF_INET) |
| 1006 | inet_ntop(AF_INET, |
| 1007 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 1008 | pn, sizeof(pn)); |
| 1009 | else |
| 1010 | inet_ntop(AF_INET6, |
| 1011 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 1012 | pn, sizeof(pn)); |
| 1013 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 1014 | get_localtime(s->logs.accept_date.tv_sec, &tm); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1015 | |
| 1016 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 1017 | tolog = fe->to_log; |
| 1018 | |
| 1019 | h = tmpline; |
| 1020 | if (fe->to_log & LW_REQHDR && |
| 1021 | txn->req.cap && |
| 1022 | (h < tmpline + sizeof(tmpline) - 10)) { |
| 1023 | *(h++) = ' '; |
| 1024 | *(h++) = '{'; |
| 1025 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 1026 | if (hdr) |
| 1027 | *(h++) = '|'; |
| 1028 | if (txn->req.cap[hdr] != NULL) |
| 1029 | h = encode_string(h, tmpline + sizeof(tmpline) - 7, |
| 1030 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 1031 | } |
| 1032 | *(h++) = '}'; |
| 1033 | } |
| 1034 | |
| 1035 | if (fe->to_log & LW_RSPHDR && |
| 1036 | txn->rsp.cap && |
| 1037 | (h < tmpline + sizeof(tmpline) - 7)) { |
| 1038 | *(h++) = ' '; |
| 1039 | *(h++) = '{'; |
| 1040 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 1041 | if (hdr) |
| 1042 | *(h++) = '|'; |
| 1043 | if (txn->rsp.cap[hdr] != NULL) |
| 1044 | h = encode_string(h, tmpline + sizeof(tmpline) - 4, |
| 1045 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 1046 | } |
| 1047 | *(h++) = '}'; |
| 1048 | } |
| 1049 | |
| 1050 | if (h < tmpline + sizeof(tmpline) - 4) { |
| 1051 | *(h++) = ' '; |
| 1052 | *(h++) = '"'; |
| 1053 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 1054 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 1055 | '#', url_encode_map, uri); |
| 1056 | *(h++) = '"'; |
| 1057 | } |
| 1058 | *h = '\0'; |
| 1059 | |
| 1060 | svid = (tolog & LW_SVID) ? |
| 1061 | (s->data_source != DATA_SRC_STATS) ? |
| 1062 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 1063 | |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1064 | t_request = -1; |
| 1065 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 1066 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 1067 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1068 | send_log(prx_log, LOG_INFO, |
| 1069 | "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]" |
| 1070 | " %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] | 1071 | " %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] | 1072 | pn, |
| 1073 | (s->cli_addr.ss_family == AF_INET) ? |
| 1074 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 1075 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1076 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 1077 | 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] | 1078 | fe->id, be->id, svid, |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1079 | t_request, |
| 1080 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1081 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 1082 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 1083 | (tolog & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 1084 | txn->status, |
Willy Tarreau | 8b3977f | 2008-01-18 11:16:32 +0100 | [diff] [blame] | 1085 | (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1086 | txn->cli_cookie ? txn->cli_cookie : "-", |
| 1087 | txn->srv_cookie ? txn->srv_cookie : "-", |
| 1088 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 1089 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 1090 | (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 1091 | (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-', |
| 1092 | 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] | 1093 | (s->flags & SN_REDISP)?"+":"", |
| 1094 | (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] | 1095 | s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline); |
| 1096 | |
| 1097 | s->logs.logwait = 0; |
| 1098 | } |
| 1099 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1100 | |
| 1101 | /* |
| 1102 | * Capture headers from message starting at <som> according to header list |
| 1103 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 1104 | */ |
| 1105 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1106 | char **cap, struct cap_hdr *cap_hdr) |
| 1107 | { |
| 1108 | char *eol, *sol, *col, *sov; |
| 1109 | int cur_idx; |
| 1110 | struct cap_hdr *h; |
| 1111 | int len; |
| 1112 | |
| 1113 | sol = som + hdr_idx_first_pos(idx); |
| 1114 | cur_idx = hdr_idx_first_idx(idx); |
| 1115 | |
| 1116 | while (cur_idx) { |
| 1117 | eol = sol + idx->v[cur_idx].len; |
| 1118 | |
| 1119 | col = sol; |
| 1120 | while (col < eol && *col != ':') |
| 1121 | col++; |
| 1122 | |
| 1123 | sov = col + 1; |
| 1124 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 1125 | sov++; |
| 1126 | |
| 1127 | for (h = cap_hdr; h; h = h->next) { |
| 1128 | if ((h->namelen == col - sol) && |
| 1129 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1130 | if (cap[h->index] == NULL) |
| 1131 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1132 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1133 | |
| 1134 | if (cap[h->index] == NULL) { |
| 1135 | Alert("HTTP capture : out of memory.\n"); |
| 1136 | continue; |
| 1137 | } |
| 1138 | |
| 1139 | len = eol - sov; |
| 1140 | if (len > h->len) |
| 1141 | len = h->len; |
| 1142 | |
| 1143 | memcpy(cap[h->index], sov, len); |
| 1144 | cap[h->index][len]=0; |
| 1145 | } |
| 1146 | } |
| 1147 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1148 | cur_idx = idx->v[cur_idx].next; |
| 1149 | } |
| 1150 | } |
| 1151 | |
| 1152 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1153 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1154 | */ |
| 1155 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1156 | |
| 1157 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1158 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1159 | */ |
| 1160 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1161 | ptr++; \ |
| 1162 | if (likely(ptr < end)) \ |
| 1163 | goto good; \ |
| 1164 | else { \ |
| 1165 | state = (st); \ |
| 1166 | goto http_msg_ood; \ |
| 1167 | } \ |
| 1168 | } while (0) |
| 1169 | |
| 1170 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1171 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1172 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1173 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1174 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1175 | * will give undefined results. |
| 1176 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1177 | * and that msg->sol points to the beginning of the response. |
| 1178 | * If a complete line is found (which implies that at least one CR or LF is |
| 1179 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1180 | * returned indicating an incomplete line (which does not mean that parts have |
| 1181 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1182 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1183 | * upon next call. |
| 1184 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1185 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1186 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1187 | * 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] | 1188 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1189 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1190 | const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, |
| 1191 | unsigned int state, const char *ptr, const char *end, |
| 1192 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1193 | { |
| 1194 | __label__ |
| 1195 | http_msg_rpver, |
| 1196 | http_msg_rpver_sp, |
| 1197 | http_msg_rpcode, |
| 1198 | http_msg_rpcode_sp, |
| 1199 | http_msg_rpreason, |
| 1200 | http_msg_rpline_eol, |
| 1201 | http_msg_ood, /* out of data */ |
| 1202 | http_msg_invalid; |
| 1203 | |
| 1204 | switch (state) { |
| 1205 | http_msg_rpver: |
| 1206 | case HTTP_MSG_RPVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1207 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1208 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1209 | |
| 1210 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1211 | msg->sl.st.v_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1212 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1213 | } |
| 1214 | goto http_msg_invalid; |
| 1215 | |
| 1216 | http_msg_rpver_sp: |
| 1217 | case HTTP_MSG_RPVER_SP: |
| 1218 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1219 | msg->sl.st.c = ptr - msg_buf; |
| 1220 | goto http_msg_rpcode; |
| 1221 | } |
| 1222 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1223 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1224 | /* so it's a CR/LF, this is invalid */ |
| 1225 | goto http_msg_invalid; |
| 1226 | |
| 1227 | http_msg_rpcode: |
| 1228 | case HTTP_MSG_RPCODE: |
| 1229 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1230 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1231 | |
| 1232 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1233 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 1234 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1235 | } |
| 1236 | |
| 1237 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1238 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 1239 | http_msg_rsp_reason: |
| 1240 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
| 1241 | msg->sl.st.r = ptr - msg_buf; |
| 1242 | msg->sl.st.r_l = 0; |
| 1243 | goto http_msg_rpline_eol; |
| 1244 | |
| 1245 | http_msg_rpcode_sp: |
| 1246 | case HTTP_MSG_RPCODE_SP: |
| 1247 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1248 | msg->sl.st.r = ptr - msg_buf; |
| 1249 | goto http_msg_rpreason; |
| 1250 | } |
| 1251 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1252 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1253 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1254 | goto http_msg_rsp_reason; |
| 1255 | |
| 1256 | http_msg_rpreason: |
| 1257 | case HTTP_MSG_RPREASON: |
| 1258 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1259 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
| 1260 | msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r; |
| 1261 | http_msg_rpline_eol: |
| 1262 | /* We have seen the end of line. Note that we do not |
| 1263 | * necessarily have the \n yet, but at least we know that we |
| 1264 | * have EITHER \r OR \n, otherwise the response would not be |
| 1265 | * complete. We can then record the response length and return |
| 1266 | * to the caller which will be able to register it. |
| 1267 | */ |
| 1268 | msg->sl.st.l = ptr - msg->sol; |
| 1269 | return ptr; |
| 1270 | |
| 1271 | #ifdef DEBUG_FULL |
| 1272 | default: |
| 1273 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1274 | exit(1); |
| 1275 | #endif |
| 1276 | } |
| 1277 | |
| 1278 | http_msg_ood: |
| 1279 | /* out of data */ |
| 1280 | if (ret_state) |
| 1281 | *ret_state = state; |
| 1282 | if (ret_ptr) |
| 1283 | *ret_ptr = (char *)ptr; |
| 1284 | return NULL; |
| 1285 | |
| 1286 | http_msg_invalid: |
| 1287 | /* invalid message */ |
| 1288 | if (ret_state) |
| 1289 | *ret_state = HTTP_MSG_ERROR; |
| 1290 | return NULL; |
| 1291 | } |
| 1292 | |
| 1293 | |
| 1294 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1295 | * This function parses a request line between <ptr> and <end>, starting with |
| 1296 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1297 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1298 | * will give undefined results. |
| 1299 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1300 | * and that msg->sol points to the beginning of the request. |
| 1301 | * If a complete line is found (which implies that at least one CR or LF is |
| 1302 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1303 | * returned indicating an incomplete line (which does not mean that parts have |
| 1304 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1305 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1306 | * upon next call. |
| 1307 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1308 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1309 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1310 | * 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] | 1311 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1312 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1313 | const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, |
| 1314 | unsigned int state, const char *ptr, const char *end, |
| 1315 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1316 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1317 | __label__ |
| 1318 | http_msg_rqmeth, |
| 1319 | http_msg_rqmeth_sp, |
| 1320 | http_msg_rquri, |
| 1321 | http_msg_rquri_sp, |
| 1322 | http_msg_rqver, |
| 1323 | http_msg_rqline_eol, |
| 1324 | http_msg_ood, /* out of data */ |
| 1325 | http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1326 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1327 | switch (state) { |
| 1328 | http_msg_rqmeth: |
| 1329 | case HTTP_MSG_RQMETH: |
| 1330 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1331 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1332 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1333 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1334 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1335 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1336 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1337 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1338 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1339 | /* HTTP 0.9 request */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1340 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1341 | http_msg_req09_uri: |
| 1342 | msg->sl.rq.u = ptr - msg_buf; |
| 1343 | http_msg_req09_uri_e: |
| 1344 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1345 | http_msg_req09_ver: |
| 1346 | msg->sl.rq.v = ptr - msg_buf; |
| 1347 | msg->sl.rq.v_l = 0; |
| 1348 | goto http_msg_rqline_eol; |
| 1349 | } |
| 1350 | goto http_msg_invalid; |
| 1351 | |
| 1352 | http_msg_rqmeth_sp: |
| 1353 | case HTTP_MSG_RQMETH_SP: |
| 1354 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1355 | msg->sl.rq.u = ptr - msg_buf; |
| 1356 | goto http_msg_rquri; |
| 1357 | } |
| 1358 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1359 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1360 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1361 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1362 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1363 | http_msg_rquri: |
| 1364 | case HTTP_MSG_RQURI: |
| 1365 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1366 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1367 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1368 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1369 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1370 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1371 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1372 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1373 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1374 | goto http_msg_req09_uri_e; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1375 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1376 | http_msg_rquri_sp: |
| 1377 | case HTTP_MSG_RQURI_SP: |
| 1378 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1379 | msg->sl.rq.v = ptr - msg_buf; |
| 1380 | goto http_msg_rqver; |
| 1381 | } |
| 1382 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1383 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1384 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1385 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1386 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1387 | http_msg_rqver: |
| 1388 | case HTTP_MSG_RQVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1389 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1390 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1391 | |
| 1392 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1393 | msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v; |
| 1394 | http_msg_rqline_eol: |
| 1395 | /* We have seen the end of line. Note that we do not |
| 1396 | * necessarily have the \n yet, but at least we know that we |
| 1397 | * have EITHER \r OR \n, otherwise the request would not be |
| 1398 | * complete. We can then record the request length and return |
| 1399 | * to the caller which will be able to register it. |
| 1400 | */ |
| 1401 | msg->sl.rq.l = ptr - msg->sol; |
| 1402 | return ptr; |
| 1403 | } |
| 1404 | |
| 1405 | /* neither an HTTP_VER token nor a CRLF */ |
| 1406 | goto http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1407 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1408 | #ifdef DEBUG_FULL |
| 1409 | default: |
| 1410 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1411 | exit(1); |
| 1412 | #endif |
| 1413 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1414 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1415 | http_msg_ood: |
| 1416 | /* out of data */ |
| 1417 | if (ret_state) |
| 1418 | *ret_state = state; |
| 1419 | if (ret_ptr) |
| 1420 | *ret_ptr = (char *)ptr; |
| 1421 | return NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1422 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1423 | http_msg_invalid: |
| 1424 | /* invalid message */ |
| 1425 | if (ret_state) |
| 1426 | *ret_state = HTTP_MSG_ERROR; |
| 1427 | return NULL; |
| 1428 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1429 | |
| 1430 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1431 | /* |
| 1432 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1433 | * depending on the initial msg->msg_state. It can be preempted everywhere |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1434 | * when data are missing and recalled at the exact same location with no |
| 1435 | * information loss. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1436 | * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other |
| 1437 | * fields. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1438 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1439 | void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx) |
| 1440 | { |
| 1441 | __label__ |
| 1442 | http_msg_rqbefore, |
| 1443 | http_msg_rqbefore_cr, |
| 1444 | http_msg_rqmeth, |
| 1445 | http_msg_rqline_end, |
| 1446 | http_msg_hdr_first, |
| 1447 | http_msg_hdr_name, |
| 1448 | http_msg_hdr_l1_sp, |
| 1449 | http_msg_hdr_l1_lf, |
| 1450 | http_msg_hdr_l1_lws, |
| 1451 | http_msg_hdr_val, |
| 1452 | http_msg_hdr_l2_lf, |
| 1453 | http_msg_hdr_l2_lws, |
| 1454 | http_msg_complete_header, |
| 1455 | http_msg_last_lf, |
| 1456 | http_msg_ood, /* out of data */ |
| 1457 | http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1458 | |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1459 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1460 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1461 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1462 | state = msg->msg_state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1463 | ptr = buf->lr; |
| 1464 | end = buf->r; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1465 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1466 | if (unlikely(ptr >= end)) |
| 1467 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1468 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1469 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1470 | /* |
| 1471 | * First, states that are specific to the response only. |
| 1472 | * We check them first so that request and headers are |
| 1473 | * closer to each other (accessed more often). |
| 1474 | */ |
| 1475 | http_msg_rpbefore: |
| 1476 | case HTTP_MSG_RPBEFORE: |
| 1477 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1478 | if (likely(ptr == buf->data)) { |
| 1479 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1480 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1481 | } else { |
| 1482 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1483 | /* only skip empty leading lines, don't remove them */ |
| 1484 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1485 | msg->som = ptr - buf->data; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1486 | #else |
| 1487 | /* Remove empty leading lines, as recommended by |
| 1488 | * RFC2616. This takes a lot of time because we |
| 1489 | * must move all the buffer backwards, but this |
| 1490 | * is rarely needed. The method above will be |
| 1491 | * cleaner when we'll be able to start sending |
| 1492 | * the request from any place in the buffer. |
| 1493 | */ |
| 1494 | buf->lr = ptr; |
| 1495 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1496 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1497 | msg->sol = buf->data; |
| 1498 | ptr = buf->data; |
| 1499 | end = buf->r; |
| 1500 | #endif |
| 1501 | } |
| 1502 | hdr_idx_init(idx); |
| 1503 | state = HTTP_MSG_RPVER; |
| 1504 | goto http_msg_rpver; |
| 1505 | } |
| 1506 | |
| 1507 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1508 | goto http_msg_invalid; |
| 1509 | |
| 1510 | if (unlikely(*ptr == '\n')) |
| 1511 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1512 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1513 | /* stop here */ |
| 1514 | |
| 1515 | http_msg_rpbefore_cr: |
| 1516 | case HTTP_MSG_RPBEFORE_CR: |
| 1517 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1518 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1519 | /* stop here */ |
| 1520 | |
| 1521 | http_msg_rpver: |
| 1522 | case HTTP_MSG_RPVER: |
| 1523 | case HTTP_MSG_RPVER_SP: |
| 1524 | case HTTP_MSG_RPCODE: |
| 1525 | case HTTP_MSG_RPCODE_SP: |
| 1526 | case HTTP_MSG_RPREASON: |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1527 | ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1528 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1529 | if (unlikely(!ptr)) |
| 1530 | return; |
| 1531 | |
| 1532 | /* we have a full response and we know that we have either a CR |
| 1533 | * or an LF at <ptr>. |
| 1534 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1535 | //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] | 1536 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1537 | |
| 1538 | msg->sol = ptr; |
| 1539 | if (likely(*ptr == '\r')) |
| 1540 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1541 | goto http_msg_rpline_end; |
| 1542 | |
| 1543 | http_msg_rpline_end: |
| 1544 | case HTTP_MSG_RPLINE_END: |
| 1545 | /* msg->sol must point to the first of CR or LF. */ |
| 1546 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1547 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1548 | /* stop here */ |
| 1549 | |
| 1550 | /* |
| 1551 | * Second, states that are specific to the request only |
| 1552 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1553 | http_msg_rqbefore: |
| 1554 | case HTTP_MSG_RQBEFORE: |
| 1555 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1556 | if (likely(ptr == buf->data)) { |
| 1557 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1558 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1559 | } else { |
| 1560 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1561 | /* only skip empty leading lines, don't remove them */ |
| 1562 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1563 | msg->som = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1564 | #else |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1565 | /* Remove empty leading lines, as recommended by |
| 1566 | * RFC2616. This takes a lot of time because we |
| 1567 | * must move all the buffer backwards, but this |
| 1568 | * is rarely needed. The method above will be |
| 1569 | * cleaner when we'll be able to start sending |
| 1570 | * the request from any place in the buffer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1571 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1572 | buf->lr = ptr; |
| 1573 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1574 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1575 | msg->sol = buf->data; |
| 1576 | ptr = buf->data; |
| 1577 | end = buf->r; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1578 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1579 | } |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1580 | /* we will need this when keep-alive will be supported |
| 1581 | hdr_idx_init(idx); |
| 1582 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1583 | state = HTTP_MSG_RQMETH; |
| 1584 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1585 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1586 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1587 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1588 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1589 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1590 | if (unlikely(*ptr == '\n')) |
| 1591 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1592 | 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] | 1593 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1594 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1595 | http_msg_rqbefore_cr: |
| 1596 | case HTTP_MSG_RQBEFORE_CR: |
| 1597 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1598 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1599 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1600 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1601 | http_msg_rqmeth: |
| 1602 | case HTTP_MSG_RQMETH: |
| 1603 | case HTTP_MSG_RQMETH_SP: |
| 1604 | case HTTP_MSG_RQURI: |
| 1605 | case HTTP_MSG_RQURI_SP: |
| 1606 | case HTTP_MSG_RQVER: |
| 1607 | ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1608 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1609 | if (unlikely(!ptr)) |
| 1610 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1611 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1612 | /* we have a full request and we know that we have either a CR |
| 1613 | * or an LF at <ptr>. |
| 1614 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1615 | //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] | 1616 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1617 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1618 | msg->sol = ptr; |
| 1619 | if (likely(*ptr == '\r')) |
| 1620 | 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] | 1621 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1622 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1623 | http_msg_rqline_end: |
| 1624 | case HTTP_MSG_RQLINE_END: |
| 1625 | /* check for HTTP/0.9 request : no version information available. |
| 1626 | * msg->sol must point to the first of CR or LF. |
| 1627 | */ |
| 1628 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1629 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1630 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1631 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1632 | 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] | 1633 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1634 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1635 | /* |
| 1636 | * Common states below |
| 1637 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1638 | http_msg_hdr_first: |
| 1639 | case HTTP_MSG_HDR_FIRST: |
| 1640 | msg->sol = ptr; |
| 1641 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1642 | goto http_msg_hdr_name; |
| 1643 | } |
| 1644 | |
| 1645 | if (likely(*ptr == '\r')) |
| 1646 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1647 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1648 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1649 | http_msg_hdr_name: |
| 1650 | case HTTP_MSG_HDR_NAME: |
| 1651 | /* assumes msg->sol points to the first char */ |
| 1652 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1653 | 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] | 1654 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1655 | if (likely(*ptr == ':')) { |
| 1656 | msg->col = ptr - buf->data; |
| 1657 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| 1658 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1659 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1660 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1661 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1662 | http_msg_hdr_l1_sp: |
| 1663 | case HTTP_MSG_HDR_L1_SP: |
| 1664 | /* assumes msg->sol points to the first char and msg->col to the colon */ |
| 1665 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1666 | 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] | 1667 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1668 | /* header value can be basically anything except CR/LF */ |
| 1669 | msg->sov = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1670 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1671 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1672 | goto http_msg_hdr_val; |
| 1673 | } |
| 1674 | |
| 1675 | if (likely(*ptr == '\r')) |
| 1676 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1677 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1678 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1679 | http_msg_hdr_l1_lf: |
| 1680 | case HTTP_MSG_HDR_L1_LF: |
| 1681 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1682 | 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] | 1683 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1684 | http_msg_hdr_l1_lws: |
| 1685 | case HTTP_MSG_HDR_L1_LWS: |
| 1686 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1687 | /* replace HT,CR,LF with spaces */ |
| 1688 | for (; buf->data+msg->sov < ptr; msg->sov++) |
| 1689 | buf->data[msg->sov] = ' '; |
| 1690 | goto http_msg_hdr_l1_sp; |
| 1691 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1692 | /* we had a header consisting only in spaces ! */ |
| 1693 | msg->eol = buf->data + msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1694 | goto http_msg_complete_header; |
| 1695 | |
| 1696 | http_msg_hdr_val: |
| 1697 | case HTTP_MSG_HDR_VAL: |
| 1698 | /* assumes msg->sol points to the first char, msg->col to the |
| 1699 | * colon, and msg->sov points to the first character of the |
| 1700 | * value. |
| 1701 | */ |
| 1702 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1703 | 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] | 1704 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1705 | msg->eol = ptr; |
| 1706 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1707 | * real header end in case it ends with lots of LWS, but is this |
| 1708 | * really needed ? |
| 1709 | */ |
| 1710 | if (likely(*ptr == '\r')) |
| 1711 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1712 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1713 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1714 | http_msg_hdr_l2_lf: |
| 1715 | case HTTP_MSG_HDR_L2_LF: |
| 1716 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1717 | 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] | 1718 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1719 | http_msg_hdr_l2_lws: |
| 1720 | case HTTP_MSG_HDR_L2_LWS: |
| 1721 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1722 | /* LWS: replace HT,CR,LF with spaces */ |
| 1723 | for (; msg->eol < ptr; msg->eol++) |
| 1724 | *msg->eol = ' '; |
| 1725 | goto http_msg_hdr_val; |
| 1726 | } |
| 1727 | http_msg_complete_header: |
| 1728 | /* |
| 1729 | * It was a new header, so the last one is finished. |
| 1730 | * Assumes msg->sol points to the first char, msg->col to the |
| 1731 | * colon, msg->sov points to the first character of the value |
| 1732 | * and msg->eol to the first CR or LF so we know how the line |
| 1733 | * ends. We insert last header into the index. |
| 1734 | */ |
| 1735 | /* |
| 1736 | fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol); |
| 1737 | write(2, msg->sol, msg->eol-msg->sol); |
| 1738 | fprintf(stderr,"\n"); |
| 1739 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1740 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1741 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r', |
| 1742 | idx, idx->tail) < 0)) |
| 1743 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1744 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1745 | msg->sol = ptr; |
| 1746 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1747 | goto http_msg_hdr_name; |
| 1748 | } |
| 1749 | |
| 1750 | if (likely(*ptr == '\r')) |
| 1751 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1752 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1753 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1754 | http_msg_last_lf: |
| 1755 | case HTTP_MSG_LAST_LF: |
| 1756 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1757 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1758 | ptr++; |
| 1759 | buf->lr = ptr; |
| 1760 | msg->eoh = msg->sol - buf->data; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1761 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1762 | return; |
| 1763 | #ifdef DEBUG_FULL |
| 1764 | default: |
| 1765 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1766 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1767 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1768 | } |
| 1769 | http_msg_ood: |
| 1770 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1771 | msg->msg_state = state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1772 | buf->lr = ptr; |
| 1773 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1774 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1775 | http_msg_invalid: |
| 1776 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1777 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1778 | return; |
| 1779 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1780 | |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1781 | /* This function performs all the processing enabled for the current request. |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1782 | * It normally returns zero, but may return 1 if it absolutely needs to be |
| 1783 | * called again after other functions. It relies on buffers flags, and updates |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1784 | * t->req->analysers. It might make sense to explode it into several other |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 1785 | * functions. Its behaviour is rather simple : |
| 1786 | * - all enabled analysers are called in turn from the lower to the higher |
| 1787 | * bit. |
| 1788 | * - if an analyser does not have enough data, it must return without calling |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 1789 | * other ones. It should also probably reset the BF_WRITE_ENA bit to ensure |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 1790 | * that unprocessed data will not be forwarded. But that probably depends on |
| 1791 | * the protocol. Generally it is not reset in case of errors. |
| 1792 | * - if an analyser has enough data, it just has to pass on to the next |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 1793 | * analyser without touching BF_WRITE_ENA (it is enabled prior to |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 1794 | * analysis). |
| 1795 | * - if an analyser thinks it has no added value anymore staying here, it must |
| 1796 | * reset its bit from the analysers flags in order not to be called anymore. |
| 1797 | * |
| 1798 | * In the future, analysers should be able to indicate that they want to be |
| 1799 | * called after XXX bytes have been received (or transfered), and the min of |
| 1800 | * all's wishes will be used to ring back (unless a special condition occurs). |
| 1801 | * |
| 1802 | * |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1803 | */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1804 | int process_request(struct session *t) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1805 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1806 | struct buffer *req = t->req; |
| 1807 | struct buffer *rep = t->rep; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1808 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 1809 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1810 | now_ms, __FUNCTION__, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 1811 | t, |
| 1812 | req, |
| 1813 | req->rex, req->wex, |
| 1814 | req->flags, |
| 1815 | req->l, |
| 1816 | req->analysers); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1817 | |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 1818 | /* The tcp-inspect analyser is always called alone */ |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1819 | if (req->analysers & AN_REQ_INSPECT) { |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1820 | struct tcp_rule *rule; |
| 1821 | int partial; |
| 1822 | |
Willy Tarreau | f495ddf | 2008-08-17 14:38:41 +0200 | [diff] [blame] | 1823 | /* We will abort if we encounter a read error. In theory, we |
| 1824 | * should not abort if we get a close, it might be valid, |
| 1825 | * although very unlikely. FIXME: we'll abort for now, this |
| 1826 | * will be easier to change later. |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1827 | */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1828 | if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1829 | req->analysers = 0; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 1830 | //t->fe->failed_req++; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1831 | if (!(t->flags & SN_ERR_MASK)) |
| 1832 | t->flags |= SN_ERR_CLICL; |
| 1833 | if (!(t->flags & SN_FINST_MASK)) |
| 1834 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1835 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1836 | } |
| 1837 | |
| 1838 | /* Abort if client read timeout has expired */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1839 | else if (req->flags & BF_READ_TIMEOUT) { |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1840 | req->analysers = 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1841 | t->fe->failed_req++; |
| 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 | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1847 | } |
| 1848 | |
| 1849 | /* We don't know whether we have enough data, so must proceed |
| 1850 | * this way : |
| 1851 | * - iterate through all rules in their declaration order |
| 1852 | * - if one rule returns MISS, it means the inspect delay is |
| 1853 | * not over yet, then return immediately, otherwise consider |
| 1854 | * it as a non-match. |
| 1855 | * - if one rule returns OK, then return OK |
| 1856 | * - if one rule returns KO, then return KO |
| 1857 | */ |
| 1858 | |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 1859 | if (req->flags & BF_SHUTR || tick_is_expired(req->analyse_exp, now_ms)) |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1860 | partial = 0; |
| 1861 | else |
| 1862 | partial = ACL_PARTIAL; |
| 1863 | |
| 1864 | list_for_each_entry(rule, &t->fe->tcp_req.inspect_rules, list) { |
| 1865 | int ret = ACL_PAT_PASS; |
| 1866 | |
| 1867 | if (rule->cond) { |
| 1868 | ret = acl_exec_cond(rule->cond, t->fe, t, NULL, ACL_DIR_REQ | partial); |
| 1869 | if (ret == ACL_PAT_MISS) { |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 1870 | buffer_write_dis(req); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1871 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 1872 | if (!tick_isset(req->analyse_exp)) |
| 1873 | req->analyse_exp = tick_add_ifset(now_ms, t->fe->tcp_req.inspect_delay); |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1874 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1875 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1876 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1877 | ret = acl_pass(ret); |
| 1878 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 1879 | ret = !ret; |
| 1880 | } |
| 1881 | |
| 1882 | if (ret) { |
| 1883 | /* we have a matching rule. */ |
| 1884 | if (rule->action == TCP_ACT_REJECT) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1885 | buffer_abort(req); |
| 1886 | buffer_abort(rep); |
| 1887 | //FIXME: this delete this |
| 1888 | //fd_delete(t->cli_fd); |
| 1889 | //t->cli_state = CL_STCLOSE; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1890 | req->analysers = 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1891 | t->fe->failed_req++; |
| 1892 | if (!(t->flags & SN_ERR_MASK)) |
| 1893 | t->flags |= SN_ERR_PRXCOND; |
| 1894 | if (!(t->flags & SN_FINST_MASK)) |
| 1895 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1896 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1897 | } |
| 1898 | /* otherwise accept */ |
| 1899 | break; |
| 1900 | } |
| 1901 | } |
| 1902 | |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1903 | /* if we get there, it means we have no rule which matches, or |
| 1904 | * we have an explicit accept, so we apply the default accept. |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1905 | */ |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1906 | req->analysers &= ~AN_REQ_INSPECT; |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 1907 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 1908 | } |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 1909 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1910 | if (req->analysers & AN_REQ_HTTP_HDR) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1911 | /* |
| 1912 | * Now parse the partial (or complete) lines. |
| 1913 | * We will check the request syntax, and also join multi-line |
| 1914 | * headers. An index of all the lines will be elaborated while |
| 1915 | * parsing. |
| 1916 | * |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1917 | * For the parsing, we use a 28 states FSM. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1918 | * |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1919 | * Here is the information we currently have : |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1920 | * req->data + req->som = beginning of request |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1921 | * req->data + req->eoh = end of processed headers / start of current one |
| 1922 | * req->data + req->eol = end of current header or line (LF or CRLF) |
| 1923 | * req->lr = first non-visited byte |
| 1924 | * req->r = end of data |
| 1925 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1926 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1927 | int cur_idx; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1928 | struct http_txn *txn = &t->txn; |
| 1929 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1930 | struct proxy *cur_proxy; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1931 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1932 | if (likely(req->lr < req->r)) |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1933 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1934 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1935 | /* 1: we might have to print this header in debug mode */ |
| 1936 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1937 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1938 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1939 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1940 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1941 | sol = req->data + msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1942 | eol = sol + msg->sl.rq.l; |
| 1943 | debug_hdr("clireq", t, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 1944 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1945 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 1946 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1947 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1948 | while (cur_idx) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1949 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1950 | debug_hdr("clihdr", t, sol, eol); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1951 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 1952 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1953 | } |
| 1954 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1955 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1956 | |
| 1957 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1958 | * Now we quickly check if we have found a full valid request. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1959 | * If not so, we check the FD and buffer states before leaving. |
| 1960 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1961 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 1962 | * requests are checked first. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1963 | * |
| 1964 | */ |
| 1965 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1966 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1967 | /* |
| 1968 | * First, let's catch bad requests. |
| 1969 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1970 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1971 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1972 | |
| 1973 | /* 1: Since we are in header mode, if there's no space |
| 1974 | * left for headers, we won't be able to free more |
| 1975 | * later, so the session will never terminate. We |
| 1976 | * must terminate it now. |
| 1977 | */ |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 1978 | if (unlikely(req->flags & BF_FULL)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1979 | /* FIXME: check if URI is set and return Status |
| 1980 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1981 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1982 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1983 | } |
| 1984 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 1985 | /* 2: have we encountered a read error ? */ |
| 1986 | else if (req->flags & BF_READ_ERROR) { |
| 1987 | /* we cannot return any message on error */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1988 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 1989 | req->analysers = 0; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 1990 | //t->fe->failed_req++; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1991 | if (!(t->flags & SN_ERR_MASK)) |
| 1992 | t->flags |= SN_ERR_CLICL; |
| 1993 | if (!(t->flags & SN_FINST_MASK)) |
| 1994 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1995 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1996 | } |
| 1997 | |
| 1998 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 1999 | else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2000 | /* read timeout : give up with an error message. */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2001 | txn->status = 408; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2002 | client_retnclose(t, error_message(t, HTTP_ERR_408)); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2003 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2004 | req->analysers = 0; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 2005 | t->fe->failed_req++; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2006 | if (!(t->flags & SN_ERR_MASK)) |
| 2007 | t->flags |= SN_ERR_CLITO; |
| 2008 | if (!(t->flags & SN_FINST_MASK)) |
| 2009 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2010 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2011 | } |
| 2012 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2013 | /* 4: have we encountered a close ? */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 2014 | else if (req->flags & BF_SHUTR) { |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2015 | txn->status = 400; |
| 2016 | client_retnclose(t, error_message(t, HTTP_ERR_400)); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2017 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2018 | req->analysers = 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2019 | t->fe->failed_req++; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2020 | |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2021 | if (!(t->flags & SN_ERR_MASK)) |
| 2022 | t->flags |= SN_ERR_CLICL; |
| 2023 | if (!(t->flags & SN_FINST_MASK)) |
| 2024 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2025 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2026 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2027 | |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 2028 | buffer_write_dis(req); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2029 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 2030 | if (!tick_isset(req->analyse_exp)) |
| 2031 | req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2032 | |
| 2033 | /* we're not ready yet */ |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2034 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2035 | } |
| 2036 | |
| 2037 | |
| 2038 | /**************************************************************** |
| 2039 | * More interesting part now : we know that we have a complete * |
| 2040 | * request which at least looks like HTTP. We have an indicator * |
| 2041 | * of each header's length, so we can parse them quickly. * |
| 2042 | ****************************************************************/ |
| 2043 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2044 | req->analysers &= ~AN_REQ_HTTP_HDR; |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 2045 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 2046 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2047 | /* ensure we keep this pointer to the beginning of the message */ |
| 2048 | msg->sol = req->data + msg->som; |
| 2049 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2050 | /* |
| 2051 | * 1: identify the method |
| 2052 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2053 | 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] | 2054 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2055 | /* we can make use of server redirect on GET and HEAD */ |
| 2056 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2057 | t->flags |= SN_REDIRECTABLE; |
| 2058 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2059 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2060 | * 2: check if the URI matches the monitor_uri. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2061 | * We have to do this for every request which gets in, because |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2062 | * the monitor-uri is defined by the frontend. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2063 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2064 | if (unlikely((t->fe->monitor_uri_len != 0) && |
| 2065 | (t->fe->monitor_uri_len == msg->sl.rq.u_l) && |
| 2066 | !memcmp(&req->data[msg->sl.rq.u], |
| 2067 | t->fe->monitor_uri, |
| 2068 | t->fe->monitor_uri_len))) { |
| 2069 | /* |
| 2070 | * We have found the monitor URI |
| 2071 | */ |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2072 | struct acl_cond *cond; |
| 2073 | cur_proxy = t->fe; |
| 2074 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2075 | t->flags |= SN_MONITOR; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2076 | |
| 2077 | /* Check if we want to fail this monitor request or not */ |
| 2078 | list_for_each_entry(cond, &cur_proxy->mon_fail_cond, list) { |
| 2079 | 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] | 2080 | |
| 2081 | ret = acl_pass(ret); |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2082 | if (cond->pol == ACL_COND_UNLESS) |
| 2083 | ret = !ret; |
| 2084 | |
| 2085 | if (ret) { |
| 2086 | /* we fail this request, let's return 503 service unavail */ |
| 2087 | txn->status = 503; |
| 2088 | client_retnclose(t, error_message(t, HTTP_ERR_503)); |
| 2089 | goto return_prx_cond; |
| 2090 | } |
| 2091 | } |
| 2092 | |
| 2093 | /* nothing to fail, let's reply normaly */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2094 | txn->status = 200; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2095 | client_retnclose(t, &http_200_chunk); |
| 2096 | goto return_prx_cond; |
| 2097 | } |
| 2098 | |
| 2099 | /* |
| 2100 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2101 | * Note: we cannot log anymore if the request has been |
| 2102 | * classified as invalid. |
| 2103 | */ |
| 2104 | if (unlikely(t->logs.logwait & LW_REQ)) { |
| 2105 | /* we have a complete HTTP request that we must log */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 2106 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2107 | int urilen = msg->sl.rq.l; |
| 2108 | |
| 2109 | if (urilen >= REQURI_LEN) |
| 2110 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2111 | memcpy(txn->uri, &req->data[msg->som], urilen); |
| 2112 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2113 | |
| 2114 | if (!(t->logs.logwait &= ~LW_REQ)) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 2115 | http_sess_log(t); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2116 | } else { |
| 2117 | Alert("HTTP logging : out of memory.\n"); |
| 2118 | } |
| 2119 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2120 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2121 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2122 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 2123 | if (unlikely(msg->sl.rq.v_l == 0)) { |
| 2124 | int delta; |
| 2125 | char *cur_end; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2126 | msg->sol = req->data + msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2127 | cur_end = msg->sol + msg->sl.rq.l; |
| 2128 | delta = 0; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2129 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2130 | if (msg->sl.rq.u_l == 0) { |
| 2131 | /* if no URI was set, add "/" */ |
| 2132 | delta = buffer_replace2(req, cur_end, cur_end, " /", 2); |
| 2133 | cur_end += delta; |
| 2134 | msg->eoh += delta; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2135 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2136 | /* add HTTP version */ |
| 2137 | delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11); |
| 2138 | msg->eoh += delta; |
| 2139 | cur_end += delta; |
| 2140 | cur_end = (char *)http_parse_reqline(msg, req->data, |
| 2141 | HTTP_MSG_RQMETH, |
| 2142 | msg->sol, cur_end + 1, |
| 2143 | NULL, NULL); |
| 2144 | if (unlikely(!cur_end)) |
| 2145 | goto return_bad_req; |
| 2146 | |
| 2147 | /* we have a full HTTP/1.0 request now and we know that |
| 2148 | * we have either a CR or an LF at <ptr>. |
| 2149 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2150 | 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] | 2151 | } |
| 2152 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2153 | |
| 2154 | /* 5: we may need to capture headers */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2155 | if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap)) |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 2156 | capture_headers(req->data + msg->som, &txn->hdr_idx, |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2157 | txn->req.cap, t->fe->req_cap); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2158 | |
| 2159 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2160 | * 6: we will have to evaluate the filters. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2161 | * As opposed to version 1.2, now they will be evaluated in the |
| 2162 | * filters order and not in the header order. This means that |
| 2163 | * each filter has to be validated among all headers. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2164 | * |
| 2165 | * We can now check whether we want to switch to another |
| 2166 | * backend, in which case we will re-check the backend's |
| 2167 | * filters and various options. In order to support 3-level |
| 2168 | * switching, here's how we should proceed : |
| 2169 | * |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2170 | * a) run be. |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2171 | * if (switch) then switch ->be to the new backend. |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2172 | * b) run be if (be != fe). |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2173 | * There cannot be any switch from there, so ->be cannot be |
| 2174 | * changed anymore. |
| 2175 | * |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2176 | * => filters always apply to ->be, then ->be may change. |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2177 | * |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2178 | * The response path will be able to apply either ->be, or |
| 2179 | * ->be then ->fe filters in order to match the reverse of |
| 2180 | * the forward sequence. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2181 | */ |
| 2182 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2183 | do { |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2184 | struct acl_cond *cond; |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2185 | struct redirect_rule *rule; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2186 | struct proxy *rule_set = t->be; |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2187 | cur_proxy = t->be; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2188 | |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2189 | /* first check whether we have some ACLs set to redirect this request */ |
| 2190 | list_for_each_entry(rule, &cur_proxy->redirect_rules, list) { |
| 2191 | 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] | 2192 | |
| 2193 | ret = acl_pass(ret); |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2194 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2195 | ret = !ret; |
| 2196 | |
| 2197 | if (ret) { |
| 2198 | struct chunk rdr = { trash, 0 }; |
| 2199 | const char *msg_fmt; |
| 2200 | |
| 2201 | /* build redirect message */ |
| 2202 | switch(rule->code) { |
| 2203 | case 303: |
| 2204 | rdr.len = strlen(HTTP_303); |
| 2205 | msg_fmt = HTTP_303; |
| 2206 | break; |
| 2207 | case 301: |
| 2208 | rdr.len = strlen(HTTP_301); |
| 2209 | msg_fmt = HTTP_301; |
| 2210 | break; |
| 2211 | case 302: |
| 2212 | default: |
| 2213 | rdr.len = strlen(HTTP_302); |
| 2214 | msg_fmt = HTTP_302; |
| 2215 | break; |
| 2216 | } |
| 2217 | |
| 2218 | if (unlikely(rdr.len > sizeof(trash))) |
| 2219 | goto return_bad_req; |
| 2220 | memcpy(rdr.str, msg_fmt, rdr.len); |
| 2221 | |
| 2222 | switch(rule->type) { |
| 2223 | case REDIRECT_TYPE_PREFIX: { |
| 2224 | const char *path; |
| 2225 | int pathlen; |
| 2226 | |
| 2227 | path = http_get_path(txn); |
| 2228 | /* build message using path */ |
| 2229 | if (path) { |
| 2230 | pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path; |
| 2231 | } else { |
| 2232 | path = "/"; |
| 2233 | pathlen = 1; |
| 2234 | } |
| 2235 | |
| 2236 | if (rdr.len + rule->rdr_len + pathlen > sizeof(trash) - 4) |
| 2237 | goto return_bad_req; |
| 2238 | |
| 2239 | /* add prefix */ |
| 2240 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 2241 | rdr.len += rule->rdr_len; |
| 2242 | |
| 2243 | /* add path */ |
| 2244 | memcpy(rdr.str + rdr.len, path, pathlen); |
| 2245 | rdr.len += pathlen; |
| 2246 | break; |
| 2247 | } |
| 2248 | case REDIRECT_TYPE_LOCATION: |
| 2249 | default: |
| 2250 | if (rdr.len + rule->rdr_len > sizeof(trash) - 4) |
| 2251 | goto return_bad_req; |
| 2252 | |
| 2253 | /* add location */ |
| 2254 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 2255 | rdr.len += rule->rdr_len; |
| 2256 | break; |
| 2257 | } |
| 2258 | |
| 2259 | /* add end of headers */ |
| 2260 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 2261 | rdr.len += 4; |
| 2262 | |
| 2263 | txn->status = rule->code; |
| 2264 | /* let's log the request time */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2265 | t->logs.tv_request = now; |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2266 | client_retnclose(t, &rdr); |
| 2267 | goto return_prx_cond; |
| 2268 | } |
| 2269 | } |
| 2270 | |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2271 | /* first check whether we have some ACLs set to block this request */ |
| 2272 | list_for_each_entry(cond, &cur_proxy->block_cond, list) { |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 2273 | 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] | 2274 | |
| 2275 | ret = acl_pass(ret); |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2276 | if (cond->pol == ACL_COND_UNLESS) |
| 2277 | ret = !ret; |
| 2278 | |
| 2279 | if (ret) { |
| 2280 | txn->status = 403; |
| 2281 | /* let's log the request time */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2282 | t->logs.tv_request = now; |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 2283 | client_retnclose(t, error_message(t, HTTP_ERR_403)); |
| 2284 | goto return_prx_cond; |
| 2285 | } |
| 2286 | } |
| 2287 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2288 | /* try headers filters */ |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2289 | if (rule_set->req_exp != NULL) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2290 | if (apply_filters_to_request(t, req, rule_set->req_exp) < 0) |
| 2291 | goto return_bad_req; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2292 | } |
| 2293 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2294 | if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) { |
| 2295 | /* to ensure correct connection accounting on |
| 2296 | * the backend, we count the connection for the |
| 2297 | * one managing the queue. |
| 2298 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2299 | t->be->beconn++; |
| 2300 | if (t->be->beconn > t->be->beconn_max) |
| 2301 | t->be->beconn_max = t->be->beconn; |
| 2302 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2303 | t->flags |= SN_BE_ASSIGNED; |
| 2304 | } |
| 2305 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2306 | /* has the request been denied ? */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2307 | if (txn->flags & TX_CLDENY) { |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2308 | /* no need to go further */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2309 | txn->status = 403; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2310 | /* let's log the request time */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2311 | t->logs.tv_request = now; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2312 | client_retnclose(t, error_message(t, HTTP_ERR_403)); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2313 | goto return_prx_cond; |
| 2314 | } |
| 2315 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2316 | /* We might have to check for "Connection:" */ |
Krzysztof Oledzki | 336d475 | 2007-12-25 02:40:22 +0100 | [diff] [blame] | 2317 | 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] | 2318 | !(t->flags & SN_CONN_CLOSED)) { |
| 2319 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2320 | int cur_idx, old_idx, delta, val; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2321 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2322 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2323 | 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] | 2324 | old_idx = 0; |
| 2325 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2326 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 2327 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2328 | cur_ptr = cur_next; |
| 2329 | cur_end = cur_ptr + cur_hdr->len; |
| 2330 | cur_next = cur_end + cur_hdr->cr + 1; |
| 2331 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2332 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 2333 | if (val) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2334 | /* 3 possibilities : |
| 2335 | * - we have already set Connection: close, |
| 2336 | * so we remove this line. |
| 2337 | * - we have not yet set Connection: close, |
| 2338 | * but this line indicates close. We leave |
| 2339 | * it untouched and set the flag. |
| 2340 | * - we have not yet set Connection: close, |
| 2341 | * and this line indicates non-close. We |
| 2342 | * replace it. |
| 2343 | */ |
| 2344 | if (t->flags & SN_CONN_CLOSED) { |
| 2345 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2346 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2347 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2348 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 2349 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2350 | cur_hdr->len = 0; |
| 2351 | } else { |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2352 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 2353 | delta = buffer_replace2(req, cur_ptr + val, cur_end, |
| 2354 | "close", 5); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2355 | cur_next += delta; |
| 2356 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2357 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2358 | } |
| 2359 | t->flags |= SN_CONN_CLOSED; |
| 2360 | } |
| 2361 | } |
| 2362 | old_idx = cur_idx; |
| 2363 | } |
Willy Tarreau | f2f0ee8 | 2007-03-30 12:02:43 +0200 | [diff] [blame] | 2364 | } |
| 2365 | /* add request headers from the rule sets in the same order */ |
| 2366 | for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) { |
| 2367 | if (unlikely(http_header_add_tail(req, |
| 2368 | &txn->req, |
| 2369 | &txn->hdr_idx, |
| 2370 | rule_set->req_add[cur_idx])) < 0) |
| 2371 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2372 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2373 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2374 | /* check if stats URI was requested, and if an auth is needed */ |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 2375 | if (rule_set->uri_auth != NULL && |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2376 | (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) { |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2377 | /* we have to check the URI and auth for this request. |
| 2378 | * FIXME!!! that one is rather dangerous, we want to |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2379 | * make it follow standard rules (eg: clear req->analysers). |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2380 | */ |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2381 | if (stats_check_uri_auth(t, rule_set)) |
| 2382 | return 1; |
| 2383 | } |
| 2384 | |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2385 | /* now check whether we have some switching rules for this request */ |
| 2386 | if (!(t->flags & SN_BE_ASSIGNED)) { |
| 2387 | struct switching_rule *rule; |
| 2388 | |
| 2389 | list_for_each_entry(rule, &cur_proxy->switching_rules, list) { |
| 2390 | int ret; |
| 2391 | |
| 2392 | 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] | 2393 | |
| 2394 | ret = acl_pass(ret); |
Willy Tarreau | a8cfa34 | 2008-07-09 11:23:31 +0200 | [diff] [blame] | 2395 | if (rule->cond->pol == ACL_COND_UNLESS) |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2396 | ret = !ret; |
| 2397 | |
| 2398 | if (ret) { |
| 2399 | t->be = rule->be.backend; |
| 2400 | t->be->beconn++; |
| 2401 | if (t->be->beconn > t->be->beconn_max) |
| 2402 | t->be->beconn_max = t->be->beconn; |
| 2403 | t->be->cum_beconn++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2404 | |
| 2405 | /* assign new parameters to the session from the new backend */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2406 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 2407 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2408 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2409 | t->flags |= SN_BE_ASSIGNED; |
| 2410 | break; |
| 2411 | } |
| 2412 | } |
| 2413 | } |
| 2414 | |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 2415 | if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) { |
| 2416 | /* No backend was set, but there was a default |
| 2417 | * backend set in the frontend, so we use it and |
| 2418 | * loop again. |
| 2419 | */ |
| 2420 | t->be = cur_proxy->defbe.be; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2421 | t->be->beconn++; |
| 2422 | if (t->be->beconn > t->be->beconn_max) |
| 2423 | t->be->beconn_max = t->be->beconn; |
| 2424 | t->be->cum_beconn++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2425 | |
| 2426 | /* assign new parameters to the session from the new backend */ |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 2427 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 2428 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2429 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 2430 | t->flags |= SN_BE_ASSIGNED; |
| 2431 | } |
| 2432 | } 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] | 2433 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2434 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2435 | if (!(t->flags & SN_BE_ASSIGNED)) { |
| 2436 | /* To ensure correct connection accounting on |
| 2437 | * the backend, we count the connection for the |
| 2438 | * one managing the queue. |
| 2439 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2440 | t->be->beconn++; |
| 2441 | if (t->be->beconn > t->be->beconn_max) |
| 2442 | t->be->beconn_max = t->be->beconn; |
| 2443 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2444 | t->flags |= SN_BE_ASSIGNED; |
| 2445 | } |
| 2446 | |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2447 | /* |
| 2448 | * Right now, we know that we have processed the entire headers |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2449 | * and that unwanted requests have been filtered out. We can do |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 2450 | * whatever we want with the remaining request. Also, now we |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2451 | * may have separate values for ->fe, ->be. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2452 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2453 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 2454 | /* |
| 2455 | * If HTTP PROXY is set we simply get remote server address |
| 2456 | * parsing incoming request. |
| 2457 | */ |
| 2458 | if ((t->be->options & PR_O_HTTP_PROXY) && !(t->flags & SN_ADDR_SET)) { |
| 2459 | url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &t->srv_addr); |
| 2460 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2461 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2462 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2463 | * 7: the appsession cookie was looked up very early in 1.2, |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2464 | * so let's do the same now. |
| 2465 | */ |
| 2466 | |
| 2467 | /* It needs to look into the URI */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2468 | if (t->be->appsession_name) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 2469 | 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] | 2470 | } |
| 2471 | |
| 2472 | |
| 2473 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2474 | * 8: Now we can work with the cookies. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2475 | * Note that doing so might move headers in the request, but |
| 2476 | * the fields will stay coherent and the URI will not move. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2477 | * This should only be performed in the backend. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2478 | */ |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 2479 | if ((t->be->cookie_name || t->be->appsession_name || t->be->capture_name) |
| 2480 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2481 | manage_client_side_cookies(t, req); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2482 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2483 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2484 | /* |
Willy Tarreau | bb046ac | 2007-03-03 19:17:03 +0100 | [diff] [blame] | 2485 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 2486 | * asks for it. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2487 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2488 | if ((t->fe->options | t->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2489 | if (t->cli_addr.ss_family == AF_INET) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2490 | /* Add an X-Forwarded-For header unless the source IP is |
| 2491 | * in the 'except' network range. |
| 2492 | */ |
| 2493 | if ((!t->fe->except_mask.s_addr || |
| 2494 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr) |
| 2495 | != t->fe->except_net.s_addr) && |
| 2496 | (!t->be->except_mask.s_addr || |
| 2497 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr) |
| 2498 | != t->be->except_net.s_addr)) { |
| 2499 | int len; |
| 2500 | unsigned char *pn; |
| 2501 | pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr; |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2502 | |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2503 | /* Note: we rely on the backend to get the header name to be used for |
| 2504 | * x-forwarded-for, because the header is really meant for the backends. |
| 2505 | * However, if the backend did not specify any option, we have to rely |
| 2506 | * on the frontend's header name. |
| 2507 | */ |
| 2508 | if (t->be->fwdfor_hdr_len) { |
| 2509 | len = t->be->fwdfor_hdr_len; |
| 2510 | memcpy(trash, t->be->fwdfor_hdr_name, len); |
| 2511 | } else { |
| 2512 | len = t->fe->fwdfor_hdr_len; |
| 2513 | memcpy(trash, t->fe->fwdfor_hdr_name, len); |
| 2514 | } |
| 2515 | 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] | 2516 | |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2517 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2518 | &txn->hdr_idx, trash, len)) < 0) |
| 2519 | goto return_bad_req; |
| 2520 | } |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2521 | } |
| 2522 | else if (t->cli_addr.ss_family == AF_INET6) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2523 | /* FIXME: for the sake of completeness, we should also support |
| 2524 | * 'except' here, although it is mostly useless in this case. |
| 2525 | */ |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2526 | int len; |
| 2527 | char pn[INET6_ADDRSTRLEN]; |
| 2528 | inet_ntop(AF_INET6, |
| 2529 | (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr, |
| 2530 | pn, sizeof(pn)); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2531 | |
| 2532 | /* Note: we rely on the backend to get the header name to be used for |
| 2533 | * x-forwarded-for, because the header is really meant for the backends. |
| 2534 | * However, if the backend did not specify any option, we have to rely |
| 2535 | * on the frontend's header name. |
| 2536 | */ |
| 2537 | if (t->be->fwdfor_hdr_len) { |
| 2538 | len = t->be->fwdfor_hdr_len; |
| 2539 | memcpy(trash, t->be->fwdfor_hdr_name, len); |
| 2540 | } else { |
| 2541 | len = t->fe->fwdfor_hdr_len; |
| 2542 | memcpy(trash, t->fe->fwdfor_hdr_name, len); |
| 2543 | } |
| 2544 | len += sprintf(trash + len, ": %s", pn); |
| 2545 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2546 | if (unlikely(http_header_add_tail2(req, &txn->req, |
| 2547 | &txn->hdr_idx, trash, len)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2548 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2549 | } |
| 2550 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2551 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2552 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2553 | * 10: add "Connection: close" if needed and not yet set. |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2554 | * 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] | 2555 | */ |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2556 | if (!(t->flags & SN_CONN_CLOSED) && |
Krzysztof Oledzki | 336d475 | 2007-12-25 02:40:22 +0100 | [diff] [blame] | 2557 | ((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] | 2558 | if ((unlikely(msg->sl.rq.v_l != 8) || |
| 2559 | unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) && |
| 2560 | unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx, |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2561 | "Connection: close", 17)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2562 | goto return_bad_req; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2563 | t->flags |= SN_CONN_CLOSED; |
Willy Tarreau | e15d913 | 2006-12-14 22:26:42 +0100 | [diff] [blame] | 2564 | } |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2565 | /* Before we switch to data, was assignment set in manage_client_side_cookie? |
| 2566 | * If not assigned, perhaps we are balancing on url_param, but this is a |
| 2567 | * POST; and the parameters are in the body, maybe scan there to find our server. |
| 2568 | * (unless headers overflowed the buffer?) |
| 2569 | */ |
| 2570 | if (!(t->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 2571 | t->txn.meth == HTTP_METH_POST && t->be->url_param_name != NULL && |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2572 | 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] | 2573 | memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) { |
| 2574 | /* are there enough bytes here? total == l || r || rlim ? |
| 2575 | * len is unsigned, but eoh is int, |
| 2576 | * how many bytes of body have we received? |
| 2577 | * eoh is the first empty line of the header |
| 2578 | */ |
| 2579 | /* 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] | 2580 | 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] | 2581 | |
| 2582 | /* If we have HTTP/1.1 and Expect: 100-continue, then abort. |
| 2583 | * We can't assume responsibility for the server's decision, |
| 2584 | * on this URI and header set. See rfc2616: 14.20, 8.2.3, |
| 2585 | * We also can't change our mind later, about which server to choose, so round robin. |
| 2586 | */ |
| 2587 | if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) { |
| 2588 | struct hdr_ctx ctx; |
| 2589 | ctx.idx = 0; |
| 2590 | /* Expect is allowed in 1.1, look for it */ |
| 2591 | http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx); |
| 2592 | if (ctx.idx != 0 && |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2593 | 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] | 2594 | /* We can't reliablly stall and wait for data, because of |
| 2595 | * .NET clients that don't conform to rfc2616; so, no need for |
| 2596 | * the next block to check length expectations. |
| 2597 | * We could send 100 status back to the client, but then we need to |
| 2598 | * re-write headers, and send the message. And this isn't the right |
| 2599 | * place for that action. |
| 2600 | * TODO: support Expect elsewhere and delete this block. |
| 2601 | */ |
| 2602 | goto end_check_maybe_wait_for_body; |
| 2603 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2604 | |
| 2605 | if (likely(len > t->be->url_param_post_limit)) { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2606 | /* nothing to do, we got enough */ |
| 2607 | } else { |
| 2608 | /* limit implies we are supposed to need this many bytes |
| 2609 | * to find the parameter. Let's see how many bytes we can wait for. |
| 2610 | */ |
| 2611 | long long hint = len; |
| 2612 | struct hdr_ctx ctx; |
| 2613 | ctx.idx = 0; |
| 2614 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx); |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2615 | if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) { |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 2616 | buffer_write_dis(req); |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2617 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2618 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2619 | else { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2620 | ctx.idx = 0; |
| 2621 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx); |
| 2622 | /* now if we have a length, we'll take the hint */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2623 | if (ctx.idx) { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2624 | /* We have Content-Length */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2625 | if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint)) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2626 | hint = 0; /* parse failure, untrusted client */ |
| 2627 | else { |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2628 | if (hint > 0) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2629 | msg->hdr_content_len = hint; |
| 2630 | else |
| 2631 | hint = 0; /* bad client, sent negative length */ |
| 2632 | } |
| 2633 | } |
| 2634 | /* 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] | 2635 | if (t->be->url_param_post_limit < hint) |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2636 | hint = t->be->url_param_post_limit; |
| 2637 | /* now do we really need to buffer more data? */ |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2638 | if (len < hint) { |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 2639 | buffer_write_dis(req); |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2640 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2641 | } |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2642 | /* else... There are no body bytes to wait for */ |
| 2643 | } |
| 2644 | } |
| 2645 | } |
| 2646 | end_check_maybe_wait_for_body: |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2647 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2648 | /************************************************************* |
| 2649 | * OK, that's finished for the headers. We have done what we * |
| 2650 | * could. Let's switch to the DATA state. * |
| 2651 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2652 | |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2653 | buffer_set_rlim(req, BUFSIZE); /* no more rewrite needed */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 2654 | t->logs.tv_request = now; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2655 | |
Willy Tarreau | 1fa3126 | 2007-12-03 00:36:16 +0100 | [diff] [blame] | 2656 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 2657 | * which may be the same as the connect timeout if unspecified. |
| 2658 | * If unset, then set it to zero because we really want it to |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2659 | * eventually expire. We build the tarpit as an analyser. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2660 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2661 | if (txn->flags & TX_CLTARPIT) { |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2662 | buffer_flush(t->req); |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2663 | /* flush the request so that we can drop the connection early |
| 2664 | * if the client closes first. |
| 2665 | */ |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 2666 | buffer_write_dis(req); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2667 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 2668 | req->analyse_exp = tick_add_ifset(now_ms, t->be->timeout.tarpit); |
| 2669 | if (!req->analyse_exp) |
| 2670 | req->analyse_exp = now_ms; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2671 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2672 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2673 | /* OK let's go on with the BODY now */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2674 | goto end_of_headers; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2675 | |
| 2676 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2677 | txn->req.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2678 | txn->status = 400; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2679 | req->analysers = 0; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2680 | client_retnclose(t, error_message(t, HTTP_ERR_400)); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 2681 | t->fe->failed_req++; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2682 | return_prx_cond: |
| 2683 | if (!(t->flags & SN_ERR_MASK)) |
| 2684 | t->flags |= SN_ERR_PRXCOND; |
| 2685 | if (!(t->flags & SN_FINST_MASK)) |
| 2686 | t->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2687 | return 0; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2688 | end_of_headers: |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2689 | ; // to keep gcc happy |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2690 | } |
| 2691 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2692 | if (req->analysers & AN_REQ_HTTP_TARPIT) { |
| 2693 | struct http_txn *txn = &t->txn; |
| 2694 | |
| 2695 | /* This connection is being tarpitted. The CLIENT side has |
| 2696 | * already set the connect expiration date to the right |
| 2697 | * timeout. We just have to check that the client is still |
| 2698 | * there and that the timeout has not expired. |
| 2699 | */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 2700 | if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 && |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2701 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 2702 | return 0; |
| 2703 | |
| 2704 | /* We will set the queue timer to the time spent, just for |
| 2705 | * logging purposes. We fake a 500 server error, so that the |
| 2706 | * attacker will not suspect his connection has been tarpitted. |
| 2707 | * It will not cause trouble to the logs because we can exclude |
| 2708 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 2709 | */ |
| 2710 | trace_term(t, TT_HTTP_SRV_2); |
| 2711 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 2712 | |
| 2713 | txn->status = 500; |
| 2714 | if (req->flags != BF_READ_ERROR) |
| 2715 | client_retnclose(t, error_message(t, HTTP_ERR_500)); |
| 2716 | |
| 2717 | req->analysers = 0; |
| 2718 | req->analyse_exp = TICK_ETERNITY; |
| 2719 | |
| 2720 | t->fe->failed_req++; |
| 2721 | if (!(t->flags & SN_ERR_MASK)) |
| 2722 | t->flags |= SN_ERR_PRXCOND; |
| 2723 | if (!(t->flags & SN_FINST_MASK)) |
| 2724 | t->flags |= SN_FINST_T; |
| 2725 | return 0; |
| 2726 | } |
| 2727 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2728 | if (req->analysers & AN_REQ_HTTP_BODY) { |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2729 | /* We have to parse the HTTP request body to find any required data. |
| 2730 | * "balance url_param check_post" should have been the only way to get |
| 2731 | * into this. We were brought here after HTTP header analysis, so all |
| 2732 | * related structures are ready. |
| 2733 | */ |
| 2734 | struct http_msg *msg = &t->txn.req; |
| 2735 | unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1; |
| 2736 | long long limit = t->be->url_param_post_limit; |
| 2737 | struct hdr_ctx ctx; |
| 2738 | |
| 2739 | ctx.idx = 0; |
| 2740 | |
| 2741 | /* now if we have a length, we'll take the hint */ |
| 2742 | http_find_header2("Transfer-Encoding", 17, msg->sol, &t->txn.hdr_idx, &ctx); |
| 2743 | if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) { |
| 2744 | unsigned int chunk = 0; |
| 2745 | while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) { |
| 2746 | char c = msg->sol[body]; |
| 2747 | if (ishex(c)) { |
| 2748 | unsigned int hex = toupper(c) - '0'; |
| 2749 | if (hex > 9) |
| 2750 | hex -= 'A' - '9' - 1; |
| 2751 | chunk = (chunk << 4) | hex; |
| 2752 | } else |
| 2753 | break; |
| 2754 | body++; |
| 2755 | } |
| 2756 | if (body + 2 >= req->l) /* we want CRLF too */ |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2757 | goto http_body_end; /* end of buffer? data missing! */ |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2758 | |
| 2759 | if (memcmp(msg->sol+body, "\r\n", 2) != 0) |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2760 | 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] | 2761 | |
| 2762 | body += 2; // skip CRLF |
| 2763 | |
| 2764 | /* if we support more then one chunk here, we have to do it again when assigning server |
| 2765 | * 1. how much entity data do we have? new var |
| 2766 | * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here |
| 2767 | * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found) |
| 2768 | */ |
| 2769 | |
| 2770 | if (chunk < limit) |
| 2771 | limit = chunk; /* only reading one chunk */ |
| 2772 | } else { |
| 2773 | if (msg->hdr_content_len < limit) |
| 2774 | limit = msg->hdr_content_len; |
| 2775 | } |
| 2776 | |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2777 | http_body_end: |
| 2778 | /* we leave once we know we have nothing left to do. This means that we have |
| 2779 | * enough bytes, or that we know we'll not get any more (buffer full, read |
| 2780 | * buffer closed). |
| 2781 | */ |
| 2782 | if (req->l - body >= limit || /* enough bytes! */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 2783 | req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR | BF_READ_TIMEOUT) || |
Willy Tarreau | c52164a | 2008-08-17 19:17:57 +0200 | [diff] [blame] | 2784 | tick_is_expired(req->analyse_exp, now_ms)) { |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2785 | /* 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] | 2786 | t->logs.tv_request = now; /* update the request timer to reflect full request */ |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2787 | req->analysers &= ~AN_REQ_HTTP_BODY; |
Willy Tarreau | ffab5b4 | 2008-08-17 18:03:28 +0200 | [diff] [blame] | 2788 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2789 | } |
Willy Tarreau | c52164a | 2008-08-17 19:17:57 +0200 | [diff] [blame] | 2790 | else { |
| 2791 | /* Not enough data. We'll re-use the http-request |
| 2792 | * timeout here. Ideally, we should set the timeout |
| 2793 | * relative to the accept() date. We just set the |
| 2794 | * request timeout once at the beginning of the |
| 2795 | * request. |
| 2796 | */ |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 2797 | buffer_write_dis(req); |
Willy Tarreau | c52164a | 2008-08-17 19:17:57 +0200 | [diff] [blame] | 2798 | if (!tick_isset(req->analyse_exp)) |
| 2799 | req->analyse_exp = tick_add_ifset(now_ms, t->fe->timeout.httpreq); |
| 2800 | return 0; |
| 2801 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2802 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2803 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2804 | /* Note: eventhough nobody should set an unknown flag, clearing them right now will |
| 2805 | * probably reduce one day's debugging session. |
| 2806 | */ |
| 2807 | #ifdef DEBUG_DEV |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2808 | if (req->analysers & ~(AN_REQ_INSPECT | AN_REQ_HTTP_HDR | AN_REQ_HTTP_TARPIT | AN_REQ_HTTP_BODY)) { |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2809 | fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n", |
| 2810 | __FILE__, __LINE__, req->analysers); |
| 2811 | ABORT_NOW(); |
| 2812 | } |
| 2813 | #endif |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2814 | req->analysers &= AN_REQ_INSPECT | AN_REQ_HTTP_HDR | AN_REQ_HTTP_TARPIT | AN_REQ_HTTP_BODY; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2815 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2816 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2817 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2818 | /* This function performs all the processing enabled for the current response. |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2819 | * It normally returns zero, but may return 1 if it absolutely needs to be |
| 2820 | * called again after other functions. It relies on buffers flags, and updates |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2821 | * t->rep->analysers. It might make sense to explode it into several other |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2822 | * functions. It works like process_request (see indications above). |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2823 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2824 | int process_response(struct session *t) |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2825 | { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2826 | struct http_txn *txn = &t->txn; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2827 | struct buffer *req = t->req; |
| 2828 | struct buffer *rep = t->rep; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2829 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 2830 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2831 | now_ms, __FUNCTION__, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 2832 | t, |
| 2833 | rep, |
| 2834 | rep->rex, rep->wex, |
| 2835 | rep->flags, |
| 2836 | rep->l, |
| 2837 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 2838 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2839 | if (rep->analysers & AN_RTR_HTTP_HDR) { /* receiving server headers */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2840 | /* |
| 2841 | * Now parse the partial (or complete) lines. |
| 2842 | * We will check the response syntax, and also join multi-line |
| 2843 | * headers. An index of all the lines will be elaborated while |
| 2844 | * parsing. |
| 2845 | * |
| 2846 | * For the parsing, we use a 28 states FSM. |
| 2847 | * |
| 2848 | * Here is the information we currently have : |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2849 | * rep->data + rep->som = beginning of response |
| 2850 | * rep->data + rep->eoh = end of processed headers / start of current one |
| 2851 | * 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] | 2852 | * rep->lr = first non-visited byte |
| 2853 | * rep->r = end of data |
| 2854 | */ |
Willy Tarreau | 7f875f6 | 2008-08-11 17:35:01 +0200 | [diff] [blame] | 2855 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2856 | int cur_idx; |
| 2857 | struct http_msg *msg = &txn->rsp; |
| 2858 | struct proxy *cur_proxy; |
| 2859 | |
| 2860 | if (likely(rep->lr < rep->r)) |
| 2861 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
| 2862 | |
| 2863 | /* 1: we might have to print this header in debug mode */ |
| 2864 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2865 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
| 2866 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
| 2867 | char *eol, *sol; |
| 2868 | |
| 2869 | sol = rep->data + msg->som; |
| 2870 | eol = sol + msg->sl.rq.l; |
| 2871 | debug_hdr("srvrep", t, sol, eol); |
| 2872 | |
| 2873 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2874 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
| 2875 | |
| 2876 | while (cur_idx) { |
| 2877 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2878 | debug_hdr("srvhdr", t, sol, eol); |
| 2879 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2880 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2881 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2882 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2883 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2884 | /* |
| 2885 | * Now we quickly check if we have found a full valid response. |
| 2886 | * If not so, we check the FD and buffer states before leaving. |
| 2887 | * A full response is indicated by the fact that we have seen |
| 2888 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 2889 | * responses are checked first. |
| 2890 | * |
| 2891 | * Depending on whether the client is still there or not, we |
| 2892 | * may send an error response back or not. Note that normally |
| 2893 | * we should only check for HTTP status there, and check I/O |
| 2894 | * errors somewhere else. |
| 2895 | */ |
| 2896 | |
| 2897 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2898 | /* Invalid response */ |
| 2899 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 2900 | hdr_response_bad: |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2901 | //buffer_shutr(rep); |
| 2902 | //buffer_shutw(req); |
| 2903 | //fd_delete(req->cons->fd); |
| 2904 | //req->cons->state = SI_ST_CLO; |
| 2905 | buffer_shutr_now(rep); |
| 2906 | buffer_shutw_now(req); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2907 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2908 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2909 | t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2910 | //sess_change_server(t, NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2911 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2912 | t->be->failed_resp++; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2913 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2914 | txn->status = 502; |
| 2915 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 2916 | if (!(t->flags & SN_ERR_MASK)) |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2917 | t->flags |= SN_ERR_PRXCOND; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2918 | if (!(t->flags & SN_FINST_MASK)) |
| 2919 | t->flags |= SN_FINST_H; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 2920 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2921 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 2922 | // process_srv_queue(t->srv); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2923 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2924 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2925 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2926 | /* too large response does not fit in buffer. */ |
| 2927 | else if (rep->flags & BF_FULL) { |
| 2928 | goto hdr_response_bad; |
| 2929 | } |
| 2930 | /* read error */ |
| 2931 | else if (rep->flags & BF_READ_ERROR) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2932 | buffer_shutr_now(rep); |
| 2933 | buffer_shutw_now(req); |
| 2934 | //fd_delete(req->cons->fd); |
| 2935 | //req->cons->state = SI_ST_CLO; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2936 | //if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2937 | //t->srv->cur_sess--; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2938 | //t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2939 | //sess_change_server(t, NULL); |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2940 | //} |
| 2941 | //t->be->failed_resp++; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2942 | rep->analysers = 0; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2943 | txn->status = 502; |
| 2944 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 2945 | if (!(t->flags & SN_ERR_MASK)) |
| 2946 | t->flags |= SN_ERR_SRVCL; |
| 2947 | if (!(t->flags & SN_FINST_MASK)) |
| 2948 | t->flags |= SN_FINST_H; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 2949 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2950 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 2951 | // process_srv_queue(t->srv); |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2952 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2953 | return 0; |
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 | /* read timeout : return a 504 to the client. */ |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2956 | else if (rep->flags & BF_READ_TIMEOUT) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2957 | buffer_shutr_now(rep); |
| 2958 | buffer_shutw_now(req); |
| 2959 | //fd_delete(req->cons->fd); |
| 2960 | //req->cons->state = SI_ST_CLO; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2961 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2962 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2963 | t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2964 | //sess_change_server(t, NULL); |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2965 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2966 | t->be->failed_resp++; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2967 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2968 | txn->status = 504; |
| 2969 | client_return(t, error_message(t, HTTP_ERR_504)); |
| 2970 | if (!(t->flags & SN_ERR_MASK)) |
| 2971 | t->flags |= SN_ERR_SRVTO; |
| 2972 | if (!(t->flags & SN_FINST_MASK)) |
| 2973 | t->flags |= SN_FINST_H; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 2974 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2975 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 2976 | // process_srv_queue(t->srv); |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2977 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2978 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2979 | /* write error to client, or close from server */ |
Willy Tarreau | e5ed406 | 2008-08-30 03:17:31 +0200 | [diff] [blame] | 2980 | else if (rep->flags & (BF_WRITE_ERROR|BF_SHUTR)) { |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2981 | buffer_shutr_now(rep); |
| 2982 | buffer_shutw_now(req); |
| 2983 | //fd_delete(req->cons->fd); |
| 2984 | //req->cons->state = SI_ST_CLO; |
| 2985 | if (t->srv) { |
| 2986 | //t->srv->cur_sess--; |
| 2987 | t->srv->failed_resp++; |
| 2988 | //sess_change_server(t, NULL); |
| 2989 | } |
| 2990 | t->be->failed_resp++; |
| 2991 | rep->analysers = 0; |
| 2992 | txn->status = 502; |
| 2993 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 2994 | if (!(t->flags & SN_ERR_MASK)) |
| 2995 | t->flags |= SN_ERR_SRVCL; |
| 2996 | if (!(t->flags & SN_FINST_MASK)) |
| 2997 | t->flags |= SN_FINST_H; |
| 2998 | |
| 2999 | //if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
| 3000 | // process_srv_queue(t->srv); |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 3001 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 3002 | return 0; |
| 3003 | } |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 3004 | buffer_write_dis(rep); |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 3005 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3006 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3007 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3008 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3009 | /***************************************************************** |
| 3010 | * More interesting part now : we know that we have a complete * |
| 3011 | * response which at least looks like HTTP. We have an indicator * |
| 3012 | * of each header's length, so we can parse them quickly. * |
| 3013 | ****************************************************************/ |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3014 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3015 | rep->analysers &= ~AN_RTR_HTTP_HDR; |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 3016 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3017 | /* ensure we keep this pointer to the beginning of the message */ |
| 3018 | msg->sol = rep->data + msg->som; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3019 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3020 | /* |
| 3021 | * 1: get the status code and check for cacheability. |
| 3022 | */ |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3023 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3024 | t->logs.logwait &= ~LW_RESP; |
| 3025 | 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] | 3026 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3027 | switch (txn->status) { |
| 3028 | case 200: |
| 3029 | case 203: |
| 3030 | case 206: |
| 3031 | case 300: |
| 3032 | case 301: |
| 3033 | case 410: |
| 3034 | /* RFC2616 @13.4: |
| 3035 | * "A response received with a status code of |
| 3036 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 3037 | * by a cache (...) unless a cache-control |
| 3038 | * directive prohibits caching." |
| 3039 | * |
| 3040 | * RFC2616 @9.5: POST method : |
| 3041 | * "Responses to this method are not cacheable, |
| 3042 | * unless the response includes appropriate |
| 3043 | * Cache-Control or Expires header fields." |
| 3044 | */ |
| 3045 | if (likely(txn->meth != HTTP_METH_POST) && |
| 3046 | (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
| 3047 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 3048 | break; |
| 3049 | default: |
| 3050 | break; |
| 3051 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3052 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3053 | /* |
| 3054 | * 2: we may need to capture headers |
| 3055 | */ |
| 3056 | if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap)) |
| 3057 | capture_headers(rep->data + msg->som, &txn->hdr_idx, |
| 3058 | txn->rsp.cap, t->fe->rsp_cap); |
| 3059 | |
| 3060 | /* |
| 3061 | * 3: we will have to evaluate the filters. |
| 3062 | * As opposed to version 1.2, now they will be evaluated in the |
| 3063 | * filters order and not in the header order. This means that |
| 3064 | * each filter has to be validated among all headers. |
| 3065 | * |
| 3066 | * Filters are tried with ->be first, then with ->fe if it is |
| 3067 | * different from ->be. |
| 3068 | */ |
| 3069 | |
| 3070 | t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */ |
| 3071 | |
| 3072 | cur_proxy = t->be; |
| 3073 | while (1) { |
| 3074 | struct proxy *rule_set = cur_proxy; |
| 3075 | |
| 3076 | /* try headers filters */ |
| 3077 | if (rule_set->rsp_exp != NULL) { |
| 3078 | if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) { |
| 3079 | return_bad_resp: |
| 3080 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3081 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3082 | t->srv->failed_resp++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3083 | //sess_change_server(t, NULL); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3084 | } |
| 3085 | cur_proxy->failed_resp++; |
| 3086 | return_srv_prx_502: |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3087 | buffer_shutr_now(rep); |
| 3088 | buffer_shutw_now(req); |
| 3089 | //fd_delete(req->cons->fd); |
| 3090 | //req->cons->state = SI_ST_CLO; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3091 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3092 | txn->status = 502; |
| 3093 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 3094 | if (!(t->flags & SN_ERR_MASK)) |
| 3095 | t->flags |= SN_ERR_PRXCOND; |
| 3096 | if (!(t->flags & SN_FINST_MASK)) |
| 3097 | t->flags |= SN_FINST_H; |
| 3098 | /* We used to have a free connection slot. Since we'll never use it, |
| 3099 | * we have to inform the server that it may be used by another session. |
| 3100 | */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3101 | //if (t->srv && may_dequeue_tasks(t->srv, cur_proxy)) |
| 3102 | // process_srv_queue(t->srv); |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3103 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3104 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3105 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3106 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3107 | /* has the response been denied ? */ |
| 3108 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | f899b94 | 2008-03-28 18:09:38 +0100 | [diff] [blame] | 3109 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3110 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3111 | t->srv->failed_secu++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3112 | //sess_change_server(t, NULL); |
Willy Tarreau | f899b94 | 2008-03-28 18:09:38 +0100 | [diff] [blame] | 3113 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3114 | cur_proxy->denied_resp++; |
| 3115 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 3116 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3117 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3118 | /* We might have to check for "Connection:" */ |
| 3119 | if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) && |
| 3120 | !(t->flags & SN_CONN_CLOSED)) { |
| 3121 | char *cur_ptr, *cur_end, *cur_next; |
| 3122 | int cur_idx, old_idx, delta, val; |
| 3123 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3124 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3125 | cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 3126 | old_idx = 0; |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 3127 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3128 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 3129 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 3130 | cur_ptr = cur_next; |
| 3131 | cur_end = cur_ptr + cur_hdr->len; |
| 3132 | cur_next = cur_end + cur_hdr->cr + 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3133 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3134 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 3135 | if (val) { |
| 3136 | /* 3 possibilities : |
| 3137 | * - we have already set Connection: close, |
| 3138 | * so we remove this line. |
| 3139 | * - we have not yet set Connection: close, |
| 3140 | * but this line indicates close. We leave |
| 3141 | * it untouched and set the flag. |
| 3142 | * - we have not yet set Connection: close, |
| 3143 | * and this line indicates non-close. We |
| 3144 | * replace it. |
| 3145 | */ |
| 3146 | if (t->flags & SN_CONN_CLOSED) { |
| 3147 | delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0); |
| 3148 | txn->rsp.eoh += delta; |
| 3149 | cur_next += delta; |
| 3150 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 3151 | txn->hdr_idx.used--; |
| 3152 | cur_hdr->len = 0; |
| 3153 | } else { |
| 3154 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 3155 | delta = buffer_replace2(rep, cur_ptr + val, cur_end, |
| 3156 | "close", 5); |
| 3157 | cur_next += delta; |
| 3158 | cur_hdr->len += delta; |
| 3159 | txn->rsp.eoh += delta; |
| 3160 | } |
| 3161 | t->flags |= SN_CONN_CLOSED; |
| 3162 | } |
| 3163 | } |
| 3164 | old_idx = cur_idx; |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 3165 | } |
| 3166 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3167 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3168 | /* add response headers from the rule sets in the same order */ |
| 3169 | for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) { |
| 3170 | if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, |
| 3171 | rule_set->rsp_add[cur_idx])) < 0) |
| 3172 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 3173 | } |
| 3174 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3175 | /* check whether we're already working on the frontend */ |
| 3176 | if (cur_proxy == t->fe) |
| 3177 | break; |
| 3178 | cur_proxy = t->fe; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3179 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3180 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3181 | /* |
| 3182 | * 4: check for server cookie. |
| 3183 | */ |
| 3184 | if (t->be->cookie_name || t->be->appsession_name || t->be->capture_name |
| 3185 | || (t->be->options & PR_O_CHK_CACHE)) |
| 3186 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3187 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3188 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3189 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3190 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3191 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3192 | if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0) |
| 3193 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3194 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3195 | /* |
| 3196 | * 6: add server cookie in the response if needed |
| 3197 | */ |
| 3198 | if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) && |
| 3199 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) { |
| 3200 | int len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3201 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3202 | /* the server is known, it's not the one the client requested, we have to |
| 3203 | * insert a set-cookie here, except if we want to insert only on POST |
| 3204 | * requests and this one isn't. Note that servers which don't have cookies |
| 3205 | * (eg: some backup servers) will return a full cookie removal request. |
| 3206 | */ |
| 3207 | len = sprintf(trash, "Set-Cookie: %s=%s; path=/", |
| 3208 | t->be->cookie_name, |
| 3209 | 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] | 3210 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3211 | if (t->be->cookie_domain) |
| 3212 | len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3213 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3214 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3215 | trash, len)) < 0) |
| 3216 | goto return_bad_resp; |
| 3217 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3218 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3219 | /* Here, we will tell an eventual cache on the client side that we don't |
| 3220 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 3221 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 3222 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 3223 | */ |
| 3224 | if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) { |
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 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3227 | |
| 3228 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3229 | "Cache-control: private", 22)) < 0) |
| 3230 | goto return_bad_resp; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3231 | } |
| 3232 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3233 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3234 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3235 | /* |
| 3236 | * 7: check if result will be cacheable with a cookie. |
| 3237 | * We'll block the response if security checks have caught |
| 3238 | * nasty things such as a cacheable cookie. |
| 3239 | */ |
| 3240 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) == |
| 3241 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) && |
| 3242 | (t->be->options & PR_O_CHK_CACHE)) { |
| 3243 | |
| 3244 | /* we're in presence of a cacheable response containing |
| 3245 | * a set-cookie header. We'll block it as requested by |
| 3246 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3247 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3248 | if (t->srv) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3249 | //t->srv->cur_sess--; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3250 | t->srv->failed_secu++; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3251 | //sess_change_server(t, NULL); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3252 | } |
| 3253 | t->be->denied_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3254 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3255 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 3256 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 3257 | send_log(t->be, LOG_ALERT, |
| 3258 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 3259 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 3260 | goto return_srv_prx_502; |
| 3261 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3262 | |
| 3263 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3264 | * 8: add "Connection: close" if needed and not yet set. |
| 3265 | * 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] | 3266 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3267 | if (!(t->flags & SN_CONN_CLOSED) && |
| 3268 | ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) { |
| 3269 | if ((unlikely(msg->sl.st.v_l != 8) || |
| 3270 | unlikely(req->data[msg->som + 7] != '0')) && |
| 3271 | unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3272 | "Connection: close", 17)) < 0) |
| 3273 | goto return_bad_resp; |
| 3274 | t->flags |= SN_CONN_CLOSED; |
| 3275 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3276 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3277 | /************************************************************* |
| 3278 | * OK, that's finished for the headers. We have done what we * |
| 3279 | * could. Let's switch to the DATA state. * |
| 3280 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3281 | |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 3282 | buffer_set_rlim(rep, BUFSIZE); /* no more rewrite needed */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3283 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3284 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3285 | #ifdef CONFIG_HAP_TCPSPLICE |
| 3286 | if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
| 3287 | /* TCP splicing supported by both FE and BE */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 3288 | tcp_splice_splicefd(rep->cons->fd, rep->prod->fd, 0); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3289 | } |
| 3290 | #endif |
| 3291 | /* if the user wants to log as soon as possible, without counting |
| 3292 | * bytes from the server, then this is the right moment. We have |
| 3293 | * to temporarily assign bytes_out to log what we currently have. |
| 3294 | */ |
| 3295 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 3296 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 3297 | t->logs.bytes_out = txn->rsp.eoh; |
| 3298 | if (t->fe->to_log & LW_REQ) |
| 3299 | http_sess_log(t); |
| 3300 | else |
| 3301 | tcp_sess_log(t); |
| 3302 | t->logs.bytes_out = 0; |
| 3303 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3304 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3305 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 3306 | * otherwise we would not let the client side wake up. |
| 3307 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3308 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3309 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3310 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3311 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3312 | /* Note: eventhough nobody should set an unknown flag, clearing them right now will |
| 3313 | * probably reduce one day's debugging session. |
| 3314 | */ |
| 3315 | #ifdef DEBUG_DEV |
| 3316 | if (rep->analysers & ~(AN_RTR_HTTP_HDR)) { |
| 3317 | fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n", |
| 3318 | __FILE__, __LINE__, rep->analysers); |
| 3319 | ABORT_NOW(); |
| 3320 | } |
| 3321 | #endif |
| 3322 | rep->analysers &= AN_RTR_HTTP_HDR; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3323 | return 0; |
| 3324 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3325 | |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 3326 | ///* |
| 3327 | // * Manages the client FSM and its socket. It normally returns zero, but may |
| 3328 | // * return 1 if it absolutely wants to be called again. |
| 3329 | // * |
| 3330 | // * Note: process_cli is the ONLY function allowed to set cli_state to anything |
| 3331 | // * but CL_STCLOSE. |
| 3332 | // */ |
| 3333 | //int process_cli(struct session *t) |
| 3334 | //{ |
| 3335 | // struct buffer *req = t->req; |
| 3336 | // struct buffer *rep = t->rep; |
| 3337 | // |
| 3338 | // DPRINTF(stderr,"[%u] %s: fd=%d[%d] c=%s set(r,w)=%d,%d exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n", |
| 3339 | // now_ms, __FUNCTION__, |
| 3340 | // t->cli_fd, t->cli_fd >= 0 ? fdtab[t->cli_fd].state : 0, /* fd,state*/ |
| 3341 | // cli_stnames[t->cli_state], |
| 3342 | // t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_RD) : 0, |
| 3343 | // t->cli_fd >= 0 && fdtab[t->cli_fd].state != FD_STCLOSE ? EV_FD_ISSET(t->cli_fd, DIR_WR) : 0, |
| 3344 | // req->rex, rep->wex, |
| 3345 | // req->flags, rep->flags, |
| 3346 | // req->l, rep->l); |
| 3347 | // |
| 3348 | // update_state: |
| 3349 | // /* FIXME: we still have to check for CL_STSHUTR because client_retnclose |
| 3350 | // * still set this state (and will do until unix sockets are converted). |
| 3351 | // */ |
| 3352 | // if (t->cli_state == CL_STDATA || t->cli_state == CL_STSHUTR) { |
| 3353 | // /* we can skip most of the tests at once if some conditions are not met */ |
| 3354 | // if (!((fdtab[t->cli_fd].state == FD_STERROR) || |
| 3355 | // (req->flags & (BF_READ_TIMEOUT|BF_READ_ERROR|BF_SHUTR_NOW)) || |
| 3356 | // (rep->flags & (BF_WRITE_TIMEOUT|BF_WRITE_ERROR|BF_SHUTW_NOW)) || |
| 3357 | // (!(req->flags & BF_SHUTR) && req->flags & (BF_READ_NULL|BF_SHUTW)) || |
| 3358 | // (!(rep->flags & BF_SHUTW) && |
| 3359 | // (rep->flags & (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR)) == (BF_EMPTY|BF_MAY_FORWARD|BF_SHUTR)))) |
| 3360 | // goto update_timeouts; |
| 3361 | // |
| 3362 | // /* read or write error */ |
| 3363 | // if (fdtab[t->cli_fd].state == FD_STERROR) { |
| 3364 | // buffer_shutr(req); |
| 3365 | // req->flags |= BF_READ_ERROR; |
| 3366 | // buffer_shutw(rep); |
| 3367 | // rep->flags |= BF_WRITE_ERROR; |
| 3368 | // fd_delete(t->cli_fd); |
| 3369 | // t->cli_state = CL_STCLOSE; |
| 3370 | // trace_term(t, TT_HTTP_CLI_1); |
| 3371 | // if (!req->analysers) { |
| 3372 | // if (!(t->flags & SN_ERR_MASK)) |
| 3373 | // t->flags |= SN_ERR_CLICL; |
| 3374 | // if (!(t->flags & SN_FINST_MASK)) { |
| 3375 | // if (req->cons->err_type <= SI_ET_QUEUE_ABRT) |
| 3376 | // t->flags |= SN_FINST_Q; |
| 3377 | // else if (req->cons->err_type <= SI_ET_CONN_OTHER) |
| 3378 | // t->flags |= SN_FINST_C; |
| 3379 | // else |
| 3380 | // t->flags |= SN_FINST_D; |
| 3381 | // } |
| 3382 | // } |
| 3383 | // goto update_state; |
| 3384 | // } |
| 3385 | // /* last read, or end of server write */ |
| 3386 | // else if (!(req->flags & BF_SHUTR) && /* not already done */ |
| 3387 | // req->flags & (BF_READ_NULL|BF_SHUTR_NOW|BF_SHUTW)) { |
| 3388 | // buffer_shutr(req); |
| 3389 | // if (!(rep->flags & BF_SHUTW)) { |
| 3390 | // EV_FD_CLR(t->cli_fd, DIR_RD); |
| 3391 | // trace_term(t, TT_HTTP_CLI_2); |
| 3392 | // } else { |
| 3393 | // /* output was already closed */ |
| 3394 | // fd_delete(t->cli_fd); |
| 3395 | // t->cli_state = CL_STCLOSE; |
| 3396 | // trace_term(t, TT_HTTP_CLI_3); |
| 3397 | // } |
| 3398 | // goto update_state; |
| 3399 | // } |
| 3400 | // /* last server read and buffer empty : we only check them when we're |
| 3401 | // * allowed to forward the data. |
| 3402 | // */ |
| 3403 | // else if (!(rep->flags & BF_SHUTW) && /* not already done */ |
| 3404 | // ((rep->flags & BF_SHUTW_NOW) || |
| 3405 | // (rep->flags & BF_EMPTY && rep->flags & BF_MAY_FORWARD && |
| 3406 | // rep->flags & BF_SHUTR && !(t->flags & SN_SELF_GEN)))) { |
| 3407 | // buffer_shutw(rep); |
| 3408 | // if (!(req->flags & BF_SHUTR)) { |
| 3409 | // EV_FD_CLR(t->cli_fd, DIR_WR); |
| 3410 | // shutdown(t->cli_fd, SHUT_WR); |
| 3411 | // trace_term(t, TT_HTTP_CLI_4); |
| 3412 | // } else { |
| 3413 | // fd_delete(t->cli_fd); |
| 3414 | // t->cli_state = CL_STCLOSE; |
| 3415 | // trace_term(t, TT_HTTP_CLI_5); |
| 3416 | // } |
| 3417 | // goto update_state; |
| 3418 | // } |
| 3419 | // /* read timeout */ |
| 3420 | // else if ((req->flags & (BF_SHUTR|BF_READ_TIMEOUT)) == BF_READ_TIMEOUT) { |
| 3421 | // buffer_shutr(req); |
| 3422 | // if (!(rep->flags & BF_SHUTW)) { |
| 3423 | // EV_FD_CLR(t->cli_fd, DIR_RD); |
| 3424 | // trace_term(t, TT_HTTP_CLI_6); |
| 3425 | // } else { |
| 3426 | // /* output was already closed */ |
| 3427 | // fd_delete(t->cli_fd); |
| 3428 | // t->cli_state = CL_STCLOSE; |
| 3429 | // trace_term(t, TT_HTTP_CLI_7); |
| 3430 | // } |
| 3431 | // if (!req->analysers) { |
| 3432 | // if (!(t->flags & SN_ERR_MASK)) |
| 3433 | // t->flags |= SN_ERR_CLITO; |
| 3434 | // if (!(t->flags & SN_FINST_MASK)) { |
| 3435 | // if (req->cons->err_type <= SI_ET_QUEUE_ABRT) |
| 3436 | // t->flags |= SN_FINST_Q; |
| 3437 | // else if (req->cons->err_type <= SI_ET_CONN_OTHER) |
| 3438 | // t->flags |= SN_FINST_C; |
| 3439 | // else |
| 3440 | // t->flags |= SN_FINST_D; |
| 3441 | // } |
| 3442 | // } |
| 3443 | // goto update_state; |
| 3444 | // } |
| 3445 | // /* write timeout */ |
| 3446 | // else if ((rep->flags & (BF_SHUTW|BF_WRITE_TIMEOUT)) == BF_WRITE_TIMEOUT) { |
| 3447 | // buffer_shutw(rep); |
| 3448 | // if (!(req->flags & BF_SHUTR)) { |
| 3449 | // EV_FD_CLR(t->cli_fd, DIR_WR); |
| 3450 | // shutdown(t->cli_fd, SHUT_WR); |
| 3451 | // trace_term(t, TT_HTTP_CLI_8); |
| 3452 | // } else { |
| 3453 | // fd_delete(t->cli_fd); |
| 3454 | // t->cli_state = CL_STCLOSE; |
| 3455 | // trace_term(t, TT_HTTP_CLI_9); |
| 3456 | // } |
| 3457 | // if (!req->analysers) { |
| 3458 | // if (!(t->flags & SN_ERR_MASK)) |
| 3459 | // t->flags |= SN_ERR_CLITO; |
| 3460 | // if (!(t->flags & SN_FINST_MASK)) { |
| 3461 | // if (req->cons->err_type <= SI_ET_QUEUE_ABRT) |
| 3462 | // t->flags |= SN_FINST_Q; |
| 3463 | // else if (req->cons->err_type <= SI_ET_CONN_OTHER) |
| 3464 | // t->flags |= SN_FINST_C; |
| 3465 | // else |
| 3466 | // t->flags |= SN_FINST_D; |
| 3467 | // } |
| 3468 | // } |
| 3469 | // goto update_state; |
| 3470 | // } |
| 3471 | // |
| 3472 | // update_timeouts: |
| 3473 | // /* manage read timeout */ |
| 3474 | // if (!(req->flags & BF_SHUTR)) { |
| 3475 | // if (req->flags & BF_FULL) { |
| 3476 | // /* no room to read more data */ |
| 3477 | // if (EV_FD_COND_C(t->cli_fd, DIR_RD)) { |
| 3478 | // /* stop reading until we get some space */ |
| 3479 | // req->rex = TICK_ETERNITY; |
| 3480 | // } |
| 3481 | // } else { |
| 3482 | // EV_FD_COND_S(t->cli_fd, DIR_RD); |
| 3483 | // req->rex = tick_add_ifset(now_ms, t->fe->timeout.client); |
| 3484 | // } |
| 3485 | // } |
| 3486 | // |
| 3487 | // /* manage write timeout */ |
| 3488 | // if (!(rep->flags & BF_SHUTW)) { |
| 3489 | // /* first, we may have to produce data (eg: stats). |
| 3490 | // * right now, this is limited to the SHUTR state. |
| 3491 | // */ |
| 3492 | // if (req->flags & BF_SHUTR && t->flags & SN_SELF_GEN) { |
| 3493 | // produce_content(t); |
| 3494 | // if (rep->flags & BF_EMPTY) { |
| 3495 | // buffer_shutw(rep); |
| 3496 | // fd_delete(t->cli_fd); |
| 3497 | // t->cli_state = CL_STCLOSE; |
| 3498 | // trace_term(t, TT_HTTP_CLI_10); |
| 3499 | // goto update_state; |
| 3500 | // } |
| 3501 | // } |
| 3502 | // |
| 3503 | // /* we don't enable client write if the buffer is empty, nor if the server has to analyze it */ |
| 3504 | // if ((rep->flags & (BF_EMPTY|BF_MAY_FORWARD)) != BF_MAY_FORWARD) { |
| 3505 | // if (EV_FD_COND_C(t->cli_fd, DIR_WR)) { |
| 3506 | // /* stop writing */ |
| 3507 | // rep->wex = TICK_ETERNITY; |
| 3508 | // } |
| 3509 | // } else { |
| 3510 | // /* buffer not empty */ |
| 3511 | // EV_FD_COND_S(t->cli_fd, DIR_WR); |
| 3512 | // if (!tick_isset(rep->wex)) { |
| 3513 | // /* restart writing */ |
| 3514 | // rep->wex = tick_add_ifset(now_ms, t->fe->timeout.client); |
| 3515 | // if (!(req->flags & BF_SHUTR) && tick_isset(rep->wex) && tick_isset(req->rex)) { |
| 3516 | // /* FIXME: to prevent the client from expiring read timeouts during writes, |
| 3517 | // * we refresh it, except if it was already infinite. */ |
| 3518 | // req->rex = rep->wex; |
| 3519 | // } |
| 3520 | // } |
| 3521 | // } |
| 3522 | // } |
| 3523 | // return 0; /* other cases change nothing */ |
| 3524 | // } |
| 3525 | // else if (t->cli_state == CL_STCLOSE) { /* CL_STCLOSE: nothing to do */ |
| 3526 | // if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 3527 | // int len; |
| 3528 | // len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)req->cons->fd); |
| 3529 | // write(1, trash, len); |
| 3530 | // } |
| 3531 | // return 0; |
| 3532 | // } |
| 3533 | //#ifdef DEBUG_DEV |
| 3534 | // fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, t->cli_state); |
| 3535 | // ABORT_NOW(); |
| 3536 | //#endif |
| 3537 | // return 0; |
| 3538 | //} |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3539 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3540 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3541 | /* Return 1 if the pending connection has failed and should be retried, |
| 3542 | * otherwise zero. We may only come here in SI_ST_CON state, which means that |
| 3543 | * the socket's file descriptor is known. |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3544 | */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3545 | int tcp_connection_status(struct session *t) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3546 | { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3547 | struct buffer *req = t->req; |
| 3548 | struct buffer *rep = t->rep; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3549 | int conn_err = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3550 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3551 | DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n", |
| 3552 | now_ms, __FUNCTION__, |
| 3553 | cli_stnames[t->cli_state], |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3554 | rep->rex, req->wex, |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 3555 | req->flags, rep->flags, |
| 3556 | req->l, rep->l); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3557 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3558 | if ((req->flags & BF_SHUTW_NOW) || |
| 3559 | (rep->flags & BF_SHUTW) || |
| 3560 | ((req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */ |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 3561 | ((req->flags & BF_EMPTY && !(req->flags & BF_WRITE_ACTIVITY)) || |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3562 | t->be->options & PR_O_ABRT_CLOSE))) { /* give up */ |
| 3563 | |
| 3564 | trace_term(t, TT_HTTP_SRV_5); |
| 3565 | req->wex = TICK_ETERNITY; |
| 3566 | fd_delete(req->cons->fd); |
| 3567 | if (t->srv) { |
| 3568 | t->srv->cur_sess--; |
| 3569 | sess_change_server(t, NULL); |
| 3570 | } |
| 3571 | /* note that this must not return any error because it would be able to |
| 3572 | * overwrite the client_retnclose() output. |
| 3573 | */ |
| 3574 | //srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL); |
| 3575 | |
| 3576 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3577 | buffer_shutw(req); |
| 3578 | buffer_shutr(rep); |
| 3579 | req->cons->state = SI_ST_CLO; |
| 3580 | if (!req->cons->err_type) |
| 3581 | req->cons->err_type = SI_ET_CONN_ABRT; |
| 3582 | req->cons->err_loc = t->srv; |
| 3583 | return 0; |
| 3584 | } |
| 3585 | |
| 3586 | /* check for timeouts and asynchronous connect errors */ |
| 3587 | if (fdtab[req->cons->fd].state == FD_STERROR) { |
| 3588 | conn_err = SI_ET_CONN_ERR; |
| 3589 | if (!req->cons->err_type) |
| 3590 | req->cons->err_type = SI_ET_CONN_ERR; |
| 3591 | } |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 3592 | else if (!(req->flags & BF_WRITE_ACTIVITY)) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3593 | /* nothing happened, maybe we timed out */ |
| 3594 | if (tick_is_expired(req->wex, now_ms)) { |
| 3595 | conn_err = SI_ET_CONN_TO; |
| 3596 | if (!req->cons->err_type) |
| 3597 | req->cons->err_type = SI_ET_CONN_TO; |
| 3598 | } |
| 3599 | else |
| 3600 | return 0; /* let's wait a bit more */ |
| 3601 | } |
| 3602 | |
| 3603 | if (conn_err) { |
| 3604 | fd_delete(req->cons->fd); |
| 3605 | req->cons->state = SI_ST_CLO; |
| 3606 | |
| 3607 | if (t->srv) { |
| 3608 | t->srv->cur_sess--; |
| 3609 | sess_change_server(t, NULL); |
| 3610 | req->cons->err_loc = t->srv; |
| 3611 | } |
| 3612 | |
| 3613 | /* ensure that we have enough retries left */ |
| 3614 | if (srv_count_retry_down(t, conn_err)) |
| 3615 | return 0; |
| 3616 | |
| 3617 | if (conn_err == SI_ET_CONN_ERR) { |
| 3618 | /* we encountered an immediate connection error, and we |
| 3619 | * will have to retry connecting to the same server, most |
| 3620 | * likely leading to the same result. To avoid this, we |
| 3621 | * fake a connection timeout to retry after a turn-around |
| 3622 | * time of 1 second. We will wait in the previous if block. |
| 3623 | */ |
| 3624 | req->cons->state = SI_ST_TAR; |
Willy Tarreau | 3537467 | 2008-09-03 18:11:02 +0200 | [diff] [blame] | 3625 | req->cons->exp = tick_add(now_ms, MS_TO_TICKS(1000)); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3626 | return 0; |
| 3627 | } |
| 3628 | |
| 3629 | if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) { |
| 3630 | /* We're on our last chance, and the REDISP option was specified. |
| 3631 | * We will ignore cookie and force to balance or use the dispatcher. |
| 3632 | */ |
| 3633 | /* let's try to offer this slot to anybody */ |
| 3634 | if (may_dequeue_tasks(t->srv, t->be)) |
| 3635 | process_srv_queue(t->srv); |
| 3636 | |
| 3637 | /* it's left to the dispatcher to choose a server */ |
| 3638 | t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
| 3639 | t->prev_srv = t->srv; |
| 3640 | } else { |
| 3641 | /* we just want to retry */ |
| 3642 | if (t->srv) |
| 3643 | t->srv->retries++; |
| 3644 | t->be->retries++; |
| 3645 | |
| 3646 | /* Now we will try to either reconnect to the same server or |
| 3647 | * connect to another server. If the connection gets queued |
| 3648 | * because all servers are saturated, then we will go back to |
| 3649 | * the idle state where the buffer's consumer is marked as |
| 3650 | * unknown. |
| 3651 | */ |
| 3652 | if (srv_retryable_connect(t)) { |
| 3653 | /* success or unrecoverable error */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3654 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3655 | return 0; |
| 3656 | } |
| 3657 | } |
| 3658 | |
| 3659 | /* We'll rely on the caller to try to get a connection again */ |
| 3660 | return 1; |
| 3661 | } |
| 3662 | else { |
| 3663 | /* no error and write OK : connection succeeded */ |
| 3664 | t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 3665 | req->cons->state = SI_ST_EST; |
| 3666 | req->cons->err_type = SI_ET_NONE; |
| 3667 | req->cons->err_loc = NULL; |
| 3668 | |
| 3669 | if (req->flags & BF_EMPTY) { |
| 3670 | EV_FD_CLR(req->cons->fd, DIR_WR); |
| 3671 | req->wex = TICK_ETERNITY; |
| 3672 | } else { |
| 3673 | EV_FD_SET(req->cons->fd, DIR_WR); |
| 3674 | req->wex = tick_add_ifset(now_ms, t->be->timeout.server); |
| 3675 | if (tick_isset(req->wex)) { |
| 3676 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 3677 | * we refresh it. */ |
| 3678 | rep->rex = req->wex; |
| 3679 | } |
| 3680 | } |
| 3681 | |
| 3682 | if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */ |
| 3683 | if (!(rep->flags & BF_HIJACK)) { |
| 3684 | EV_FD_SET(req->cons->fd, DIR_RD); |
| 3685 | rep->rex = tick_add_ifset(now_ms, t->be->timeout.server); |
| 3686 | } |
| 3687 | buffer_set_rlim(rep, BUFSIZE); /* no rewrite needed */ |
| 3688 | |
| 3689 | /* if the user wants to log as soon as possible, without counting |
| 3690 | bytes from the server, then this is the right moment. */ |
| 3691 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 3692 | t->logs.t_close = t->logs.t_connect; /* to get a valid end date */ |
| 3693 | tcp_sess_log(t); |
| 3694 | } |
| 3695 | #ifdef CONFIG_HAP_TCPSPLICE |
| 3696 | if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
| 3697 | /* TCP splicing supported by both FE and BE */ |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 3698 | tcp_splice_splicefd(req->prod->fd, req->cons->fd, 0); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3699 | } |
| 3700 | #endif |
| 3701 | } |
| 3702 | else { |
| 3703 | rep->analysers |= AN_RTR_HTTP_HDR; |
| 3704 | buffer_set_rlim(rep, BUFSIZE - MAXREWRITE); /* rewrite needed */ |
| 3705 | t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE; |
| 3706 | /* reset hdr_idx which was already initialized by the request. |
| 3707 | * right now, the http parser does it. |
| 3708 | * hdr_idx_init(&t->txn.hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3709 | */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3710 | } |
| 3711 | |
Willy Tarreau | 9a2d154 | 2008-08-30 12:31:07 +0200 | [diff] [blame] | 3712 | rep->flags |= BF_READ_ATTACHED; /* producer is now attached */ |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3713 | req->wex = TICK_ETERNITY; |
| 3714 | return 0; |
| 3715 | } |
| 3716 | } |
| 3717 | |
| 3718 | |
| 3719 | /* |
| 3720 | * This function tries to assign a server to a stream_sock interface. |
| 3721 | * It may be called only for t->req->cons->state = one of { SI_ST_INI, |
| 3722 | * SI_ST_TAR, SI_ST_QUE }. It returns one of those states, SI_ST_ASS |
| 3723 | * in case of success, or SI_ST_CLO in case of failure. It returns 1 if |
| 3724 | * it returns SI_ST_ASS, otherwise zero. |
| 3725 | */ |
| 3726 | int stream_sock_assign_server(struct session *t) |
| 3727 | { |
| 3728 | DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n", |
| 3729 | now_ms, __FUNCTION__, |
| 3730 | cli_stnames[t->cli_state], |
| 3731 | t->rep->rex, t->req->wex, |
| 3732 | t->req->flags, t->rep->flags, |
| 3733 | t->req->l, t->rep->l); |
| 3734 | |
| 3735 | if (t->req->cons->state == SI_ST_TAR) { |
| 3736 | /* connection might be aborted */ |
| 3737 | if ((t->req->flags & BF_SHUTW_NOW) || |
| 3738 | (t->rep->flags & BF_SHUTW) || |
| 3739 | ((t->req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */ |
| 3740 | (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3741 | |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3742 | trace_term(t, TT_HTTP_SRV_1); |
Willy Tarreau | 3537467 | 2008-09-03 18:11:02 +0200 | [diff] [blame] | 3743 | t->req->cons->exp = TICK_ETERNITY; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3744 | |
| 3745 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3746 | buffer_shutr(t->rep); |
| 3747 | buffer_shutw(t->req); |
| 3748 | if (!t->req->cons->err_type) |
| 3749 | t->req->cons->err_type = SI_ET_CONN_ABRT; |
| 3750 | t->req->cons->state = SI_ST_CLO; |
| 3751 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3752 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3753 | |
Willy Tarreau | 3537467 | 2008-09-03 18:11:02 +0200 | [diff] [blame] | 3754 | if (!tick_is_expired(t->req->cons->exp, now_ms)) |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3755 | return 0; /* still in turn-around */ |
| 3756 | |
| 3757 | t->req->cons->state = SI_ST_INI; |
Willy Tarreau | 3537467 | 2008-09-03 18:11:02 +0200 | [diff] [blame] | 3758 | t->req->cons->exp = TICK_ETERNITY; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3759 | } |
| 3760 | else if (t->req->cons->state == SI_ST_QUE) { |
| 3761 | if (t->pend_pos) { |
| 3762 | /* request still in queue... */ |
Willy Tarreau | 3537467 | 2008-09-03 18:11:02 +0200 | [diff] [blame] | 3763 | if (tick_is_expired(t->req->cons->exp, now_ms)) { |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3764 | /* ... and timeout expired */ |
| 3765 | trace_term(t, TT_HTTP_SRV_3); |
Willy Tarreau | 3537467 | 2008-09-03 18:11:02 +0200 | [diff] [blame] | 3766 | t->req->cons->exp = TICK_ETERNITY; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3767 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3768 | if (t->srv) |
| 3769 | t->srv->failed_conns++; |
| 3770 | t->be->failed_conns++; |
| 3771 | |
| 3772 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3773 | buffer_shutr(t->rep); |
| 3774 | buffer_shutw(t->req); |
| 3775 | t->req->flags |= BF_WRITE_TIMEOUT; |
| 3776 | if (!t->req->cons->err_type) |
| 3777 | t->req->cons->err_type = SI_ET_QUEUE_TO; |
| 3778 | t->req->cons->state = SI_ST_CLO; |
| 3779 | return 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3780 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3781 | /* connection remains in queue, check if we have to abort it */ |
| 3782 | if ((t->req->flags & BF_SHUTW_NOW) || |
| 3783 | (t->rep->flags & BF_SHUTW) || |
| 3784 | ((t->req->flags & BF_SHUTR) && /* FIXME: this should not prevent a connection from establishing */ |
| 3785 | (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { |
| 3786 | /* give up */ |
| 3787 | trace_term(t, TT_HTTP_SRV_1); |
Willy Tarreau | 3537467 | 2008-09-03 18:11:02 +0200 | [diff] [blame] | 3788 | t->req->cons->exp = TICK_ETERNITY; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3789 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3790 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3791 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3792 | buffer_shutr(t->rep); |
| 3793 | buffer_shutw(t->req); |
| 3794 | if (!t->req->cons->err_type) |
| 3795 | t->req->cons->err_type = SI_ET_QUEUE_ABRT; |
| 3796 | t->req->cons->state = SI_ST_CLO; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3797 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3798 | return 0; |
| 3799 | } |
| 3800 | /* The connection is not in the queue anymore */ |
| 3801 | t->req->cons->state = SI_ST_INI; |
Willy Tarreau | 3537467 | 2008-09-03 18:11:02 +0200 | [diff] [blame] | 3802 | t->req->cons->exp = TICK_ETERNITY; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3803 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3804 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3805 | /* we may get here from above */ |
| 3806 | if (t->req->cons->state == SI_ST_INI) { |
| 3807 | /* no connection in progress, we have to get a new one */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3808 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3809 | /* first, check if the connection has been aborted */ |
| 3810 | if ((t->req->flags & BF_SHUTW_NOW) || |
| 3811 | (t->rep->flags & BF_SHUTW) || |
| 3812 | ((t->req->flags & BF_SHUTR) && |
| 3813 | (t->req->flags & BF_EMPTY || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3814 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3815 | trace_term(t, TT_HTTP_SRV_1); |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3816 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3817 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3818 | buffer_shutr(t->rep); |
| 3819 | buffer_shutw(t->req); |
| 3820 | if (!t->req->cons->err_type) |
| 3821 | t->req->cons->err_type = SI_ET_CONN_ABRT; |
| 3822 | t->req->cons->state = SI_ST_CLO; |
| 3823 | return 0; |
| 3824 | } |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3825 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3826 | /* try to get a server assigned */ |
| 3827 | if (srv_redispatch_connect(t) != 0) { |
| 3828 | /* we did not get any server, let's check the cause */ |
| 3829 | if (t->req->cons->state == SI_ST_QUE) { |
| 3830 | /* the connection was queued, that's OK */ |
| 3831 | return 0; |
| 3832 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3833 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3834 | trace_term(t, TT_HTTP_SRV_2); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3835 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3836 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3837 | buffer_shutr(t->rep); |
| 3838 | buffer_shutw(t->req); |
| 3839 | t->req->flags |= BF_WRITE_ERROR; |
| 3840 | if (!t->req->cons->err_type) |
| 3841 | t->req->cons->err_type = SI_ET_CONN_OTHER; |
| 3842 | t->req->cons->state = SI_ST_CLO; |
| 3843 | return 0; |
| 3844 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3845 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3846 | t->req->cons->state = SI_ST_ASS; |
| 3847 | /* Once the server is assigned, we have to return because |
| 3848 | * the caller might be interested in checking several |
| 3849 | * things before connecting. |
| 3850 | */ |
| 3851 | return 1; |
| 3852 | } |
| 3853 | return 0; |
| 3854 | } |
Krzysztof Piotr Oledzki | efe3b6f | 2008-05-23 23:49:32 +0200 | [diff] [blame] | 3855 | |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 3856 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3857 | /* |
| 3858 | * This function tries to establish a connection to an assigned server. It also |
| 3859 | * performs connection retries. It may only be called with t->req->cons->state |
| 3860 | * in { SI_ST_ASS, SI_ST_CON }. It may also set the state to SI_ST_INI, |
| 3861 | * SI_ST_EST, or SI_ST_CLO. |
| 3862 | */ |
| 3863 | int stream_sock_connect_server(struct session *t) |
| 3864 | { |
| 3865 | if (t->req->cons->state == SI_ST_ASS) { |
| 3866 | /* server assigned to request, we have to try to connect now */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3867 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3868 | if (!srv_retryable_connect(t)) { |
| 3869 | /* we need to redispatch */ |
| 3870 | t->req->cons->state = SI_ST_INI; |
| 3871 | return 0; |
| 3872 | } |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame] | 3873 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3874 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 3875 | if (t->req->cons->state != SI_ST_CON) { |
| 3876 | /* it was an error */ |
| 3877 | trace_term(t, TT_HTTP_SRV_4); |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3878 | |
| 3879 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3880 | buffer_shutr(t->rep); |
| 3881 | buffer_shutw(t->req); |
| 3882 | t->req->flags |= BF_WRITE_ERROR; |
| 3883 | if (!t->req->cons->err_type) |
| 3884 | t->req->cons->err_type = SI_ET_CONN_OTHER; |
| 3885 | t->req->cons->state = SI_ST_CLO; |
| 3886 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3887 | } |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3888 | /* We have a socket and switched to SI_ST_CON */ |
| 3889 | } |
| 3890 | |
| 3891 | /* we may also get here from above */ |
| 3892 | if (t->req->cons->state == SI_ST_CON) { |
| 3893 | /* connection in progress or just completed */ |
| 3894 | if (!tcp_connection_status(t)) |
| 3895 | return 0; |
| 3896 | } |
| 3897 | return 0; |
| 3898 | } |
| 3899 | |
| 3900 | |
| 3901 | /* |
| 3902 | * Tries to establish a connection to the server and associate it to the |
| 3903 | * request buffer's consumer side. It is assumed that this function will not be |
Willy Tarreau | 3da77c5 | 2008-08-29 09:58:42 +0200 | [diff] [blame] | 3904 | * be called with SI_ST_EST nor with BF_WRITE_ENA cleared. It normally |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3905 | * returns zero, but may return 1 if it absolutely wants to be called again. |
| 3906 | */ |
| 3907 | int process_srv_conn(struct session *t) |
| 3908 | { |
| 3909 | DPRINTF(stderr,"[%u] %s: c=%s exp(r,w)=%u,%u req=%08x rep=%08x rql=%d rpl=%d\n", |
| 3910 | now_ms, __FUNCTION__, |
| 3911 | cli_stnames[t->cli_state], |
| 3912 | t->rep->rex, t->req->wex, |
| 3913 | t->req->flags, t->rep->flags, |
| 3914 | t->req->l, t->rep->l); |
| 3915 | |
| 3916 | do { |
| 3917 | if (t->req->cons->state == SI_ST_INI || |
| 3918 | t->req->cons->state == SI_ST_TAR || |
| 3919 | t->req->cons->state == SI_ST_QUE) { |
| 3920 | /* try to assign a server */ |
| 3921 | if (!stream_sock_assign_server(t)) |
| 3922 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3923 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3924 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3925 | if (t->req->cons->state == SI_ST_ASS && |
| 3926 | t->srv && t->srv->rdr_len && t->flags & SN_REDIRECTABLE) { |
| 3927 | /* Server supporting redirection and it is possible. |
| 3928 | * Invalid requests are reported as such. It concerns all |
| 3929 | * the largest ones. |
| 3930 | */ |
| 3931 | struct http_txn *txn = &t->txn; |
| 3932 | struct chunk rdr; |
| 3933 | char *path; |
| 3934 | int len; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3935 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3936 | /* 1: create the response header */ |
| 3937 | rdr.len = strlen(HTTP_302); |
| 3938 | rdr.str = trash; |
| 3939 | memcpy(rdr.str, HTTP_302, rdr.len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3940 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3941 | /* 2: add the server's prefix */ |
| 3942 | if (rdr.len + t->srv->rdr_len > sizeof(trash)) |
| 3943 | goto cancel_redir; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3944 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3945 | memcpy(rdr.str + rdr.len, t->srv->rdr_pfx, t->srv->rdr_len); |
| 3946 | rdr.len += t->srv->rdr_len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3947 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3948 | /* 3: add the request URI */ |
| 3949 | path = http_get_path(txn); |
| 3950 | if (!path) |
| 3951 | goto cancel_redir; |
| 3952 | len = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path; |
| 3953 | if (rdr.len + len > sizeof(trash) - 4) /* 4 for CRLF-CRLF */ |
| 3954 | goto cancel_redir; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3955 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3956 | memcpy(rdr.str + rdr.len, path, len); |
| 3957 | rdr.len += len; |
| 3958 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 3959 | rdr.len += 4; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3960 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3961 | srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, 302, &rdr); |
| 3962 | trace_term(t, TT_HTTP_SRV_3); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3963 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3964 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
| 3965 | if (t->srv) |
| 3966 | t->srv->cum_sess++; |
| 3967 | |
| 3968 | t->req->cons->state = SI_ST_CLO; |
| 3969 | return 0; |
| 3970 | cancel_redir: |
| 3971 | //txn->status = 400; |
| 3972 | //t->fe->failed_req++; |
| 3973 | //srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_C, |
| 3974 | // 400, error_message(t, HTTP_ERR_400)); |
| 3975 | trace_term(t, TT_HTTP_SRV_4); |
| 3976 | |
| 3977 | // FIXME: should we set rep->MAY_FORWARD ? |
| 3978 | buffer_shutw(t->req); |
| 3979 | buffer_shutr(t->rep); |
| 3980 | if (!t->req->cons->err_type) |
| 3981 | t->req->cons->err_type = SI_ET_CONN_OTHER; |
| 3982 | t->req->cons->state = SI_ST_CLO; |
| 3983 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3984 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3985 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3986 | if (t->req->cons->state == SI_ST_CON || |
| 3987 | t->req->cons->state == SI_ST_ASS) { |
| 3988 | stream_sock_connect_server(t); |
| 3989 | } |
| 3990 | } while (t->req->cons->state != SI_ST_CLO && |
| 3991 | t->req->cons->state != SI_ST_CON && |
| 3992 | t->req->cons->state != SI_ST_EST); |
| 3993 | return 0; |
| 3994 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3995 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3996 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3997 | /* |
| 3998 | * 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] | 3999 | * called with client socket shut down on input. Right now, only statistics can |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 4000 | * be produced. It stops by itself by unsetting the BF_HIJACK flag from the |
| 4001 | * buffer, which it uses to keep on being called when there is free space in |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4002 | * 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] | 4003 | * when it wants to stop sending data, otherwise 0. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4004 | */ |
| 4005 | int produce_content(struct session *s) |
| 4006 | { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4007 | if (s->data_source == DATA_SRC_NONE) { |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 4008 | buffer_stop_hijack(s->rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4009 | return 1; |
| 4010 | } |
| 4011 | else if (s->data_source == DATA_SRC_STATS) { |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 4012 | /* dump server statistics */ |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 4013 | int ret = stats_dump_http(s, s->be->uri_auth); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 4014 | if (ret >= 0) |
| 4015 | return ret; |
| 4016 | /* -1 indicates an error */ |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 4017 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4018 | |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 4019 | /* unknown data source or internal error */ |
| 4020 | s->txn.status = 500; |
| 4021 | client_retnclose(s, error_message(s, HTTP_ERR_500)); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 4022 | trace_term(s, TT_HTTP_CNT_1); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 4023 | if (!(s->flags & SN_ERR_MASK)) |
| 4024 | s->flags |= SN_ERR_PRXCOND; |
| 4025 | if (!(s->flags & SN_FINST_MASK)) |
| 4026 | s->flags |= SN_FINST_R; |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 4027 | buffer_stop_hijack(s->rep); |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 4028 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4029 | } |
| 4030 | |
| 4031 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4032 | /* Iterate the same filter through all request headers. |
| 4033 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4034 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 4035 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4036 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4037 | 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] | 4038 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4039 | char term; |
| 4040 | char *cur_ptr, *cur_end, *cur_next; |
| 4041 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4042 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4043 | struct hdr_idx_elem *cur_hdr; |
| 4044 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 4045 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4046 | last_hdr = 0; |
| 4047 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4048 | 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] | 4049 | old_idx = 0; |
| 4050 | |
| 4051 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4052 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4053 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4054 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4055 | (exp->action == ACT_ALLOW || |
| 4056 | exp->action == ACT_DENY || |
| 4057 | exp->action == ACT_TARPIT)) |
| 4058 | return 0; |
| 4059 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4060 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4061 | if (!cur_idx) |
| 4062 | break; |
| 4063 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4064 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4065 | cur_ptr = cur_next; |
| 4066 | cur_end = cur_ptr + cur_hdr->len; |
| 4067 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4068 | |
| 4069 | /* Now we have one header between cur_ptr and cur_end, |
| 4070 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4071 | */ |
| 4072 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4073 | /* The annoying part is that pattern matching needs |
| 4074 | * that we modify the contents to null-terminate all |
| 4075 | * strings before testing them. |
| 4076 | */ |
| 4077 | |
| 4078 | term = *cur_end; |
| 4079 | *cur_end = '\0'; |
| 4080 | |
| 4081 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4082 | switch (exp->action) { |
| 4083 | case ACT_SETBE: |
| 4084 | /* It is not possible to jump a second time. |
| 4085 | * FIXME: should we return an HTTP/500 here so that |
| 4086 | * the admin knows there's a problem ? |
| 4087 | */ |
| 4088 | if (t->be != t->fe) |
| 4089 | break; |
| 4090 | |
| 4091 | /* Swithing Proxy */ |
| 4092 | t->be = (struct proxy *) exp->replace; |
| 4093 | |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 4094 | /* right now, the backend switch is not overly complicated |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4095 | * because we have associated req_cap and rsp_cap to the |
| 4096 | * frontend, and the beconn will be updated later. |
| 4097 | */ |
| 4098 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 4099 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 4100 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 4101 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4102 | last_hdr = 1; |
| 4103 | break; |
| 4104 | |
| 4105 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4106 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4107 | last_hdr = 1; |
| 4108 | break; |
| 4109 | |
| 4110 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4111 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4112 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4113 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4114 | break; |
| 4115 | |
| 4116 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4117 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4118 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4119 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4120 | break; |
| 4121 | |
| 4122 | case ACT_REPLACE: |
| 4123 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4124 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 4125 | /* FIXME: if the user adds a newline in the replacement, the |
| 4126 | * index will not be recalculated for now, and the new line |
| 4127 | * will not be counted as a new header. |
| 4128 | */ |
| 4129 | |
| 4130 | cur_end += delta; |
| 4131 | cur_next += delta; |
| 4132 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4133 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4134 | break; |
| 4135 | |
| 4136 | case ACT_REMOVE: |
| 4137 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 4138 | cur_next += delta; |
| 4139 | |
| 4140 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4141 | txn->req.eoh += delta; |
| 4142 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4143 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4144 | cur_hdr->len = 0; |
| 4145 | cur_end = NULL; /* null-term has been rewritten */ |
| 4146 | break; |
| 4147 | |
| 4148 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4149 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4150 | if (cur_end) |
| 4151 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4152 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4153 | /* keep the link from this header to next one in case of later |
| 4154 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4155 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4156 | old_idx = cur_idx; |
| 4157 | } |
| 4158 | return 0; |
| 4159 | } |
| 4160 | |
| 4161 | |
| 4162 | /* Apply the filter to the request line. |
| 4163 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 4164 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4165 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 4166 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4167 | */ |
| 4168 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 4169 | { |
| 4170 | char term; |
| 4171 | char *cur_ptr, *cur_end; |
| 4172 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4173 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4174 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4175 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4176 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4177 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4178 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4179 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4180 | (exp->action == ACT_ALLOW || |
| 4181 | exp->action == ACT_DENY || |
| 4182 | exp->action == ACT_TARPIT)) |
| 4183 | return 0; |
| 4184 | else if (exp->action == ACT_REMOVE) |
| 4185 | return 0; |
| 4186 | |
| 4187 | done = 0; |
| 4188 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4189 | 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] | 4190 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4191 | |
| 4192 | /* Now we have the request line between cur_ptr and cur_end */ |
| 4193 | |
| 4194 | /* The annoying part is that pattern matching needs |
| 4195 | * that we modify the contents to null-terminate all |
| 4196 | * strings before testing them. |
| 4197 | */ |
| 4198 | |
| 4199 | term = *cur_end; |
| 4200 | *cur_end = '\0'; |
| 4201 | |
| 4202 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4203 | switch (exp->action) { |
| 4204 | case ACT_SETBE: |
| 4205 | /* It is not possible to jump a second time. |
| 4206 | * FIXME: should we return an HTTP/500 here so that |
| 4207 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4208 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4209 | if (t->be != t->fe) |
| 4210 | break; |
| 4211 | |
| 4212 | /* Swithing Proxy */ |
| 4213 | t->be = (struct proxy *) exp->replace; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4214 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4215 | /* right now, the backend switch is not too much complicated |
| 4216 | * because we have associated req_cap and rsp_cap to the |
| 4217 | * frontend, and the beconn will be updated later. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4218 | */ |
| 4219 | |
Willy Tarreau | d7c30f9 | 2007-12-03 01:38:36 +0100 | [diff] [blame] | 4220 | t->rep->rto = t->req->wto = t->be->timeout.server; |
| 4221 | t->req->cto = t->be->timeout.connect; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 4222 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4223 | done = 1; |
| 4224 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4225 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4226 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4227 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4228 | done = 1; |
| 4229 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4230 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4231 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4232 | txn->flags |= TX_CLDENY; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4233 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4234 | done = 1; |
| 4235 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4236 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4237 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4238 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4239 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4240 | done = 1; |
| 4241 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4242 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4243 | case ACT_REPLACE: |
| 4244 | *cur_end = term; /* restore the string terminator */ |
| 4245 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4246 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 4247 | /* FIXME: if the user adds a newline in the replacement, the |
| 4248 | * index will not be recalculated for now, and the new line |
| 4249 | * will not be counted as a new header. |
| 4250 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4251 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4252 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4253 | cur_end += delta; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4254 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4255 | 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] | 4256 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4257 | HTTP_MSG_RQMETH, |
| 4258 | cur_ptr, cur_end + 1, |
| 4259 | NULL, NULL); |
| 4260 | if (unlikely(!cur_end)) |
| 4261 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4262 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4263 | /* we have a full request and we know that we have either a CR |
| 4264 | * or an LF at <ptr>. |
| 4265 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4266 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 4267 | 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] | 4268 | /* there is no point trying this regex on headers */ |
| 4269 | return 1; |
| 4270 | } |
| 4271 | } |
| 4272 | *cur_end = term; /* restore the string terminator */ |
| 4273 | return done; |
| 4274 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 4275 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4276 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4277 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4278 | /* |
| 4279 | * Apply all the req filters <exp> to all headers in buffer <req> of session <t>. |
| 4280 | * 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] | 4281 | * unparsable request. Since it can manage the switch to another backend, it |
| 4282 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4283 | */ |
| 4284 | int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 4285 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4286 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4287 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4288 | while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4289 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4290 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4291 | /* |
| 4292 | * The interleaving of transformations and verdicts |
| 4293 | * makes it difficult to decide to continue or stop |
| 4294 | * the evaluation. |
| 4295 | */ |
| 4296 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4297 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4298 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 4299 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) { |
| 4300 | exp = exp->next; |
| 4301 | continue; |
| 4302 | } |
| 4303 | |
| 4304 | /* Apply the filter to the request line. */ |
| 4305 | ret = apply_filter_to_req_line(t, req, exp); |
| 4306 | if (unlikely(ret < 0)) |
| 4307 | return -1; |
| 4308 | |
| 4309 | if (likely(ret == 0)) { |
| 4310 | /* The filter did not match the request, it can be |
| 4311 | * iterated through all headers. |
| 4312 | */ |
| 4313 | apply_filter_to_req_headers(t, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4314 | } |
| 4315 | exp = exp->next; |
| 4316 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4317 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4318 | } |
| 4319 | |
| 4320 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4321 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4322 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 4323 | * Manage client-side cookie. It can impact performance by about 2% so it is |
| 4324 | * desirable to call it only when needed. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4325 | */ |
| 4326 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 4327 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4328 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4329 | char *p1, *p2, *p3, *p4; |
| 4330 | char *del_colon, *del_cookie, *colon; |
| 4331 | int app_cookies; |
| 4332 | |
| 4333 | appsess *asession_temp = NULL; |
| 4334 | appsess local_asession; |
| 4335 | |
| 4336 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4337 | int cur_idx, old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4338 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4339 | /* Iterate through the headers. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4340 | * we start with the start line. |
| 4341 | */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 4342 | old_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4343 | 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] | 4344 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4345 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4346 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4347 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4348 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4349 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4350 | cur_ptr = cur_next; |
| 4351 | cur_end = cur_ptr + cur_hdr->len; |
| 4352 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4353 | |
| 4354 | /* We have one full header between cur_ptr and cur_end, and the |
| 4355 | * next header starts at cur_next. We're only interested in |
| 4356 | * "Cookie:" headers. |
| 4357 | */ |
| 4358 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4359 | val = http_header_match2(cur_ptr, cur_end, "Cookie", 6); |
| 4360 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4361 | old_idx = cur_idx; |
| 4362 | continue; |
| 4363 | } |
| 4364 | |
| 4365 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 4366 | * attributes whose name begin with a '$', and associate them with |
| 4367 | * the right cookie, if we want to delete this cookie. |
| 4368 | * So there are 3 cases for each cookie read : |
| 4369 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 4370 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 4371 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 4372 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 4373 | * "special" cookie. |
| 4374 | * At the end of loop, if a "special" cookie remains, we may have to |
| 4375 | * remove it. If no application cookie persists in the header, we |
| 4376 | * *MUST* delete it |
| 4377 | */ |
| 4378 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4379 | colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4380 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4381 | /* del_cookie == NULL => nothing to be deleted */ |
| 4382 | del_colon = del_cookie = NULL; |
| 4383 | app_cookies = 0; |
| 4384 | |
| 4385 | while (p1 < cur_end) { |
| 4386 | /* skip spaces and colons, but keep an eye on these ones */ |
| 4387 | while (p1 < cur_end) { |
| 4388 | if (*p1 == ';' || *p1 == ',') |
| 4389 | colon = p1; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4390 | else if (!isspace((unsigned char)*p1)) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4391 | break; |
| 4392 | p1++; |
| 4393 | } |
| 4394 | |
| 4395 | if (p1 == cur_end) |
| 4396 | break; |
| 4397 | |
| 4398 | /* p1 is at the beginning of the cookie name */ |
| 4399 | p2 = p1; |
| 4400 | while (p2 < cur_end && *p2 != '=') |
| 4401 | p2++; |
| 4402 | |
| 4403 | if (p2 == cur_end) |
| 4404 | break; |
| 4405 | |
| 4406 | p3 = p2 + 1; /* skips the '=' sign */ |
| 4407 | if (p3 == cur_end) |
| 4408 | break; |
| 4409 | |
| 4410 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4411 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4412 | p4++; |
| 4413 | |
| 4414 | /* here, we have the cookie name between p1 and p2, |
| 4415 | * and its value between p3 and p4. |
| 4416 | * we can process it : |
| 4417 | * |
| 4418 | * Cookie: NAME=VALUE; |
| 4419 | * | || || | |
| 4420 | * | || || +--> p4 |
| 4421 | * | || |+-------> p3 |
| 4422 | * | || +--------> p2 |
| 4423 | * | |+------------> p1 |
| 4424 | * | +-------------> colon |
| 4425 | * +--------------------> cur_ptr |
| 4426 | */ |
| 4427 | |
| 4428 | if (*p1 == '$') { |
| 4429 | /* skip this one */ |
| 4430 | } |
| 4431 | else { |
| 4432 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4433 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4434 | txn->cli_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4435 | (p4 - p1 >= t->fe->capture_namelen) && |
| 4436 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4437 | int log_len = p4 - p1; |
| 4438 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 4439 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4440 | Alert("HTTP logging : out of memory.\n"); |
| 4441 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4442 | if (log_len > t->fe->capture_len) |
| 4443 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4444 | memcpy(txn->cli_cookie, p1, log_len); |
| 4445 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4446 | } |
| 4447 | } |
| 4448 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4449 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 4450 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4451 | /* Cool... it's the right one */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4452 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4453 | char *delim; |
| 4454 | |
| 4455 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 4456 | * have the server ID betweek p3 and delim, and the original cookie between |
| 4457 | * delim+1 and p4. Otherwise, delim==p4 : |
| 4458 | * |
| 4459 | * Cookie: NAME=SRV~VALUE; |
| 4460 | * | || || | | |
| 4461 | * | || || | +--> p4 |
| 4462 | * | || || +--------> delim |
| 4463 | * | || |+-----------> p3 |
| 4464 | * | || +------------> p2 |
| 4465 | * | |+----------------> p1 |
| 4466 | * | +-----------------> colon |
| 4467 | * +------------------------> cur_ptr |
| 4468 | */ |
| 4469 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4470 | if (t->be->options & PR_O_COOK_PFX) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4471 | for (delim = p3; delim < p4; delim++) |
| 4472 | if (*delim == COOKIE_DELIM) |
| 4473 | break; |
| 4474 | } |
| 4475 | else |
| 4476 | delim = p4; |
| 4477 | |
| 4478 | |
| 4479 | /* Here, we'll look for the first running server which supports the cookie. |
| 4480 | * This allows to share a same cookie between several servers, for example |
| 4481 | * to dedicate backup servers to specific servers only. |
| 4482 | * However, to prevent clients from sticking to cookie-less backup server |
| 4483 | * when they have incidentely learned an empty cookie, we simply ignore |
| 4484 | * empty cookies and mark them as invalid. |
| 4485 | */ |
| 4486 | if (delim == p3) |
| 4487 | srv = NULL; |
| 4488 | |
| 4489 | while (srv) { |
Willy Tarreau | 92f2ab1 | 2007-02-02 22:14:47 +0100 | [diff] [blame] | 4490 | if (srv->cookie && (srv->cklen == delim - p3) && |
| 4491 | !memcmp(p3, srv->cookie, delim - p3)) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4492 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4493 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4494 | txn->flags &= ~TX_CK_MASK; |
| 4495 | txn->flags |= TX_CK_VALID; |
| 4496 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4497 | t->srv = srv; |
| 4498 | break; |
| 4499 | } else { |
| 4500 | /* we found a server, but it's down */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4501 | txn->flags &= ~TX_CK_MASK; |
| 4502 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4503 | } |
| 4504 | } |
| 4505 | srv = srv->next; |
| 4506 | } |
| 4507 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4508 | if (!srv && !(txn->flags & TX_CK_DOWN)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4509 | /* no server matched this cookie */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4510 | txn->flags &= ~TX_CK_MASK; |
| 4511 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4512 | } |
| 4513 | |
| 4514 | /* depending on the cookie mode, we may have to either : |
| 4515 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 4516 | * the server never sees it ; |
| 4517 | * - remove the server id from the cookie value, and tag the cookie as an |
| 4518 | * application cookie so that it does not get accidentely removed later, |
| 4519 | * if we're in cookie prefix mode |
| 4520 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4521 | if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4522 | int delta; /* negative */ |
| 4523 | |
| 4524 | delta = buffer_replace2(req, p3, delim + 1, NULL, 0); |
| 4525 | p4 += delta; |
| 4526 | cur_end += delta; |
| 4527 | cur_next += delta; |
| 4528 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4529 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4530 | |
| 4531 | del_cookie = del_colon = NULL; |
| 4532 | app_cookies++; /* protect the header from deletion */ |
| 4533 | } |
| 4534 | else if (del_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4535 | (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] | 4536 | del_cookie = p1; |
| 4537 | del_colon = colon; |
| 4538 | } |
| 4539 | } else { |
| 4540 | /* now we know that we must keep this cookie since it's |
| 4541 | * not ours. But if we wanted to delete our cookie |
| 4542 | * earlier, we cannot remove the complete header, but we |
| 4543 | * can remove the previous block itself. |
| 4544 | */ |
| 4545 | app_cookies++; |
| 4546 | |
| 4547 | if (del_cookie != NULL) { |
| 4548 | int delta; /* negative */ |
| 4549 | |
| 4550 | delta = buffer_replace2(req, del_cookie, p1, NULL, 0); |
| 4551 | p4 += delta; |
| 4552 | cur_end += delta; |
| 4553 | cur_next += delta; |
| 4554 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4555 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4556 | del_cookie = del_colon = NULL; |
| 4557 | } |
| 4558 | } |
| 4559 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4560 | if ((t->be->appsession_name != NULL) && |
| 4561 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4562 | /* first, let's see if the cookie is our appcookie*/ |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4563 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4564 | /* Cool... it's the right one */ |
| 4565 | |
| 4566 | asession_temp = &local_asession; |
| 4567 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4568 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4569 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 4570 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 4571 | return; |
| 4572 | } |
| 4573 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4574 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 4575 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4576 | asession_temp->serverid = NULL; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4577 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4578 | /* only do insert, if lookup fails */ |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4579 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 4580 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4581 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4582 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4583 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4584 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 4585 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 4586 | return; |
| 4587 | } |
| 4588 | |
| 4589 | asession_temp->sessid = local_asession.sessid; |
| 4590 | asession_temp->serverid = local_asession.serverid; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4591 | asession_temp->request_count = 0; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4592 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4593 | } else { |
| 4594 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4595 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4596 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4597 | if (asession_temp->serverid == NULL) { |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4598 | /* TODO redispatch request */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4599 | Alert("Found Application Session without matching server.\n"); |
| 4600 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4601 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4602 | while (srv) { |
| 4603 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4604 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4605 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4606 | txn->flags &= ~TX_CK_MASK; |
| 4607 | txn->flags |= TX_CK_VALID; |
| 4608 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4609 | t->srv = srv; |
| 4610 | break; |
| 4611 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4612 | txn->flags &= ~TX_CK_MASK; |
| 4613 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4614 | } |
| 4615 | } |
| 4616 | srv = srv->next; |
| 4617 | }/* end while(srv) */ |
| 4618 | }/* end else if server == NULL */ |
| 4619 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 4620 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4621 | asession_temp->request_count++; |
| 4622 | #if defined(DEBUG_HASH) |
| 4623 | Alert("manage_client_side_cookies\n"); |
| 4624 | appsession_hash_dump(&(t->be->htbl_proxy)); |
| 4625 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4626 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 4627 | } |
| 4628 | |
| 4629 | /* we'll have to look for another cookie ... */ |
| 4630 | p1 = p4; |
| 4631 | } /* while (p1 < cur_end) */ |
| 4632 | |
| 4633 | /* There's no more cookie on this line. |
| 4634 | * We may have marked the last one(s) for deletion. |
| 4635 | * We must do this now in two ways : |
| 4636 | * - if there is no app cookie, we simply delete the header ; |
| 4637 | * - if there are app cookies, we must delete the end of the |
| 4638 | * string properly, including the colon/semi-colon before |
| 4639 | * the cookie name. |
| 4640 | */ |
| 4641 | if (del_cookie != NULL) { |
| 4642 | int delta; |
| 4643 | if (app_cookies) { |
| 4644 | delta = buffer_replace2(req, del_colon, cur_end, NULL, 0); |
| 4645 | cur_end = del_colon; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4646 | cur_hdr->len += delta; |
| 4647 | } else { |
| 4648 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4649 | |
| 4650 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4651 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4652 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4653 | cur_hdr->len = 0; |
| 4654 | } |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 4655 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4656 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4657 | } |
| 4658 | |
| 4659 | /* keep the link from this header to next one */ |
| 4660 | old_idx = cur_idx; |
| 4661 | } /* end of cookie processing on this header */ |
| 4662 | } |
| 4663 | |
| 4664 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4665 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 4666 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 4667 | */ |
| 4668 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4669 | { |
| 4670 | char term; |
| 4671 | char *cur_ptr, *cur_end, *cur_next; |
| 4672 | int cur_idx, old_idx, last_hdr; |
| 4673 | struct http_txn *txn = &t->txn; |
| 4674 | struct hdr_idx_elem *cur_hdr; |
| 4675 | int len, delta; |
| 4676 | |
| 4677 | last_hdr = 0; |
| 4678 | |
| 4679 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4680 | old_idx = 0; |
| 4681 | |
| 4682 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4683 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4684 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4685 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4686 | (exp->action == ACT_ALLOW || |
| 4687 | exp->action == ACT_DENY)) |
| 4688 | return 0; |
| 4689 | |
| 4690 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 4691 | if (!cur_idx) |
| 4692 | break; |
| 4693 | |
| 4694 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4695 | cur_ptr = cur_next; |
| 4696 | cur_end = cur_ptr + cur_hdr->len; |
| 4697 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4698 | |
| 4699 | /* Now we have one header between cur_ptr and cur_end, |
| 4700 | * and the next header starts at cur_next. |
| 4701 | */ |
| 4702 | |
| 4703 | /* The annoying part is that pattern matching needs |
| 4704 | * that we modify the contents to null-terminate all |
| 4705 | * strings before testing them. |
| 4706 | */ |
| 4707 | |
| 4708 | term = *cur_end; |
| 4709 | *cur_end = '\0'; |
| 4710 | |
| 4711 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4712 | switch (exp->action) { |
| 4713 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4714 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4715 | last_hdr = 1; |
| 4716 | break; |
| 4717 | |
| 4718 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4719 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4720 | last_hdr = 1; |
| 4721 | break; |
| 4722 | |
| 4723 | case ACT_REPLACE: |
| 4724 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4725 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 4726 | /* FIXME: if the user adds a newline in the replacement, the |
| 4727 | * index will not be recalculated for now, and the new line |
| 4728 | * will not be counted as a new header. |
| 4729 | */ |
| 4730 | |
| 4731 | cur_end += delta; |
| 4732 | cur_next += delta; |
| 4733 | cur_hdr->len += delta; |
| 4734 | txn->rsp.eoh += delta; |
| 4735 | break; |
| 4736 | |
| 4737 | case ACT_REMOVE: |
| 4738 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 4739 | cur_next += delta; |
| 4740 | |
| 4741 | /* FIXME: this should be a separate function */ |
| 4742 | txn->rsp.eoh += delta; |
| 4743 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4744 | txn->hdr_idx.used--; |
| 4745 | cur_hdr->len = 0; |
| 4746 | cur_end = NULL; /* null-term has been rewritten */ |
| 4747 | break; |
| 4748 | |
| 4749 | } |
| 4750 | } |
| 4751 | if (cur_end) |
| 4752 | *cur_end = term; /* restore the string terminator */ |
| 4753 | |
| 4754 | /* keep the link from this header to next one in case of later |
| 4755 | * removal of next header. |
| 4756 | */ |
| 4757 | old_idx = cur_idx; |
| 4758 | } |
| 4759 | return 0; |
| 4760 | } |
| 4761 | |
| 4762 | |
| 4763 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 4764 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 4765 | * or -1 if a replacement resulted in an invalid status line. |
| 4766 | */ |
| 4767 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4768 | { |
| 4769 | char term; |
| 4770 | char *cur_ptr, *cur_end; |
| 4771 | int done; |
| 4772 | struct http_txn *txn = &t->txn; |
| 4773 | int len, delta; |
| 4774 | |
| 4775 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4776 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4777 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4778 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4779 | (exp->action == ACT_ALLOW || |
| 4780 | exp->action == ACT_DENY)) |
| 4781 | return 0; |
| 4782 | else if (exp->action == ACT_REMOVE) |
| 4783 | return 0; |
| 4784 | |
| 4785 | done = 0; |
| 4786 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4787 | 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] | 4788 | cur_end = cur_ptr + txn->rsp.sl.rq.l; |
| 4789 | |
| 4790 | /* Now we have the status line between cur_ptr and cur_end */ |
| 4791 | |
| 4792 | /* The annoying part is that pattern matching needs |
| 4793 | * that we modify the contents to null-terminate all |
| 4794 | * strings before testing them. |
| 4795 | */ |
| 4796 | |
| 4797 | term = *cur_end; |
| 4798 | *cur_end = '\0'; |
| 4799 | |
| 4800 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4801 | switch (exp->action) { |
| 4802 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4803 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4804 | done = 1; |
| 4805 | break; |
| 4806 | |
| 4807 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4808 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4809 | done = 1; |
| 4810 | break; |
| 4811 | |
| 4812 | case ACT_REPLACE: |
| 4813 | *cur_end = term; /* restore the string terminator */ |
| 4814 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4815 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 4816 | /* FIXME: if the user adds a newline in the replacement, the |
| 4817 | * index will not be recalculated for now, and the new line |
| 4818 | * will not be counted as a new header. |
| 4819 | */ |
| 4820 | |
| 4821 | txn->rsp.eoh += delta; |
| 4822 | cur_end += delta; |
| 4823 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4824 | 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] | 4825 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 4826 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4827 | cur_ptr, cur_end + 1, |
| 4828 | NULL, NULL); |
| 4829 | if (unlikely(!cur_end)) |
| 4830 | return -1; |
| 4831 | |
| 4832 | /* we have a full respnse and we know that we have either a CR |
| 4833 | * or an LF at <ptr>. |
| 4834 | */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4835 | 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] | 4836 | hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r'); |
| 4837 | /* there is no point trying this regex on headers */ |
| 4838 | return 1; |
| 4839 | } |
| 4840 | } |
| 4841 | *cur_end = term; /* restore the string terminator */ |
| 4842 | return done; |
| 4843 | } |
| 4844 | |
| 4845 | |
| 4846 | |
| 4847 | /* |
| 4848 | * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>. |
| 4849 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 4850 | * unparsable response. |
| 4851 | */ |
| 4852 | int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4853 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4854 | struct http_txn *txn = &t->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4855 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4856 | while (exp && !(txn->flags & TX_SVDENY)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4857 | int ret; |
| 4858 | |
| 4859 | /* |
| 4860 | * The interleaving of transformations and verdicts |
| 4861 | * makes it difficult to decide to continue or stop |
| 4862 | * the evaluation. |
| 4863 | */ |
| 4864 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4865 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4866 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 4867 | exp->action == ACT_PASS)) { |
| 4868 | exp = exp->next; |
| 4869 | continue; |
| 4870 | } |
| 4871 | |
| 4872 | /* Apply the filter to the status line. */ |
| 4873 | ret = apply_filter_to_sts_line(t, rtr, exp); |
| 4874 | if (unlikely(ret < 0)) |
| 4875 | return -1; |
| 4876 | |
| 4877 | if (likely(ret == 0)) { |
| 4878 | /* The filter did not match the response, it can be |
| 4879 | * iterated through all headers. |
| 4880 | */ |
| 4881 | apply_filter_to_resp_headers(t, rtr, exp); |
| 4882 | } |
| 4883 | exp = exp->next; |
| 4884 | } |
| 4885 | return 0; |
| 4886 | } |
| 4887 | |
| 4888 | |
| 4889 | |
| 4890 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 4891 | * Manage server-side cookies. It can impact performance by about 2% so it is |
| 4892 | * desirable to call it only when needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4893 | */ |
| 4894 | void manage_server_side_cookies(struct session *t, struct buffer *rtr) |
| 4895 | { |
| 4896 | struct http_txn *txn = &t->txn; |
| 4897 | char *p1, *p2, *p3, *p4; |
| 4898 | |
| 4899 | appsess *asession_temp = NULL; |
| 4900 | appsess local_asession; |
| 4901 | |
| 4902 | char *cur_ptr, *cur_end, *cur_next; |
| 4903 | int cur_idx, old_idx, delta; |
| 4904 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4905 | /* Iterate through the headers. |
| 4906 | * we start with the start line. |
| 4907 | */ |
| 4908 | old_idx = 0; |
| 4909 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4910 | |
| 4911 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 4912 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4913 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4914 | |
| 4915 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4916 | cur_ptr = cur_next; |
| 4917 | cur_end = cur_ptr + cur_hdr->len; |
| 4918 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4919 | |
| 4920 | /* We have one full header between cur_ptr and cur_end, and the |
| 4921 | * next header starts at cur_next. We're only interested in |
| 4922 | * "Cookie:" headers. |
| 4923 | */ |
| 4924 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4925 | val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10); |
| 4926 | if (!val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4927 | old_idx = cur_idx; |
| 4928 | continue; |
| 4929 | } |
| 4930 | |
| 4931 | /* 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] | 4932 | txn->flags |= TX_SCK_ANY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4933 | |
| 4934 | |
| 4935 | /* maybe we only wanted to see if there was a set-cookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4936 | if (t->be->cookie_name == NULL && |
| 4937 | t->be->appsession_name == NULL && |
| 4938 | t->be->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4939 | return; |
| 4940 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4941 | p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4942 | |
| 4943 | 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] | 4944 | if (p1 == cur_end || *p1 == ';') /* end of cookie */ |
| 4945 | break; |
| 4946 | |
| 4947 | /* p1 is at the beginning of the cookie name */ |
| 4948 | p2 = p1; |
| 4949 | |
| 4950 | while (p2 < cur_end && *p2 != '=' && *p2 != ';') |
| 4951 | p2++; |
| 4952 | |
| 4953 | if (p2 == cur_end || *p2 == ';') /* next cookie */ |
| 4954 | break; |
| 4955 | |
| 4956 | p3 = p2 + 1; /* skip the '=' sign */ |
| 4957 | if (p3 == cur_end) |
| 4958 | break; |
| 4959 | |
| 4960 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4961 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';') |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4962 | p4++; |
| 4963 | |
| 4964 | /* here, we have the cookie name between p1 and p2, |
| 4965 | * and its value between p3 and p4. |
| 4966 | * we can process it. |
| 4967 | */ |
| 4968 | |
| 4969 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4970 | if (t->be->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4971 | txn->srv_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4972 | (p4 - p1 >= t->be->capture_namelen) && |
| 4973 | memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4974 | int log_len = p4 - p1; |
| 4975 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 4976 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4977 | Alert("HTTP logging : out of memory.\n"); |
| 4978 | } |
| 4979 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4980 | if (log_len > t->be->capture_len) |
| 4981 | log_len = t->be->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4982 | memcpy(txn->srv_cookie, p1, log_len); |
| 4983 | txn->srv_cookie[log_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4984 | } |
| 4985 | |
| 4986 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4987 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 4988 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4989 | /* Cool... it's the right one */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4990 | txn->flags |= TX_SCK_SEEN; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4991 | |
| 4992 | /* If the cookie is in insert mode on a known server, we'll delete |
| 4993 | * this occurrence because we'll insert another one later. |
| 4994 | * We'll delete it too if the "indirect" option is set and we're in |
| 4995 | * a direct access. */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4996 | if (((t->srv) && (t->be->options & PR_O_COOK_INS)) || |
| 4997 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4998 | /* this header must be deleted */ |
| 4999 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 5000 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5001 | txn->hdr_idx.used--; |
| 5002 | cur_hdr->len = 0; |
| 5003 | cur_next += delta; |
| 5004 | txn->rsp.eoh += delta; |
| 5005 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5006 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5007 | } |
| 5008 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5009 | (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5010 | /* replace bytes p3->p4 with the cookie name associated |
| 5011 | * with this server since we know it. |
| 5012 | */ |
| 5013 | delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen); |
| 5014 | cur_hdr->len += delta; |
| 5015 | cur_next += delta; |
| 5016 | txn->rsp.eoh += delta; |
| 5017 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5018 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5019 | } |
| 5020 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5021 | (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5022 | /* insert the cookie name associated with this server |
| 5023 | * before existing cookie, and insert a delimitor between them.. |
| 5024 | */ |
| 5025 | delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1); |
| 5026 | cur_hdr->len += delta; |
| 5027 | cur_next += delta; |
| 5028 | txn->rsp.eoh += delta; |
| 5029 | |
| 5030 | p3[t->srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5031 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5032 | } |
| 5033 | } |
| 5034 | /* next, let's see if the cookie is our appcookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5035 | else if ((t->be->appsession_name != NULL) && |
| 5036 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5037 | |
| 5038 | /* Cool... it's the right one */ |
| 5039 | |
| 5040 | size_t server_id_len = strlen(t->srv->id) + 1; |
| 5041 | asession_temp = &local_asession; |
| 5042 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5043 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5044 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5045 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5046 | return; |
| 5047 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5048 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 5049 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5050 | asession_temp->serverid = NULL; |
| 5051 | |
| 5052 | /* only do insert, if lookup fails */ |
Ryan Warnick | 6d0b1fa | 2008-02-17 11:24:35 +0100 | [diff] [blame] | 5053 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 5054 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5055 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5056 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 5057 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 5058 | return; |
| 5059 | } |
| 5060 | asession_temp->sessid = local_asession.sessid; |
| 5061 | asession_temp->serverid = local_asession.serverid; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5062 | asession_temp->request_count = 0; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5063 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
| 5064 | } else { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5065 | /* free wasted memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5066 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5067 | } |
| 5068 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5069 | if (asession_temp->serverid == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5070 | if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5071 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5072 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5073 | return; |
| 5074 | } |
| 5075 | asession_temp->serverid[0] = '\0'; |
| 5076 | } |
| 5077 | |
| 5078 | if (asession_temp->serverid[0] == '\0') |
| 5079 | memcpy(asession_temp->serverid, t->srv->id, server_id_len); |
| 5080 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 5081 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5082 | asession_temp->request_count++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5083 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5084 | Alert("manage_server_side_cookies\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5085 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5086 | #endif |
| 5087 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 5088 | break; /* we don't want to loop again since there cannot be another cookie on the same line */ |
| 5089 | } /* we're now at the end of the cookie value */ |
| 5090 | |
| 5091 | /* keep the link from this header to next one */ |
| 5092 | old_idx = cur_idx; |
| 5093 | } /* end of cookie processing on this header */ |
| 5094 | } |
| 5095 | |
| 5096 | |
| 5097 | |
| 5098 | /* |
| 5099 | * Check if response is cacheable or not. Updates t->flags. |
| 5100 | */ |
| 5101 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 5102 | { |
| 5103 | struct http_txn *txn = &t->txn; |
| 5104 | char *p1, *p2; |
| 5105 | |
| 5106 | char *cur_ptr, *cur_end, *cur_next; |
| 5107 | int cur_idx; |
| 5108 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 5109 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5110 | return; |
| 5111 | |
| 5112 | /* Iterate through the headers. |
| 5113 | * we start with the start line. |
| 5114 | */ |
| 5115 | cur_idx = 0; |
| 5116 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 5117 | |
| 5118 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 5119 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5120 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5121 | |
| 5122 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 5123 | cur_ptr = cur_next; |
| 5124 | cur_end = cur_ptr + cur_hdr->len; |
| 5125 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5126 | |
| 5127 | /* We have one full header between cur_ptr and cur_end, and the |
| 5128 | * next header starts at cur_next. We're only interested in |
| 5129 | * "Cookie:" headers. |
| 5130 | */ |
| 5131 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5132 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 5133 | if (val) { |
| 5134 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 5135 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 5136 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 5137 | return; |
| 5138 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5139 | } |
| 5140 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5141 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 5142 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5143 | continue; |
| 5144 | |
| 5145 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 5146 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5147 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5148 | |
| 5149 | if (p1 >= cur_end) /* no more info */ |
| 5150 | continue; |
| 5151 | |
| 5152 | /* p1 is at the beginning of the value */ |
| 5153 | p2 = p1; |
| 5154 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 5155 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5156 | p2++; |
| 5157 | |
| 5158 | /* we have a complete value between p1 and p2 */ |
| 5159 | if (p2 < cur_end && *p2 == '=') { |
| 5160 | /* we have something of the form no-cache="set-cookie" */ |
| 5161 | if ((cur_end - p1 >= 21) && |
| 5162 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 5163 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5164 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5165 | continue; |
| 5166 | } |
| 5167 | |
| 5168 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 5169 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 5170 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 5171 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 5172 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5173 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5174 | return; |
| 5175 | } |
| 5176 | |
| 5177 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5178 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5179 | continue; |
| 5180 | } |
| 5181 | } |
| 5182 | } |
| 5183 | |
| 5184 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5185 | /* |
| 5186 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 5187 | * If the server is found, it's assigned to the session. |
| 5188 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5189 | 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] | 5190 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5191 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5192 | appsess *asession_temp = NULL; |
| 5193 | appsess local_asession; |
| 5194 | char *request_line; |
| 5195 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5196 | if (t->be->appsession_name == NULL || |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 5197 | (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) || |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5198 | (request_line = memchr(begin, ';', len)) == NULL || |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5199 | ((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] | 5200 | return; |
| 5201 | |
| 5202 | /* skip ';' */ |
| 5203 | request_line++; |
| 5204 | |
| 5205 | /* look if we have a jsessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5206 | 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] | 5207 | return; |
| 5208 | |
| 5209 | /* skip jsessionid= */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5210 | request_line += t->be->appsession_name_len + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5211 | |
| 5212 | /* First try if we already have an appsession */ |
| 5213 | asession_temp = &local_asession; |
| 5214 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5215 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5216 | Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n"); |
| 5217 | send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n"); |
| 5218 | return; |
| 5219 | } |
| 5220 | |
| 5221 | /* Copy the sessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5222 | memcpy(asession_temp->sessid, request_line, t->be->appsession_len); |
| 5223 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5224 | asession_temp->serverid = NULL; |
| 5225 | |
| 5226 | /* only do insert, if lookup fails */ |
Ryan Warnick | 6d0b1fa | 2008-02-17 11:24:35 +0100 | [diff] [blame] | 5227 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 5228 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5229 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5230 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5231 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5232 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 5233 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 5234 | return; |
| 5235 | } |
| 5236 | asession_temp->sessid = local_asession.sessid; |
| 5237 | asession_temp->serverid = local_asession.serverid; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5238 | asession_temp->request_count=0; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5239 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5240 | } |
| 5241 | else { |
| 5242 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5243 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5244 | } |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5245 | |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 5246 | asession_temp->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5247 | asession_temp->request_count++; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5248 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5249 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5250 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5251 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5252 | #endif |
| 5253 | if (asession_temp->serverid == NULL) { |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5254 | /* TODO redispatch request */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5255 | Alert("Found Application Session without matching server.\n"); |
| 5256 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5257 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5258 | while (srv) { |
| 5259 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5260 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5261 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5262 | txn->flags &= ~TX_CK_MASK; |
| 5263 | txn->flags |= TX_CK_VALID; |
| 5264 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5265 | t->srv = srv; |
| 5266 | break; |
| 5267 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5268 | txn->flags &= ~TX_CK_MASK; |
| 5269 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5270 | } |
| 5271 | } |
| 5272 | srv = srv->next; |
| 5273 | } |
| 5274 | } |
| 5275 | } |
| 5276 | |
| 5277 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5278 | /* |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5279 | * In a GET or HEAD request, check if the requested URI matches the stats uri |
| 5280 | * 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] | 5281 | * |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5282 | * 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] | 5283 | * 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] | 5284 | * produce_content() can be called to start sending data. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5285 | * |
| 5286 | * Returns 1 if the session's state changes, otherwise 0. |
| 5287 | */ |
| 5288 | int stats_check_uri_auth(struct session *t, struct proxy *backend) |
| 5289 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5290 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5291 | struct uri_auth *uri_auth = backend->uri_auth; |
| 5292 | struct user_auth *user; |
| 5293 | int authenticated, cur_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5294 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5295 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5296 | memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats)); |
| 5297 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5298 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5299 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5300 | return 0; |
| 5301 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5302 | h = t->req->data + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5303 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5304 | /* the URI is in h */ |
| 5305 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5306 | return 0; |
| 5307 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 5308 | h += uri_auth->uri_len; |
| 5309 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) { |
| 5310 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5311 | t->data_ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 5312 | break; |
| 5313 | } |
| 5314 | h++; |
| 5315 | } |
| 5316 | |
| 5317 | if (uri_auth->refresh) { |
| 5318 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 5319 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) { |
| 5320 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5321 | t->data_ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 5322 | break; |
| 5323 | } |
| 5324 | h++; |
| 5325 | } |
| 5326 | } |
| 5327 | |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 5328 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 5329 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) { |
| 5330 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5331 | t->data_ctx.stats.flags |= STAT_FMT_CSV; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 5332 | break; |
| 5333 | } |
| 5334 | h++; |
| 5335 | } |
| 5336 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5337 | t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO; |
| 5338 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5339 | /* we are in front of a interceptable URI. Let's check |
| 5340 | * if there's an authentication and if it's valid. |
| 5341 | */ |
| 5342 | user = uri_auth->users; |
| 5343 | if (!user) { |
| 5344 | /* no user auth required, it's OK */ |
| 5345 | authenticated = 1; |
| 5346 | } else { |
| 5347 | authenticated = 0; |
| 5348 | |
| 5349 | /* a user list is defined, we have to check. |
| 5350 | * skip 21 chars for "Authorization: Basic ". |
| 5351 | */ |
| 5352 | |
| 5353 | /* FIXME: this should move to an earlier place */ |
| 5354 | cur_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5355 | h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 5356 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 5357 | int len = txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5358 | if (len > 14 && |
| 5359 | !strncasecmp("Authorization:", h, 14)) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5360 | txn->auth_hdr.str = h; |
| 5361 | txn->auth_hdr.len = len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5362 | break; |
| 5363 | } |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5364 | h += len + txn->hdr_idx.v[cur_idx].cr + 1; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5365 | } |
| 5366 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5367 | if (txn->auth_hdr.len < 21 || |
| 5368 | memcmp(txn->auth_hdr.str + 14, " Basic ", 7)) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5369 | user = NULL; |
| 5370 | |
| 5371 | while (user) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5372 | if ((txn->auth_hdr.len == user->user_len + 14 + 7) |
| 5373 | && !memcmp(txn->auth_hdr.str + 14 + 7, |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5374 | user->user_pwd, user->user_len)) { |
| 5375 | authenticated = 1; |
| 5376 | break; |
| 5377 | } |
| 5378 | user = user->next; |
| 5379 | } |
| 5380 | } |
| 5381 | |
| 5382 | if (!authenticated) { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5383 | struct chunk msg; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5384 | |
| 5385 | /* no need to go further */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5386 | msg.str = trash; |
| 5387 | msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm); |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5388 | txn->status = 401; |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5389 | client_retnclose(t, &msg); |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 5390 | trace_term(t, TT_HTTP_URI_1); |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 5391 | t->req->analysers = 0; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5392 | if (!(t->flags & SN_ERR_MASK)) |
| 5393 | t->flags |= SN_ERR_PRXCOND; |
| 5394 | if (!(t->flags & SN_FINST_MASK)) |
| 5395 | t->flags |= SN_FINST_R; |
| 5396 | return 1; |
| 5397 | } |
| 5398 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 5399 | /* The request is valid, the user is authenticated. Let's start sending |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5400 | * data. |
| 5401 | */ |
Willy Tarreau | 72b179a | 2008-08-28 16:01:32 +0200 | [diff] [blame] | 5402 | buffer_shutw_now(t->req); |
| 5403 | buffer_shutr_now(t->rep); |
| 5404 | buffer_start_hijack(t->rep); |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 5405 | t->logs.tv_request = now; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5406 | t->data_source = DATA_SRC_STATS; |
| 5407 | t->data_state = DATA_ST_INIT; |
Willy Tarreau | 91e9993 | 2008-06-30 07:51:00 +0200 | [diff] [blame] | 5408 | t->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5409 | produce_content(t); |
| 5410 | return 1; |
| 5411 | } |
| 5412 | |
| 5413 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5414 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5415 | * Print a debug line with a header |
| 5416 | */ |
| 5417 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 5418 | { |
| 5419 | int len, max; |
| 5420 | len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 5421 | dir, (unsigned short)t->req->prod->fd, (unsigned short)t->req->cons->fd); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5422 | max = end - start; |
| 5423 | UBOUND(max, sizeof(trash) - len - 1); |
| 5424 | len += strlcpy2(trash + len, start, max + 1); |
| 5425 | trash[len++] = '\n'; |
| 5426 | write(1, trash, len); |
| 5427 | } |
| 5428 | |
| 5429 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5430 | /************************************************************************/ |
| 5431 | /* The code below is dedicated to ACL parsing and matching */ |
| 5432 | /************************************************************************/ |
| 5433 | |
| 5434 | |
| 5435 | |
| 5436 | |
| 5437 | /* 1. Check on METHOD |
| 5438 | * We use the pre-parsed method if it is known, and store its number as an |
| 5439 | * integer. If it is unknown, we use the pointer and the length. |
| 5440 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5441 | 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] | 5442 | { |
| 5443 | int len, meth; |
| 5444 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5445 | len = strlen(*text); |
| 5446 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5447 | |
| 5448 | pattern->val.i = meth; |
| 5449 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5450 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5451 | if (!pattern->ptr.str) |
| 5452 | return 0; |
| 5453 | pattern->len = len; |
| 5454 | } |
| 5455 | return 1; |
| 5456 | } |
| 5457 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5458 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5459 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5460 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5461 | { |
| 5462 | int meth; |
| 5463 | struct http_txn *txn = l7; |
| 5464 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5465 | if (!txn) |
| 5466 | return 0; |
| 5467 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5468 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5469 | return 0; |
| 5470 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5471 | meth = txn->meth; |
| 5472 | test->i = meth; |
| 5473 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5474 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5475 | /* ensure the indexes are not affected */ |
| 5476 | return 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5477 | test->len = txn->req.sl.rq.m_l; |
| 5478 | test->ptr = txn->req.sol; |
| 5479 | } |
| 5480 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5481 | return 1; |
| 5482 | } |
| 5483 | |
| 5484 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 5485 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 5486 | int icase; |
| 5487 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5488 | if (test->i != pattern->val.i) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5489 | return ACL_PAT_FAIL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5490 | |
| 5491 | if (test->i != HTTP_METH_OTHER) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5492 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5493 | |
| 5494 | /* Other method, we must compare the strings */ |
| 5495 | if (pattern->len != test->len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5496 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 5497 | |
| 5498 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 5499 | if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) || |
| 5500 | (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 5501 | return ACL_PAT_FAIL; |
| 5502 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5503 | } |
| 5504 | |
| 5505 | /* 2. Check on Request/Status Version |
| 5506 | * We simply compare strings here. |
| 5507 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5508 | 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] | 5509 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5510 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5511 | if (!pattern->ptr.str) |
| 5512 | return 0; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5513 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5514 | return 1; |
| 5515 | } |
| 5516 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5517 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5518 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5519 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5520 | { |
| 5521 | struct http_txn *txn = l7; |
| 5522 | char *ptr; |
| 5523 | int len; |
| 5524 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5525 | if (!txn) |
| 5526 | return 0; |
| 5527 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5528 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5529 | return 0; |
| 5530 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5531 | len = txn->req.sl.rq.v_l; |
| 5532 | ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som; |
| 5533 | |
| 5534 | while ((len-- > 0) && (*ptr++ != '/')); |
| 5535 | if (len <= 0) |
| 5536 | return 0; |
| 5537 | |
| 5538 | test->ptr = ptr; |
| 5539 | test->len = len; |
| 5540 | |
| 5541 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5542 | return 1; |
| 5543 | } |
| 5544 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5545 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5546 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5547 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5548 | { |
| 5549 | struct http_txn *txn = l7; |
| 5550 | char *ptr; |
| 5551 | int len; |
| 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->rsp.msg_state != HTTP_MSG_BODY) |
| 5557 | return 0; |
| 5558 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5559 | len = txn->rsp.sl.st.v_l; |
| 5560 | ptr = txn->rsp.sol; |
| 5561 | |
| 5562 | while ((len-- > 0) && (*ptr++ != '/')); |
| 5563 | if (len <= 0) |
| 5564 | return 0; |
| 5565 | |
| 5566 | test->ptr = ptr; |
| 5567 | test->len = len; |
| 5568 | |
| 5569 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5570 | return 1; |
| 5571 | } |
| 5572 | |
| 5573 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5574 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5575 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5576 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5577 | { |
| 5578 | struct http_txn *txn = l7; |
| 5579 | char *ptr; |
| 5580 | int len; |
| 5581 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5582 | if (!txn) |
| 5583 | return 0; |
| 5584 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5585 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5586 | return 0; |
| 5587 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5588 | len = txn->rsp.sl.st.c_l; |
| 5589 | ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som; |
| 5590 | |
| 5591 | test->i = __strl2ui(ptr, len); |
| 5592 | test->flags = ACL_TEST_F_VOL_1ST; |
| 5593 | return 1; |
| 5594 | } |
| 5595 | |
| 5596 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5597 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5598 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5599 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5600 | { |
| 5601 | struct http_txn *txn = l7; |
| 5602 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5603 | if (!txn) |
| 5604 | return 0; |
| 5605 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5606 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5607 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5608 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5609 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5610 | /* ensure the indexes are not affected */ |
| 5611 | return 0; |
| 5612 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5613 | test->len = txn->req.sl.rq.u_l; |
| 5614 | test->ptr = txn->req.sol + txn->req.sl.rq.u; |
| 5615 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 5616 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 5617 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5618 | return 1; |
| 5619 | } |
| 5620 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5621 | static int |
| 5622 | acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5623 | struct acl_expr *expr, struct acl_test *test) |
| 5624 | { |
| 5625 | struct http_txn *txn = l7; |
| 5626 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5627 | if (!txn) |
| 5628 | return 0; |
| 5629 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5630 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5631 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5632 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5633 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5634 | /* ensure the indexes are not affected */ |
| 5635 | return 0; |
| 5636 | |
| 5637 | /* Parse HTTP request */ |
| 5638 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
| 5639 | test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr; |
| 5640 | test->i = AF_INET; |
| 5641 | |
| 5642 | /* |
| 5643 | * If we are parsing url in frontend space, we prepare backend stage |
| 5644 | * to not parse again the same url ! optimization lazyness... |
| 5645 | */ |
| 5646 | if (px->options & PR_O_HTTP_PROXY) |
| 5647 | l4->flags |= SN_ADDR_SET; |
| 5648 | |
| 5649 | test->flags = ACL_TEST_F_READ_ONLY; |
| 5650 | return 1; |
| 5651 | } |
| 5652 | |
| 5653 | static int |
| 5654 | acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5655 | struct acl_expr *expr, struct acl_test *test) |
| 5656 | { |
| 5657 | struct http_txn *txn = l7; |
| 5658 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5659 | if (!txn) |
| 5660 | return 0; |
| 5661 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5662 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5663 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5664 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 5665 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5666 | /* ensure the indexes are not affected */ |
| 5667 | return 0; |
| 5668 | |
| 5669 | /* Same optimization as url_ip */ |
| 5670 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
| 5671 | test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port); |
| 5672 | |
| 5673 | if (px->options & PR_O_HTTP_PROXY) |
| 5674 | l4->flags |= SN_ADDR_SET; |
| 5675 | |
| 5676 | test->flags = ACL_TEST_F_READ_ONLY; |
| 5677 | return 1; |
| 5678 | } |
| 5679 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5680 | /* 5. Check on HTTP header. A pointer to the beginning of the value is returned. |
| 5681 | * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr(). |
| 5682 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5683 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5684 | 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] | 5685 | struct acl_expr *expr, struct acl_test *test) |
| 5686 | { |
| 5687 | struct http_txn *txn = l7; |
| 5688 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5689 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5690 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5691 | if (!txn) |
| 5692 | return 0; |
| 5693 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5694 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 5695 | /* search for header from the beginning */ |
| 5696 | ctx->idx = 0; |
| 5697 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5698 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 5699 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 5700 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5701 | test->len = ctx->vlen; |
| 5702 | test->ptr = (char *)ctx->line + ctx->val; |
| 5703 | return 1; |
| 5704 | } |
| 5705 | |
| 5706 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 5707 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5708 | return 0; |
| 5709 | } |
| 5710 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5711 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5712 | acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5713 | struct acl_expr *expr, struct acl_test *test) |
| 5714 | { |
| 5715 | struct http_txn *txn = l7; |
| 5716 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5717 | if (!txn) |
| 5718 | return 0; |
| 5719 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5720 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5721 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5722 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5723 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5724 | /* ensure the indexes are not affected */ |
| 5725 | return 0; |
| 5726 | |
| 5727 | return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test); |
| 5728 | } |
| 5729 | |
| 5730 | static int |
| 5731 | acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5732 | struct acl_expr *expr, struct acl_test *test) |
| 5733 | { |
| 5734 | struct http_txn *txn = l7; |
| 5735 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5736 | if (!txn) |
| 5737 | return 0; |
| 5738 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5739 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5740 | return 0; |
| 5741 | |
| 5742 | return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test); |
| 5743 | } |
| 5744 | |
| 5745 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 5746 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 5747 | */ |
| 5748 | static int |
| 5749 | 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] | 5750 | struct acl_expr *expr, struct acl_test *test) |
| 5751 | { |
| 5752 | struct http_txn *txn = l7; |
| 5753 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5754 | struct hdr_ctx ctx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5755 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5756 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5757 | if (!txn) |
| 5758 | return 0; |
| 5759 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5760 | ctx.idx = 0; |
| 5761 | cnt = 0; |
| 5762 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx)) |
| 5763 | cnt++; |
| 5764 | |
| 5765 | test->i = cnt; |
| 5766 | test->flags = ACL_TEST_F_VOL_HDR; |
| 5767 | return 1; |
| 5768 | } |
| 5769 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5770 | static int |
| 5771 | acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5772 | struct acl_expr *expr, struct acl_test *test) |
| 5773 | { |
| 5774 | struct http_txn *txn = l7; |
| 5775 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5776 | if (!txn) |
| 5777 | return 0; |
| 5778 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5779 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5780 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5781 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5782 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5783 | /* ensure the indexes are not affected */ |
| 5784 | return 0; |
| 5785 | |
| 5786 | return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test); |
| 5787 | } |
| 5788 | |
| 5789 | static int |
| 5790 | acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5791 | struct acl_expr *expr, struct acl_test *test) |
| 5792 | { |
| 5793 | struct http_txn *txn = l7; |
| 5794 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5795 | if (!txn) |
| 5796 | return 0; |
| 5797 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5798 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5799 | return 0; |
| 5800 | |
| 5801 | return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test); |
| 5802 | } |
| 5803 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5804 | /* 7. Check on HTTP header's integer value. The integer value is returned. |
| 5805 | * FIXME: the type is 'int', it may not be appropriate for everything. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5806 | * 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] | 5807 | */ |
| 5808 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5809 | 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] | 5810 | struct acl_expr *expr, struct acl_test *test) |
| 5811 | { |
| 5812 | struct http_txn *txn = l7; |
| 5813 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5814 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5815 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5816 | if (!txn) |
| 5817 | return 0; |
| 5818 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5819 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 5820 | /* search for header from the beginning */ |
| 5821 | ctx->idx = 0; |
| 5822 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5823 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 5824 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 5825 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5826 | test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen); |
| 5827 | return 1; |
| 5828 | } |
| 5829 | |
| 5830 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 5831 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5832 | return 0; |
| 5833 | } |
| 5834 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5835 | static int |
| 5836 | acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5837 | struct acl_expr *expr, struct acl_test *test) |
| 5838 | { |
| 5839 | struct http_txn *txn = l7; |
| 5840 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5841 | if (!txn) |
| 5842 | return 0; |
| 5843 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5844 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5845 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5846 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5847 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5848 | /* ensure the indexes are not affected */ |
| 5849 | return 0; |
| 5850 | |
| 5851 | return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test); |
| 5852 | } |
| 5853 | |
| 5854 | static int |
| 5855 | acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5856 | struct acl_expr *expr, struct acl_test *test) |
| 5857 | { |
| 5858 | struct http_txn *txn = l7; |
| 5859 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5860 | if (!txn) |
| 5861 | return 0; |
| 5862 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5863 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5864 | return 0; |
| 5865 | |
| 5866 | return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test); |
| 5867 | } |
| 5868 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5869 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 5870 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 5871 | */ |
| 5872 | static int |
| 5873 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5874 | struct acl_expr *expr, struct acl_test *test) |
| 5875 | { |
| 5876 | struct http_txn *txn = l7; |
| 5877 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5878 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5879 | if (!txn) |
| 5880 | return 0; |
| 5881 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5882 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5883 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5884 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5885 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5886 | /* ensure the indexes are not affected */ |
| 5887 | return 0; |
| 5888 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5889 | end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 5890 | ptr = http_get_path(txn); |
| 5891 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5892 | return 0; |
| 5893 | |
| 5894 | /* OK, we got the '/' ! */ |
| 5895 | test->ptr = ptr; |
| 5896 | |
| 5897 | while (ptr < end && *ptr != '?') |
| 5898 | ptr++; |
| 5899 | |
| 5900 | test->len = ptr - test->ptr; |
| 5901 | |
| 5902 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 5903 | test->flags = ACL_TEST_F_VOL_1ST; |
| 5904 | return 1; |
| 5905 | } |
| 5906 | |
| 5907 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5908 | |
| 5909 | /************************************************************************/ |
| 5910 | /* All supported keywords must be declared here. */ |
| 5911 | /************************************************************************/ |
| 5912 | |
| 5913 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 5914 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5915 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT }, |
| 5916 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5917 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 5918 | { "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] | 5919 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5920 | { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5921 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 5922 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 5923 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 5924 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 5925 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 5926 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 5927 | { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE }, |
| 5928 | { "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] | 5929 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5930 | /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */ |
| 5931 | { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5932 | { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 5933 | { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 5934 | { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 5935 | { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 5936 | { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 5937 | { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 5938 | { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
| 5939 | { "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] | 5940 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5941 | { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 5942 | { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE }, |
| 5943 | { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE }, |
| 5944 | { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE }, |
| 5945 | { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE }, |
| 5946 | { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE }, |
| 5947 | { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE }, |
| 5948 | { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
| 5949 | { "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] | 5950 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5951 | { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5952 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 5953 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 5954 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 5955 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 5956 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 5957 | { "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] | 5958 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 5959 | { NULL, NULL, NULL, NULL }, |
| 5960 | |
| 5961 | #if 0 |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5962 | { "line", acl_parse_str, acl_fetch_line, acl_match_str }, |
| 5963 | { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg }, |
| 5964 | { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg }, |
| 5965 | { "line_end", acl_parse_str, acl_fetch_line, acl_match_end }, |
| 5966 | { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub }, |
| 5967 | { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir }, |
| 5968 | { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom }, |
| 5969 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5970 | { "cook", acl_parse_str, acl_fetch_cook, acl_match_str }, |
| 5971 | { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg }, |
| 5972 | { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg }, |
| 5973 | { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end }, |
| 5974 | { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub }, |
| 5975 | { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir }, |
| 5976 | { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom }, |
| 5977 | { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst }, |
| 5978 | |
| 5979 | { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str }, |
| 5980 | { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg }, |
| 5981 | { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str }, |
| 5982 | { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 }, |
| 5983 | { NULL, NULL, NULL, NULL }, |
| 5984 | #endif |
| 5985 | }}; |
| 5986 | |
| 5987 | |
| 5988 | __attribute__((constructor)) |
| 5989 | static void __http_protocol_init(void) |
| 5990 | { |
| 5991 | acl_register_keywords(&acl_kws); |
| 5992 | } |
| 5993 | |
| 5994 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5995 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5996 | * Local variables: |
| 5997 | * c-indent-level: 8 |
| 5998 | * c-basic-offset: 8 |
| 5999 | * End: |
| 6000 | */ |