Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP protocol analyzer |
| 3 | * |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4 | * Copyright 2000-2007 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> |
| 33 | #include <common/time.h> |
| 34 | #include <common/uri_auth.h> |
| 35 | #include <common/version.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 36 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 37 | #include <types/acl.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 38 | #include <types/capture.h> |
| 39 | #include <types/client.h> |
| 40 | #include <types/global.h> |
| 41 | #include <types/httperr.h> |
| 42 | #include <types/polling.h> |
| 43 | #include <types/proxy.h> |
| 44 | #include <types/server.h> |
| 45 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 46 | #include <proto/acl.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 47 | #include <proto/backend.h> |
| 48 | #include <proto/buffers.h> |
| 49 | #include <proto/fd.h> |
| 50 | #include <proto/log.h> |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 51 | #include <proto/hdr_idx.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 52 | #include <proto/proto_http.h> |
| 53 | #include <proto/queue.h> |
| 54 | #include <proto/session.h> |
| 55 | #include <proto/task.h> |
| 56 | |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 57 | #ifdef CONFIG_HAP_TCPSPLICE |
| 58 | #include <libtcpsplice.h> |
| 59 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 60 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 61 | #define DEBUG_PARSE_NO_SPEEDUP |
| 62 | #undef DEBUG_PARSE_NO_SPEEDUP |
| 63 | |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 64 | /* This is used to perform a quick jump as an alternative to a break/continue |
| 65 | * instruction. The first argument is the label for normal operation, and the |
| 66 | * second one is the break/continue instruction in the no_speedup mode. |
| 67 | */ |
| 68 | |
| 69 | #ifdef DEBUG_PARSE_NO_SPEEDUP |
| 70 | #define QUICK_JUMP(x,y) y |
| 71 | #else |
| 72 | #define QUICK_JUMP(x,y) goto x |
| 73 | #endif |
| 74 | |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 75 | /* This is used by remote monitoring */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 76 | const char HTTP_200[] = |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 77 | "HTTP/1.0 200 OK\r\n" |
| 78 | "Cache-Control: no-cache\r\n" |
| 79 | "Connection: close\r\n" |
| 80 | "Content-Type: text/html\r\n" |
| 81 | "\r\n" |
| 82 | "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n"; |
| 83 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 84 | const struct chunk http_200_chunk = { |
| 85 | .str = (char *)&HTTP_200, |
| 86 | .len = sizeof(HTTP_200)-1 |
| 87 | }; |
| 88 | |
| 89 | const char *HTTP_302 = |
| 90 | "HTTP/1.0 302 Found\r\n" |
| 91 | "Cache-Control: no-cache\r\n" |
| 92 | "Connection: close\r\n" |
| 93 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 94 | |
| 95 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 96 | const char *HTTP_303 = |
| 97 | "HTTP/1.0 303 See Other\r\n" |
| 98 | "Cache-Control: no-cache\r\n" |
| 99 | "Connection: close\r\n" |
| 100 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 101 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 102 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 103 | const char *HTTP_401_fmt = |
| 104 | "HTTP/1.0 401 Unauthorized\r\n" |
| 105 | "Cache-Control: no-cache\r\n" |
| 106 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 107 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 108 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 109 | "\r\n" |
| 110 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 111 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 112 | |
| 113 | const int http_err_codes[HTTP_ERR_SIZE] = { |
| 114 | [HTTP_ERR_400] = 400, |
| 115 | [HTTP_ERR_403] = 403, |
| 116 | [HTTP_ERR_408] = 408, |
| 117 | [HTTP_ERR_500] = 500, |
| 118 | [HTTP_ERR_502] = 502, |
| 119 | [HTTP_ERR_503] = 503, |
| 120 | [HTTP_ERR_504] = 504, |
| 121 | }; |
| 122 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 123 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 124 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 125 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 126 | "Cache-Control: no-cache\r\n" |
| 127 | "Connection: close\r\n" |
| 128 | "Content-Type: text/html\r\n" |
| 129 | "\r\n" |
| 130 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 131 | |
| 132 | [HTTP_ERR_403] = |
| 133 | "HTTP/1.0 403 Forbidden\r\n" |
| 134 | "Cache-Control: no-cache\r\n" |
| 135 | "Connection: close\r\n" |
| 136 | "Content-Type: text/html\r\n" |
| 137 | "\r\n" |
| 138 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 139 | |
| 140 | [HTTP_ERR_408] = |
| 141 | "HTTP/1.0 408 Request Time-out\r\n" |
| 142 | "Cache-Control: no-cache\r\n" |
| 143 | "Connection: close\r\n" |
| 144 | "Content-Type: text/html\r\n" |
| 145 | "\r\n" |
| 146 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 147 | |
| 148 | [HTTP_ERR_500] = |
| 149 | "HTTP/1.0 500 Server Error\r\n" |
| 150 | "Cache-Control: no-cache\r\n" |
| 151 | "Connection: close\r\n" |
| 152 | "Content-Type: text/html\r\n" |
| 153 | "\r\n" |
| 154 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 155 | |
| 156 | [HTTP_ERR_502] = |
| 157 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 158 | "Cache-Control: no-cache\r\n" |
| 159 | "Connection: close\r\n" |
| 160 | "Content-Type: text/html\r\n" |
| 161 | "\r\n" |
| 162 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 163 | |
| 164 | [HTTP_ERR_503] = |
| 165 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 166 | "Cache-Control: no-cache\r\n" |
| 167 | "Connection: close\r\n" |
| 168 | "Content-Type: text/html\r\n" |
| 169 | "\r\n" |
| 170 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 171 | |
| 172 | [HTTP_ERR_504] = |
| 173 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 174 | "Cache-Control: no-cache\r\n" |
| 175 | "Connection: close\r\n" |
| 176 | "Content-Type: text/html\r\n" |
| 177 | "\r\n" |
| 178 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 179 | |
| 180 | }; |
| 181 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 182 | /* We must put the messages here since GCC cannot initialize consts depending |
| 183 | * on strlen(). |
| 184 | */ |
| 185 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 186 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 187 | #define FD_SETS_ARE_BITFIELDS |
| 188 | #ifdef FD_SETS_ARE_BITFIELDS |
| 189 | /* |
| 190 | * This map is used with all the FD_* macros to check whether a particular bit |
| 191 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 192 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 193 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 194 | * exclusively to the macros. |
| 195 | */ |
| 196 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 197 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 198 | |
| 199 | #else |
| 200 | #error "Check if your OS uses bitfields for fd_sets" |
| 201 | #endif |
| 202 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 203 | void init_proto_http() |
| 204 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 205 | int i; |
| 206 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 207 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 208 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 209 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 210 | if (!http_err_msgs[msg]) { |
| 211 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 212 | abort(); |
| 213 | } |
| 214 | |
| 215 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 216 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 217 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 218 | |
| 219 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 220 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 221 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 222 | * printable characters above. |
| 223 | */ |
| 224 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 225 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 226 | for (i = 0; i < 32; i++) { |
| 227 | FD_SET(i, hdr_encode_map); |
| 228 | FD_SET(i, url_encode_map); |
| 229 | } |
| 230 | for (i = 127; i < 256; i++) { |
| 231 | FD_SET(i, hdr_encode_map); |
| 232 | FD_SET(i, url_encode_map); |
| 233 | } |
| 234 | |
| 235 | tmp = "\"#{|}"; |
| 236 | while (*tmp) { |
| 237 | FD_SET(*tmp, hdr_encode_map); |
| 238 | tmp++; |
| 239 | } |
| 240 | |
| 241 | tmp = "\"#"; |
| 242 | while (*tmp) { |
| 243 | FD_SET(*tmp, url_encode_map); |
| 244 | tmp++; |
| 245 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 246 | |
| 247 | /* memory allocations */ |
| 248 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 249 | pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 250 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 251 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 252 | /* |
| 253 | * We have 26 list of methods (1 per first letter), each of which can have |
| 254 | * up to 3 entries (2 valid, 1 null). |
| 255 | */ |
| 256 | struct http_method_desc { |
| 257 | http_meth_t meth; |
| 258 | int len; |
| 259 | const char text[8]; |
| 260 | }; |
| 261 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 262 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 263 | ['C' - 'A'] = { |
| 264 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 265 | }, |
| 266 | ['D' - 'A'] = { |
| 267 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 268 | }, |
| 269 | ['G' - 'A'] = { |
| 270 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 271 | }, |
| 272 | ['H' - 'A'] = { |
| 273 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 274 | }, |
| 275 | ['P' - 'A'] = { |
| 276 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 277 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 278 | }, |
| 279 | ['T' - 'A'] = { |
| 280 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 281 | }, |
| 282 | /* rest is empty like this : |
| 283 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 284 | */ |
| 285 | }; |
| 286 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 287 | /* It is about twice as fast on recent architectures to lookup a byte in a |
| 288 | * table than two perform a boolean AND or OR between two tests. Refer to |
| 289 | * RFC2616 for those chars. |
| 290 | */ |
| 291 | |
| 292 | const char http_is_spht[256] = { |
| 293 | [' '] = 1, ['\t'] = 1, |
| 294 | }; |
| 295 | |
| 296 | const char http_is_crlf[256] = { |
| 297 | ['\r'] = 1, ['\n'] = 1, |
| 298 | }; |
| 299 | |
| 300 | const char http_is_lws[256] = { |
| 301 | [' '] = 1, ['\t'] = 1, |
| 302 | ['\r'] = 1, ['\n'] = 1, |
| 303 | }; |
| 304 | |
| 305 | const char http_is_sep[256] = { |
| 306 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 307 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 308 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 309 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 310 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 311 | }; |
| 312 | |
| 313 | const char http_is_ctl[256] = { |
| 314 | [0 ... 31] = 1, |
| 315 | [127] = 1, |
| 316 | }; |
| 317 | |
| 318 | /* |
| 319 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 320 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 321 | * them correctly. Instead, define every non-CTL char's status. |
| 322 | */ |
| 323 | const char http_is_token[256] = { |
| 324 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 325 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 326 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 327 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 328 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 329 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 330 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 331 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 332 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 333 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 334 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 335 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 336 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 337 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 338 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 339 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 340 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 341 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 342 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 343 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 344 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 345 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 346 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 347 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 348 | }; |
| 349 | |
| 350 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 351 | /* |
| 352 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 353 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 354 | */ |
| 355 | const char http_is_ver_token[256] = { |
| 356 | ['.'] = 1, ['/'] = 1, |
| 357 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 358 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| 359 | ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| 360 | }; |
| 361 | |
| 362 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 363 | #ifdef DEBUG_FULL |
| 364 | static char *cli_stnames[5] = {"HDR", "DAT", "SHR", "SHW", "CLS" }; |
| 365 | static char *srv_stnames[7] = {"IDL", "CON", "HDR", "DAT", "SHR", "SHW", "CLS" }; |
| 366 | #endif |
| 367 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 368 | static void http_sess_log(struct session *s); |
| 369 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 370 | /* |
| 371 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 372 | * CRLF. Text length is measured first, so it cannot be NULL. |
| 373 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 374 | * of headers is automatically adjusted. The number of bytes added is returned |
| 375 | * on success, otherwise <0 is returned indicating an error. |
| 376 | */ |
| 377 | int http_header_add_tail(struct buffer *b, struct http_msg *msg, |
| 378 | struct hdr_idx *hdr_idx, const char *text) |
| 379 | { |
| 380 | int bytes, len; |
| 381 | |
| 382 | len = strlen(text); |
| 383 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 384 | if (!bytes) |
| 385 | return -1; |
| 386 | msg->eoh += bytes; |
| 387 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 388 | } |
| 389 | |
| 390 | /* |
| 391 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 392 | * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then |
| 393 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 394 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 395 | * of headers is automatically adjusted. The number of bytes added is returned |
| 396 | * on success, otherwise <0 is returned indicating an error. |
| 397 | */ |
| 398 | int http_header_add_tail2(struct buffer *b, struct http_msg *msg, |
| 399 | struct hdr_idx *hdr_idx, const char *text, int len) |
| 400 | { |
| 401 | int bytes; |
| 402 | |
| 403 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 404 | if (!bytes) |
| 405 | return -1; |
| 406 | msg->eoh += bytes; |
| 407 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 408 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 409 | |
| 410 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 411 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 412 | * If so, returns the position of the first non-space character relative to |
| 413 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 414 | * to return a pointer to the place after the first space. Returns 0 if the |
| 415 | * header name does not match. Checks are case-insensitive. |
| 416 | */ |
| 417 | int http_header_match2(const char *hdr, const char *end, |
| 418 | const char *name, int len) |
| 419 | { |
| 420 | const char *val; |
| 421 | |
| 422 | if (hdr + len >= end) |
| 423 | return 0; |
| 424 | if (hdr[len] != ':') |
| 425 | return 0; |
| 426 | if (strncasecmp(hdr, name, len) != 0) |
| 427 | return 0; |
| 428 | val = hdr + len + 1; |
| 429 | while (val < end && HTTP_IS_SPHT(*val)) |
| 430 | val++; |
| 431 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 432 | return len + 2; /* we may replace starting from second space */ |
| 433 | return val - hdr; |
| 434 | } |
| 435 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 436 | /* Find the end of the header value contained between <s> and <e>. |
| 437 | * See RFC2616, par 2.2 for more information. Note that it requires |
| 438 | * a valid header to return a valid result. |
| 439 | */ |
| 440 | const char *find_hdr_value_end(const char *s, const char *e) |
| 441 | { |
| 442 | int quoted, qdpair; |
| 443 | |
| 444 | quoted = qdpair = 0; |
| 445 | for (; s < e; s++) { |
| 446 | if (qdpair) qdpair = 0; |
| 447 | else if (quoted && *s == '\\') qdpair = 1; |
| 448 | else if (quoted && *s == '"') quoted = 0; |
| 449 | else if (*s == '"') quoted = 1; |
| 450 | else if (*s == ',') return s; |
| 451 | } |
| 452 | return s; |
| 453 | } |
| 454 | |
| 455 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 456 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 457 | * structure holds everything necessary to use the header and find next |
| 458 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 459 | * beginning. Otherwise, the next occurrence is returned. The function returns |
| 460 | * 1 when it finds a value, and 0 when there is no more. |
| 461 | */ |
| 462 | int http_find_header2(const char *name, int len, |
| 463 | const char *sol, struct hdr_idx *idx, |
| 464 | struct hdr_ctx *ctx) |
| 465 | { |
| 466 | __label__ return_hdr, next_hdr; |
| 467 | const char *eol, *sov; |
| 468 | int cur_idx; |
| 469 | |
| 470 | if (ctx->idx) { |
| 471 | /* We have previously returned a value, let's search |
| 472 | * another one on the same line. |
| 473 | */ |
| 474 | cur_idx = ctx->idx; |
| 475 | sol = ctx->line; |
| 476 | sov = sol + ctx->val + ctx->vlen; |
| 477 | eol = sol + idx->v[cur_idx].len; |
| 478 | |
| 479 | if (sov >= eol) |
| 480 | /* no more values in this header */ |
| 481 | goto next_hdr; |
| 482 | |
| 483 | /* values remaining for this header, skip the comma */ |
| 484 | sov++; |
| 485 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 486 | sov++; |
| 487 | |
| 488 | goto return_hdr; |
| 489 | } |
| 490 | |
| 491 | /* first request for this header */ |
| 492 | sol += hdr_idx_first_pos(idx); |
| 493 | cur_idx = hdr_idx_first_idx(idx); |
| 494 | |
| 495 | while (cur_idx) { |
| 496 | eol = sol + idx->v[cur_idx].len; |
| 497 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 498 | if (len == 0) { |
| 499 | /* No argument was passed, we want any header. |
| 500 | * To achieve this, we simply build a fake request. */ |
| 501 | while (sol + len < eol && sol[len] != ':') |
| 502 | len++; |
| 503 | name = sol; |
| 504 | } |
| 505 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 506 | if ((len < eol - sol) && |
| 507 | (sol[len] == ':') && |
| 508 | (strncasecmp(sol, name, len) == 0)) { |
| 509 | |
| 510 | sov = sol + len + 1; |
| 511 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 512 | sov++; |
| 513 | return_hdr: |
| 514 | ctx->line = sol; |
| 515 | ctx->idx = cur_idx; |
| 516 | ctx->val = sov - sol; |
| 517 | |
| 518 | eol = find_hdr_value_end(sov, eol); |
| 519 | ctx->vlen = eol - sov; |
| 520 | return 1; |
| 521 | } |
| 522 | next_hdr: |
| 523 | sol = eol + idx->v[cur_idx].cr + 1; |
| 524 | cur_idx = idx->v[cur_idx].next; |
| 525 | } |
| 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | int http_find_header(const char *name, |
| 530 | const char *sol, struct hdr_idx *idx, |
| 531 | struct hdr_ctx *ctx) |
| 532 | { |
| 533 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 534 | } |
| 535 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 536 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 537 | * returns a message to the client ; the connection is shut down for read, |
| 538 | * and the request is cleared so that no server connection can be initiated. |
| 539 | * The client must be in a valid state for this (HEADER, DATA ...). |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 540 | * Nothing is performed on the server side. The message is contained in a |
| 541 | * "chunk". If it is null, then an empty message is used. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 542 | * The reply buffer doesn't need to be empty before this. |
| 543 | */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 544 | void client_retnclose(struct session *s, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 545 | { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 546 | EV_FD_CLR(s->cli_fd, DIR_RD); |
| 547 | EV_FD_SET(s->cli_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 548 | buffer_shutr(s->req); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 549 | if (!tv_add_ifset(&s->rep->wex, &now, &s->fe->clitimeout)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 550 | tv_eternity(&s->rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 551 | s->cli_state = CL_STSHUTR; |
| 552 | buffer_flush(s->rep); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 553 | if (msg->len) |
| 554 | buffer_write(s->rep, msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 555 | s->req->l = 0; |
| 556 | } |
| 557 | |
| 558 | |
| 559 | /* |
| 560 | * returns a message into the rep buffer, and flushes the req buffer. |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 561 | * The reply buffer doesn't need to be empty before this. The message |
| 562 | * is contained in a "chunk". If it is null, then an empty message is |
| 563 | * used. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 564 | */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 565 | void client_return(struct session *s, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 566 | { |
| 567 | buffer_flush(s->rep); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 568 | if (msg->len) |
| 569 | buffer_write(s->rep, msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 570 | s->req->l = 0; |
| 571 | } |
| 572 | |
| 573 | |
| 574 | /* This function turns the server state into the SV_STCLOSE, and sets |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 575 | * indicators accordingly. Note that if <status> is 0, or if the message |
| 576 | * pointer is NULL, then no message is returned. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 577 | */ |
| 578 | void srv_close_with_err(struct session *t, int err, int finst, |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 579 | int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 580 | { |
| 581 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 582 | if (status > 0 && msg) { |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 583 | t->txn.status = status; |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 584 | if (t->fe->mode == PR_MODE_HTTP) |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 585 | client_return(t, msg); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 586 | } |
| 587 | if (!(t->flags & SN_ERR_MASK)) |
| 588 | t->flags |= err; |
| 589 | if (!(t->flags & SN_FINST_MASK)) |
| 590 | t->flags |= finst; |
| 591 | } |
| 592 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 593 | /* This function returns the appropriate error location for the given session |
| 594 | * and message. |
| 595 | */ |
| 596 | |
| 597 | struct chunk *error_message(struct session *s, int msgnum) |
| 598 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 599 | if (s->be->errmsg[msgnum].str) |
| 600 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 601 | else if (s->fe->errmsg[msgnum].str) |
| 602 | return &s->fe->errmsg[msgnum]; |
| 603 | else |
| 604 | return &http_err_chunks[msgnum]; |
| 605 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 606 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 607 | /* |
| 608 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 609 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 610 | */ |
| 611 | static http_meth_t find_http_meth(const char *str, const int len) |
| 612 | { |
| 613 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 614 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 615 | |
| 616 | m = ((unsigned)*str - 'A'); |
| 617 | |
| 618 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 619 | for (h = http_methods[m]; h->len > 0; h++) { |
| 620 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 621 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 622 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 623 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 624 | }; |
| 625 | return HTTP_METH_OTHER; |
| 626 | } |
| 627 | return HTTP_METH_NONE; |
| 628 | |
| 629 | } |
| 630 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 631 | /* Processes the client and server jobs of a session task, then |
| 632 | * puts it back to the wait queue in a clean state, or |
| 633 | * cleans up its resources if it must be deleted. Returns |
| 634 | * the time the task accepts to wait, or TIME_ETERNITY for |
| 635 | * infinity. |
| 636 | */ |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 637 | void process_session(struct task *t, struct timeval *next) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 638 | { |
| 639 | struct session *s = t->context; |
| 640 | int fsm_resync = 0; |
| 641 | |
| 642 | do { |
| 643 | fsm_resync = 0; |
| 644 | //fprintf(stderr,"before_cli:cli=%d, srv=%d\n", s->cli_state, s->srv_state); |
| 645 | fsm_resync |= process_cli(s); |
| 646 | //fprintf(stderr,"cli/srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state); |
| 647 | fsm_resync |= process_srv(s); |
| 648 | //fprintf(stderr,"after_srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state); |
| 649 | } while (fsm_resync); |
| 650 | |
Willy Tarreau | f41d4b1 | 2007-04-28 23:26:14 +0200 | [diff] [blame] | 651 | if (likely(s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE)) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 652 | s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE; |
| 653 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 654 | |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 655 | t->expire = s->req->rex; |
| 656 | tv_min(&t->expire, &s->req->rex, &s->req->wex); |
| 657 | tv_bound(&t->expire, &s->req->cex); |
| 658 | tv_bound(&t->expire, &s->rep->rex); |
| 659 | tv_bound(&t->expire, &s->rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 660 | |
| 661 | /* restore t to its place in the task list */ |
| 662 | task_queue(t); |
| 663 | |
| 664 | #ifdef DEBUG_FULL |
| 665 | /* DEBUG code : this should never ever happen, otherwise it indicates |
| 666 | * that a task still has something to do and will provoke a quick loop. |
| 667 | */ |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 668 | if (tv_remain2(&now, &t->expire) <= 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 669 | exit(100); |
| 670 | #endif |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 671 | *next = t->expire; |
| 672 | return; /* nothing more to do */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 673 | } |
| 674 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 675 | s->fe->feconn--; |
| 676 | if (s->flags & SN_BE_ASSIGNED) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 677 | s->be->beconn--; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 678 | actconn--; |
| 679 | |
Willy Tarreau | f41d4b1 | 2007-04-28 23:26:14 +0200 | [diff] [blame] | 680 | if (unlikely((global.mode & MODE_DEBUG) && |
| 681 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 682 | int len; |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 683 | len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 684 | s->uniq_id, s->be->id, |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 685 | (unsigned short)s->cli_fd, (unsigned short)s->srv_fd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 686 | write(1, trash, len); |
| 687 | } |
| 688 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 689 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 690 | if (s->req != NULL) |
| 691 | s->logs.bytes_in = s->req->total; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 692 | if (s->rep != NULL) |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 693 | s->logs.bytes_out = s->rep->total; |
| 694 | |
| 695 | s->fe->bytes_in += s->logs.bytes_in; |
| 696 | s->fe->bytes_out += s->logs.bytes_out; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 697 | if (s->be != s->fe) { |
| 698 | s->be->bytes_in += s->logs.bytes_in; |
| 699 | s->be->bytes_out += s->logs.bytes_out; |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 700 | } |
| 701 | if (s->srv) { |
| 702 | s->srv->bytes_in += s->logs.bytes_in; |
| 703 | s->srv->bytes_out += s->logs.bytes_out; |
| 704 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 705 | |
| 706 | /* let's do a final log if we need it */ |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 707 | if (s->logs.logwait && |
| 708 | !(s->flags & SN_MONITOR) && |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 709 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 710 | if (s->fe->to_log & LW_REQ) |
| 711 | http_sess_log(s); |
| 712 | else |
| 713 | tcp_sess_log(s); |
| 714 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 715 | |
| 716 | /* the task MUST not be in the run queue anymore */ |
| 717 | task_delete(t); |
| 718 | session_free(s); |
| 719 | task_free(t); |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 720 | tv_eternity(next); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 724 | extern const char sess_term_cond[8]; |
| 725 | extern const char sess_fin_state[8]; |
| 726 | extern const char *monthname[12]; |
| 727 | const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */ |
| 728 | const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown, |
| 729 | Set-cookie seen and left unchanged (passive), Set-cookie Deleted, |
| 730 | unknown, Set-cookie Rewritten */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 731 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 732 | struct pool_head *pool2_capture; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 733 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 734 | /* |
| 735 | * send a log for the session when we have enough info about it. |
| 736 | * Will not log if the frontend has no log defined. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 737 | */ |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 738 | static void http_sess_log(struct session *s) |
| 739 | { |
| 740 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 741 | struct proxy *fe = s->fe; |
| 742 | struct proxy *be = s->be; |
| 743 | struct proxy *prx_log; |
| 744 | struct http_txn *txn = &s->txn; |
| 745 | int tolog; |
| 746 | char *uri, *h; |
| 747 | char *svid; |
| 748 | struct tm *tm; |
| 749 | static char tmpline[MAX_SYSLOG_LEN]; |
| 750 | int hdr; |
| 751 | |
| 752 | if (fe->logfac1 < 0 && fe->logfac2 < 0) |
| 753 | return; |
| 754 | prx_log = fe; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 755 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 756 | if (s->cli_addr.ss_family == AF_INET) |
| 757 | inet_ntop(AF_INET, |
| 758 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 759 | pn, sizeof(pn)); |
| 760 | else |
| 761 | inet_ntop(AF_INET6, |
| 762 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 763 | pn, sizeof(pn)); |
| 764 | |
| 765 | tm = localtime((time_t *)&s->logs.tv_accept.tv_sec); |
| 766 | |
| 767 | |
| 768 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 769 | tolog = fe->to_log; |
| 770 | |
| 771 | h = tmpline; |
| 772 | if (fe->to_log & LW_REQHDR && |
| 773 | txn->req.cap && |
| 774 | (h < tmpline + sizeof(tmpline) - 10)) { |
| 775 | *(h++) = ' '; |
| 776 | *(h++) = '{'; |
| 777 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 778 | if (hdr) |
| 779 | *(h++) = '|'; |
| 780 | if (txn->req.cap[hdr] != NULL) |
| 781 | h = encode_string(h, tmpline + sizeof(tmpline) - 7, |
| 782 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 783 | } |
| 784 | *(h++) = '}'; |
| 785 | } |
| 786 | |
| 787 | if (fe->to_log & LW_RSPHDR && |
| 788 | txn->rsp.cap && |
| 789 | (h < tmpline + sizeof(tmpline) - 7)) { |
| 790 | *(h++) = ' '; |
| 791 | *(h++) = '{'; |
| 792 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 793 | if (hdr) |
| 794 | *(h++) = '|'; |
| 795 | if (txn->rsp.cap[hdr] != NULL) |
| 796 | h = encode_string(h, tmpline + sizeof(tmpline) - 4, |
| 797 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 798 | } |
| 799 | *(h++) = '}'; |
| 800 | } |
| 801 | |
| 802 | if (h < tmpline + sizeof(tmpline) - 4) { |
| 803 | *(h++) = ' '; |
| 804 | *(h++) = '"'; |
| 805 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 806 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 807 | '#', url_encode_map, uri); |
| 808 | *(h++) = '"'; |
| 809 | } |
| 810 | *h = '\0'; |
| 811 | |
| 812 | svid = (tolog & LW_SVID) ? |
| 813 | (s->data_source != DATA_SRC_STATS) ? |
| 814 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 815 | |
| 816 | send_log(prx_log, LOG_INFO, |
| 817 | "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]" |
| 818 | " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld" |
| 819 | " %s %s %c%c%c%c %d/%d/%d/%d %d/%d%s\n", |
| 820 | pn, |
| 821 | (s->cli_addr.ss_family == AF_INET) ? |
| 822 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 823 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
| 824 | tm->tm_mday, monthname[tm->tm_mon], tm->tm_year+1900, |
| 825 | tm->tm_hour, tm->tm_min, tm->tm_sec, s->logs.tv_accept.tv_usec/1000, |
| 826 | fe->id, be->id, svid, |
| 827 | s->logs.t_request, |
| 828 | (s->logs.t_queue >= 0) ? s->logs.t_queue - s->logs.t_request : -1, |
| 829 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 830 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 831 | (tolog & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 832 | txn->status, |
| 833 | (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_in, |
| 834 | txn->cli_cookie ? txn->cli_cookie : "-", |
| 835 | txn->srv_cookie ? txn->srv_cookie : "-", |
| 836 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 837 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 838 | (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 839 | (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-', |
| 840 | actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0, |
| 841 | s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline); |
| 842 | |
| 843 | s->logs.logwait = 0; |
| 844 | } |
| 845 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 846 | |
| 847 | /* |
| 848 | * Capture headers from message starting at <som> according to header list |
| 849 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 850 | */ |
| 851 | void capture_headers(char *som, struct hdr_idx *idx, |
| 852 | char **cap, struct cap_hdr *cap_hdr) |
| 853 | { |
| 854 | char *eol, *sol, *col, *sov; |
| 855 | int cur_idx; |
| 856 | struct cap_hdr *h; |
| 857 | int len; |
| 858 | |
| 859 | sol = som + hdr_idx_first_pos(idx); |
| 860 | cur_idx = hdr_idx_first_idx(idx); |
| 861 | |
| 862 | while (cur_idx) { |
| 863 | eol = sol + idx->v[cur_idx].len; |
| 864 | |
| 865 | col = sol; |
| 866 | while (col < eol && *col != ':') |
| 867 | col++; |
| 868 | |
| 869 | sov = col + 1; |
| 870 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 871 | sov++; |
| 872 | |
| 873 | for (h = cap_hdr; h; h = h->next) { |
| 874 | if ((h->namelen == col - sol) && |
| 875 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 876 | if (cap[h->index] == NULL) |
| 877 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 878 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 879 | |
| 880 | if (cap[h->index] == NULL) { |
| 881 | Alert("HTTP capture : out of memory.\n"); |
| 882 | continue; |
| 883 | } |
| 884 | |
| 885 | len = eol - sov; |
| 886 | if (len > h->len) |
| 887 | len = h->len; |
| 888 | |
| 889 | memcpy(cap[h->index], sov, len); |
| 890 | cap[h->index][len]=0; |
| 891 | } |
| 892 | } |
| 893 | sol = eol + idx->v[cur_idx].cr + 1; |
| 894 | cur_idx = idx->v[cur_idx].next; |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 899 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 900 | */ |
| 901 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 902 | |
| 903 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 904 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 905 | */ |
| 906 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 907 | ptr++; \ |
| 908 | if (likely(ptr < end)) \ |
| 909 | goto good; \ |
| 910 | else { \ |
| 911 | state = (st); \ |
| 912 | goto http_msg_ood; \ |
| 913 | } \ |
| 914 | } while (0) |
| 915 | |
| 916 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 917 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 918 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 919 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 920 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 921 | * will give undefined results. |
| 922 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 923 | * and that msg->sol points to the beginning of the response. |
| 924 | * If a complete line is found (which implies that at least one CR or LF is |
| 925 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 926 | * returned indicating an incomplete line (which does not mean that parts have |
| 927 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 928 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 929 | * upon next call. |
| 930 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 931 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 932 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 933 | * 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] | 934 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 935 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 936 | const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, int state, |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 937 | const char *ptr, const char *end, |
| 938 | char **ret_ptr, int *ret_state) |
| 939 | { |
| 940 | __label__ |
| 941 | http_msg_rpver, |
| 942 | http_msg_rpver_sp, |
| 943 | http_msg_rpcode, |
| 944 | http_msg_rpcode_sp, |
| 945 | http_msg_rpreason, |
| 946 | http_msg_rpline_eol, |
| 947 | http_msg_ood, /* out of data */ |
| 948 | http_msg_invalid; |
| 949 | |
| 950 | switch (state) { |
| 951 | http_msg_rpver: |
| 952 | case HTTP_MSG_RPVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 953 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 954 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 955 | |
| 956 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 957 | msg->sl.st.v_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 958 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 959 | } |
| 960 | goto http_msg_invalid; |
| 961 | |
| 962 | http_msg_rpver_sp: |
| 963 | case HTTP_MSG_RPVER_SP: |
| 964 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 965 | msg->sl.st.c = ptr - msg_buf; |
| 966 | goto http_msg_rpcode; |
| 967 | } |
| 968 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 969 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 970 | /* so it's a CR/LF, this is invalid */ |
| 971 | goto http_msg_invalid; |
| 972 | |
| 973 | http_msg_rpcode: |
| 974 | case HTTP_MSG_RPCODE: |
| 975 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 976 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 977 | |
| 978 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 979 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 980 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 981 | } |
| 982 | |
| 983 | /* so it's a CR/LF, so there is no reason phrase */ |
| 984 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 985 | http_msg_rsp_reason: |
| 986 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
| 987 | msg->sl.st.r = ptr - msg_buf; |
| 988 | msg->sl.st.r_l = 0; |
| 989 | goto http_msg_rpline_eol; |
| 990 | |
| 991 | http_msg_rpcode_sp: |
| 992 | case HTTP_MSG_RPCODE_SP: |
| 993 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 994 | msg->sl.st.r = ptr - msg_buf; |
| 995 | goto http_msg_rpreason; |
| 996 | } |
| 997 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 998 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 999 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1000 | goto http_msg_rsp_reason; |
| 1001 | |
| 1002 | http_msg_rpreason: |
| 1003 | case HTTP_MSG_RPREASON: |
| 1004 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1005 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
| 1006 | msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r; |
| 1007 | http_msg_rpline_eol: |
| 1008 | /* We have seen the end of line. Note that we do not |
| 1009 | * necessarily have the \n yet, but at least we know that we |
| 1010 | * have EITHER \r OR \n, otherwise the response would not be |
| 1011 | * complete. We can then record the response length and return |
| 1012 | * to the caller which will be able to register it. |
| 1013 | */ |
| 1014 | msg->sl.st.l = ptr - msg->sol; |
| 1015 | return ptr; |
| 1016 | |
| 1017 | #ifdef DEBUG_FULL |
| 1018 | default: |
| 1019 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1020 | exit(1); |
| 1021 | #endif |
| 1022 | } |
| 1023 | |
| 1024 | http_msg_ood: |
| 1025 | /* out of data */ |
| 1026 | if (ret_state) |
| 1027 | *ret_state = state; |
| 1028 | if (ret_ptr) |
| 1029 | *ret_ptr = (char *)ptr; |
| 1030 | return NULL; |
| 1031 | |
| 1032 | http_msg_invalid: |
| 1033 | /* invalid message */ |
| 1034 | if (ret_state) |
| 1035 | *ret_state = HTTP_MSG_ERROR; |
| 1036 | return NULL; |
| 1037 | } |
| 1038 | |
| 1039 | |
| 1040 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1041 | * This function parses a request line between <ptr> and <end>, starting with |
| 1042 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1043 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1044 | * will give undefined results. |
| 1045 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1046 | * and that msg->sol points to the beginning of the request. |
| 1047 | * If a complete line is found (which implies that at least one CR or LF is |
| 1048 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1049 | * returned indicating an incomplete line (which does not mean that parts have |
| 1050 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1051 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1052 | * upon next call. |
| 1053 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1054 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1055 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1056 | * 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] | 1057 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1058 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1059 | const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, int state, |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1060 | const char *ptr, const char *end, |
| 1061 | char **ret_ptr, int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1062 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1063 | __label__ |
| 1064 | http_msg_rqmeth, |
| 1065 | http_msg_rqmeth_sp, |
| 1066 | http_msg_rquri, |
| 1067 | http_msg_rquri_sp, |
| 1068 | http_msg_rqver, |
| 1069 | http_msg_rqline_eol, |
| 1070 | http_msg_ood, /* out of data */ |
| 1071 | http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1072 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1073 | switch (state) { |
| 1074 | http_msg_rqmeth: |
| 1075 | case HTTP_MSG_RQMETH: |
| 1076 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1077 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1078 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1079 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1080 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1081 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1082 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1083 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1084 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1085 | /* HTTP 0.9 request */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1086 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1087 | http_msg_req09_uri: |
| 1088 | msg->sl.rq.u = ptr - msg_buf; |
| 1089 | http_msg_req09_uri_e: |
| 1090 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1091 | http_msg_req09_ver: |
| 1092 | msg->sl.rq.v = ptr - msg_buf; |
| 1093 | msg->sl.rq.v_l = 0; |
| 1094 | goto http_msg_rqline_eol; |
| 1095 | } |
| 1096 | goto http_msg_invalid; |
| 1097 | |
| 1098 | http_msg_rqmeth_sp: |
| 1099 | case HTTP_MSG_RQMETH_SP: |
| 1100 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1101 | msg->sl.rq.u = ptr - msg_buf; |
| 1102 | goto http_msg_rquri; |
| 1103 | } |
| 1104 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1105 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1106 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1107 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1108 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1109 | http_msg_rquri: |
| 1110 | case HTTP_MSG_RQURI: |
| 1111 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1112 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1113 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1114 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1115 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1116 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1117 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1118 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1119 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1120 | goto http_msg_req09_uri_e; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1121 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1122 | http_msg_rquri_sp: |
| 1123 | case HTTP_MSG_RQURI_SP: |
| 1124 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1125 | msg->sl.rq.v = ptr - msg_buf; |
| 1126 | goto http_msg_rqver; |
| 1127 | } |
| 1128 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1129 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1130 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1131 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1132 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1133 | http_msg_rqver: |
| 1134 | case HTTP_MSG_RQVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1135 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1136 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1137 | |
| 1138 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1139 | msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v; |
| 1140 | http_msg_rqline_eol: |
| 1141 | /* We have seen the end of line. Note that we do not |
| 1142 | * necessarily have the \n yet, but at least we know that we |
| 1143 | * have EITHER \r OR \n, otherwise the request would not be |
| 1144 | * complete. We can then record the request length and return |
| 1145 | * to the caller which will be able to register it. |
| 1146 | */ |
| 1147 | msg->sl.rq.l = ptr - msg->sol; |
| 1148 | return ptr; |
| 1149 | } |
| 1150 | |
| 1151 | /* neither an HTTP_VER token nor a CRLF */ |
| 1152 | goto http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1153 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1154 | #ifdef DEBUG_FULL |
| 1155 | default: |
| 1156 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1157 | exit(1); |
| 1158 | #endif |
| 1159 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1160 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1161 | http_msg_ood: |
| 1162 | /* out of data */ |
| 1163 | if (ret_state) |
| 1164 | *ret_state = state; |
| 1165 | if (ret_ptr) |
| 1166 | *ret_ptr = (char *)ptr; |
| 1167 | return NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1168 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1169 | http_msg_invalid: |
| 1170 | /* invalid message */ |
| 1171 | if (ret_state) |
| 1172 | *ret_state = HTTP_MSG_ERROR; |
| 1173 | return NULL; |
| 1174 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1175 | |
| 1176 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1177 | /* |
| 1178 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1179 | * depending on the initial msg->msg_state. It can be preempted everywhere |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1180 | * when data are missing and recalled at the exact same location with no |
| 1181 | * information loss. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1182 | * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other |
| 1183 | * fields. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1184 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1185 | void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx) |
| 1186 | { |
| 1187 | __label__ |
| 1188 | http_msg_rqbefore, |
| 1189 | http_msg_rqbefore_cr, |
| 1190 | http_msg_rqmeth, |
| 1191 | http_msg_rqline_end, |
| 1192 | http_msg_hdr_first, |
| 1193 | http_msg_hdr_name, |
| 1194 | http_msg_hdr_l1_sp, |
| 1195 | http_msg_hdr_l1_lf, |
| 1196 | http_msg_hdr_l1_lws, |
| 1197 | http_msg_hdr_val, |
| 1198 | http_msg_hdr_l2_lf, |
| 1199 | http_msg_hdr_l2_lws, |
| 1200 | http_msg_complete_header, |
| 1201 | http_msg_last_lf, |
| 1202 | http_msg_ood, /* out of data */ |
| 1203 | http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1204 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1205 | int state; /* updated only when leaving the FSM */ |
| 1206 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1207 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1208 | state = msg->msg_state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1209 | ptr = buf->lr; |
| 1210 | end = buf->r; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1211 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1212 | if (unlikely(ptr >= end)) |
| 1213 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1214 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1215 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1216 | /* |
| 1217 | * First, states that are specific to the response only. |
| 1218 | * We check them first so that request and headers are |
| 1219 | * closer to each other (accessed more often). |
| 1220 | */ |
| 1221 | http_msg_rpbefore: |
| 1222 | case HTTP_MSG_RPBEFORE: |
| 1223 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1224 | if (likely(ptr == buf->data)) { |
| 1225 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1226 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1227 | } else { |
| 1228 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1229 | /* only skip empty leading lines, don't remove them */ |
| 1230 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1231 | msg->som = ptr - buf->data; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1232 | #else |
| 1233 | /* Remove empty leading lines, as recommended by |
| 1234 | * RFC2616. This takes a lot of time because we |
| 1235 | * must move all the buffer backwards, but this |
| 1236 | * is rarely needed. The method above will be |
| 1237 | * cleaner when we'll be able to start sending |
| 1238 | * the request from any place in the buffer. |
| 1239 | */ |
| 1240 | buf->lr = ptr; |
| 1241 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1242 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1243 | msg->sol = buf->data; |
| 1244 | ptr = buf->data; |
| 1245 | end = buf->r; |
| 1246 | #endif |
| 1247 | } |
| 1248 | hdr_idx_init(idx); |
| 1249 | state = HTTP_MSG_RPVER; |
| 1250 | goto http_msg_rpver; |
| 1251 | } |
| 1252 | |
| 1253 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1254 | goto http_msg_invalid; |
| 1255 | |
| 1256 | if (unlikely(*ptr == '\n')) |
| 1257 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1258 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1259 | /* stop here */ |
| 1260 | |
| 1261 | http_msg_rpbefore_cr: |
| 1262 | case HTTP_MSG_RPBEFORE_CR: |
| 1263 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1264 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1265 | /* stop here */ |
| 1266 | |
| 1267 | http_msg_rpver: |
| 1268 | case HTTP_MSG_RPVER: |
| 1269 | case HTTP_MSG_RPVER_SP: |
| 1270 | case HTTP_MSG_RPCODE: |
| 1271 | case HTTP_MSG_RPCODE_SP: |
| 1272 | case HTTP_MSG_RPREASON: |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1273 | ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1274 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1275 | if (unlikely(!ptr)) |
| 1276 | return; |
| 1277 | |
| 1278 | /* we have a full response and we know that we have either a CR |
| 1279 | * or an LF at <ptr>. |
| 1280 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1281 | //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] | 1282 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1283 | |
| 1284 | msg->sol = ptr; |
| 1285 | if (likely(*ptr == '\r')) |
| 1286 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1287 | goto http_msg_rpline_end; |
| 1288 | |
| 1289 | http_msg_rpline_end: |
| 1290 | case HTTP_MSG_RPLINE_END: |
| 1291 | /* msg->sol must point to the first of CR or LF. */ |
| 1292 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1293 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1294 | /* stop here */ |
| 1295 | |
| 1296 | /* |
| 1297 | * Second, states that are specific to the request only |
| 1298 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1299 | http_msg_rqbefore: |
| 1300 | case HTTP_MSG_RQBEFORE: |
| 1301 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1302 | if (likely(ptr == buf->data)) { |
| 1303 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1304 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1305 | } else { |
| 1306 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1307 | /* only skip empty leading lines, don't remove them */ |
| 1308 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1309 | msg->som = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1310 | #else |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1311 | /* Remove empty leading lines, as recommended by |
| 1312 | * RFC2616. This takes a lot of time because we |
| 1313 | * must move all the buffer backwards, but this |
| 1314 | * is rarely needed. The method above will be |
| 1315 | * cleaner when we'll be able to start sending |
| 1316 | * the request from any place in the buffer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1317 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1318 | buf->lr = ptr; |
| 1319 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1320 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1321 | msg->sol = buf->data; |
| 1322 | ptr = buf->data; |
| 1323 | end = buf->r; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1324 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1325 | } |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1326 | /* we will need this when keep-alive will be supported |
| 1327 | hdr_idx_init(idx); |
| 1328 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1329 | state = HTTP_MSG_RQMETH; |
| 1330 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1331 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1332 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1333 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1334 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1335 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1336 | if (unlikely(*ptr == '\n')) |
| 1337 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1338 | 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] | 1339 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1340 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1341 | http_msg_rqbefore_cr: |
| 1342 | case HTTP_MSG_RQBEFORE_CR: |
| 1343 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1344 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1345 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1346 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1347 | http_msg_rqmeth: |
| 1348 | case HTTP_MSG_RQMETH: |
| 1349 | case HTTP_MSG_RQMETH_SP: |
| 1350 | case HTTP_MSG_RQURI: |
| 1351 | case HTTP_MSG_RQURI_SP: |
| 1352 | case HTTP_MSG_RQVER: |
| 1353 | ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1354 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1355 | if (unlikely(!ptr)) |
| 1356 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1357 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1358 | /* we have a full request and we know that we have either a CR |
| 1359 | * or an LF at <ptr>. |
| 1360 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1361 | //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] | 1362 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1363 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1364 | msg->sol = ptr; |
| 1365 | if (likely(*ptr == '\r')) |
| 1366 | 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] | 1367 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1368 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1369 | http_msg_rqline_end: |
| 1370 | case HTTP_MSG_RQLINE_END: |
| 1371 | /* check for HTTP/0.9 request : no version information available. |
| 1372 | * msg->sol must point to the first of CR or LF. |
| 1373 | */ |
| 1374 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1375 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1376 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1377 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1378 | 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] | 1379 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1380 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1381 | /* |
| 1382 | * Common states below |
| 1383 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1384 | http_msg_hdr_first: |
| 1385 | case HTTP_MSG_HDR_FIRST: |
| 1386 | msg->sol = ptr; |
| 1387 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1388 | goto http_msg_hdr_name; |
| 1389 | } |
| 1390 | |
| 1391 | if (likely(*ptr == '\r')) |
| 1392 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1393 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1394 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1395 | http_msg_hdr_name: |
| 1396 | case HTTP_MSG_HDR_NAME: |
| 1397 | /* assumes msg->sol points to the first char */ |
| 1398 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1399 | 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] | 1400 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1401 | if (likely(*ptr == ':')) { |
| 1402 | msg->col = ptr - buf->data; |
| 1403 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| 1404 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1405 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1406 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1407 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1408 | http_msg_hdr_l1_sp: |
| 1409 | case HTTP_MSG_HDR_L1_SP: |
| 1410 | /* assumes msg->sol points to the first char and msg->col to the colon */ |
| 1411 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1412 | 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] | 1413 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1414 | /* header value can be basically anything except CR/LF */ |
| 1415 | msg->sov = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1416 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1417 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1418 | goto http_msg_hdr_val; |
| 1419 | } |
| 1420 | |
| 1421 | if (likely(*ptr == '\r')) |
| 1422 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1423 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1424 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1425 | http_msg_hdr_l1_lf: |
| 1426 | case HTTP_MSG_HDR_L1_LF: |
| 1427 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1428 | 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] | 1429 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1430 | http_msg_hdr_l1_lws: |
| 1431 | case HTTP_MSG_HDR_L1_LWS: |
| 1432 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1433 | /* replace HT,CR,LF with spaces */ |
| 1434 | for (; buf->data+msg->sov < ptr; msg->sov++) |
| 1435 | buf->data[msg->sov] = ' '; |
| 1436 | goto http_msg_hdr_l1_sp; |
| 1437 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1438 | /* we had a header consisting only in spaces ! */ |
| 1439 | msg->eol = buf->data + msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1440 | goto http_msg_complete_header; |
| 1441 | |
| 1442 | http_msg_hdr_val: |
| 1443 | case HTTP_MSG_HDR_VAL: |
| 1444 | /* assumes msg->sol points to the first char, msg->col to the |
| 1445 | * colon, and msg->sov points to the first character of the |
| 1446 | * value. |
| 1447 | */ |
| 1448 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1449 | 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] | 1450 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1451 | msg->eol = ptr; |
| 1452 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1453 | * real header end in case it ends with lots of LWS, but is this |
| 1454 | * really needed ? |
| 1455 | */ |
| 1456 | if (likely(*ptr == '\r')) |
| 1457 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1458 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1459 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1460 | http_msg_hdr_l2_lf: |
| 1461 | case HTTP_MSG_HDR_L2_LF: |
| 1462 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1463 | 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] | 1464 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1465 | http_msg_hdr_l2_lws: |
| 1466 | case HTTP_MSG_HDR_L2_LWS: |
| 1467 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1468 | /* LWS: replace HT,CR,LF with spaces */ |
| 1469 | for (; msg->eol < ptr; msg->eol++) |
| 1470 | *msg->eol = ' '; |
| 1471 | goto http_msg_hdr_val; |
| 1472 | } |
| 1473 | http_msg_complete_header: |
| 1474 | /* |
| 1475 | * It was a new header, so the last one is finished. |
| 1476 | * Assumes msg->sol points to the first char, msg->col to the |
| 1477 | * colon, msg->sov points to the first character of the value |
| 1478 | * and msg->eol to the first CR or LF so we know how the line |
| 1479 | * ends. We insert last header into the index. |
| 1480 | */ |
| 1481 | /* |
| 1482 | fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol); |
| 1483 | write(2, msg->sol, msg->eol-msg->sol); |
| 1484 | fprintf(stderr,"\n"); |
| 1485 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1486 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1487 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r', |
| 1488 | idx, idx->tail) < 0)) |
| 1489 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1490 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1491 | msg->sol = ptr; |
| 1492 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1493 | goto http_msg_hdr_name; |
| 1494 | } |
| 1495 | |
| 1496 | if (likely(*ptr == '\r')) |
| 1497 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1498 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1499 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1500 | http_msg_last_lf: |
| 1501 | case HTTP_MSG_LAST_LF: |
| 1502 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1503 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1504 | ptr++; |
| 1505 | buf->lr = ptr; |
| 1506 | msg->eoh = msg->sol - buf->data; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1507 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1508 | return; |
| 1509 | #ifdef DEBUG_FULL |
| 1510 | default: |
| 1511 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1512 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1513 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1514 | } |
| 1515 | http_msg_ood: |
| 1516 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1517 | msg->msg_state = state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1518 | buf->lr = ptr; |
| 1519 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1520 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1521 | http_msg_invalid: |
| 1522 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1523 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1524 | return; |
| 1525 | } |
| 1526 | |
| 1527 | /* |
| 1528 | * manages the client FSM and its socket. BTW, it also tries to handle the |
| 1529 | * cookie. It returns 1 if a state has changed (and a resync may be needed), |
| 1530 | * 0 else. |
| 1531 | */ |
| 1532 | int process_cli(struct session *t) |
| 1533 | { |
| 1534 | int s = t->srv_state; |
| 1535 | int c = t->cli_state; |
| 1536 | struct buffer *req = t->req; |
| 1537 | struct buffer *rep = t->rep; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1538 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1539 | DPRINTF(stderr,"process_cli: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%d.%d,%d.%d\n", |
| 1540 | cli_stnames[c], srv_stnames[s], |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 1541 | EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR), |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1542 | req->rex.tv_sec, req->rex.tv_usec, |
| 1543 | rep->wex.tv_sec, rep->wex.tv_usec); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1544 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1545 | if (c == CL_STHEADERS) { |
| 1546 | /* |
| 1547 | * Now parse the partial (or complete) lines. |
| 1548 | * We will check the request syntax, and also join multi-line |
| 1549 | * headers. An index of all the lines will be elaborated while |
| 1550 | * parsing. |
| 1551 | * |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1552 | * For the parsing, we use a 28 states FSM. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1553 | * |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1554 | * Here is the information we currently have : |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1555 | * req->data + req->som = beginning of request |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1556 | * req->data + req->eoh = end of processed headers / start of current one |
| 1557 | * req->data + req->eol = end of current header or line (LF or CRLF) |
| 1558 | * req->lr = first non-visited byte |
| 1559 | * req->r = end of data |
| 1560 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1561 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1562 | int cur_idx; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1563 | struct http_txn *txn = &t->txn; |
| 1564 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1565 | struct proxy *cur_proxy; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1566 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1567 | if (likely(req->lr < req->r)) |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1568 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1569 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1570 | /* 1: we might have to print this header in debug mode */ |
| 1571 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1572 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1573 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1574 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1575 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1576 | sol = req->data + msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1577 | eol = sol + msg->sl.rq.l; |
| 1578 | debug_hdr("clireq", t, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 1579 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1580 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 1581 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1582 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1583 | while (cur_idx) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1584 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1585 | debug_hdr("clihdr", t, sol, eol); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1586 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 1587 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1588 | } |
| 1589 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1590 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1591 | |
| 1592 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1593 | * Now we quickly check if we have found a full valid request. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1594 | * If not so, we check the FD and buffer states before leaving. |
| 1595 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1596 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 1597 | * requests are checked first. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1598 | * |
| 1599 | */ |
| 1600 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1601 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1602 | /* |
| 1603 | * First, let's catch bad requests. |
| 1604 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1605 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1606 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1607 | |
| 1608 | /* 1: Since we are in header mode, if there's no space |
| 1609 | * left for headers, we won't be able to free more |
| 1610 | * later, so the session will never terminate. We |
| 1611 | * must terminate it now. |
| 1612 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1613 | if (unlikely(req->l >= req->rlim - req->data)) { |
| 1614 | /* FIXME: check if URI is set and return Status |
| 1615 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1616 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1617 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1618 | } |
| 1619 | |
| 1620 | /* 2: have we encountered a read error or a close ? */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1621 | else if (unlikely(req->flags & (BF_READ_ERROR | BF_READ_NULL))) { |
| 1622 | /* read error, or last read : give up. */ |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 1623 | buffer_shutr(req); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1624 | fd_delete(t->cli_fd); |
| 1625 | t->cli_state = CL_STCLOSE; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 1626 | t->fe->failed_req++; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1627 | if (!(t->flags & SN_ERR_MASK)) |
| 1628 | t->flags |= SN_ERR_CLICL; |
| 1629 | if (!(t->flags & SN_FINST_MASK)) |
| 1630 | t->flags |= SN_FINST_R; |
| 1631 | return 1; |
| 1632 | } |
| 1633 | |
| 1634 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 1635 | else if (unlikely(tv_isle(&req->rex, &now))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1636 | /* read timeout : give up with an error message. */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1637 | txn->status = 408; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 1638 | client_retnclose(t, error_message(t, HTTP_ERR_408)); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 1639 | t->fe->failed_req++; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1640 | if (!(t->flags & SN_ERR_MASK)) |
| 1641 | t->flags |= SN_ERR_CLITO; |
| 1642 | if (!(t->flags & SN_FINST_MASK)) |
| 1643 | t->flags |= SN_FINST_R; |
| 1644 | return 1; |
| 1645 | } |
| 1646 | |
| 1647 | /* 4: do we need to re-enable the read socket ? */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 1648 | else if (unlikely(EV_FD_COND_S(t->cli_fd, DIR_RD))) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 1649 | /* fd in DIR_RD was disabled, perhaps because of a previous buffer |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1650 | * full. We cannot loop here since stream_sock_read will disable it only if |
| 1651 | * req->l == rlim-data |
| 1652 | */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 1653 | if (!tv_add_ifset(&req->rex, &now, &t->fe->clitimeout)) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1654 | tv_eternity(&req->rex); |
| 1655 | } |
| 1656 | return t->cli_state != CL_STHEADERS; |
| 1657 | } |
| 1658 | |
| 1659 | |
| 1660 | /**************************************************************** |
| 1661 | * More interesting part now : we know that we have a complete * |
| 1662 | * request which at least looks like HTTP. We have an indicator * |
| 1663 | * of each header's length, so we can parse them quickly. * |
| 1664 | ****************************************************************/ |
| 1665 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1666 | /* ensure we keep this pointer to the beginning of the message */ |
| 1667 | msg->sol = req->data + msg->som; |
| 1668 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1669 | /* |
| 1670 | * 1: identify the method |
| 1671 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1672 | 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] | 1673 | |
| 1674 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1675 | * 2: check if the URI matches the monitor_uri. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1676 | * We have to do this for every request which gets in, because |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1677 | * the monitor-uri is defined by the frontend. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1678 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1679 | if (unlikely((t->fe->monitor_uri_len != 0) && |
| 1680 | (t->fe->monitor_uri_len == msg->sl.rq.u_l) && |
| 1681 | !memcmp(&req->data[msg->sl.rq.u], |
| 1682 | t->fe->monitor_uri, |
| 1683 | t->fe->monitor_uri_len))) { |
| 1684 | /* |
| 1685 | * We have found the monitor URI |
| 1686 | */ |
| 1687 | t->flags |= SN_MONITOR; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1688 | txn->status = 200; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1689 | client_retnclose(t, &http_200_chunk); |
| 1690 | goto return_prx_cond; |
| 1691 | } |
| 1692 | |
| 1693 | /* |
| 1694 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 1695 | * Note: we cannot log anymore if the request has been |
| 1696 | * classified as invalid. |
| 1697 | */ |
| 1698 | if (unlikely(t->logs.logwait & LW_REQ)) { |
| 1699 | /* we have a complete HTTP request that we must log */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 1700 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1701 | int urilen = msg->sl.rq.l; |
| 1702 | |
| 1703 | if (urilen >= REQURI_LEN) |
| 1704 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1705 | memcpy(txn->uri, &req->data[msg->som], urilen); |
| 1706 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1707 | |
| 1708 | if (!(t->logs.logwait &= ~LW_REQ)) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1709 | http_sess_log(t); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1710 | } else { |
| 1711 | Alert("HTTP logging : out of memory.\n"); |
| 1712 | } |
| 1713 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1714 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1715 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1716 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 1717 | if (unlikely(msg->sl.rq.v_l == 0)) { |
| 1718 | int delta; |
| 1719 | char *cur_end; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1720 | msg->sol = req->data + msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1721 | cur_end = msg->sol + msg->sl.rq.l; |
| 1722 | delta = 0; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1723 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1724 | if (msg->sl.rq.u_l == 0) { |
| 1725 | /* if no URI was set, add "/" */ |
| 1726 | delta = buffer_replace2(req, cur_end, cur_end, " /", 2); |
| 1727 | cur_end += delta; |
| 1728 | msg->eoh += delta; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1729 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1730 | /* add HTTP version */ |
| 1731 | delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11); |
| 1732 | msg->eoh += delta; |
| 1733 | cur_end += delta; |
| 1734 | cur_end = (char *)http_parse_reqline(msg, req->data, |
| 1735 | HTTP_MSG_RQMETH, |
| 1736 | msg->sol, cur_end + 1, |
| 1737 | NULL, NULL); |
| 1738 | if (unlikely(!cur_end)) |
| 1739 | goto return_bad_req; |
| 1740 | |
| 1741 | /* we have a full HTTP/1.0 request now and we know that |
| 1742 | * we have either a CR or an LF at <ptr>. |
| 1743 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1744 | 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] | 1745 | } |
| 1746 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1747 | |
| 1748 | /* 5: we may need to capture headers */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1749 | if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap)) |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1750 | capture_headers(req->data + msg->som, &txn->hdr_idx, |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1751 | txn->req.cap, t->fe->req_cap); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1752 | |
| 1753 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1754 | * 6: we will have to evaluate the filters. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1755 | * As opposed to version 1.2, now they will be evaluated in the |
| 1756 | * filters order and not in the header order. This means that |
| 1757 | * each filter has to be validated among all headers. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1758 | * |
| 1759 | * We can now check whether we want to switch to another |
| 1760 | * backend, in which case we will re-check the backend's |
| 1761 | * filters and various options. In order to support 3-level |
| 1762 | * switching, here's how we should proceed : |
| 1763 | * |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1764 | * a) run be. |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1765 | * if (switch) then switch ->be to the new backend. |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1766 | * b) run be if (be != fe). |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1767 | * There cannot be any switch from there, so ->be cannot be |
| 1768 | * changed anymore. |
| 1769 | * |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1770 | * => filters always apply to ->be, then ->be may change. |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1771 | * |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1772 | * The response path will be able to apply either ->be, or |
| 1773 | * ->be then ->fe filters in order to match the reverse of |
| 1774 | * the forward sequence. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1775 | */ |
| 1776 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1777 | do { |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 1778 | struct acl_cond *cond; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1779 | struct proxy *rule_set = t->be; |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1780 | cur_proxy = t->be; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1781 | |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 1782 | /* first check whether we have some ACLs set to block this request */ |
| 1783 | list_for_each_entry(cond, &cur_proxy->block_cond, list) { |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 1784 | int ret = acl_exec_cond(cond, cur_proxy, t, txn, ACL_DIR_REQ); |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 1785 | if (cond->pol == ACL_COND_UNLESS) |
| 1786 | ret = !ret; |
| 1787 | |
| 1788 | if (ret) { |
| 1789 | txn->status = 403; |
| 1790 | /* let's log the request time */ |
| 1791 | t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 1792 | client_retnclose(t, error_message(t, HTTP_ERR_403)); |
| 1793 | goto return_prx_cond; |
| 1794 | } |
| 1795 | } |
| 1796 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1797 | /* try headers filters */ |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 1798 | if (rule_set->req_exp != NULL) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1799 | if (apply_filters_to_request(t, req, rule_set->req_exp) < 0) |
| 1800 | goto return_bad_req; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 1801 | } |
| 1802 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 1803 | if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) { |
| 1804 | /* to ensure correct connection accounting on |
| 1805 | * the backend, we count the connection for the |
| 1806 | * one managing the queue. |
| 1807 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1808 | t->be->beconn++; |
| 1809 | if (t->be->beconn > t->be->beconn_max) |
| 1810 | t->be->beconn_max = t->be->beconn; |
| 1811 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 1812 | t->flags |= SN_BE_ASSIGNED; |
| 1813 | } |
| 1814 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1815 | /* has the request been denied ? */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 1816 | if (txn->flags & TX_CLDENY) { |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1817 | /* no need to go further */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1818 | txn->status = 403; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1819 | /* let's log the request time */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 1820 | t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 1821 | client_retnclose(t, error_message(t, HTTP_ERR_403)); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1822 | goto return_prx_cond; |
| 1823 | } |
| 1824 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1825 | /* We might have to check for "Connection:" */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1826 | if (((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1827 | !(t->flags & SN_CONN_CLOSED)) { |
| 1828 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1829 | int cur_idx, old_idx, delta, val; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1830 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1831 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1832 | 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] | 1833 | old_idx = 0; |
| 1834 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1835 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 1836 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1837 | cur_ptr = cur_next; |
| 1838 | cur_end = cur_ptr + cur_hdr->len; |
| 1839 | cur_next = cur_end + cur_hdr->cr + 1; |
| 1840 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1841 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 1842 | if (val) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1843 | /* 3 possibilities : |
| 1844 | * - we have already set Connection: close, |
| 1845 | * so we remove this line. |
| 1846 | * - we have not yet set Connection: close, |
| 1847 | * but this line indicates close. We leave |
| 1848 | * it untouched and set the flag. |
| 1849 | * - we have not yet set Connection: close, |
| 1850 | * and this line indicates non-close. We |
| 1851 | * replace it. |
| 1852 | */ |
| 1853 | if (t->flags & SN_CONN_CLOSED) { |
| 1854 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1855 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1856 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1857 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 1858 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1859 | cur_hdr->len = 0; |
| 1860 | } else { |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1861 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 1862 | delta = buffer_replace2(req, cur_ptr + val, cur_end, |
| 1863 | "close", 5); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1864 | cur_next += delta; |
| 1865 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1866 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1867 | } |
| 1868 | t->flags |= SN_CONN_CLOSED; |
| 1869 | } |
| 1870 | } |
| 1871 | old_idx = cur_idx; |
| 1872 | } |
Willy Tarreau | f2f0ee8 | 2007-03-30 12:02:43 +0200 | [diff] [blame] | 1873 | } |
| 1874 | /* add request headers from the rule sets in the same order */ |
| 1875 | for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) { |
| 1876 | if (unlikely(http_header_add_tail(req, |
| 1877 | &txn->req, |
| 1878 | &txn->hdr_idx, |
| 1879 | rule_set->req_add[cur_idx])) < 0) |
| 1880 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1881 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 1882 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1883 | /* check if stats URI was requested, and if an auth is needed */ |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 1884 | if (rule_set->uri_auth != NULL && |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1885 | (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) { |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 1886 | /* we have to check the URI and auth for this request */ |
| 1887 | if (stats_check_uri_auth(t, rule_set)) |
| 1888 | return 1; |
| 1889 | } |
| 1890 | |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 1891 | if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) { |
| 1892 | /* No backend was set, but there was a default |
| 1893 | * backend set in the frontend, so we use it and |
| 1894 | * loop again. |
| 1895 | */ |
| 1896 | t->be = cur_proxy->defbe.be; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1897 | t->be->beconn++; |
| 1898 | if (t->be->beconn > t->be->beconn_max) |
| 1899 | t->be->beconn_max = t->be->beconn; |
| 1900 | t->be->cum_beconn++; |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 1901 | t->flags |= SN_BE_ASSIGNED; |
| 1902 | } |
| 1903 | } 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] | 1904 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1905 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 1906 | if (!(t->flags & SN_BE_ASSIGNED)) { |
| 1907 | /* To ensure correct connection accounting on |
| 1908 | * the backend, we count the connection for the |
| 1909 | * one managing the queue. |
| 1910 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1911 | t->be->beconn++; |
| 1912 | if (t->be->beconn > t->be->beconn_max) |
| 1913 | t->be->beconn_max = t->be->beconn; |
| 1914 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 1915 | t->flags |= SN_BE_ASSIGNED; |
| 1916 | } |
| 1917 | |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1918 | /* |
| 1919 | * Right now, we know that we have processed the entire headers |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1920 | * and that unwanted requests have been filtered out. We can do |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1921 | * whatever we want with the remaining request. Also, now we |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1922 | * may have separate values for ->fe, ->be. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1923 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1924 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1925 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1926 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1927 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1928 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1929 | * 7: the appsession cookie was looked up very early in 1.2, |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1930 | * so let's do the same now. |
| 1931 | */ |
| 1932 | |
| 1933 | /* It needs to look into the URI */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1934 | if (t->be->appsession_name) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1935 | 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] | 1936 | } |
| 1937 | |
| 1938 | |
| 1939 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1940 | * 8: Now we can work with the cookies. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1941 | * Note that doing so might move headers in the request, but |
| 1942 | * the fields will stay coherent and the URI will not move. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1943 | * This should only be performed in the backend. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1944 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 1945 | if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1946 | manage_client_side_cookies(t, req); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1947 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1948 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1949 | /* |
Willy Tarreau | bb046ac | 2007-03-03 19:17:03 +0100 | [diff] [blame] | 1950 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 1951 | * asks for it. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1952 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1953 | if ((t->fe->options | t->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1954 | if (t->cli_addr.ss_family == AF_INET) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 1955 | /* Add an X-Forwarded-For header unless the source IP is |
| 1956 | * in the 'except' network range. |
| 1957 | */ |
| 1958 | if ((!t->fe->except_mask.s_addr || |
| 1959 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr) |
| 1960 | != t->fe->except_net.s_addr) && |
| 1961 | (!t->be->except_mask.s_addr || |
| 1962 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr) |
| 1963 | != t->be->except_net.s_addr)) { |
| 1964 | int len; |
| 1965 | unsigned char *pn; |
| 1966 | pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr; |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 1967 | |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 1968 | len = sprintf(trash, "X-Forwarded-For: %d.%d.%d.%d", |
| 1969 | pn[0], pn[1], pn[2], pn[3]); |
| 1970 | |
| 1971 | if (unlikely(http_header_add_tail2(req, &txn->req, |
| 1972 | &txn->hdr_idx, trash, len)) < 0) |
| 1973 | goto return_bad_req; |
| 1974 | } |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1975 | } |
| 1976 | else if (t->cli_addr.ss_family == AF_INET6) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 1977 | /* FIXME: for the sake of completeness, we should also support |
| 1978 | * 'except' here, although it is mostly useless in this case. |
| 1979 | */ |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1980 | int len; |
| 1981 | char pn[INET6_ADDRSTRLEN]; |
| 1982 | inet_ntop(AF_INET6, |
| 1983 | (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr, |
| 1984 | pn, sizeof(pn)); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 1985 | len = sprintf(trash, "X-Forwarded-For: %s", pn); |
| 1986 | if (unlikely(http_header_add_tail2(req, &txn->req, |
| 1987 | &txn->hdr_idx, trash, len)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1988 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1989 | } |
| 1990 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1991 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1992 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1993 | * 10: add "Connection: close" if needed and not yet set. |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 1994 | * 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] | 1995 | */ |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 1996 | if (!(t->flags & SN_CONN_CLOSED) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1997 | ((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE)) { |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 1998 | if ((unlikely(msg->sl.rq.v_l != 8) || |
| 1999 | unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) && |
| 2000 | unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx, |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2001 | "Connection: close", 17)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2002 | goto return_bad_req; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2003 | t->flags |= SN_CONN_CLOSED; |
Willy Tarreau | e15d913 | 2006-12-14 22:26:42 +0100 | [diff] [blame] | 2004 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2005 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2006 | /************************************************************* |
| 2007 | * OK, that's finished for the headers. We have done what we * |
| 2008 | * could. Let's switch to the DATA state. * |
| 2009 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2010 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2011 | t->cli_state = CL_STDATA; |
| 2012 | req->rlim = req->data + BUFSIZE; /* no more rewrite needed */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2013 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2014 | t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2015 | |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 2016 | if (!tv_isset(&t->fe->clitimeout) || |
| 2017 | (t->srv_state < SV_STDATA && tv_isset(&t->be->srvtimeout))) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2018 | /* If the client has no timeout, or if the server is not ready yet, |
| 2019 | * and we know for sure that it can expire, then it's cleaner to |
| 2020 | * disable the timeout on the client side so that too low values |
| 2021 | * cannot make the sessions abort too early. |
| 2022 | * |
| 2023 | * FIXME-20050705: the server needs a way to re-enable this time-out |
| 2024 | * when it switches its state, otherwise a client can stay connected |
| 2025 | * indefinitely. This now seems to be OK. |
| 2026 | */ |
| 2027 | tv_eternity(&req->rex); |
| 2028 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2029 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2030 | /* When a connection is tarpitted, we use the queue timeout for the |
| 2031 | * tarpit delay, which currently happens to be the server's connect |
| 2032 | * timeout. If unset, then set it to zero because we really want it |
| 2033 | * to expire at one moment. |
| 2034 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2035 | if (txn->flags & TX_CLTARPIT) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2036 | t->req->l = 0; |
| 2037 | /* flush the request so that we can drop the connection early |
| 2038 | * if the client closes first. |
| 2039 | */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2040 | if (!tv_add_ifset(&req->cex, &now, &t->be->contimeout)) |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 2041 | req->cex = now; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2042 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2043 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2044 | /* OK let's go on with the BODY now */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2045 | goto process_data; |
| 2046 | |
| 2047 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2048 | txn->req.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2049 | txn->status = 400; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2050 | client_retnclose(t, error_message(t, HTTP_ERR_400)); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 2051 | t->fe->failed_req++; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2052 | return_prx_cond: |
| 2053 | if (!(t->flags & SN_ERR_MASK)) |
| 2054 | t->flags |= SN_ERR_PRXCOND; |
| 2055 | if (!(t->flags & SN_FINST_MASK)) |
| 2056 | t->flags |= SN_FINST_R; |
| 2057 | return 1; |
| 2058 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2059 | } |
| 2060 | else if (c == CL_STDATA) { |
| 2061 | process_data: |
| 2062 | /* FIXME: this error handling is partly buggy because we always report |
| 2063 | * a 'DATA' phase while we don't know if the server was in IDLE, CONN |
| 2064 | * or HEADER phase. BTW, it's not logical to expire the client while |
| 2065 | * we're waiting for the server to connect. |
| 2066 | */ |
| 2067 | /* read or write error */ |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2068 | if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2069 | buffer_shutr(req); |
| 2070 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2071 | fd_delete(t->cli_fd); |
| 2072 | t->cli_state = CL_STCLOSE; |
| 2073 | if (!(t->flags & SN_ERR_MASK)) |
| 2074 | t->flags |= SN_ERR_CLICL; |
| 2075 | if (!(t->flags & SN_FINST_MASK)) { |
| 2076 | if (t->pend_pos) |
| 2077 | t->flags |= SN_FINST_Q; |
| 2078 | else if (s == SV_STCONN) |
| 2079 | t->flags |= SN_FINST_C; |
| 2080 | else |
| 2081 | t->flags |= SN_FINST_D; |
| 2082 | } |
| 2083 | return 1; |
| 2084 | } |
| 2085 | /* last read, or end of server write */ |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2086 | else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2087 | EV_FD_CLR(t->cli_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2088 | buffer_shutr(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2089 | t->cli_state = CL_STSHUTR; |
| 2090 | return 1; |
| 2091 | } |
| 2092 | /* last server read and buffer empty */ |
| 2093 | else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2094 | EV_FD_CLR(t->cli_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2095 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2096 | shutdown(t->cli_fd, SHUT_WR); |
| 2097 | /* We must ensure that the read part is still alive when switching |
| 2098 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2099 | EV_FD_SET(t->cli_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2100 | tv_add_ifset(&req->rex, &now, &t->fe->clitimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2101 | t->cli_state = CL_STSHUTW; |
| 2102 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 2103 | return 1; |
| 2104 | } |
| 2105 | /* read timeout */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2106 | else if (tv_isle(&req->rex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2107 | EV_FD_CLR(t->cli_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2108 | buffer_shutr(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2109 | t->cli_state = CL_STSHUTR; |
| 2110 | if (!(t->flags & SN_ERR_MASK)) |
| 2111 | t->flags |= SN_ERR_CLITO; |
| 2112 | if (!(t->flags & SN_FINST_MASK)) { |
| 2113 | if (t->pend_pos) |
| 2114 | t->flags |= SN_FINST_Q; |
| 2115 | else if (s == SV_STCONN) |
| 2116 | t->flags |= SN_FINST_C; |
| 2117 | else |
| 2118 | t->flags |= SN_FINST_D; |
| 2119 | } |
| 2120 | return 1; |
| 2121 | } |
| 2122 | /* write timeout */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2123 | else if (tv_isle(&rep->wex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2124 | EV_FD_CLR(t->cli_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2125 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2126 | shutdown(t->cli_fd, SHUT_WR); |
| 2127 | /* We must ensure that the read part is still alive when switching |
| 2128 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2129 | EV_FD_SET(t->cli_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2130 | tv_add_ifset(&req->rex, &now, &t->fe->clitimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2131 | |
| 2132 | t->cli_state = CL_STSHUTW; |
| 2133 | if (!(t->flags & SN_ERR_MASK)) |
| 2134 | t->flags |= SN_ERR_CLITO; |
| 2135 | if (!(t->flags & SN_FINST_MASK)) { |
| 2136 | if (t->pend_pos) |
| 2137 | t->flags |= SN_FINST_Q; |
| 2138 | else if (s == SV_STCONN) |
| 2139 | t->flags |= SN_FINST_C; |
| 2140 | else |
| 2141 | t->flags |= SN_FINST_D; |
| 2142 | } |
| 2143 | return 1; |
| 2144 | } |
| 2145 | |
| 2146 | if (req->l >= req->rlim - req->data) { |
| 2147 | /* no room to read more data */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2148 | if (EV_FD_COND_C(t->cli_fd, DIR_RD)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2149 | /* stop reading until we get some space */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2150 | tv_eternity(&req->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2151 | } |
| 2152 | } else { |
| 2153 | /* there's still some space in the buffer */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2154 | if (EV_FD_COND_S(t->cli_fd, DIR_RD)) { |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 2155 | if (!tv_isset(&t->fe->clitimeout) || |
| 2156 | (t->srv_state < SV_STDATA && tv_isset(&t->be->srvtimeout))) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2157 | /* If the client has no timeout, or if the server not ready yet, and we |
| 2158 | * know for sure that it can expire, then it's cleaner to disable the |
| 2159 | * timeout on the client side so that too low values cannot make the |
| 2160 | * sessions abort too early. |
| 2161 | */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2162 | tv_eternity(&req->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2163 | else |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 2164 | tv_add(&req->rex, &now, &t->fe->clitimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2165 | } |
| 2166 | } |
| 2167 | |
| 2168 | if ((rep->l == 0) || |
| 2169 | ((s < SV_STDATA) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2170 | if (EV_FD_COND_C(t->cli_fd, DIR_WR)) { |
| 2171 | /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2172 | tv_eternity(&rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2173 | } |
| 2174 | } else { |
| 2175 | /* buffer not empty */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2176 | if (EV_FD_COND_S(t->cli_fd, DIR_WR)) { |
| 2177 | /* restart writing */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2178 | if (tv_add_ifset(&rep->wex, &now, &t->fe->clitimeout)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2179 | /* FIXME: to prevent the client from expiring read timeouts during writes, |
| 2180 | * we refresh it. */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2181 | req->rex = rep->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2182 | } |
| 2183 | else |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2184 | tv_eternity(&rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2185 | } |
| 2186 | } |
| 2187 | return 0; /* other cases change nothing */ |
| 2188 | } |
| 2189 | else if (c == CL_STSHUTR) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2190 | if (rep->flags & BF_WRITE_ERROR) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2191 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2192 | fd_delete(t->cli_fd); |
| 2193 | t->cli_state = CL_STCLOSE; |
| 2194 | if (!(t->flags & SN_ERR_MASK)) |
| 2195 | t->flags |= SN_ERR_CLICL; |
| 2196 | if (!(t->flags & SN_FINST_MASK)) { |
| 2197 | if (t->pend_pos) |
| 2198 | t->flags |= SN_FINST_Q; |
| 2199 | else if (s == SV_STCONN) |
| 2200 | t->flags |= SN_FINST_C; |
| 2201 | else |
| 2202 | t->flags |= SN_FINST_D; |
| 2203 | } |
| 2204 | return 1; |
| 2205 | } |
| 2206 | else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0) |
| 2207 | && !(t->flags & SN_SELF_GEN)) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2208 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2209 | fd_delete(t->cli_fd); |
| 2210 | t->cli_state = CL_STCLOSE; |
| 2211 | return 1; |
| 2212 | } |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2213 | else if (tv_isle(&rep->wex, &now)) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2214 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2215 | fd_delete(t->cli_fd); |
| 2216 | t->cli_state = CL_STCLOSE; |
| 2217 | if (!(t->flags & SN_ERR_MASK)) |
| 2218 | t->flags |= SN_ERR_CLITO; |
| 2219 | if (!(t->flags & SN_FINST_MASK)) { |
| 2220 | if (t->pend_pos) |
| 2221 | t->flags |= SN_FINST_Q; |
| 2222 | else if (s == SV_STCONN) |
| 2223 | t->flags |= SN_FINST_C; |
| 2224 | else |
| 2225 | t->flags |= SN_FINST_D; |
| 2226 | } |
| 2227 | return 1; |
| 2228 | } |
| 2229 | |
| 2230 | if (t->flags & SN_SELF_GEN) { |
| 2231 | produce_content(t); |
| 2232 | if (rep->l == 0) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2233 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2234 | fd_delete(t->cli_fd); |
| 2235 | t->cli_state = CL_STCLOSE; |
| 2236 | return 1; |
| 2237 | } |
| 2238 | } |
| 2239 | |
| 2240 | if ((rep->l == 0) |
| 2241 | || ((s == SV_STHEADERS) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2242 | if (EV_FD_COND_C(t->cli_fd, DIR_WR)) { |
| 2243 | /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2244 | tv_eternity(&rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2245 | } |
| 2246 | } else { |
| 2247 | /* buffer not empty */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2248 | if (EV_FD_COND_S(t->cli_fd, DIR_WR)) { |
| 2249 | /* restart writing */ |
Willy Tarreau | 33014d0 | 2007-06-03 15:25:37 +0200 | [diff] [blame] | 2250 | if (!tv_add_ifset(&rep->wex, &now, &t->fe->clitimeout)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2251 | tv_eternity(&rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2252 | } |
| 2253 | } |
| 2254 | return 0; |
| 2255 | } |
| 2256 | else if (c == CL_STSHUTW) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2257 | if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2258 | buffer_shutr(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2259 | fd_delete(t->cli_fd); |
| 2260 | t->cli_state = CL_STCLOSE; |
| 2261 | if (!(t->flags & SN_ERR_MASK)) |
| 2262 | t->flags |= SN_ERR_CLICL; |
| 2263 | if (!(t->flags & SN_FINST_MASK)) { |
| 2264 | if (t->pend_pos) |
| 2265 | t->flags |= SN_FINST_Q; |
| 2266 | else if (s == SV_STCONN) |
| 2267 | t->flags |= SN_FINST_C; |
| 2268 | else |
| 2269 | t->flags |= SN_FINST_D; |
| 2270 | } |
| 2271 | return 1; |
| 2272 | } |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2273 | else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2274 | buffer_shutr(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2275 | fd_delete(t->cli_fd); |
| 2276 | t->cli_state = CL_STCLOSE; |
| 2277 | return 1; |
| 2278 | } |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2279 | else if (tv_isle(&req->rex, &now)) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2280 | buffer_shutr(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2281 | fd_delete(t->cli_fd); |
| 2282 | t->cli_state = CL_STCLOSE; |
| 2283 | if (!(t->flags & SN_ERR_MASK)) |
| 2284 | t->flags |= SN_ERR_CLITO; |
| 2285 | if (!(t->flags & SN_FINST_MASK)) { |
| 2286 | if (t->pend_pos) |
| 2287 | t->flags |= SN_FINST_Q; |
| 2288 | else if (s == SV_STCONN) |
| 2289 | t->flags |= SN_FINST_C; |
| 2290 | else |
| 2291 | t->flags |= SN_FINST_D; |
| 2292 | } |
| 2293 | return 1; |
| 2294 | } |
| 2295 | else if (req->l >= req->rlim - req->data) { |
| 2296 | /* no room to read more data */ |
| 2297 | |
| 2298 | /* FIXME-20050705: is it possible for a client to maintain a session |
| 2299 | * after the timeout by sending more data after it receives a close ? |
| 2300 | */ |
| 2301 | |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2302 | if (EV_FD_COND_C(t->cli_fd, DIR_RD)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2303 | /* stop reading until we get some space */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2304 | tv_eternity(&req->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2305 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 2306 | } |
| 2307 | } else { |
| 2308 | /* there's still some space in the buffer */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2309 | if (EV_FD_COND_S(t->cli_fd, DIR_RD)) { |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2310 | if (!tv_add_ifset(&req->rex, &now, &t->fe->clitimeout)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2311 | tv_eternity(&req->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2312 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 2313 | } |
| 2314 | } |
| 2315 | return 0; |
| 2316 | } |
| 2317 | else { /* CL_STCLOSE: nothing to do */ |
| 2318 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 2319 | int len; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2320 | len = sprintf(trash, "%08x:%s.clicls[%04x:%04x]\n", t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2321 | write(1, trash, len); |
| 2322 | } |
| 2323 | return 0; |
| 2324 | } |
| 2325 | return 0; |
| 2326 | } |
| 2327 | |
| 2328 | |
| 2329 | /* |
| 2330 | * manages the server FSM and its socket. It returns 1 if a state has changed |
| 2331 | * (and a resync may be needed), 0 else. |
| 2332 | */ |
| 2333 | int process_srv(struct session *t) |
| 2334 | { |
| 2335 | int s = t->srv_state; |
| 2336 | int c = t->cli_state; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2337 | struct http_txn *txn = &t->txn; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2338 | struct buffer *req = t->req; |
| 2339 | struct buffer *rep = t->rep; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2340 | int conn_err; |
| 2341 | |
| 2342 | #ifdef DEBUG_FULL |
| 2343 | fprintf(stderr,"process_srv: c=%s, s=%s\n", cli_stnames[c], srv_stnames[s]); |
| 2344 | #endif |
Willy Tarreau | ee99136 | 2007-05-14 14:37:50 +0200 | [diff] [blame] | 2345 | |
| 2346 | #if 0 |
| 2347 | fprintf(stderr,"%s:%d fe->clito=%d.%d, fe->conto=%d.%d, fe->srvto=%d.%d\n", |
| 2348 | __FUNCTION__, __LINE__, |
| 2349 | t->fe->clitimeout.tv_sec, t->fe->clitimeout.tv_usec, |
| 2350 | t->fe->contimeout.tv_sec, t->fe->contimeout.tv_usec, |
| 2351 | t->fe->srvtimeout.tv_sec, t->fe->srvtimeout.tv_usec); |
| 2352 | fprintf(stderr,"%s:%d be->clito=%d.%d, be->conto=%d.%d, be->srvto=%d.%d\n", |
| 2353 | __FUNCTION__, __LINE__, |
| 2354 | t->be->clitimeout.tv_sec, t->be->clitimeout.tv_usec, |
| 2355 | t->be->contimeout.tv_sec, t->be->contimeout.tv_usec, |
| 2356 | t->be->srvtimeout.tv_sec, t->be->srvtimeout.tv_usec); |
| 2357 | |
| 2358 | fprintf(stderr,"%s:%d req->cto=%d.%d, req->rto=%d.%d, req->wto=%d.%d\n", |
| 2359 | __FUNCTION__, __LINE__, |
| 2360 | req->cto.tv_sec, req->cto.tv_usec, |
| 2361 | req->rto.tv_sec, req->rto.tv_usec, |
| 2362 | req->wto.tv_sec, req->wto.tv_usec); |
| 2363 | |
| 2364 | fprintf(stderr,"%s:%d rep->cto=%d.%d, rep->rto=%d.%d, rep->wto=%d.%d\n", |
| 2365 | __FUNCTION__, __LINE__, |
| 2366 | rep->cto.tv_sec, rep->cto.tv_usec, |
| 2367 | rep->rto.tv_sec, rep->rto.tv_usec, |
| 2368 | rep->wto.tv_sec, rep->wto.tv_usec); |
| 2369 | #endif |
| 2370 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2371 | //fprintf(stderr,"process_srv: c=%d, s=%d, cr=%d, cw=%d, sr=%d, sw=%d\n", c, s, |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2372 | //EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR), |
| 2373 | //EV_FD_ISSET(t->srv_fd, DIR_RD), EV_FD_ISSET(t->srv_fd, DIR_WR) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2374 | //); |
| 2375 | if (s == SV_STIDLE) { |
| 2376 | if (c == CL_STHEADERS) |
| 2377 | return 0; /* stay in idle, waiting for data to reach the client side */ |
| 2378 | else if (c == CL_STCLOSE || c == CL_STSHUTW || |
| 2379 | (c == CL_STSHUTR && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2380 | (t->req->l == 0 || t->be->options & PR_O_ABRT_CLOSE))) { /* give up */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2381 | tv_eternity(&req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2382 | if (t->pend_pos) |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2383 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2384 | /* note that this must not return any error because it would be able to |
| 2385 | * overwrite the client_retnclose() output. |
| 2386 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2387 | if (txn->flags & TX_CLTARPIT) |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 2388 | srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_T, 0, NULL); |
Willy Tarreau | 08fa2e3 | 2006-09-03 10:47:37 +0200 | [diff] [blame] | 2389 | else |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 2390 | srv_close_with_err(t, SN_ERR_CLICL, t->pend_pos ? SN_FINST_Q : SN_FINST_C, 0, NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2391 | |
| 2392 | return 1; |
| 2393 | } |
| 2394 | else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2395 | if (txn->flags & TX_CLTARPIT) { |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 2396 | /* This connection is being tarpitted. The CLIENT side has |
| 2397 | * already set the connect expiration date to the right |
| 2398 | * timeout. We just have to check that it has not expired. |
| 2399 | */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2400 | if (!tv_isle(&req->cex, &now)) |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 2401 | return 0; |
| 2402 | |
| 2403 | /* We will set the queue timer to the time spent, just for |
| 2404 | * logging purposes. We fake a 500 server error, so that the |
| 2405 | * attacker will not suspect his connection has been tarpitted. |
| 2406 | * It will not cause trouble to the logs because we can exclude |
| 2407 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 2408 | */ |
| 2409 | tv_eternity(&req->cex); |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2410 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 2411 | srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T, |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2412 | 500, error_message(t, HTTP_ERR_500)); |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 2413 | return 1; |
| 2414 | } |
| 2415 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2416 | /* Right now, we will need to create a connection to the server. |
| 2417 | * We might already have tried, and got a connection pending, in |
| 2418 | * which case we will not do anything till it's pending. It's up |
| 2419 | * to any other session to release it and wake us up again. |
| 2420 | */ |
| 2421 | if (t->pend_pos) { |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2422 | if (!tv_isle(&req->cex, &now)) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2423 | return 0; |
| 2424 | else { |
| 2425 | /* we've been waiting too long here */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2426 | tv_eternity(&req->cex); |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2427 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2428 | srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q, |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2429 | 503, error_message(t, HTTP_ERR_503)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2430 | if (t->srv) |
| 2431 | t->srv->failed_conns++; |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 2432 | t->fe->failed_conns++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2433 | return 1; |
| 2434 | } |
| 2435 | } |
| 2436 | |
| 2437 | do { |
| 2438 | /* first, get a connection */ |
| 2439 | if (srv_redispatch_connect(t)) |
| 2440 | return t->srv_state != SV_STIDLE; |
| 2441 | |
| 2442 | /* try to (re-)connect to the server, and fail if we expire the |
| 2443 | * number of retries. |
| 2444 | */ |
| 2445 | if (srv_retryable_connect(t)) { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2446 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2447 | return t->srv_state != SV_STIDLE; |
| 2448 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2449 | } while (1); |
| 2450 | } |
| 2451 | } |
| 2452 | else if (s == SV_STCONN) { /* connection in progress */ |
| 2453 | if (c == CL_STCLOSE || c == CL_STSHUTW || |
| 2454 | (c == CL_STSHUTR && |
Willy Tarreau | c9b654b | 2007-05-08 14:46:53 +0200 | [diff] [blame] | 2455 | ((t->req->l == 0 && !(req->flags & BF_WRITE_STATUS)) || |
| 2456 | t->be->options & PR_O_ABRT_CLOSE))) { /* give up */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2457 | tv_eternity(&req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2458 | fd_delete(t->srv_fd); |
| 2459 | if (t->srv) |
| 2460 | t->srv->cur_sess--; |
| 2461 | |
| 2462 | /* note that this must not return any error because it would be able to |
| 2463 | * overwrite the client_retnclose() output. |
| 2464 | */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 2465 | srv_close_with_err(t, SN_ERR_CLICL, SN_FINST_C, 0, NULL); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2466 | return 1; |
| 2467 | } |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2468 | if (!(req->flags & BF_WRITE_STATUS) && !tv_isle(&req->cex, &now)) { |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2469 | //fprintf(stderr,"1: c=%d, s=%d, now=%d.%06d, exp=%d.%06d\n", c, s, now.tv_sec, now.tv_usec, req->cex.tv_sec, req->cex.tv_usec); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2470 | return 0; /* nothing changed */ |
| 2471 | } |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2472 | else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2473 | /* timeout, asynchronous connect error or first write error */ |
| 2474 | //fprintf(stderr,"2: c=%d, s=%d\n", c, s); |
| 2475 | |
| 2476 | fd_delete(t->srv_fd); |
| 2477 | if (t->srv) |
| 2478 | t->srv->cur_sess--; |
| 2479 | |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2480 | if (!(req->flags & BF_WRITE_STATUS)) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2481 | conn_err = SN_ERR_SRVTO; // it was a connect timeout. |
| 2482 | else |
| 2483 | conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error. |
| 2484 | |
| 2485 | /* ensure that we have enough retries left */ |
| 2486 | if (srv_count_retry_down(t, conn_err)) |
| 2487 | return 1; |
| 2488 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2489 | if (t->srv && t->conn_retries == 0 && t->be->options & PR_O_REDISP) { |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 2490 | /* We're on our last chance, and the REDISP option was specified. |
| 2491 | * We will ignore cookie and force to balance or use the dispatcher. |
| 2492 | */ |
| 2493 | /* let's try to offer this slot to anybody */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2494 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 2495 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 2496 | |
| 2497 | if (t->srv) |
| 2498 | t->srv->failed_conns++; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2499 | t->be->failed_conns++; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 2500 | |
| 2501 | t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
| 2502 | t->srv = NULL; /* it's left to the dispatcher to choose a server */ |
Willy Tarreau | a5e6575 | 2007-03-18 20:53:22 +0100 | [diff] [blame] | 2503 | http_flush_cookie_flags(txn); |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 2504 | |
| 2505 | /* first, get a connection */ |
| 2506 | if (srv_redispatch_connect(t)) |
| 2507 | return t->srv_state != SV_STIDLE; |
| 2508 | } |
| 2509 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2510 | do { |
| 2511 | /* Now we will try to either reconnect to the same server or |
| 2512 | * connect to another server. If the connection gets queued |
| 2513 | * because all servers are saturated, then we will go back to |
| 2514 | * the SV_STIDLE state. |
| 2515 | */ |
| 2516 | if (srv_retryable_connect(t)) { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2517 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2518 | return t->srv_state != SV_STCONN; |
| 2519 | } |
| 2520 | |
| 2521 | /* we need to redispatch the connection to another server */ |
| 2522 | if (srv_redispatch_connect(t)) |
| 2523 | return t->srv_state != SV_STCONN; |
| 2524 | } while (1); |
| 2525 | } |
| 2526 | else { /* no error or write 0 */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2527 | t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2528 | |
| 2529 | //fprintf(stderr,"3: c=%d, s=%d\n", c, s); |
| 2530 | if (req->l == 0) /* nothing to write */ { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2531 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2532 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2533 | } else /* need the right to write */ { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2534 | EV_FD_SET(t->srv_fd, DIR_WR); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2535 | if (tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2536 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 2537 | * we refresh it. */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2538 | rep->rex = req->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2539 | } |
| 2540 | else |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2541 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2542 | } |
| 2543 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2544 | if (t->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2545 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2546 | if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2547 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2548 | |
| 2549 | t->srv_state = SV_STDATA; |
| 2550 | if (t->srv) |
| 2551 | t->srv->cum_sess++; |
| 2552 | rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */ |
| 2553 | |
| 2554 | /* if the user wants to log as soon as possible, without counting |
| 2555 | bytes from the server, then this is the right moment. */ |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 2556 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2557 | t->logs.t_close = t->logs.t_connect; /* to get a valid end date */ |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 2558 | tcp_sess_log(t); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2559 | } |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 2560 | #ifdef CONFIG_HAP_TCPSPLICE |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2561 | if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 2562 | /* TCP splicing supported by both FE and BE */ |
| 2563 | tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0); |
| 2564 | } |
| 2565 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2566 | } |
| 2567 | else { |
| 2568 | t->srv_state = SV_STHEADERS; |
| 2569 | if (t->srv) |
| 2570 | t->srv->cum_sess++; |
| 2571 | rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2572 | t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE; |
| 2573 | /* reset hdr_idx which was already initialized by the request. |
| 2574 | * right now, the http parser does it. |
| 2575 | * hdr_idx_init(&t->txn.hdr_idx); |
| 2576 | */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2577 | } |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2578 | tv_eternity(&req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2579 | return 1; |
| 2580 | } |
| 2581 | } |
| 2582 | else if (s == SV_STHEADERS) { /* receiving server headers */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2583 | /* |
| 2584 | * Now parse the partial (or complete) lines. |
| 2585 | * We will check the response syntax, and also join multi-line |
| 2586 | * headers. An index of all the lines will be elaborated while |
| 2587 | * parsing. |
| 2588 | * |
| 2589 | * For the parsing, we use a 28 states FSM. |
| 2590 | * |
| 2591 | * Here is the information we currently have : |
| 2592 | * rep->data + req->som = beginning of response |
| 2593 | * rep->data + req->eoh = end of processed headers / start of current one |
| 2594 | * rep->data + req->eol = end of current header or line (LF or CRLF) |
| 2595 | * rep->lr = first non-visited byte |
| 2596 | * rep->r = end of data |
| 2597 | */ |
| 2598 | |
| 2599 | int cur_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2600 | struct http_msg *msg = &txn->rsp; |
| 2601 | struct proxy *cur_proxy; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2602 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2603 | if (likely(rep->lr < rep->r)) |
| 2604 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2605 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2606 | /* 1: we might have to print this header in debug mode */ |
| 2607 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2608 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
| 2609 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
| 2610 | char *eol, *sol; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2611 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2612 | sol = rep->data + msg->som; |
| 2613 | eol = sol + msg->sl.rq.l; |
| 2614 | debug_hdr("srvrep", t, sol, eol); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2615 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2616 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2617 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2618 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2619 | while (cur_idx) { |
| 2620 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2621 | debug_hdr("srvhdr", t, sol, eol); |
| 2622 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2623 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 2624 | } |
| 2625 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2626 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2627 | |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2628 | if ((rep->l < rep->rlim - rep->data) && EV_FD_COND_S(t->srv_fd, DIR_RD)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2629 | /* fd in DIR_RD was disabled, perhaps because of a previous buffer |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2630 | * full. We cannot loop here since stream_sock_read will disable it only if |
| 2631 | * rep->l == rlim-data |
| 2632 | */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2633 | if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2634 | tv_eternity(&rep->rex); |
| 2635 | } |
| 2636 | |
| 2637 | |
| 2638 | /* |
| 2639 | * Now we quickly check if we have found a full valid response. |
| 2640 | * If not so, we check the FD and buffer states before leaving. |
| 2641 | * A full response is indicated by the fact that we have seen |
| 2642 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 2643 | * responses are checked first. |
| 2644 | * |
| 2645 | * Depending on whether the client is still there or not, we |
| 2646 | * may send an error response back or not. Note that normally |
| 2647 | * we should only check for HTTP status there, and check I/O |
| 2648 | * errors somewhere else. |
| 2649 | */ |
| 2650 | |
| 2651 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
| 2652 | |
| 2653 | /* Invalid response, or read error or write error */ |
| 2654 | if (unlikely((msg->msg_state == HTTP_MSG_ERROR) || |
| 2655 | (req->flags & BF_WRITE_ERROR) || |
| 2656 | (rep->flags & BF_READ_ERROR))) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2657 | buffer_shutr(rep); |
| 2658 | buffer_shutw(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2659 | fd_delete(t->srv_fd); |
| 2660 | if (t->srv) { |
| 2661 | t->srv->cur_sess--; |
| 2662 | t->srv->failed_resp++; |
| 2663 | } |
| 2664 | t->be->failed_resp++; |
| 2665 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2666 | txn->status = 502; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2667 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 2668 | if (!(t->flags & SN_ERR_MASK)) |
| 2669 | t->flags |= SN_ERR_SRVCL; |
| 2670 | if (!(t->flags & SN_FINST_MASK)) |
| 2671 | t->flags |= SN_FINST_H; |
| 2672 | /* We used to have a free connection slot. Since we'll never use it, |
| 2673 | * we have to inform the server that it may be used by another session. |
| 2674 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2675 | if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 2676 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2677 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2678 | return 1; |
| 2679 | } |
| 2680 | |
| 2681 | /* end of client write or end of server read. |
| 2682 | * since we are in header mode, if there's no space left for headers, we |
| 2683 | * won't be able to free more later, so the session will never terminate. |
| 2684 | */ |
| 2685 | else if (unlikely(rep->flags & BF_READ_NULL || |
| 2686 | c == CL_STSHUTW || c == CL_STCLOSE || |
| 2687 | rep->l >= rep->rlim - rep->data)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2688 | EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2689 | buffer_shutr(rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2690 | t->srv_state = SV_STSHUTR; |
| 2691 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 2692 | return 1; |
| 2693 | } |
| 2694 | |
| 2695 | /* read timeout : return a 504 to the client. |
| 2696 | */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2697 | else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_RD) && |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2698 | tv_isle(&rep->rex, &now))) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2699 | buffer_shutr(rep); |
| 2700 | buffer_shutw(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2701 | fd_delete(t->srv_fd); |
| 2702 | if (t->srv) { |
| 2703 | t->srv->cur_sess--; |
| 2704 | t->srv->failed_resp++; |
| 2705 | } |
| 2706 | t->be->failed_resp++; |
| 2707 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2708 | txn->status = 504; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2709 | client_return(t, error_message(t, HTTP_ERR_504)); |
| 2710 | if (!(t->flags & SN_ERR_MASK)) |
| 2711 | t->flags |= SN_ERR_SRVTO; |
| 2712 | if (!(t->flags & SN_FINST_MASK)) |
| 2713 | t->flags |= SN_FINST_H; |
| 2714 | /* We used to have a free connection slot. Since we'll never use it, |
| 2715 | * we have to inform the server that it may be used by another session. |
| 2716 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2717 | if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 2718 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2719 | return 1; |
| 2720 | } |
| 2721 | |
| 2722 | /* last client read and buffer empty */ |
| 2723 | /* FIXME!!! here, we don't want to switch to SHUTW if the |
| 2724 | * client shuts read too early, because we may still have |
| 2725 | * some work to do on the headers. |
| 2726 | * The side-effect is that if the client completely closes its |
| 2727 | * connection during SV_STHEADER, the connection to the server |
| 2728 | * is kept until a response comes back or the timeout is reached. |
| 2729 | */ |
| 2730 | else if (unlikely((/*c == CL_STSHUTR ||*/ c == CL_STCLOSE) && |
| 2731 | (req->l == 0))) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2732 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2733 | buffer_shutw(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2734 | |
| 2735 | /* We must ensure that the read part is still |
| 2736 | * alive when switching to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2737 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2738 | tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2739 | |
| 2740 | shutdown(t->srv_fd, SHUT_WR); |
| 2741 | t->srv_state = SV_STSHUTW; |
| 2742 | return 1; |
| 2743 | } |
| 2744 | |
| 2745 | /* write timeout */ |
| 2746 | /* FIXME!!! here, we don't want to switch to SHUTW if the |
| 2747 | * client shuts read too early, because we may still have |
| 2748 | * some work to do on the headers. |
| 2749 | */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2750 | else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_WR) && |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2751 | tv_isle(&req->wex, &now))) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2752 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2753 | buffer_shutw(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2754 | shutdown(t->srv_fd, SHUT_WR); |
| 2755 | /* We must ensure that the read part is still alive |
| 2756 | * when switching to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2757 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2758 | tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2759 | |
| 2760 | t->srv_state = SV_STSHUTW; |
| 2761 | if (!(t->flags & SN_ERR_MASK)) |
| 2762 | t->flags |= SN_ERR_SRVTO; |
| 2763 | if (!(t->flags & SN_FINST_MASK)) |
| 2764 | t->flags |= SN_FINST_H; |
| 2765 | return 1; |
| 2766 | } |
| 2767 | |
| 2768 | /* |
| 2769 | * And now the non-error cases. |
| 2770 | */ |
| 2771 | |
| 2772 | /* Data remaining in the request buffer. |
| 2773 | * This happens during the first pass here, and during |
| 2774 | * long posts. |
| 2775 | */ |
| 2776 | else if (likely(req->l)) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2777 | if (EV_FD_COND_S(t->srv_fd, DIR_WR)) { |
| 2778 | /* restart writing */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2779 | if (tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2780 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 2781 | * we refresh it. */ |
| 2782 | rep->rex = req->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2783 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2784 | else |
| 2785 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2786 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2787 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2788 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2789 | /* nothing left in the request buffer */ |
| 2790 | else { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2791 | if (EV_FD_COND_C(t->srv_fd, DIR_WR)) { |
| 2792 | /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2793 | tv_eternity(&req->wex); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2794 | } |
| 2795 | } |
| 2796 | |
| 2797 | return t->srv_state != SV_STHEADERS; |
| 2798 | } |
| 2799 | |
| 2800 | |
| 2801 | /***************************************************************** |
| 2802 | * More interesting part now : we know that we have a complete * |
| 2803 | * response which at least looks like HTTP. We have an indicator * |
| 2804 | * of each header's length, so we can parse them quickly. * |
| 2805 | ****************************************************************/ |
| 2806 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2807 | /* ensure we keep this pointer to the beginning of the message */ |
| 2808 | msg->sol = rep->data + msg->som; |
| 2809 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2810 | /* |
| 2811 | * 1: get the status code and check for cacheability. |
| 2812 | */ |
| 2813 | |
| 2814 | t->logs.logwait &= ~LW_RESP; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2815 | txn->status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2816 | |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2817 | switch (txn->status) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2818 | case 200: |
| 2819 | case 203: |
| 2820 | case 206: |
| 2821 | case 300: |
| 2822 | case 301: |
| 2823 | case 410: |
| 2824 | /* RFC2616 @13.4: |
| 2825 | * "A response received with a status code of |
| 2826 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 2827 | * by a cache (...) unless a cache-control |
| 2828 | * directive prohibits caching." |
| 2829 | * |
| 2830 | * RFC2616 @9.5: POST method : |
| 2831 | * "Responses to this method are not cacheable, |
| 2832 | * unless the response includes appropriate |
| 2833 | * Cache-Control or Expires header fields." |
| 2834 | */ |
| 2835 | if (likely(txn->meth != HTTP_METH_POST) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2836 | unlikely(t->be->options & PR_O_CHK_CACHE)) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2837 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2838 | break; |
| 2839 | default: |
| 2840 | break; |
| 2841 | } |
| 2842 | |
| 2843 | /* |
| 2844 | * 2: we may need to capture headers |
| 2845 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2846 | if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2847 | capture_headers(rep->data + msg->som, &txn->hdr_idx, |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2848 | txn->rsp.cap, t->fe->rsp_cap); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2849 | |
| 2850 | /* |
| 2851 | * 3: we will have to evaluate the filters. |
| 2852 | * As opposed to version 1.2, now they will be evaluated in the |
| 2853 | * filters order and not in the header order. This means that |
| 2854 | * each filter has to be validated among all headers. |
| 2855 | * |
| 2856 | * Filters are tried with ->be first, then with ->fe if it is |
| 2857 | * different from ->be. |
| 2858 | */ |
| 2859 | |
| 2860 | t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */ |
| 2861 | |
| 2862 | cur_proxy = t->be; |
| 2863 | while (1) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2864 | struct proxy *rule_set = cur_proxy; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2865 | |
| 2866 | /* try headers filters */ |
| 2867 | if (rule_set->rsp_exp != NULL) { |
| 2868 | if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) { |
| 2869 | return_bad_resp: |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2870 | if (t->srv) { |
| 2871 | t->srv->cur_sess--; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2872 | t->srv->failed_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2873 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2874 | cur_proxy->failed_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2875 | return_srv_prx_502: |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2876 | buffer_shutr(rep); |
| 2877 | buffer_shutw(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2878 | fd_delete(t->srv_fd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2879 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2880 | txn->status = 502; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2881 | client_return(t, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2882 | if (!(t->flags & SN_ERR_MASK)) |
| 2883 | t->flags |= SN_ERR_PRXCOND; |
| 2884 | if (!(t->flags & SN_FINST_MASK)) |
| 2885 | t->flags |= SN_FINST_H; |
| 2886 | /* We used to have a free connection slot. Since we'll never use it, |
| 2887 | * we have to inform the server that it may be used by another session. |
| 2888 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2889 | if (t->srv && may_dequeue_tasks(t->srv, cur_proxy)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 2890 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2891 | return 1; |
| 2892 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2893 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2894 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2895 | /* has the response been denied ? */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2896 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2897 | if (t->srv) { |
| 2898 | t->srv->cur_sess--; |
| 2899 | t->srv->failed_secu++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2900 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2901 | cur_proxy->denied_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2902 | goto return_srv_prx_502; |
| 2903 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2904 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2905 | /* We might have to check for "Connection:" */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2906 | if (((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2907 | !(t->flags & SN_CONN_CLOSED)) { |
| 2908 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2909 | int cur_idx, old_idx, delta, val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2910 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2911 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2912 | cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 2913 | old_idx = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2914 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2915 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 2916 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 2917 | cur_ptr = cur_next; |
| 2918 | cur_end = cur_ptr + cur_hdr->len; |
| 2919 | cur_next = cur_end + cur_hdr->cr + 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2920 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2921 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 2922 | if (val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2923 | /* 3 possibilities : |
| 2924 | * - we have already set Connection: close, |
| 2925 | * so we remove this line. |
| 2926 | * - we have not yet set Connection: close, |
| 2927 | * but this line indicates close. We leave |
| 2928 | * it untouched and set the flag. |
| 2929 | * - we have not yet set Connection: close, |
| 2930 | * and this line indicates non-close. We |
| 2931 | * replace it. |
| 2932 | */ |
| 2933 | if (t->flags & SN_CONN_CLOSED) { |
| 2934 | delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0); |
| 2935 | txn->rsp.eoh += delta; |
| 2936 | cur_next += delta; |
| 2937 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 2938 | txn->hdr_idx.used--; |
| 2939 | cur_hdr->len = 0; |
| 2940 | } else { |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2941 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 2942 | delta = buffer_replace2(rep, cur_ptr + val, cur_end, |
| 2943 | "close", 5); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2944 | cur_next += delta; |
| 2945 | cur_hdr->len += delta; |
| 2946 | txn->rsp.eoh += delta; |
| 2947 | } |
| 2948 | t->flags |= SN_CONN_CLOSED; |
| 2949 | } |
| 2950 | } |
| 2951 | old_idx = cur_idx; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2952 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2953 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2954 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2955 | /* add response headers from the rule sets in the same order */ |
| 2956 | for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) { |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2957 | if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, |
| 2958 | rule_set->rsp_add[cur_idx])) < 0) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2959 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2960 | } |
| 2961 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2962 | /* check whether we're already working on the frontend */ |
| 2963 | if (cur_proxy == t->fe) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2964 | break; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2965 | cur_proxy = t->fe; |
| 2966 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2967 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2968 | /* |
| 2969 | * 4: check for server cookie. |
| 2970 | */ |
| 2971 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2972 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2973 | /* |
| 2974 | * 5: add server cookie in the response if needed |
| 2975 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2976 | if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) && |
| 2977 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2978 | int len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2979 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2980 | /* the server is known, it's not the one the client requested, we have to |
| 2981 | * insert a set-cookie here, except if we want to insert only on POST |
| 2982 | * requests and this one isn't. Note that servers which don't have cookies |
| 2983 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2984 | */ |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2985 | len = sprintf(trash, "Set-Cookie: %s=%s; path=/", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2986 | t->be->cookie_name, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2987 | 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] | 2988 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2989 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 2990 | trash, len)) < 0) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2991 | goto return_bad_resp; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2992 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2993 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2994 | /* Here, we will tell an eventual cache on the client side that we don't |
| 2995 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 2996 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 2997 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 2998 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2999 | if (t->be->options & PR_O_COOK_NOC) { |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3000 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3001 | "Cache-control: private", 22)) < 0) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3002 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3003 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3004 | } |
| 3005 | |
| 3006 | |
| 3007 | /* |
| 3008 | * 6: check for cache-control or pragma headers. |
| 3009 | */ |
| 3010 | check_response_for_cacheability(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3011 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3012 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3013 | /* |
| 3014 | * 7: check if result will be cacheable with a cookie. |
| 3015 | * We'll block the response if security checks have caught |
| 3016 | * nasty things such as a cacheable cookie. |
| 3017 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3018 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) == |
| 3019 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3020 | (t->be->options & PR_O_CHK_CACHE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3021 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3022 | /* we're in presence of a cacheable response containing |
| 3023 | * a set-cookie header. We'll block it as requested by |
| 3024 | * the 'checkcache' option, and send an alert. |
| 3025 | */ |
| 3026 | if (t->srv) { |
| 3027 | t->srv->cur_sess--; |
| 3028 | t->srv->failed_secu++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3029 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3030 | t->be->denied_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3031 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3032 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3033 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3034 | send_log(t->be, LOG_ALERT, |
| 3035 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3036 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3037 | goto return_srv_prx_502; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3038 | } |
| 3039 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3040 | /* |
| 3041 | * 8: add "Connection: close" if needed and not yet set. |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 3042 | * 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] | 3043 | */ |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 3044 | if (!(t->flags & SN_CONN_CLOSED) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3045 | ((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE)) { |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 3046 | if ((unlikely(msg->sl.st.v_l != 8) || |
| 3047 | unlikely(req->data[msg->som + 7] != '0')) && |
| 3048 | unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3049 | "Connection: close", 17)) < 0) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3050 | goto return_bad_resp; |
| 3051 | t->flags |= SN_CONN_CLOSED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3052 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3053 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3054 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3055 | /************************************************************* |
| 3056 | * OK, that's finished for the headers. We have done what we * |
| 3057 | * could. Let's switch to the DATA state. * |
| 3058 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3059 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3060 | t->srv_state = SV_STDATA; |
| 3061 | rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 3062 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3063 | |
| 3064 | /* client connection already closed or option 'forceclose' required : |
| 3065 | * we close the server's outgoing connection right now. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3066 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3067 | if ((req->l == 0) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3068 | (c == CL_STSHUTR || c == CL_STCLOSE || t->be->options & PR_O_FORCE_CLO)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3069 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3070 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3071 | |
| 3072 | /* We must ensure that the read part is still alive when switching |
| 3073 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3074 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3075 | tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3076 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3077 | shutdown(t->srv_fd, SHUT_WR); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3078 | t->srv_state = SV_STSHUTW; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3079 | } |
| 3080 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3081 | #ifdef CONFIG_HAP_TCPSPLICE |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3082 | if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3083 | /* TCP splicing supported by both FE and BE */ |
| 3084 | tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3085 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3086 | #endif |
| 3087 | /* if the user wants to log as soon as possible, without counting |
| 3088 | bytes from the server, then this is the right moment. */ |
| 3089 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 3090 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
Willy Tarreau | b498717 | 2007-03-18 16:28:03 +0100 | [diff] [blame] | 3091 | t->logs.bytes_in = txn->rsp.eoh; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 3092 | if (t->fe->to_log & LW_REQ) |
| 3093 | http_sess_log(t); |
| 3094 | else |
| 3095 | tcp_sess_log(t); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3096 | } |
| 3097 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3098 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 3099 | * otherwise we would not let the client side wake up. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3100 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3101 | |
| 3102 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3103 | } |
| 3104 | else if (s == SV_STDATA) { |
| 3105 | /* read or write error */ |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3106 | if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3107 | buffer_shutr(rep); |
| 3108 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3109 | fd_delete(t->srv_fd); |
| 3110 | if (t->srv) { |
| 3111 | t->srv->cur_sess--; |
| 3112 | t->srv->failed_resp++; |
| 3113 | } |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 3114 | t->be->failed_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3115 | t->srv_state = SV_STCLOSE; |
| 3116 | if (!(t->flags & SN_ERR_MASK)) |
| 3117 | t->flags |= SN_ERR_SRVCL; |
| 3118 | if (!(t->flags & SN_FINST_MASK)) |
| 3119 | t->flags |= SN_FINST_D; |
| 3120 | /* We used to have a free connection slot. Since we'll never use it, |
| 3121 | * we have to inform the server that it may be used by another session. |
| 3122 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3123 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3124 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3125 | |
| 3126 | return 1; |
| 3127 | } |
| 3128 | /* last read, or end of client write */ |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3129 | else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3130 | EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3131 | buffer_shutr(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3132 | t->srv_state = SV_STSHUTR; |
| 3133 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 3134 | return 1; |
| 3135 | } |
| 3136 | /* end of client read and no more data to send */ |
| 3137 | else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3138 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3139 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3140 | shutdown(t->srv_fd, SHUT_WR); |
| 3141 | /* We must ensure that the read part is still alive when switching |
| 3142 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3143 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3144 | tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3145 | |
| 3146 | t->srv_state = SV_STSHUTW; |
| 3147 | return 1; |
| 3148 | } |
| 3149 | /* read timeout */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3150 | else if (tv_isle(&rep->rex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3151 | EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3152 | buffer_shutr(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3153 | t->srv_state = SV_STSHUTR; |
| 3154 | if (!(t->flags & SN_ERR_MASK)) |
| 3155 | t->flags |= SN_ERR_SRVTO; |
| 3156 | if (!(t->flags & SN_FINST_MASK)) |
| 3157 | t->flags |= SN_FINST_D; |
| 3158 | return 1; |
| 3159 | } |
| 3160 | /* write timeout */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3161 | else if (tv_isle(&req->wex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3162 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3163 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3164 | shutdown(t->srv_fd, SHUT_WR); |
| 3165 | /* We must ensure that the read part is still alive when switching |
| 3166 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3167 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3168 | tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3169 | t->srv_state = SV_STSHUTW; |
| 3170 | if (!(t->flags & SN_ERR_MASK)) |
| 3171 | t->flags |= SN_ERR_SRVTO; |
| 3172 | if (!(t->flags & SN_FINST_MASK)) |
| 3173 | t->flags |= SN_FINST_D; |
| 3174 | return 1; |
| 3175 | } |
| 3176 | |
| 3177 | /* recompute request time-outs */ |
| 3178 | if (req->l == 0) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3179 | if (EV_FD_COND_C(t->srv_fd, DIR_WR)) { |
| 3180 | /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3181 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3182 | } |
| 3183 | } |
| 3184 | else { /* buffer not empty, there are still data to be transferred */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3185 | if (EV_FD_COND_S(t->srv_fd, DIR_WR)) { |
| 3186 | /* restart writing */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3187 | if (tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3188 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 3189 | * we refresh it. */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3190 | rep->rex = req->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3191 | } |
| 3192 | else |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3193 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3194 | } |
| 3195 | } |
| 3196 | |
| 3197 | /* recompute response time-outs */ |
| 3198 | if (rep->l == BUFSIZE) { /* no room to read more data */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3199 | if (EV_FD_COND_C(t->srv_fd, DIR_RD)) { |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3200 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3201 | } |
| 3202 | } |
| 3203 | else { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3204 | if (EV_FD_COND_S(t->srv_fd, DIR_RD)) { |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3205 | if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3206 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3207 | } |
| 3208 | } |
| 3209 | |
| 3210 | return 0; /* other cases change nothing */ |
| 3211 | } |
| 3212 | else if (s == SV_STSHUTR) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3213 | if (req->flags & BF_WRITE_ERROR) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3214 | //EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3215 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3216 | fd_delete(t->srv_fd); |
| 3217 | if (t->srv) { |
| 3218 | t->srv->cur_sess--; |
| 3219 | t->srv->failed_resp++; |
| 3220 | } |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 3221 | t->be->failed_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3222 | //close(t->srv_fd); |
| 3223 | t->srv_state = SV_STCLOSE; |
| 3224 | if (!(t->flags & SN_ERR_MASK)) |
| 3225 | t->flags |= SN_ERR_SRVCL; |
| 3226 | if (!(t->flags & SN_FINST_MASK)) |
| 3227 | t->flags |= SN_FINST_D; |
| 3228 | /* We used to have a free connection slot. Since we'll never use it, |
| 3229 | * we have to inform the server that it may be used by another session. |
| 3230 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3231 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3232 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3233 | |
| 3234 | return 1; |
| 3235 | } |
| 3236 | else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3237 | //EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3238 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3239 | fd_delete(t->srv_fd); |
| 3240 | if (t->srv) |
| 3241 | t->srv->cur_sess--; |
| 3242 | //close(t->srv_fd); |
| 3243 | t->srv_state = SV_STCLOSE; |
| 3244 | /* We used to have a free connection slot. Since we'll never use it, |
| 3245 | * we have to inform the server that it may be used by another session. |
| 3246 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3247 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3248 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3249 | |
| 3250 | return 1; |
| 3251 | } |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3252 | else if (tv_isle(&req->wex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3253 | //EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3254 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3255 | fd_delete(t->srv_fd); |
| 3256 | if (t->srv) |
| 3257 | t->srv->cur_sess--; |
| 3258 | //close(t->srv_fd); |
| 3259 | t->srv_state = SV_STCLOSE; |
| 3260 | if (!(t->flags & SN_ERR_MASK)) |
| 3261 | t->flags |= SN_ERR_SRVTO; |
| 3262 | if (!(t->flags & SN_FINST_MASK)) |
| 3263 | t->flags |= SN_FINST_D; |
| 3264 | /* We used to have a free connection slot. Since we'll never use it, |
| 3265 | * we have to inform the server that it may be used by another session. |
| 3266 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3267 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3268 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3269 | |
| 3270 | return 1; |
| 3271 | } |
| 3272 | else if (req->l == 0) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3273 | if (EV_FD_COND_C(t->srv_fd, DIR_WR)) { |
| 3274 | /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3275 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3276 | } |
| 3277 | } |
| 3278 | else { /* buffer not empty */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3279 | if (EV_FD_COND_S(t->srv_fd, DIR_WR)) { |
| 3280 | /* restart writing */ |
Willy Tarreau | 33014d0 | 2007-06-03 15:25:37 +0200 | [diff] [blame] | 3281 | if (!tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3282 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3283 | } |
| 3284 | } |
| 3285 | return 0; |
| 3286 | } |
| 3287 | else if (s == SV_STSHUTW) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3288 | if (rep->flags & BF_READ_ERROR) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3289 | //EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3290 | buffer_shutr(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3291 | fd_delete(t->srv_fd); |
| 3292 | if (t->srv) { |
| 3293 | t->srv->cur_sess--; |
| 3294 | t->srv->failed_resp++; |
| 3295 | } |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 3296 | t->be->failed_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3297 | //close(t->srv_fd); |
| 3298 | t->srv_state = SV_STCLOSE; |
| 3299 | if (!(t->flags & SN_ERR_MASK)) |
| 3300 | t->flags |= SN_ERR_SRVCL; |
| 3301 | if (!(t->flags & SN_FINST_MASK)) |
| 3302 | t->flags |= SN_FINST_D; |
| 3303 | /* We used to have a free connection slot. Since we'll never use it, |
| 3304 | * we have to inform the server that it may be used by another session. |
| 3305 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3306 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3307 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3308 | |
| 3309 | return 1; |
| 3310 | } |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3311 | else if (rep->flags & BF_READ_NULL || c == CL_STSHUTW || c == CL_STCLOSE) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3312 | //EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3313 | buffer_shutr(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3314 | fd_delete(t->srv_fd); |
| 3315 | if (t->srv) |
| 3316 | t->srv->cur_sess--; |
| 3317 | //close(t->srv_fd); |
| 3318 | t->srv_state = SV_STCLOSE; |
| 3319 | /* We used to have a free connection slot. Since we'll never use it, |
| 3320 | * we have to inform the server that it may be used by another session. |
| 3321 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3322 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3323 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3324 | |
| 3325 | return 1; |
| 3326 | } |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3327 | else if (tv_isle(&rep->rex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3328 | //EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3329 | buffer_shutr(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3330 | fd_delete(t->srv_fd); |
| 3331 | if (t->srv) |
| 3332 | t->srv->cur_sess--; |
| 3333 | //close(t->srv_fd); |
| 3334 | t->srv_state = SV_STCLOSE; |
| 3335 | if (!(t->flags & SN_ERR_MASK)) |
| 3336 | t->flags |= SN_ERR_SRVTO; |
| 3337 | if (!(t->flags & SN_FINST_MASK)) |
| 3338 | t->flags |= SN_FINST_D; |
| 3339 | /* We used to have a free connection slot. Since we'll never use it, |
| 3340 | * we have to inform the server that it may be used by another session. |
| 3341 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3342 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3343 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3344 | |
| 3345 | return 1; |
| 3346 | } |
| 3347 | else if (rep->l == BUFSIZE) { /* no room to read more data */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3348 | if (EV_FD_COND_C(t->srv_fd, DIR_RD)) { |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3349 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3350 | } |
| 3351 | } |
| 3352 | else { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3353 | if (EV_FD_COND_S(t->srv_fd, DIR_RD)) { |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3354 | if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3355 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3356 | } |
| 3357 | } |
| 3358 | return 0; |
| 3359 | } |
| 3360 | else { /* SV_STCLOSE : nothing to do */ |
| 3361 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 3362 | int len; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3363 | len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3364 | t->uniq_id, t->be->id, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3365 | write(1, trash, len); |
| 3366 | } |
| 3367 | return 0; |
| 3368 | } |
| 3369 | return 0; |
| 3370 | } |
| 3371 | |
| 3372 | |
| 3373 | /* |
| 3374 | * Produces data for the session <s> depending on its source. Expects to be |
| 3375 | * called with s->cli_state == CL_STSHUTR. Right now, only statistics can be |
| 3376 | * produced. It stops by itself by unsetting the SN_SELF_GEN flag from the |
| 3377 | * session, which it uses to keep on being called when there is free space in |
| 3378 | * the buffer, of simply by letting an empty buffer upon return. It returns 1 |
| 3379 | * if it changes the session state from CL_STSHUTR, otherwise 0. |
| 3380 | */ |
| 3381 | int produce_content(struct session *s) |
| 3382 | { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3383 | if (s->data_source == DATA_SRC_NONE) { |
| 3384 | s->flags &= ~SN_SELF_GEN; |
| 3385 | return 1; |
| 3386 | } |
| 3387 | else if (s->data_source == DATA_SRC_STATS) { |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3388 | /* dump server statistics */ |
| 3389 | return produce_content_stats(s); |
| 3390 | } |
| 3391 | else { |
| 3392 | /* unknown data source */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 3393 | s->txn.status = 500; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3394 | client_retnclose(s, error_message(s, HTTP_ERR_500)); |
| 3395 | if (!(s->flags & SN_ERR_MASK)) |
| 3396 | s->flags |= SN_ERR_PRXCOND; |
| 3397 | if (!(s->flags & SN_FINST_MASK)) |
| 3398 | s->flags |= SN_FINST_R; |
| 3399 | s->flags &= ~SN_SELF_GEN; |
| 3400 | return 1; |
| 3401 | } |
| 3402 | } |
| 3403 | |
| 3404 | |
| 3405 | /* |
| 3406 | * Produces statistics data for the session <s>. Expects to be called with |
| 3407 | * s->cli_state == CL_STSHUTR. It stops by itself by unsetting the SN_SELF_GEN |
| 3408 | * flag from the session, which it uses to keep on being called when there is |
| 3409 | * free space in the buffer, of simply by letting an empty buffer upon return. |
| 3410 | * It returns 1 if it changes the session state from CL_STSHUTR, otherwise 0. |
| 3411 | */ |
| 3412 | int produce_content_stats(struct session *s) |
| 3413 | { |
| 3414 | struct buffer *rep = s->rep; |
| 3415 | struct proxy *px; |
| 3416 | struct chunk msg; |
| 3417 | unsigned int up; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3418 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3419 | msg.len = 0; |
| 3420 | msg.str = trash; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3421 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3422 | switch (s->data_state) { |
| 3423 | case DATA_ST_INIT: |
| 3424 | /* the function had not been called yet */ |
| 3425 | s->flags |= SN_SELF_GEN; // more data will follow |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3426 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3427 | chunk_printf(&msg, sizeof(trash), |
| 3428 | "HTTP/1.0 200 OK\r\n" |
| 3429 | "Cache-Control: no-cache\r\n" |
| 3430 | "Connection: close\r\n" |
| 3431 | "Content-Type: text/html\r\n" |
| 3432 | "\r\n"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3433 | |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 3434 | s->txn.status = 200; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3435 | client_retnclose(s, &msg); // send the start of the response. |
| 3436 | msg.len = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3437 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3438 | if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is |
| 3439 | s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy |
| 3440 | if (!(s->flags & SN_FINST_MASK)) |
| 3441 | s->flags |= SN_FINST_R; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3442 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 3443 | if (s->txn.meth == HTTP_METH_HEAD) { |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 3444 | /* that's all we return in case of HEAD request */ |
| 3445 | s->data_state = DATA_ST_FIN; |
| 3446 | s->flags &= ~SN_SELF_GEN; |
| 3447 | return 1; |
| 3448 | } |
| 3449 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3450 | s->data_state = DATA_ST_HEAD; /* let's start producing data */ |
| 3451 | /* fall through */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3452 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3453 | case DATA_ST_HEAD: |
| 3454 | /* WARNING! This must fit in the first buffer !!! */ |
| 3455 | chunk_printf(&msg, sizeof(trash), |
| 3456 | "<html><head><title>Statistics Report for " PRODUCT_NAME "</title>\n" |
| 3457 | "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n" |
| 3458 | "<style type=\"text/css\"><!--\n" |
| 3459 | "body {" |
| 3460 | " font-family: helvetica, arial;" |
| 3461 | " font-size: 12px;" |
| 3462 | " font-weight: normal;" |
| 3463 | " color: black;" |
| 3464 | " background: white;" |
| 3465 | "}\n" |
| 3466 | "th,td {" |
| 3467 | " font-size: 0.8em;" |
| 3468 | " align: center;" |
Willy Tarreau | f2b74c2 | 2007-03-25 22:44:08 +0200 | [diff] [blame] | 3469 | "}\n" |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3470 | "h1 {" |
| 3471 | " font-size: xx-large;" |
| 3472 | " margin-bottom: 0.5em;" |
| 3473 | "}\n" |
| 3474 | "h2 {" |
| 3475 | " font-family: helvetica, arial;" |
| 3476 | " font-size: x-large;" |
| 3477 | " font-weight: bold;" |
| 3478 | " font-style: italic;" |
| 3479 | " color: #6020a0;" |
| 3480 | " margin-top: 0em;" |
| 3481 | " margin-bottom: 0em;" |
| 3482 | "}\n" |
| 3483 | "h3 {" |
| 3484 | " font-family: helvetica, arial;" |
| 3485 | " font-size: 16px;" |
| 3486 | " font-weight: bold;" |
| 3487 | " color: #b00040;" |
| 3488 | " background: #e8e8d0;" |
| 3489 | " margin-top: 0em;" |
| 3490 | " margin-bottom: 0em;" |
| 3491 | "}\n" |
| 3492 | "li {" |
| 3493 | " margin-top: 0.25em;" |
| 3494 | " margin-right: 2em;" |
| 3495 | "}\n" |
| 3496 | ".hr {margin-top: 0.25em;" |
| 3497 | " border-color: black;" |
| 3498 | " border-bottom-style: solid;" |
| 3499 | "}\n" |
| 3500 | ".pxname {background: #b00040;color: #ffff40;font-weight: bold;}\n" |
| 3501 | ".titre {background: #20D0D0;color: #000000;font-weight: bold;}\n" |
| 3502 | ".total {background: #20D0D0;color: #ffff80;}\n" |
| 3503 | ".frontend {background: #e8e8d0;}\n" |
| 3504 | ".backend {background: #e8e8d0;}\n" |
| 3505 | ".active0 {background: #ff9090;}\n" |
| 3506 | ".active1 {background: #ffd020;}\n" |
| 3507 | ".active2 {background: #ffffa0;}\n" |
| 3508 | ".active3 {background: #c0ffc0;}\n" |
| 3509 | ".active4 {background: #e0e0e0;}\n" |
| 3510 | ".backup0 {background: #ff9090;}\n" |
| 3511 | ".backup1 {background: #ff80ff;}\n" |
| 3512 | ".backup2 {background: #c060ff;}\n" |
| 3513 | ".backup3 {background: #b0d0ff;}\n" |
| 3514 | ".backup4 {background: #e0e0e0;}\n" |
| 3515 | "table.tbl { border-collapse: collapse; border-style: none;}\n" |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 3516 | "table.tbl td { border-width: 1px 1px 1px 1px; border-style: solid solid solid solid; padding: 2px 3px; border-color: gray;}\n" |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3517 | "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n" |
| 3518 | "table.tbl th.empty { border-style: none; empty-cells: hide;}\n" |
| 3519 | "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n" |
| 3520 | "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n" |
| 3521 | "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n" |
Willy Tarreau | f2b74c2 | 2007-03-25 22:44:08 +0200 | [diff] [blame] | 3522 | "-->\n" |
| 3523 | "</style></head>\n"); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3524 | |
| 3525 | if (buffer_write_chunk(rep, &msg) != 0) |
| 3526 | return 0; |
| 3527 | |
| 3528 | s->data_state = DATA_ST_INFO; |
| 3529 | /* fall through */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3530 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3531 | case DATA_ST_INFO: |
| 3532 | up = (now.tv_sec - start_date.tv_sec); |
| 3533 | |
| 3534 | /* WARNING! this has to fit the first packet too. |
| 3535 | * We are around 3.5 kB, add adding entries will |
| 3536 | * become tricky if we want to support 4kB buffers ! |
| 3537 | */ |
| 3538 | chunk_printf(&msg, sizeof(trash), |
| 3539 | "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">" |
| 3540 | PRODUCT_NAME "</a></h1>\n" |
| 3541 | "<h2>Statistics Report for pid %d</h2>\n" |
| 3542 | "<hr width=\"100%%\" class=\"hr\">\n" |
| 3543 | "<h3>> General process information</h3>\n" |
| 3544 | "<table border=0 cols=3><tr><td align=\"left\" nowrap width=\"1%%\">\n" |
| 3545 | "<p><b>pid = </b> %d (nbproc = %d)<br>\n" |
| 3546 | "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n" |
| 3547 | "<b>system limits :</b> memmax = %s%s ; ulimit-n = %d<br>\n" |
| 3548 | "<b>maxsock = </b> %d<br>\n" |
| 3549 | "<b>maxconn = </b> %d (current conns = %d)<br>\n" |
| 3550 | "</td><td align=\"center\" nowrap>\n" |
Willy Tarreau | f2b74c2 | 2007-03-25 22:44:08 +0200 | [diff] [blame] | 3551 | "<table class=\"lgd\"><tr>\n" |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3552 | "<td class=\"active3\"> </td><td class=\"noborder\">active UP </td>" |
| 3553 | "<td class=\"backup3\"> </td><td class=\"noborder\">backup UP </td>" |
Willy Tarreau | f2b74c2 | 2007-03-25 22:44:08 +0200 | [diff] [blame] | 3554 | "</tr><tr>\n" |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3555 | "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>" |
| 3556 | "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>" |
Willy Tarreau | f2b74c2 | 2007-03-25 22:44:08 +0200 | [diff] [blame] | 3557 | "</tr><tr>\n" |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3558 | "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>" |
| 3559 | "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>" |
Willy Tarreau | f2b74c2 | 2007-03-25 22:44:08 +0200 | [diff] [blame] | 3560 | "</tr><tr>\n" |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3561 | "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN </td>" |
| 3562 | "<td class=\"active4\"></td><td class=\"noborder\">not checked </td>" |
| 3563 | "</tr></table>\n" |
| 3564 | "</td>" |
| 3565 | "<td align=\"left\" nowrap width=\"1%%\">" |
Willy Tarreau | f2b74c2 | 2007-03-25 22:44:08 +0200 | [diff] [blame] | 3566 | "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n" |
| 3567 | "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n" |
| 3568 | "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n" |
| 3569 | "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n" |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3570 | "</ul>" |
| 3571 | "</td>" |
| 3572 | "</tr></table>\n" |
| 3573 | "", |
| 3574 | pid, pid, global.nbproc, |
| 3575 | up / 86400, (up % 86400) / 3600, |
| 3576 | (up % 3600) / 60, (up % 60), |
| 3577 | global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited", |
| 3578 | global.rlimit_memmax ? " MB" : "", |
| 3579 | global.rlimit_nofile, |
| 3580 | global.maxsock, |
| 3581 | global.maxconn, |
| 3582 | actconn |
| 3583 | ); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3584 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3585 | if (buffer_write_chunk(rep, &msg) != 0) |
| 3586 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3587 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3588 | memset(&s->data_ctx, 0, sizeof(s->data_ctx)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3589 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3590 | s->data_ctx.stats.px = proxy; |
| 3591 | s->data_ctx.stats.px_st = DATA_ST_PX_INIT; |
| 3592 | s->data_state = DATA_ST_LIST; |
| 3593 | /* fall through */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3594 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3595 | case DATA_ST_LIST: |
| 3596 | /* dump proxies */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3597 | while (s->data_ctx.stats.px) { |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3598 | px = s->data_ctx.stats.px; |
| 3599 | /* skip the disabled proxies and non-networked ones */ |
| 3600 | if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE))) |
| 3601 | if (produce_content_stats_proxy(s, px) == 0) |
| 3602 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3603 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3604 | s->data_ctx.stats.px = px->next; |
| 3605 | s->data_ctx.stats.px_st = DATA_ST_PX_INIT; |
| 3606 | } |
| 3607 | /* here, we just have reached the last proxy */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3608 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3609 | s->data_state = DATA_ST_END; |
| 3610 | /* fall through */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3611 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3612 | case DATA_ST_END: |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 3613 | chunk_printf(&msg, sizeof(trash), "</body></html>\n"); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3614 | if (buffer_write_chunk(rep, &msg) != 0) |
| 3615 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3616 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3617 | s->data_state = DATA_ST_FIN; |
| 3618 | /* fall through */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3619 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3620 | case DATA_ST_FIN: |
| 3621 | s->flags &= ~SN_SELF_GEN; |
| 3622 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3623 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3624 | default: |
| 3625 | /* unknown state ! */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 3626 | s->txn.status = 500; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3627 | client_retnclose(s, error_message(s, HTTP_ERR_500)); |
| 3628 | if (!(s->flags & SN_ERR_MASK)) |
| 3629 | s->flags |= SN_ERR_PRXCOND; |
| 3630 | if (!(s->flags & SN_FINST_MASK)) |
| 3631 | s->flags |= SN_FINST_R; |
| 3632 | s->flags &= ~SN_SELF_GEN; |
| 3633 | return 1; |
| 3634 | } |
| 3635 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3636 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3637 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3638 | /* |
| 3639 | * Dumps statistics for a proxy. |
| 3640 | * Returns 0 if it had to stop dumping data because of lack of buffer space, |
| 3641 | * ot non-zero if everything completed. |
| 3642 | */ |
| 3643 | int produce_content_stats_proxy(struct session *s, struct proxy *px) |
| 3644 | { |
| 3645 | struct buffer *rep = s->rep; |
| 3646 | struct server *sv; |
| 3647 | struct chunk msg; |
| 3648 | |
| 3649 | msg.len = 0; |
| 3650 | msg.str = trash; |
| 3651 | |
| 3652 | switch (s->data_ctx.stats.px_st) { |
| 3653 | case DATA_ST_PX_INIT: |
| 3654 | /* we are on a new proxy */ |
| 3655 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3656 | if (s->be->uri_auth && s->be->uri_auth->scope) { |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3657 | /* we have a limited scope, we have to check the proxy name */ |
| 3658 | struct stat_scope *scope; |
| 3659 | int len; |
| 3660 | |
| 3661 | len = strlen(px->id); |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3662 | scope = s->be->uri_auth->scope; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3663 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3664 | while (scope) { |
| 3665 | /* match exact proxy name */ |
| 3666 | if (scope->px_len == len && !memcmp(px->id, scope->px_id, len)) |
| 3667 | break; |
| 3668 | |
| 3669 | /* match '.' which means 'self' proxy */ |
| 3670 | if (!strcmp(scope->px_id, ".") && px == s->fe) |
| 3671 | break; |
| 3672 | scope = scope->next; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3673 | } |
| 3674 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3675 | /* proxy name not found : don't dump anything */ |
| 3676 | if (scope == NULL) |
| 3677 | return 1; |
| 3678 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3679 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3680 | s->data_ctx.stats.px_st = DATA_ST_PX_TH; |
| 3681 | /* fall through */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3682 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3683 | case DATA_ST_PX_TH: |
| 3684 | /* print a new table */ |
| 3685 | chunk_printf(&msg, sizeof(trash), |
| 3686 | "<table cols=\"20\" class=\"tbl\" width=\"100%%\">\n" |
| 3687 | "<tr align=\"center\" class=\"titre\">" |
| 3688 | "<th colspan=2 class=\"pxname\">%s</th>" |
| 3689 | "<th colspan=18 class=\"empty\"></th>" |
| 3690 | "</tr>\n" |
| 3691 | "<tr align=\"center\" class=\"titre\">" |
| 3692 | "<th rowspan=2></th>" |
| 3693 | "<th colspan=2>Queue</th><th colspan=4>Sessions</th>" |
| 3694 | "<th colspan=2>Bytes</th><th colspan=2>Denied</th>" |
| 3695 | "<th colspan=3>Errors</th><th colspan=6>Server</th>" |
| 3696 | "</tr>\n" |
| 3697 | "<tr align=\"center\" class=\"titre\">" |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 3698 | "<th>Cur</th><th>Max</th><th>Cur</th><th>Max</th>" |
| 3699 | "<th>Limit</th><th>Cumul</th><th>In</th><th>Out</th>" |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3700 | "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>" |
| 3701 | "<th>Resp</th><th>Status</th><th>Weight</th><th>Act</th>" |
| 3702 | "<th>Bck</th><th>Check</th><th>Down</th></tr>\n" |
| 3703 | "", |
| 3704 | px->id); |
| 3705 | |
| 3706 | if (buffer_write_chunk(rep, &msg) != 0) |
| 3707 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3708 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3709 | s->data_ctx.stats.px_st = DATA_ST_PX_FE; |
| 3710 | /* fall through */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3711 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3712 | case DATA_ST_PX_FE: |
| 3713 | /* print the frontend */ |
| 3714 | if (px->cap & PR_CAP_FE) { |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3715 | chunk_printf(&msg, sizeof(trash), |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3716 | /* name, queue */ |
| 3717 | "<tr align=center class=\"frontend\"><td>Frontend</td><td colspan=2></td>" |
| 3718 | /* sessions : current, max, limit, cumul. */ |
| 3719 | "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>" |
| 3720 | /* bytes : in, out */ |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 3721 | "<td align=right>%lld</td><td align=right>%lld</td>" |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3722 | /* denied: req, resp */ |
| 3723 | "<td align=right>%d</td><td align=right>%d</td>" |
| 3724 | /* errors : request, connect, response */ |
| 3725 | "<td align=right>%d</td><td align=right></td><td align=right></td>" |
| 3726 | /* server status : reflect backend status */ |
| 3727 | "<td align=center>%s</td>" |
| 3728 | /* rest of server: nothing */ |
| 3729 | "<td align=center colspan=5></td></tr>" |
| 3730 | "", |
| 3731 | px->feconn, px->feconn_max, px->maxconn, px->cum_feconn, |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 3732 | px->bytes_in, px->bytes_out, |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3733 | px->denied_req, px->denied_resp, |
| 3734 | px->failed_req, |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3735 | px->state == PR_STRUN ? "OPEN" : |
| 3736 | px->state == PR_STIDLE ? "FULL" : "STOP"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3737 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3738 | if (buffer_write_chunk(rep, &msg) != 0) |
| 3739 | return 0; |
| 3740 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3741 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3742 | s->data_ctx.stats.sv = px->srv; /* may be NULL */ |
| 3743 | s->data_ctx.stats.px_st = DATA_ST_PX_SV; |
| 3744 | /* fall through */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3745 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3746 | case DATA_ST_PX_SV: |
| 3747 | /* stats.sv has been initialized above */ |
| 3748 | while (s->data_ctx.stats.sv != NULL) { |
| 3749 | static char *srv_hlt_st[5] = { "DOWN", "DN %d/%d ↑", "UP %d/%d ↓", "UP", "<i>no check</i>" }; |
| 3750 | int sv_state; /* 0=DOWN, 1=going up, 2=going down, 3=UP, 4=unchecked */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3751 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3752 | sv = s->data_ctx.stats.sv; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3753 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3754 | /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */ |
| 3755 | if (!(sv->state & SRV_CHECKED)) |
| 3756 | sv_state = 4; |
| 3757 | else if (sv->state & SRV_RUNNING) |
| 3758 | if (sv->health == sv->rise + sv->fall - 1) |
| 3759 | sv_state = 3; /* UP */ |
| 3760 | else |
| 3761 | sv_state = 2; /* going down */ |
| 3762 | else |
| 3763 | if (sv->health) |
| 3764 | sv_state = 1; /* going up */ |
| 3765 | else |
| 3766 | sv_state = 0; /* DOWN */ |
| 3767 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3768 | chunk_printf(&msg, sizeof(trash), |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3769 | /* name */ |
| 3770 | "<tr align=\"center\" class=\"%s%d\"><td>%s</td>" |
| 3771 | /* queue : current, max */ |
| 3772 | "<td align=right>%d</td><td align=right>%d</td>" |
| 3773 | /* sessions : current, max, limit, cumul */ |
| 3774 | "<td align=right>%d</td><td align=right>%d</td><td align=right>%s</td><td align=right>%d</td>" |
| 3775 | /* bytes : in, out */ |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 3776 | "<td align=right>%lld</td><td align=right>%lld</td>" |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3777 | /* denied: req, resp */ |
| 3778 | "<td align=right></td><td align=right>%d</td>" |
| 3779 | /* errors : request, connect, response */ |
| 3780 | "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n" |
| 3781 | "", |
Willy Tarreau | 368e96a | 2007-01-07 00:16:15 +0100 | [diff] [blame] | 3782 | (sv->state & SRV_BACKUP) ? "backup" : "active", |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3783 | sv_state, sv->id, |
| 3784 | sv->nbpend, sv->nbpend_max, |
| 3785 | sv->cur_sess, sv->cur_sess_max, sv->maxconn ? ultoa(sv->maxconn) : "-", sv->cum_sess, |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 3786 | sv->bytes_in, sv->bytes_out, |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3787 | sv->failed_secu, |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3788 | sv->failed_conns, sv->failed_resp); |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3789 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3790 | /* status */ |
| 3791 | chunk_printf(&msg, sizeof(trash), "<td nowrap>"); |
| 3792 | chunk_printf(&msg, sizeof(trash), |
| 3793 | srv_hlt_st[sv_state], |
| 3794 | (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health), |
| 3795 | (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise)); |
| 3796 | |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3797 | chunk_printf(&msg, sizeof(trash), |
| 3798 | /* weight */ |
| 3799 | "</td><td>%d</td>" |
| 3800 | /* act, bck */ |
| 3801 | "<td>%s</td><td>%s</td>" |
| 3802 | "", |
Willy Tarreau | 417fae0 | 2007-03-25 21:16:40 +0200 | [diff] [blame] | 3803 | sv->uweight, |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3804 | (sv->state & SRV_BACKUP) ? "-" : "Y", |
| 3805 | (sv->state & SRV_BACKUP) ? "Y" : "-"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3806 | |
| 3807 | /* check failures : unique, fatal */ |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3808 | if (sv->state & SRV_CHECKED) |
| 3809 | chunk_printf(&msg, sizeof(trash), |
| 3810 | "<td align=right>%d</td><td align=right>%d</td></tr>\n", |
| 3811 | sv->failed_checks, sv->down_trans); |
| 3812 | else |
| 3813 | chunk_printf(&msg, sizeof(trash), |
| 3814 | "<td colspan=2></td></tr>\n"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3815 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3816 | if (buffer_write_chunk(rep, &msg) != 0) |
| 3817 | return 0; |
| 3818 | |
| 3819 | s->data_ctx.stats.sv = sv->next; |
| 3820 | } /* while sv */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3821 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3822 | s->data_ctx.stats.px_st = DATA_ST_PX_BE; |
| 3823 | /* fall through */ |
| 3824 | |
| 3825 | case DATA_ST_PX_BE: |
| 3826 | /* print the backend */ |
| 3827 | if (px->cap & PR_CAP_BE) { |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3828 | chunk_printf(&msg, sizeof(trash), |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3829 | /* name */ |
| 3830 | "<tr align=center class=\"backend\"><td>Backend</td>" |
| 3831 | /* queue : current, max */ |
| 3832 | "<td align=right>%d</td><td align=right>%d</td>" |
| 3833 | /* sessions : current, max, limit, cumul. */ |
| 3834 | "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>" |
| 3835 | /* bytes : in, out */ |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 3836 | "<td align=right>%lld</td><td align=right>%lld</td>" |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3837 | /* denied: req, resp */ |
| 3838 | "<td align=right>%d</td><td align=right>%d</td>" |
| 3839 | /* errors : request, connect, response */ |
| 3840 | "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n" |
| 3841 | /* server status : reflect backend status (up/down) : we display UP |
| 3842 | * if the backend has known working servers or if it has no server at |
| 3843 | * all (eg: for stats). Tthen we display the total weight, number of |
| 3844 | * active and backups. */ |
| 3845 | "<td align=center>%s</td><td align=center>%d</td>" |
| 3846 | "<td align=center>%d</td><td align=center>%d</td>" |
| 3847 | /* rest of server: nothing */ |
| 3848 | "<td align=center colspan=2></td></tr>" |
| 3849 | "", |
| 3850 | px->nbpend /* or px->totpend ? */, px->nbpend_max, |
| 3851 | px->beconn, px->beconn_max, px->fullconn, px->cum_beconn, |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 3852 | px->bytes_in, px->bytes_out, |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3853 | px->denied_req, px->denied_resp, |
| 3854 | px->failed_conns, px->failed_resp, |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3855 | (px->srv_map_sz > 0 || !px->srv) ? "UP" : "DOWN", |
| 3856 | px->srv_map_sz, px->srv_act, px->srv_bck); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3857 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3858 | if (buffer_write_chunk(rep, &msg) != 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3859 | return 0; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3860 | } |
| 3861 | |
| 3862 | s->data_ctx.stats.px_st = DATA_ST_PX_END; |
| 3863 | /* fall through */ |
| 3864 | |
| 3865 | case DATA_ST_PX_END: |
| 3866 | chunk_printf(&msg, sizeof(trash), "</table><p>\n"); |
| 3867 | |
| 3868 | if (buffer_write_chunk(rep, &msg) != 0) |
| 3869 | return 0; |
| 3870 | |
| 3871 | s->data_ctx.stats.px_st = DATA_ST_PX_FIN; |
| 3872 | /* fall through */ |
| 3873 | |
| 3874 | case DATA_ST_PX_FIN: |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3875 | return 1; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3876 | |
| 3877 | default: |
| 3878 | /* unknown state, we should put an abort() here ! */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3879 | return 1; |
| 3880 | } |
| 3881 | } |
| 3882 | |
| 3883 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3884 | /* Iterate the same filter through all request headers. |
| 3885 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3886 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 3887 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3888 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3889 | int apply_filter_to_req_headers(struct session *t, struct buffer *req, struct hdr_exp *exp) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3890 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3891 | char term; |
| 3892 | char *cur_ptr, *cur_end, *cur_next; |
| 3893 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3894 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3895 | struct hdr_idx_elem *cur_hdr; |
| 3896 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 3897 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3898 | last_hdr = 0; |
| 3899 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3900 | cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3901 | old_idx = 0; |
| 3902 | |
| 3903 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3904 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3905 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3906 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3907 | (exp->action == ACT_ALLOW || |
| 3908 | exp->action == ACT_DENY || |
| 3909 | exp->action == ACT_TARPIT)) |
| 3910 | return 0; |
| 3911 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3912 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3913 | if (!cur_idx) |
| 3914 | break; |
| 3915 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3916 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3917 | cur_ptr = cur_next; |
| 3918 | cur_end = cur_ptr + cur_hdr->len; |
| 3919 | cur_next = cur_end + cur_hdr->cr + 1; |
| 3920 | |
| 3921 | /* Now we have one header between cur_ptr and cur_end, |
| 3922 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3923 | */ |
| 3924 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3925 | /* The annoying part is that pattern matching needs |
| 3926 | * that we modify the contents to null-terminate all |
| 3927 | * strings before testing them. |
| 3928 | */ |
| 3929 | |
| 3930 | term = *cur_end; |
| 3931 | *cur_end = '\0'; |
| 3932 | |
| 3933 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 3934 | switch (exp->action) { |
| 3935 | case ACT_SETBE: |
| 3936 | /* It is not possible to jump a second time. |
| 3937 | * FIXME: should we return an HTTP/500 here so that |
| 3938 | * the admin knows there's a problem ? |
| 3939 | */ |
| 3940 | if (t->be != t->fe) |
| 3941 | break; |
| 3942 | |
| 3943 | /* Swithing Proxy */ |
| 3944 | t->be = (struct proxy *) exp->replace; |
| 3945 | |
| 3946 | /* right now, the backend switch is not too much complicated |
| 3947 | * because we have associated req_cap and rsp_cap to the |
| 3948 | * frontend, and the beconn will be updated later. |
| 3949 | */ |
| 3950 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3951 | t->rep->rto = t->req->wto = t->be->srvtimeout; |
| 3952 | t->req->cto = t->be->contimeout; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3953 | last_hdr = 1; |
| 3954 | break; |
| 3955 | |
| 3956 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3957 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3958 | last_hdr = 1; |
| 3959 | break; |
| 3960 | |
| 3961 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3962 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3963 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3964 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3965 | break; |
| 3966 | |
| 3967 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3968 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3969 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3970 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3971 | break; |
| 3972 | |
| 3973 | case ACT_REPLACE: |
| 3974 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 3975 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 3976 | /* FIXME: if the user adds a newline in the replacement, the |
| 3977 | * index will not be recalculated for now, and the new line |
| 3978 | * will not be counted as a new header. |
| 3979 | */ |
| 3980 | |
| 3981 | cur_end += delta; |
| 3982 | cur_next += delta; |
| 3983 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3984 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3985 | break; |
| 3986 | |
| 3987 | case ACT_REMOVE: |
| 3988 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 3989 | cur_next += delta; |
| 3990 | |
| 3991 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3992 | txn->req.eoh += delta; |
| 3993 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 3994 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3995 | cur_hdr->len = 0; |
| 3996 | cur_end = NULL; /* null-term has been rewritten */ |
| 3997 | break; |
| 3998 | |
| 3999 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4000 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4001 | if (cur_end) |
| 4002 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4003 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4004 | /* keep the link from this header to next one in case of later |
| 4005 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4006 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4007 | old_idx = cur_idx; |
| 4008 | } |
| 4009 | return 0; |
| 4010 | } |
| 4011 | |
| 4012 | |
| 4013 | /* Apply the filter to the request line. |
| 4014 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 4015 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4016 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 4017 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4018 | */ |
| 4019 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 4020 | { |
| 4021 | char term; |
| 4022 | char *cur_ptr, *cur_end; |
| 4023 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4024 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4025 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4026 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4027 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4028 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4029 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4030 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4031 | (exp->action == ACT_ALLOW || |
| 4032 | exp->action == ACT_DENY || |
| 4033 | exp->action == ACT_TARPIT)) |
| 4034 | return 0; |
| 4035 | else if (exp->action == ACT_REMOVE) |
| 4036 | return 0; |
| 4037 | |
| 4038 | done = 0; |
| 4039 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4040 | 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] | 4041 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4042 | |
| 4043 | /* Now we have the request line between cur_ptr and cur_end */ |
| 4044 | |
| 4045 | /* The annoying part is that pattern matching needs |
| 4046 | * that we modify the contents to null-terminate all |
| 4047 | * strings before testing them. |
| 4048 | */ |
| 4049 | |
| 4050 | term = *cur_end; |
| 4051 | *cur_end = '\0'; |
| 4052 | |
| 4053 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4054 | switch (exp->action) { |
| 4055 | case ACT_SETBE: |
| 4056 | /* It is not possible to jump a second time. |
| 4057 | * FIXME: should we return an HTTP/500 here so that |
| 4058 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4059 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4060 | if (t->be != t->fe) |
| 4061 | break; |
| 4062 | |
| 4063 | /* Swithing Proxy */ |
| 4064 | t->be = (struct proxy *) exp->replace; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4065 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4066 | /* right now, the backend switch is not too much complicated |
| 4067 | * because we have associated req_cap and rsp_cap to the |
| 4068 | * frontend, and the beconn will be updated later. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4069 | */ |
| 4070 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4071 | t->rep->rto = t->req->wto = t->be->srvtimeout; |
| 4072 | t->req->cto = t->be->contimeout; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4073 | done = 1; |
| 4074 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4075 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4076 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4077 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4078 | done = 1; |
| 4079 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4080 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4081 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4082 | txn->flags |= TX_CLDENY; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4083 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4084 | done = 1; |
| 4085 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4086 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4087 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4088 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4089 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4090 | done = 1; |
| 4091 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4092 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4093 | case ACT_REPLACE: |
| 4094 | *cur_end = term; /* restore the string terminator */ |
| 4095 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4096 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 4097 | /* FIXME: if the user adds a newline in the replacement, the |
| 4098 | * index will not be recalculated for now, and the new line |
| 4099 | * will not be counted as a new header. |
| 4100 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4101 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4102 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4103 | cur_end += delta; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4104 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4105 | 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] | 4106 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4107 | HTTP_MSG_RQMETH, |
| 4108 | cur_ptr, cur_end + 1, |
| 4109 | NULL, NULL); |
| 4110 | if (unlikely(!cur_end)) |
| 4111 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4112 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4113 | /* we have a full request and we know that we have either a CR |
| 4114 | * or an LF at <ptr>. |
| 4115 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4116 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 4117 | 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] | 4118 | /* there is no point trying this regex on headers */ |
| 4119 | return 1; |
| 4120 | } |
| 4121 | } |
| 4122 | *cur_end = term; /* restore the string terminator */ |
| 4123 | return done; |
| 4124 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 4125 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4126 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4127 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4128 | /* |
| 4129 | * Apply all the req filters <exp> to all headers in buffer <req> of session <t>. |
| 4130 | * 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] | 4131 | * unparsable request. Since it can manage the switch to another backend, it |
| 4132 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4133 | */ |
| 4134 | int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 4135 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4136 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4137 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4138 | while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4139 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4140 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4141 | /* |
| 4142 | * The interleaving of transformations and verdicts |
| 4143 | * makes it difficult to decide to continue or stop |
| 4144 | * the evaluation. |
| 4145 | */ |
| 4146 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4147 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4148 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 4149 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) { |
| 4150 | exp = exp->next; |
| 4151 | continue; |
| 4152 | } |
| 4153 | |
| 4154 | /* Apply the filter to the request line. */ |
| 4155 | ret = apply_filter_to_req_line(t, req, exp); |
| 4156 | if (unlikely(ret < 0)) |
| 4157 | return -1; |
| 4158 | |
| 4159 | if (likely(ret == 0)) { |
| 4160 | /* The filter did not match the request, it can be |
| 4161 | * iterated through all headers. |
| 4162 | */ |
| 4163 | apply_filter_to_req_headers(t, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4164 | } |
| 4165 | exp = exp->next; |
| 4166 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4167 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4168 | } |
| 4169 | |
| 4170 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4171 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4172 | /* |
| 4173 | * Manager client-side cookie |
| 4174 | */ |
| 4175 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 4176 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4177 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4178 | char *p1, *p2, *p3, *p4; |
| 4179 | char *del_colon, *del_cookie, *colon; |
| 4180 | int app_cookies; |
| 4181 | |
| 4182 | appsess *asession_temp = NULL; |
| 4183 | appsess local_asession; |
| 4184 | |
| 4185 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4186 | int cur_idx, old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4187 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4188 | if (t->be->cookie_name == NULL && |
| 4189 | t->be->appsession_name == NULL && |
| 4190 | t->be->capture_name == NULL) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4191 | return; |
| 4192 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4193 | /* Iterate through the headers. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4194 | * we start with the start line. |
| 4195 | */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 4196 | old_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4197 | 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] | 4198 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4199 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4200 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4201 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4202 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4203 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4204 | cur_ptr = cur_next; |
| 4205 | cur_end = cur_ptr + cur_hdr->len; |
| 4206 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4207 | |
| 4208 | /* We have one full header between cur_ptr and cur_end, and the |
| 4209 | * next header starts at cur_next. We're only interested in |
| 4210 | * "Cookie:" headers. |
| 4211 | */ |
| 4212 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4213 | val = http_header_match2(cur_ptr, cur_end, "Cookie", 6); |
| 4214 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4215 | old_idx = cur_idx; |
| 4216 | continue; |
| 4217 | } |
| 4218 | |
| 4219 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 4220 | * attributes whose name begin with a '$', and associate them with |
| 4221 | * the right cookie, if we want to delete this cookie. |
| 4222 | * So there are 3 cases for each cookie read : |
| 4223 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 4224 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 4225 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 4226 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 4227 | * "special" cookie. |
| 4228 | * At the end of loop, if a "special" cookie remains, we may have to |
| 4229 | * remove it. If no application cookie persists in the header, we |
| 4230 | * *MUST* delete it |
| 4231 | */ |
| 4232 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4233 | colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4234 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4235 | /* del_cookie == NULL => nothing to be deleted */ |
| 4236 | del_colon = del_cookie = NULL; |
| 4237 | app_cookies = 0; |
| 4238 | |
| 4239 | while (p1 < cur_end) { |
| 4240 | /* skip spaces and colons, but keep an eye on these ones */ |
| 4241 | while (p1 < cur_end) { |
| 4242 | if (*p1 == ';' || *p1 == ',') |
| 4243 | colon = p1; |
| 4244 | else if (!isspace((int)*p1)) |
| 4245 | break; |
| 4246 | p1++; |
| 4247 | } |
| 4248 | |
| 4249 | if (p1 == cur_end) |
| 4250 | break; |
| 4251 | |
| 4252 | /* p1 is at the beginning of the cookie name */ |
| 4253 | p2 = p1; |
| 4254 | while (p2 < cur_end && *p2 != '=') |
| 4255 | p2++; |
| 4256 | |
| 4257 | if (p2 == cur_end) |
| 4258 | break; |
| 4259 | |
| 4260 | p3 = p2 + 1; /* skips the '=' sign */ |
| 4261 | if (p3 == cur_end) |
| 4262 | break; |
| 4263 | |
| 4264 | p4 = p3; |
| 4265 | while (p4 < cur_end && !isspace((int)*p4) && *p4 != ';' && *p4 != ',') |
| 4266 | p4++; |
| 4267 | |
| 4268 | /* here, we have the cookie name between p1 and p2, |
| 4269 | * and its value between p3 and p4. |
| 4270 | * we can process it : |
| 4271 | * |
| 4272 | * Cookie: NAME=VALUE; |
| 4273 | * | || || | |
| 4274 | * | || || +--> p4 |
| 4275 | * | || |+-------> p3 |
| 4276 | * | || +--------> p2 |
| 4277 | * | |+------------> p1 |
| 4278 | * | +-------------> colon |
| 4279 | * +--------------------> cur_ptr |
| 4280 | */ |
| 4281 | |
| 4282 | if (*p1 == '$') { |
| 4283 | /* skip this one */ |
| 4284 | } |
| 4285 | else { |
| 4286 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4287 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4288 | txn->cli_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4289 | (p4 - p1 >= t->fe->capture_namelen) && |
| 4290 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4291 | int log_len = p4 - p1; |
| 4292 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 4293 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4294 | Alert("HTTP logging : out of memory.\n"); |
| 4295 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4296 | if (log_len > t->fe->capture_len) |
| 4297 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4298 | memcpy(txn->cli_cookie, p1, log_len); |
| 4299 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4300 | } |
| 4301 | } |
| 4302 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4303 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 4304 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4305 | /* Cool... it's the right one */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4306 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4307 | char *delim; |
| 4308 | |
| 4309 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 4310 | * have the server ID betweek p3 and delim, and the original cookie between |
| 4311 | * delim+1 and p4. Otherwise, delim==p4 : |
| 4312 | * |
| 4313 | * Cookie: NAME=SRV~VALUE; |
| 4314 | * | || || | | |
| 4315 | * | || || | +--> p4 |
| 4316 | * | || || +--------> delim |
| 4317 | * | || |+-----------> p3 |
| 4318 | * | || +------------> p2 |
| 4319 | * | |+----------------> p1 |
| 4320 | * | +-----------------> colon |
| 4321 | * +------------------------> cur_ptr |
| 4322 | */ |
| 4323 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4324 | if (t->be->options & PR_O_COOK_PFX) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4325 | for (delim = p3; delim < p4; delim++) |
| 4326 | if (*delim == COOKIE_DELIM) |
| 4327 | break; |
| 4328 | } |
| 4329 | else |
| 4330 | delim = p4; |
| 4331 | |
| 4332 | |
| 4333 | /* Here, we'll look for the first running server which supports the cookie. |
| 4334 | * This allows to share a same cookie between several servers, for example |
| 4335 | * to dedicate backup servers to specific servers only. |
| 4336 | * However, to prevent clients from sticking to cookie-less backup server |
| 4337 | * when they have incidentely learned an empty cookie, we simply ignore |
| 4338 | * empty cookies and mark them as invalid. |
| 4339 | */ |
| 4340 | if (delim == p3) |
| 4341 | srv = NULL; |
| 4342 | |
| 4343 | while (srv) { |
Willy Tarreau | 92f2ab1 | 2007-02-02 22:14:47 +0100 | [diff] [blame] | 4344 | if (srv->cookie && (srv->cklen == delim - p3) && |
| 4345 | !memcmp(p3, srv->cookie, delim - p3)) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4346 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4347 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4348 | txn->flags &= ~TX_CK_MASK; |
| 4349 | txn->flags |= TX_CK_VALID; |
| 4350 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4351 | t->srv = srv; |
| 4352 | break; |
| 4353 | } else { |
| 4354 | /* we found a server, but it's down */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4355 | txn->flags &= ~TX_CK_MASK; |
| 4356 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4357 | } |
| 4358 | } |
| 4359 | srv = srv->next; |
| 4360 | } |
| 4361 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4362 | if (!srv && !(txn->flags & TX_CK_DOWN)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4363 | /* no server matched this cookie */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4364 | txn->flags &= ~TX_CK_MASK; |
| 4365 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4366 | } |
| 4367 | |
| 4368 | /* depending on the cookie mode, we may have to either : |
| 4369 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 4370 | * the server never sees it ; |
| 4371 | * - remove the server id from the cookie value, and tag the cookie as an |
| 4372 | * application cookie so that it does not get accidentely removed later, |
| 4373 | * if we're in cookie prefix mode |
| 4374 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4375 | if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4376 | int delta; /* negative */ |
| 4377 | |
| 4378 | delta = buffer_replace2(req, p3, delim + 1, NULL, 0); |
| 4379 | p4 += delta; |
| 4380 | cur_end += delta; |
| 4381 | cur_next += delta; |
| 4382 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4383 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4384 | |
| 4385 | del_cookie = del_colon = NULL; |
| 4386 | app_cookies++; /* protect the header from deletion */ |
| 4387 | } |
| 4388 | else if (del_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4389 | (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] | 4390 | del_cookie = p1; |
| 4391 | del_colon = colon; |
| 4392 | } |
| 4393 | } else { |
| 4394 | /* now we know that we must keep this cookie since it's |
| 4395 | * not ours. But if we wanted to delete our cookie |
| 4396 | * earlier, we cannot remove the complete header, but we |
| 4397 | * can remove the previous block itself. |
| 4398 | */ |
| 4399 | app_cookies++; |
| 4400 | |
| 4401 | if (del_cookie != NULL) { |
| 4402 | int delta; /* negative */ |
| 4403 | |
| 4404 | delta = buffer_replace2(req, del_cookie, p1, NULL, 0); |
| 4405 | p4 += delta; |
| 4406 | cur_end += delta; |
| 4407 | cur_next += delta; |
| 4408 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4409 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4410 | del_cookie = del_colon = NULL; |
| 4411 | } |
| 4412 | } |
| 4413 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4414 | if ((t->be->appsession_name != NULL) && |
| 4415 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4416 | /* first, let's see if the cookie is our appcookie*/ |
| 4417 | |
| 4418 | /* Cool... it's the right one */ |
| 4419 | |
| 4420 | asession_temp = &local_asession; |
| 4421 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4422 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4423 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 4424 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 4425 | return; |
| 4426 | } |
| 4427 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4428 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 4429 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4430 | asession_temp->serverid = NULL; |
| 4431 | |
| 4432 | /* only do insert, if lookup fails */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4433 | if (chtbl_lookup(&(t->be->htbl_proxy), (void *) &asession_temp) != 0) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4434 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4435 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4436 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4437 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 4438 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 4439 | return; |
| 4440 | } |
| 4441 | |
| 4442 | asession_temp->sessid = local_asession.sessid; |
| 4443 | asession_temp->serverid = local_asession.serverid; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4444 | chtbl_insert(&(t->be->htbl_proxy), (void *) asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4445 | } else { |
| 4446 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4447 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4448 | } |
| 4449 | |
| 4450 | if (asession_temp->serverid == NULL) { |
| 4451 | Alert("Found Application Session without matching server.\n"); |
| 4452 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4453 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4454 | while (srv) { |
| 4455 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4456 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4457 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4458 | txn->flags &= ~TX_CK_MASK; |
| 4459 | txn->flags |= TX_CK_VALID; |
| 4460 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4461 | t->srv = srv; |
| 4462 | break; |
| 4463 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4464 | txn->flags &= ~TX_CK_MASK; |
| 4465 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4466 | } |
| 4467 | } |
| 4468 | srv = srv->next; |
| 4469 | }/* end while(srv) */ |
| 4470 | }/* end else if server == NULL */ |
| 4471 | |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 4472 | tv_add(&asession_temp->expire, &now, &t->be->appsession_timeout); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4473 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 4474 | } |
| 4475 | |
| 4476 | /* we'll have to look for another cookie ... */ |
| 4477 | p1 = p4; |
| 4478 | } /* while (p1 < cur_end) */ |
| 4479 | |
| 4480 | /* There's no more cookie on this line. |
| 4481 | * We may have marked the last one(s) for deletion. |
| 4482 | * We must do this now in two ways : |
| 4483 | * - if there is no app cookie, we simply delete the header ; |
| 4484 | * - if there are app cookies, we must delete the end of the |
| 4485 | * string properly, including the colon/semi-colon before |
| 4486 | * the cookie name. |
| 4487 | */ |
| 4488 | if (del_cookie != NULL) { |
| 4489 | int delta; |
| 4490 | if (app_cookies) { |
| 4491 | delta = buffer_replace2(req, del_colon, cur_end, NULL, 0); |
| 4492 | cur_end = del_colon; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4493 | cur_hdr->len += delta; |
| 4494 | } else { |
| 4495 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4496 | |
| 4497 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4498 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4499 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4500 | cur_hdr->len = 0; |
| 4501 | } |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 4502 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4503 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4504 | } |
| 4505 | |
| 4506 | /* keep the link from this header to next one */ |
| 4507 | old_idx = cur_idx; |
| 4508 | } /* end of cookie processing on this header */ |
| 4509 | } |
| 4510 | |
| 4511 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4512 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 4513 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 4514 | */ |
| 4515 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4516 | { |
| 4517 | char term; |
| 4518 | char *cur_ptr, *cur_end, *cur_next; |
| 4519 | int cur_idx, old_idx, last_hdr; |
| 4520 | struct http_txn *txn = &t->txn; |
| 4521 | struct hdr_idx_elem *cur_hdr; |
| 4522 | int len, delta; |
| 4523 | |
| 4524 | last_hdr = 0; |
| 4525 | |
| 4526 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4527 | old_idx = 0; |
| 4528 | |
| 4529 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4530 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4531 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4532 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4533 | (exp->action == ACT_ALLOW || |
| 4534 | exp->action == ACT_DENY)) |
| 4535 | return 0; |
| 4536 | |
| 4537 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 4538 | if (!cur_idx) |
| 4539 | break; |
| 4540 | |
| 4541 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4542 | cur_ptr = cur_next; |
| 4543 | cur_end = cur_ptr + cur_hdr->len; |
| 4544 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4545 | |
| 4546 | /* Now we have one header between cur_ptr and cur_end, |
| 4547 | * and the next header starts at cur_next. |
| 4548 | */ |
| 4549 | |
| 4550 | /* The annoying part is that pattern matching needs |
| 4551 | * that we modify the contents to null-terminate all |
| 4552 | * strings before testing them. |
| 4553 | */ |
| 4554 | |
| 4555 | term = *cur_end; |
| 4556 | *cur_end = '\0'; |
| 4557 | |
| 4558 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4559 | switch (exp->action) { |
| 4560 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4561 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4562 | last_hdr = 1; |
| 4563 | break; |
| 4564 | |
| 4565 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4566 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4567 | last_hdr = 1; |
| 4568 | break; |
| 4569 | |
| 4570 | case ACT_REPLACE: |
| 4571 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4572 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 4573 | /* FIXME: if the user adds a newline in the replacement, the |
| 4574 | * index will not be recalculated for now, and the new line |
| 4575 | * will not be counted as a new header. |
| 4576 | */ |
| 4577 | |
| 4578 | cur_end += delta; |
| 4579 | cur_next += delta; |
| 4580 | cur_hdr->len += delta; |
| 4581 | txn->rsp.eoh += delta; |
| 4582 | break; |
| 4583 | |
| 4584 | case ACT_REMOVE: |
| 4585 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 4586 | cur_next += delta; |
| 4587 | |
| 4588 | /* FIXME: this should be a separate function */ |
| 4589 | txn->rsp.eoh += delta; |
| 4590 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4591 | txn->hdr_idx.used--; |
| 4592 | cur_hdr->len = 0; |
| 4593 | cur_end = NULL; /* null-term has been rewritten */ |
| 4594 | break; |
| 4595 | |
| 4596 | } |
| 4597 | } |
| 4598 | if (cur_end) |
| 4599 | *cur_end = term; /* restore the string terminator */ |
| 4600 | |
| 4601 | /* keep the link from this header to next one in case of later |
| 4602 | * removal of next header. |
| 4603 | */ |
| 4604 | old_idx = cur_idx; |
| 4605 | } |
| 4606 | return 0; |
| 4607 | } |
| 4608 | |
| 4609 | |
| 4610 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 4611 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 4612 | * or -1 if a replacement resulted in an invalid status line. |
| 4613 | */ |
| 4614 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4615 | { |
| 4616 | char term; |
| 4617 | char *cur_ptr, *cur_end; |
| 4618 | int done; |
| 4619 | struct http_txn *txn = &t->txn; |
| 4620 | int len, delta; |
| 4621 | |
| 4622 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4623 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4624 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4625 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4626 | (exp->action == ACT_ALLOW || |
| 4627 | exp->action == ACT_DENY)) |
| 4628 | return 0; |
| 4629 | else if (exp->action == ACT_REMOVE) |
| 4630 | return 0; |
| 4631 | |
| 4632 | done = 0; |
| 4633 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4634 | 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] | 4635 | cur_end = cur_ptr + txn->rsp.sl.rq.l; |
| 4636 | |
| 4637 | /* Now we have the status line between cur_ptr and cur_end */ |
| 4638 | |
| 4639 | /* The annoying part is that pattern matching needs |
| 4640 | * that we modify the contents to null-terminate all |
| 4641 | * strings before testing them. |
| 4642 | */ |
| 4643 | |
| 4644 | term = *cur_end; |
| 4645 | *cur_end = '\0'; |
| 4646 | |
| 4647 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4648 | switch (exp->action) { |
| 4649 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4650 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4651 | done = 1; |
| 4652 | break; |
| 4653 | |
| 4654 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4655 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4656 | done = 1; |
| 4657 | break; |
| 4658 | |
| 4659 | case ACT_REPLACE: |
| 4660 | *cur_end = term; /* restore the string terminator */ |
| 4661 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4662 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 4663 | /* FIXME: if the user adds a newline in the replacement, the |
| 4664 | * index will not be recalculated for now, and the new line |
| 4665 | * will not be counted as a new header. |
| 4666 | */ |
| 4667 | |
| 4668 | txn->rsp.eoh += delta; |
| 4669 | cur_end += delta; |
| 4670 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4671 | 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] | 4672 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 4673 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4674 | cur_ptr, cur_end + 1, |
| 4675 | NULL, NULL); |
| 4676 | if (unlikely(!cur_end)) |
| 4677 | return -1; |
| 4678 | |
| 4679 | /* we have a full respnse and we know that we have either a CR |
| 4680 | * or an LF at <ptr>. |
| 4681 | */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4682 | 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] | 4683 | hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r'); |
| 4684 | /* there is no point trying this regex on headers */ |
| 4685 | return 1; |
| 4686 | } |
| 4687 | } |
| 4688 | *cur_end = term; /* restore the string terminator */ |
| 4689 | return done; |
| 4690 | } |
| 4691 | |
| 4692 | |
| 4693 | |
| 4694 | /* |
| 4695 | * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>. |
| 4696 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 4697 | * unparsable response. |
| 4698 | */ |
| 4699 | int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4700 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4701 | struct http_txn *txn = &t->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4702 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4703 | while (exp && !(txn->flags & TX_SVDENY)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4704 | int ret; |
| 4705 | |
| 4706 | /* |
| 4707 | * The interleaving of transformations and verdicts |
| 4708 | * makes it difficult to decide to continue or stop |
| 4709 | * the evaluation. |
| 4710 | */ |
| 4711 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4712 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4713 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 4714 | exp->action == ACT_PASS)) { |
| 4715 | exp = exp->next; |
| 4716 | continue; |
| 4717 | } |
| 4718 | |
| 4719 | /* Apply the filter to the status line. */ |
| 4720 | ret = apply_filter_to_sts_line(t, rtr, exp); |
| 4721 | if (unlikely(ret < 0)) |
| 4722 | return -1; |
| 4723 | |
| 4724 | if (likely(ret == 0)) { |
| 4725 | /* The filter did not match the response, it can be |
| 4726 | * iterated through all headers. |
| 4727 | */ |
| 4728 | apply_filter_to_resp_headers(t, rtr, exp); |
| 4729 | } |
| 4730 | exp = exp->next; |
| 4731 | } |
| 4732 | return 0; |
| 4733 | } |
| 4734 | |
| 4735 | |
| 4736 | |
| 4737 | /* |
| 4738 | * Manager server-side cookies |
| 4739 | */ |
| 4740 | void manage_server_side_cookies(struct session *t, struct buffer *rtr) |
| 4741 | { |
| 4742 | struct http_txn *txn = &t->txn; |
| 4743 | char *p1, *p2, *p3, *p4; |
| 4744 | |
| 4745 | appsess *asession_temp = NULL; |
| 4746 | appsess local_asession; |
| 4747 | |
| 4748 | char *cur_ptr, *cur_end, *cur_next; |
| 4749 | int cur_idx, old_idx, delta; |
| 4750 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4751 | if (t->be->cookie_name == NULL && |
| 4752 | t->be->appsession_name == NULL && |
| 4753 | t->be->capture_name == NULL && |
| 4754 | !(t->be->options & PR_O_CHK_CACHE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4755 | return; |
| 4756 | |
| 4757 | /* Iterate through the headers. |
| 4758 | * we start with the start line. |
| 4759 | */ |
| 4760 | old_idx = 0; |
| 4761 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4762 | |
| 4763 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 4764 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4765 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4766 | |
| 4767 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4768 | cur_ptr = cur_next; |
| 4769 | cur_end = cur_ptr + cur_hdr->len; |
| 4770 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4771 | |
| 4772 | /* We have one full header between cur_ptr and cur_end, and the |
| 4773 | * next header starts at cur_next. We're only interested in |
| 4774 | * "Cookie:" headers. |
| 4775 | */ |
| 4776 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4777 | val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10); |
| 4778 | if (!val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4779 | old_idx = cur_idx; |
| 4780 | continue; |
| 4781 | } |
| 4782 | |
| 4783 | /* 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] | 4784 | txn->flags |= TX_SCK_ANY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4785 | |
| 4786 | |
| 4787 | /* maybe we only wanted to see if there was a set-cookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4788 | if (t->be->cookie_name == NULL && |
| 4789 | t->be->appsession_name == NULL && |
| 4790 | t->be->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4791 | return; |
| 4792 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4793 | p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4794 | |
| 4795 | 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] | 4796 | if (p1 == cur_end || *p1 == ';') /* end of cookie */ |
| 4797 | break; |
| 4798 | |
| 4799 | /* p1 is at the beginning of the cookie name */ |
| 4800 | p2 = p1; |
| 4801 | |
| 4802 | while (p2 < cur_end && *p2 != '=' && *p2 != ';') |
| 4803 | p2++; |
| 4804 | |
| 4805 | if (p2 == cur_end || *p2 == ';') /* next cookie */ |
| 4806 | break; |
| 4807 | |
| 4808 | p3 = p2 + 1; /* skip the '=' sign */ |
| 4809 | if (p3 == cur_end) |
| 4810 | break; |
| 4811 | |
| 4812 | p4 = p3; |
| 4813 | while (p4 < cur_end && !isspace((int)*p4) && *p4 != ';') |
| 4814 | p4++; |
| 4815 | |
| 4816 | /* here, we have the cookie name between p1 and p2, |
| 4817 | * and its value between p3 and p4. |
| 4818 | * we can process it. |
| 4819 | */ |
| 4820 | |
| 4821 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4822 | if (t->be->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4823 | txn->srv_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4824 | (p4 - p1 >= t->be->capture_namelen) && |
| 4825 | memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4826 | int log_len = p4 - p1; |
| 4827 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 4828 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4829 | Alert("HTTP logging : out of memory.\n"); |
| 4830 | } |
| 4831 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4832 | if (log_len > t->be->capture_len) |
| 4833 | log_len = t->be->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4834 | memcpy(txn->srv_cookie, p1, log_len); |
| 4835 | txn->srv_cookie[log_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4836 | } |
| 4837 | |
| 4838 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4839 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 4840 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4841 | /* Cool... it's the right one */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4842 | txn->flags |= TX_SCK_SEEN; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4843 | |
| 4844 | /* If the cookie is in insert mode on a known server, we'll delete |
| 4845 | * this occurrence because we'll insert another one later. |
| 4846 | * We'll delete it too if the "indirect" option is set and we're in |
| 4847 | * a direct access. */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4848 | if (((t->srv) && (t->be->options & PR_O_COOK_INS)) || |
| 4849 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4850 | /* this header must be deleted */ |
| 4851 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 4852 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4853 | txn->hdr_idx.used--; |
| 4854 | cur_hdr->len = 0; |
| 4855 | cur_next += delta; |
| 4856 | txn->rsp.eoh += delta; |
| 4857 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4858 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4859 | } |
| 4860 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4861 | (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4862 | /* replace bytes p3->p4 with the cookie name associated |
| 4863 | * with this server since we know it. |
| 4864 | */ |
| 4865 | delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen); |
| 4866 | cur_hdr->len += delta; |
| 4867 | cur_next += delta; |
| 4868 | txn->rsp.eoh += delta; |
| 4869 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4870 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4871 | } |
| 4872 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4873 | (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4874 | /* insert the cookie name associated with this server |
| 4875 | * before existing cookie, and insert a delimitor between them.. |
| 4876 | */ |
| 4877 | delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1); |
| 4878 | cur_hdr->len += delta; |
| 4879 | cur_next += delta; |
| 4880 | txn->rsp.eoh += delta; |
| 4881 | |
| 4882 | p3[t->srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4883 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4884 | } |
| 4885 | } |
| 4886 | /* next, let's see if the cookie is our appcookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4887 | else if ((t->be->appsession_name != NULL) && |
| 4888 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4889 | |
| 4890 | /* Cool... it's the right one */ |
| 4891 | |
| 4892 | size_t server_id_len = strlen(t->srv->id) + 1; |
| 4893 | asession_temp = &local_asession; |
| 4894 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4895 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4896 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4897 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4898 | return; |
| 4899 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4900 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 4901 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4902 | asession_temp->serverid = NULL; |
| 4903 | |
| 4904 | /* only do insert, if lookup fails */ |
| 4905 | if (chtbl_lookup(&(t->be->htbl_proxy), (void *) &asession_temp) != 0) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4906 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4907 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 4908 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 4909 | return; |
| 4910 | } |
| 4911 | asession_temp->sessid = local_asession.sessid; |
| 4912 | asession_temp->serverid = local_asession.serverid; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4913 | chtbl_insert(&(t->be->htbl_proxy), (void *) asession_temp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4914 | }/* end if (chtbl_lookup()) */ |
| 4915 | else { |
| 4916 | /* free wasted memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4917 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4918 | } /* end else from if (chtbl_lookup()) */ |
| 4919 | |
| 4920 | if (asession_temp->serverid == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4921 | if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4922 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4923 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4924 | return; |
| 4925 | } |
| 4926 | asession_temp->serverid[0] = '\0'; |
| 4927 | } |
| 4928 | |
| 4929 | if (asession_temp->serverid[0] == '\0') |
| 4930 | memcpy(asession_temp->serverid, t->srv->id, server_id_len); |
| 4931 | |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 4932 | tv_add(&asession_temp->expire, &now, &t->be->appsession_timeout); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4933 | |
| 4934 | #if defined(DEBUG_HASH) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4935 | print_table(&(t->be->htbl_proxy)); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4936 | #endif |
| 4937 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 4938 | break; /* we don't want to loop again since there cannot be another cookie on the same line */ |
| 4939 | } /* we're now at the end of the cookie value */ |
| 4940 | |
| 4941 | /* keep the link from this header to next one */ |
| 4942 | old_idx = cur_idx; |
| 4943 | } /* end of cookie processing on this header */ |
| 4944 | } |
| 4945 | |
| 4946 | |
| 4947 | |
| 4948 | /* |
| 4949 | * Check if response is cacheable or not. Updates t->flags. |
| 4950 | */ |
| 4951 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 4952 | { |
| 4953 | struct http_txn *txn = &t->txn; |
| 4954 | char *p1, *p2; |
| 4955 | |
| 4956 | char *cur_ptr, *cur_end, *cur_next; |
| 4957 | int cur_idx; |
| 4958 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4959 | if (!txn->flags & TX_CACHEABLE) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4960 | return; |
| 4961 | |
| 4962 | /* Iterate through the headers. |
| 4963 | * we start with the start line. |
| 4964 | */ |
| 4965 | cur_idx = 0; |
| 4966 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4967 | |
| 4968 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 4969 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4970 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4971 | |
| 4972 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4973 | cur_ptr = cur_next; |
| 4974 | cur_end = cur_ptr + cur_hdr->len; |
| 4975 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4976 | |
| 4977 | /* We have one full header between cur_ptr and cur_end, and the |
| 4978 | * next header starts at cur_next. We're only interested in |
| 4979 | * "Cookie:" headers. |
| 4980 | */ |
| 4981 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4982 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 4983 | if (val) { |
| 4984 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 4985 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 4986 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 4987 | return; |
| 4988 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4989 | } |
| 4990 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4991 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 4992 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4993 | continue; |
| 4994 | |
| 4995 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 4996 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4997 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4998 | |
| 4999 | if (p1 >= cur_end) /* no more info */ |
| 5000 | continue; |
| 5001 | |
| 5002 | /* p1 is at the beginning of the value */ |
| 5003 | p2 = p1; |
| 5004 | |
| 5005 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((int)*p2)) |
| 5006 | p2++; |
| 5007 | |
| 5008 | /* we have a complete value between p1 and p2 */ |
| 5009 | if (p2 < cur_end && *p2 == '=') { |
| 5010 | /* we have something of the form no-cache="set-cookie" */ |
| 5011 | if ((cur_end - p1 >= 21) && |
| 5012 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 5013 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5014 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5015 | continue; |
| 5016 | } |
| 5017 | |
| 5018 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 5019 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 5020 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 5021 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 5022 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5023 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5024 | return; |
| 5025 | } |
| 5026 | |
| 5027 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5028 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5029 | continue; |
| 5030 | } |
| 5031 | } |
| 5032 | } |
| 5033 | |
| 5034 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5035 | /* |
| 5036 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 5037 | * If the server is found, it's assigned to the session. |
| 5038 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5039 | 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] | 5040 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5041 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5042 | appsess *asession_temp = NULL; |
| 5043 | appsess local_asession; |
| 5044 | char *request_line; |
| 5045 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5046 | if (t->be->appsession_name == NULL || |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 5047 | (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) || |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5048 | (request_line = memchr(begin, ';', len)) == NULL || |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5049 | ((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] | 5050 | return; |
| 5051 | |
| 5052 | /* skip ';' */ |
| 5053 | request_line++; |
| 5054 | |
| 5055 | /* look if we have a jsessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5056 | 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] | 5057 | return; |
| 5058 | |
| 5059 | /* skip jsessionid= */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5060 | request_line += t->be->appsession_name_len + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5061 | |
| 5062 | /* First try if we already have an appsession */ |
| 5063 | asession_temp = &local_asession; |
| 5064 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5065 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5066 | Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n"); |
| 5067 | send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n"); |
| 5068 | return; |
| 5069 | } |
| 5070 | |
| 5071 | /* Copy the sessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5072 | memcpy(asession_temp->sessid, request_line, t->be->appsession_len); |
| 5073 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5074 | asession_temp->serverid = NULL; |
| 5075 | |
| 5076 | /* only do insert, if lookup fails */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5077 | if (chtbl_lookup(&(t->be->htbl_proxy), (void *)&asession_temp)) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5078 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5079 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5080 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5081 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 5082 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 5083 | return; |
| 5084 | } |
| 5085 | asession_temp->sessid = local_asession.sessid; |
| 5086 | asession_temp->serverid = local_asession.serverid; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5087 | chtbl_insert(&(t->be->htbl_proxy), (void *) asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5088 | } |
| 5089 | else { |
| 5090 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5091 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5092 | } |
| 5093 | |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 5094 | tv_add(&asession_temp->expire, &now, &t->be->appsession_timeout); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5095 | asession_temp->request_count++; |
| 5096 | |
| 5097 | #if defined(DEBUG_HASH) |
| 5098 | print_table(&(t->proxy->htbl_proxy)); |
| 5099 | #endif |
| 5100 | if (asession_temp->serverid == NULL) { |
| 5101 | Alert("Found Application Session without matching server.\n"); |
| 5102 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5103 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5104 | while (srv) { |
| 5105 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5106 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5107 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5108 | txn->flags &= ~TX_CK_MASK; |
| 5109 | txn->flags |= TX_CK_VALID; |
| 5110 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5111 | t->srv = srv; |
| 5112 | break; |
| 5113 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5114 | txn->flags &= ~TX_CK_MASK; |
| 5115 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5116 | } |
| 5117 | } |
| 5118 | srv = srv->next; |
| 5119 | } |
| 5120 | } |
| 5121 | } |
| 5122 | |
| 5123 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5124 | /* |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5125 | * In a GET or HEAD request, check if the requested URI matches the stats uri |
| 5126 | * 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] | 5127 | * |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5128 | * 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] | 5129 | * 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] | 5130 | * produce_content() can be called to start sending data. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5131 | * |
| 5132 | * Returns 1 if the session's state changes, otherwise 0. |
| 5133 | */ |
| 5134 | int stats_check_uri_auth(struct session *t, struct proxy *backend) |
| 5135 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5136 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5137 | struct uri_auth *uri_auth = backend->uri_auth; |
| 5138 | struct user_auth *user; |
| 5139 | int authenticated, cur_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5140 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5141 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5142 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5143 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5144 | return 0; |
| 5145 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5146 | h = t->req->data + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5147 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5148 | /* the URI is in h */ |
| 5149 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5150 | return 0; |
| 5151 | |
| 5152 | /* we are in front of a interceptable URI. Let's check |
| 5153 | * if there's an authentication and if it's valid. |
| 5154 | */ |
| 5155 | user = uri_auth->users; |
| 5156 | if (!user) { |
| 5157 | /* no user auth required, it's OK */ |
| 5158 | authenticated = 1; |
| 5159 | } else { |
| 5160 | authenticated = 0; |
| 5161 | |
| 5162 | /* a user list is defined, we have to check. |
| 5163 | * skip 21 chars for "Authorization: Basic ". |
| 5164 | */ |
| 5165 | |
| 5166 | /* FIXME: this should move to an earlier place */ |
| 5167 | cur_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5168 | h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 5169 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 5170 | int len = txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5171 | if (len > 14 && |
| 5172 | !strncasecmp("Authorization:", h, 14)) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5173 | txn->auth_hdr.str = h; |
| 5174 | txn->auth_hdr.len = len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5175 | break; |
| 5176 | } |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5177 | h += len + txn->hdr_idx.v[cur_idx].cr + 1; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5178 | } |
| 5179 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5180 | if (txn->auth_hdr.len < 21 || |
| 5181 | memcmp(txn->auth_hdr.str + 14, " Basic ", 7)) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5182 | user = NULL; |
| 5183 | |
| 5184 | while (user) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5185 | if ((txn->auth_hdr.len == user->user_len + 14 + 7) |
| 5186 | && !memcmp(txn->auth_hdr.str + 14 + 7, |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5187 | user->user_pwd, user->user_len)) { |
| 5188 | authenticated = 1; |
| 5189 | break; |
| 5190 | } |
| 5191 | user = user->next; |
| 5192 | } |
| 5193 | } |
| 5194 | |
| 5195 | if (!authenticated) { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5196 | struct chunk msg; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5197 | |
| 5198 | /* no need to go further */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5199 | msg.str = trash; |
| 5200 | msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm); |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5201 | txn->status = 401; |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5202 | client_retnclose(t, &msg); |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5203 | if (!(t->flags & SN_ERR_MASK)) |
| 5204 | t->flags |= SN_ERR_PRXCOND; |
| 5205 | if (!(t->flags & SN_FINST_MASK)) |
| 5206 | t->flags |= SN_FINST_R; |
| 5207 | return 1; |
| 5208 | } |
| 5209 | |
| 5210 | /* The request is valid, the user is authenticate. Let's start sending |
| 5211 | * data. |
| 5212 | */ |
| 5213 | t->cli_state = CL_STSHUTR; |
| 5214 | t->req->rlim = t->req->data + BUFSIZE; /* no more rewrite needed */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 5215 | t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5216 | t->data_source = DATA_SRC_STATS; |
| 5217 | t->data_state = DATA_ST_INIT; |
| 5218 | produce_content(t); |
| 5219 | return 1; |
| 5220 | } |
| 5221 | |
| 5222 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5223 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5224 | * Print a debug line with a header |
| 5225 | */ |
| 5226 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 5227 | { |
| 5228 | int len, max; |
| 5229 | len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id, |
| 5230 | dir, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd); |
| 5231 | max = end - start; |
| 5232 | UBOUND(max, sizeof(trash) - len - 1); |
| 5233 | len += strlcpy2(trash + len, start, max + 1); |
| 5234 | trash[len++] = '\n'; |
| 5235 | write(1, trash, len); |
| 5236 | } |
| 5237 | |
| 5238 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5239 | /************************************************************************/ |
| 5240 | /* The code below is dedicated to ACL parsing and matching */ |
| 5241 | /************************************************************************/ |
| 5242 | |
| 5243 | |
| 5244 | |
| 5245 | |
| 5246 | /* 1. Check on METHOD |
| 5247 | * We use the pre-parsed method if it is known, and store its number as an |
| 5248 | * integer. If it is unknown, we use the pointer and the length. |
| 5249 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5250 | 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] | 5251 | { |
| 5252 | int len, meth; |
| 5253 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5254 | len = strlen(*text); |
| 5255 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5256 | |
| 5257 | pattern->val.i = meth; |
| 5258 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5259 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5260 | if (!pattern->ptr.str) |
| 5261 | return 0; |
| 5262 | pattern->len = len; |
| 5263 | } |
| 5264 | return 1; |
| 5265 | } |
| 5266 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5267 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5268 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5269 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5270 | { |
| 5271 | int meth; |
| 5272 | struct http_txn *txn = l7; |
| 5273 | |
| 5274 | meth = txn->meth; |
| 5275 | test->i = meth; |
| 5276 | if (meth == HTTP_METH_OTHER) { |
| 5277 | test->len = txn->req.sl.rq.m_l; |
| 5278 | test->ptr = txn->req.sol; |
| 5279 | } |
| 5280 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5281 | return 1; |
| 5282 | } |
| 5283 | |
| 5284 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 5285 | { |
| 5286 | if (test->i != pattern->val.i) |
| 5287 | return 0; |
| 5288 | |
| 5289 | if (test->i != HTTP_METH_OTHER) |
| 5290 | return 1; |
| 5291 | |
| 5292 | /* Other method, we must compare the strings */ |
| 5293 | if (pattern->len != test->len) |
| 5294 | return 0; |
| 5295 | if (strncmp(pattern->ptr.str, test->ptr, test->len) != 0) |
| 5296 | return 0; |
| 5297 | return 1; |
| 5298 | } |
| 5299 | |
| 5300 | /* 2. Check on Request/Status Version |
| 5301 | * We simply compare strings here. |
| 5302 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5303 | 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] | 5304 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5305 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5306 | if (!pattern->ptr.str) |
| 5307 | return 0; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5308 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5309 | return 1; |
| 5310 | } |
| 5311 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5312 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5313 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5314 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5315 | { |
| 5316 | struct http_txn *txn = l7; |
| 5317 | char *ptr; |
| 5318 | int len; |
| 5319 | |
| 5320 | len = txn->req.sl.rq.v_l; |
| 5321 | ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som; |
| 5322 | |
| 5323 | while ((len-- > 0) && (*ptr++ != '/')); |
| 5324 | if (len <= 0) |
| 5325 | return 0; |
| 5326 | |
| 5327 | test->ptr = ptr; |
| 5328 | test->len = len; |
| 5329 | |
| 5330 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5331 | return 1; |
| 5332 | } |
| 5333 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5334 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5335 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5336 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5337 | { |
| 5338 | struct http_txn *txn = l7; |
| 5339 | char *ptr; |
| 5340 | int len; |
| 5341 | |
| 5342 | len = txn->rsp.sl.st.v_l; |
| 5343 | ptr = txn->rsp.sol; |
| 5344 | |
| 5345 | while ((len-- > 0) && (*ptr++ != '/')); |
| 5346 | if (len <= 0) |
| 5347 | return 0; |
| 5348 | |
| 5349 | test->ptr = ptr; |
| 5350 | test->len = len; |
| 5351 | |
| 5352 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5353 | return 1; |
| 5354 | } |
| 5355 | |
| 5356 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5357 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5358 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5359 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5360 | { |
| 5361 | struct http_txn *txn = l7; |
| 5362 | char *ptr; |
| 5363 | int len; |
| 5364 | |
| 5365 | len = txn->rsp.sl.st.c_l; |
| 5366 | ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som; |
| 5367 | |
| 5368 | test->i = __strl2ui(ptr, len); |
| 5369 | test->flags = ACL_TEST_F_VOL_1ST; |
| 5370 | return 1; |
| 5371 | } |
| 5372 | |
| 5373 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5374 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5375 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5376 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5377 | { |
| 5378 | struct http_txn *txn = l7; |
| 5379 | |
| 5380 | test->len = txn->req.sl.rq.u_l; |
| 5381 | test->ptr = txn->req.sol + txn->req.sl.rq.u; |
| 5382 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 5383 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 5384 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5385 | return 1; |
| 5386 | } |
| 5387 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5388 | /* 5. Check on HTTP header. A pointer to the beginning of the value is returned. */ |
| 5389 | static int |
| 5390 | acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5391 | struct acl_expr *expr, struct acl_test *test) |
| 5392 | { |
| 5393 | struct http_txn *txn = l7; |
| 5394 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5395 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
| 5396 | char *sol; |
| 5397 | |
| 5398 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 5399 | /* search for header from the beginning */ |
| 5400 | ctx->idx = 0; |
| 5401 | |
| 5402 | sol = (dir == ACL_DIR_REQ) ? txn->req.sol : txn->rsp.sol; |
| 5403 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 5404 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 5405 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5406 | test->len = ctx->vlen; |
| 5407 | test->ptr = (char *)ctx->line + ctx->val; |
| 5408 | return 1; |
| 5409 | } |
| 5410 | |
| 5411 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 5412 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5413 | return 0; |
| 5414 | } |
| 5415 | |
| 5416 | /* 6. Check on HTTP header count. The number of occurrences is returned. */ |
| 5417 | static int |
| 5418 | acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5419 | struct acl_expr *expr, struct acl_test *test) |
| 5420 | { |
| 5421 | struct http_txn *txn = l7; |
| 5422 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5423 | struct hdr_ctx ctx; |
| 5424 | char *sol; |
| 5425 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5426 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5427 | sol = (dir == ACL_DIR_REQ) ? txn->req.sol : txn->rsp.sol; |
| 5428 | |
| 5429 | ctx.idx = 0; |
| 5430 | cnt = 0; |
| 5431 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx)) |
| 5432 | cnt++; |
| 5433 | |
| 5434 | test->i = cnt; |
| 5435 | test->flags = ACL_TEST_F_VOL_HDR; |
| 5436 | return 1; |
| 5437 | } |
| 5438 | |
| 5439 | /* 7. Check on HTTP header's integer value. The integer value is returned. |
| 5440 | * FIXME: the type is 'int', it may not be appropriate for everything. |
| 5441 | */ |
| 5442 | static int |
| 5443 | acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5444 | struct acl_expr *expr, struct acl_test *test) |
| 5445 | { |
| 5446 | struct http_txn *txn = l7; |
| 5447 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5448 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
| 5449 | char *sol; |
| 5450 | |
| 5451 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 5452 | /* search for header from the beginning */ |
| 5453 | ctx->idx = 0; |
| 5454 | |
| 5455 | sol = (dir == ACL_DIR_REQ) ? txn->req.sol : txn->rsp.sol; |
| 5456 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 5457 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 5458 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5459 | test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen); |
| 5460 | return 1; |
| 5461 | } |
| 5462 | |
| 5463 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 5464 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5465 | return 0; |
| 5466 | } |
| 5467 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5468 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 5469 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 5470 | */ |
| 5471 | static int |
| 5472 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5473 | struct acl_expr *expr, struct acl_test *test) |
| 5474 | { |
| 5475 | struct http_txn *txn = l7; |
| 5476 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5477 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5478 | ptr = txn->req.sol + txn->req.sl.rq.u; |
| 5479 | end = ptr + txn->req.sl.rq.u_l; |
| 5480 | |
| 5481 | if (ptr >= end) |
| 5482 | return 0; |
| 5483 | |
| 5484 | /* RFC2616, par. 5.1.2 : |
| 5485 | * Request-URI = "*" | absuri | abspath | authority |
| 5486 | */ |
| 5487 | |
| 5488 | if (*ptr == '*') |
| 5489 | return 0; |
| 5490 | |
| 5491 | if (isalpha(*ptr)) { |
| 5492 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 5493 | ptr++; |
| 5494 | while (ptr < end && |
| 5495 | (isalnum(*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 5496 | ptr++; |
| 5497 | /* skip '://' */ |
| 5498 | if (ptr == end || *ptr++ != ':') |
| 5499 | return 0; |
| 5500 | if (ptr == end || *ptr++ != '/') |
| 5501 | return 0; |
| 5502 | if (ptr == end || *ptr++ != '/') |
| 5503 | return 0; |
| 5504 | } |
| 5505 | /* skip [user[:passwd]@]host[:[port]] */ |
| 5506 | |
| 5507 | while (ptr < end && *ptr != '/') |
| 5508 | ptr++; |
| 5509 | |
| 5510 | if (ptr == end) |
| 5511 | return 0; |
| 5512 | |
| 5513 | /* OK, we got the '/' ! */ |
| 5514 | test->ptr = ptr; |
| 5515 | |
| 5516 | while (ptr < end && *ptr != '?') |
| 5517 | ptr++; |
| 5518 | |
| 5519 | test->len = ptr - test->ptr; |
| 5520 | |
| 5521 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 5522 | test->flags = ACL_TEST_F_VOL_1ST; |
| 5523 | return 1; |
| 5524 | } |
| 5525 | |
| 5526 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5527 | |
| 5528 | /************************************************************************/ |
| 5529 | /* All supported keywords must be declared here. */ |
| 5530 | /************************************************************************/ |
| 5531 | |
| 5532 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 5533 | static struct acl_kw_list acl_kws = {{ },{ |
| 5534 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth }, |
| 5535 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str }, |
| 5536 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str }, |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5537 | { "status", acl_parse_int, acl_fetch_stcode, acl_match_int }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5538 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5539 | { "url", acl_parse_str, acl_fetch_url, acl_match_str }, |
| 5540 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg }, |
| 5541 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end }, |
| 5542 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub }, |
| 5543 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir }, |
| 5544 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom }, |
| 5545 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5546 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5547 | { "hdr", acl_parse_str, acl_fetch_hdr, acl_match_str }, |
| 5548 | { "hdr_reg", acl_parse_reg, acl_fetch_hdr, acl_match_reg }, |
| 5549 | { "hdr_beg", acl_parse_str, acl_fetch_hdr, acl_match_beg }, |
| 5550 | { "hdr_end", acl_parse_str, acl_fetch_hdr, acl_match_end }, |
| 5551 | { "hdr_sub", acl_parse_str, acl_fetch_hdr, acl_match_sub }, |
| 5552 | { "hdr_dir", acl_parse_str, acl_fetch_hdr, acl_match_dir }, |
| 5553 | { "hdr_dom", acl_parse_str, acl_fetch_hdr, acl_match_dom }, |
| 5554 | { "hdr_cnt", acl_parse_int, acl_fetch_hdr_cnt, acl_match_int }, |
| 5555 | { "hdr_val", acl_parse_int, acl_fetch_hdr_val, acl_match_int }, |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5556 | |
| 5557 | { "path", acl_parse_str, acl_fetch_path, acl_match_str }, |
| 5558 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg }, |
| 5559 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg }, |
| 5560 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end }, |
| 5561 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub }, |
| 5562 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir }, |
| 5563 | { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom }, |
| 5564 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 5565 | { NULL, NULL, NULL, NULL }, |
| 5566 | |
| 5567 | #if 0 |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5568 | { "line", acl_parse_str, acl_fetch_line, acl_match_str }, |
| 5569 | { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg }, |
| 5570 | { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg }, |
| 5571 | { "line_end", acl_parse_str, acl_fetch_line, acl_match_end }, |
| 5572 | { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub }, |
| 5573 | { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir }, |
| 5574 | { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom }, |
| 5575 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5576 | { "cook", acl_parse_str, acl_fetch_cook, acl_match_str }, |
| 5577 | { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg }, |
| 5578 | { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg }, |
| 5579 | { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end }, |
| 5580 | { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub }, |
| 5581 | { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir }, |
| 5582 | { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom }, |
| 5583 | { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst }, |
| 5584 | |
| 5585 | { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str }, |
| 5586 | { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg }, |
| 5587 | { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str }, |
| 5588 | { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 }, |
| 5589 | { NULL, NULL, NULL, NULL }, |
| 5590 | #endif |
| 5591 | }}; |
| 5592 | |
| 5593 | |
| 5594 | __attribute__((constructor)) |
| 5595 | static void __http_protocol_init(void) |
| 5596 | { |
| 5597 | acl_register_keywords(&acl_kws); |
| 5598 | } |
| 5599 | |
| 5600 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5601 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5602 | * Local variables: |
| 5603 | * c-indent-level: 8 |
| 5604 | * c-basic-offset: 8 |
| 5605 | * End: |
| 5606 | */ |