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 | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 1891 | /* now check whether we have some switching rules for this request */ |
| 1892 | if (!(t->flags & SN_BE_ASSIGNED)) { |
| 1893 | struct switching_rule *rule; |
| 1894 | |
| 1895 | list_for_each_entry(rule, &cur_proxy->switching_rules, list) { |
| 1896 | int ret; |
| 1897 | |
| 1898 | ret = acl_exec_cond(rule->cond, cur_proxy, t, txn, ACL_DIR_REQ); |
| 1899 | if (cond->pol == ACL_COND_UNLESS) |
| 1900 | ret = !ret; |
| 1901 | |
| 1902 | if (ret) { |
| 1903 | t->be = rule->be.backend; |
| 1904 | t->be->beconn++; |
| 1905 | if (t->be->beconn > t->be->beconn_max) |
| 1906 | t->be->beconn_max = t->be->beconn; |
| 1907 | t->be->cum_beconn++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 1908 | |
| 1909 | /* assign new parameters to the session from the new backend */ |
| 1910 | t->rep->rto = t->req->wto = t->be->srvtimeout; |
| 1911 | t->req->cto = t->be->contimeout; |
| 1912 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 1913 | t->flags |= SN_BE_ASSIGNED; |
| 1914 | break; |
| 1915 | } |
| 1916 | } |
| 1917 | } |
| 1918 | |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 1919 | if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) { |
| 1920 | /* No backend was set, but there was a default |
| 1921 | * backend set in the frontend, so we use it and |
| 1922 | * loop again. |
| 1923 | */ |
| 1924 | t->be = cur_proxy->defbe.be; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1925 | t->be->beconn++; |
| 1926 | if (t->be->beconn > t->be->beconn_max) |
| 1927 | t->be->beconn_max = t->be->beconn; |
| 1928 | t->be->cum_beconn++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 1929 | |
| 1930 | /* assign new parameters to the session from the new backend */ |
| 1931 | t->rep->rto = t->req->wto = t->be->srvtimeout; |
| 1932 | t->req->cto = t->be->contimeout; |
| 1933 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 1934 | t->flags |= SN_BE_ASSIGNED; |
| 1935 | } |
| 1936 | } 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] | 1937 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1938 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 1939 | if (!(t->flags & SN_BE_ASSIGNED)) { |
| 1940 | /* To ensure correct connection accounting on |
| 1941 | * the backend, we count the connection for the |
| 1942 | * one managing the queue. |
| 1943 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1944 | t->be->beconn++; |
| 1945 | if (t->be->beconn > t->be->beconn_max) |
| 1946 | t->be->beconn_max = t->be->beconn; |
| 1947 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 1948 | t->flags |= SN_BE_ASSIGNED; |
| 1949 | } |
| 1950 | |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1951 | /* |
| 1952 | * Right now, we know that we have processed the entire headers |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1953 | * and that unwanted requests have been filtered out. We can do |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1954 | * whatever we want with the remaining request. Also, now we |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1955 | * may have separate values for ->fe, ->be. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1956 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1957 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1958 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1959 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1960 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1961 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1962 | * 7: the appsession cookie was looked up very early in 1.2, |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1963 | * so let's do the same now. |
| 1964 | */ |
| 1965 | |
| 1966 | /* It needs to look into the URI */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1967 | if (t->be->appsession_name) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1968 | 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] | 1969 | } |
| 1970 | |
| 1971 | |
| 1972 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1973 | * 8: Now we can work with the cookies. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1974 | * Note that doing so might move headers in the request, but |
| 1975 | * the fields will stay coherent and the URI will not move. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1976 | * This should only be performed in the backend. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1977 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 1978 | if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1979 | manage_client_side_cookies(t, req); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1980 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1981 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1982 | /* |
Willy Tarreau | bb046ac | 2007-03-03 19:17:03 +0100 | [diff] [blame] | 1983 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 1984 | * asks for it. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1985 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1986 | if ((t->fe->options | t->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1987 | if (t->cli_addr.ss_family == AF_INET) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 1988 | /* Add an X-Forwarded-For header unless the source IP is |
| 1989 | * in the 'except' network range. |
| 1990 | */ |
| 1991 | if ((!t->fe->except_mask.s_addr || |
| 1992 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr) |
| 1993 | != t->fe->except_net.s_addr) && |
| 1994 | (!t->be->except_mask.s_addr || |
| 1995 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr) |
| 1996 | != t->be->except_net.s_addr)) { |
| 1997 | int len; |
| 1998 | unsigned char *pn; |
| 1999 | pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr; |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2000 | |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2001 | len = sprintf(trash, "X-Forwarded-For: %d.%d.%d.%d", |
| 2002 | pn[0], pn[1], pn[2], pn[3]); |
| 2003 | |
| 2004 | if (unlikely(http_header_add_tail2(req, &txn->req, |
| 2005 | &txn->hdr_idx, trash, len)) < 0) |
| 2006 | goto return_bad_req; |
| 2007 | } |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2008 | } |
| 2009 | else if (t->cli_addr.ss_family == AF_INET6) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2010 | /* FIXME: for the sake of completeness, we should also support |
| 2011 | * 'except' here, although it is mostly useless in this case. |
| 2012 | */ |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2013 | int len; |
| 2014 | char pn[INET6_ADDRSTRLEN]; |
| 2015 | inet_ntop(AF_INET6, |
| 2016 | (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr, |
| 2017 | pn, sizeof(pn)); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2018 | len = sprintf(trash, "X-Forwarded-For: %s", pn); |
| 2019 | if (unlikely(http_header_add_tail2(req, &txn->req, |
| 2020 | &txn->hdr_idx, trash, len)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2021 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2022 | } |
| 2023 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2024 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2025 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2026 | * 10: add "Connection: close" if needed and not yet set. |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2027 | * 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] | 2028 | */ |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2029 | if (!(t->flags & SN_CONN_CLOSED) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2030 | ((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE)) { |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2031 | if ((unlikely(msg->sl.rq.v_l != 8) || |
| 2032 | unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) && |
| 2033 | unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx, |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2034 | "Connection: close", 17)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2035 | goto return_bad_req; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2036 | t->flags |= SN_CONN_CLOSED; |
Willy Tarreau | e15d913 | 2006-12-14 22:26:42 +0100 | [diff] [blame] | 2037 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2038 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2039 | /************************************************************* |
| 2040 | * OK, that's finished for the headers. We have done what we * |
| 2041 | * could. Let's switch to the DATA state. * |
| 2042 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2043 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2044 | t->cli_state = CL_STDATA; |
| 2045 | req->rlim = req->data + BUFSIZE; /* no more rewrite needed */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2046 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2047 | t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2048 | |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 2049 | if (!tv_isset(&t->fe->clitimeout) || |
| 2050 | (t->srv_state < SV_STDATA && tv_isset(&t->be->srvtimeout))) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2051 | /* If the client has no timeout, or if the server is not ready yet, |
| 2052 | * and we know for sure that it can expire, then it's cleaner to |
| 2053 | * disable the timeout on the client side so that too low values |
| 2054 | * cannot make the sessions abort too early. |
| 2055 | * |
| 2056 | * FIXME-20050705: the server needs a way to re-enable this time-out |
| 2057 | * when it switches its state, otherwise a client can stay connected |
| 2058 | * indefinitely. This now seems to be OK. |
| 2059 | */ |
| 2060 | tv_eternity(&req->rex); |
| 2061 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2062 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2063 | /* When a connection is tarpitted, we use the queue timeout for the |
| 2064 | * tarpit delay, which currently happens to be the server's connect |
| 2065 | * timeout. If unset, then set it to zero because we really want it |
| 2066 | * to expire at one moment. |
| 2067 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2068 | if (txn->flags & TX_CLTARPIT) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2069 | t->req->l = 0; |
| 2070 | /* flush the request so that we can drop the connection early |
| 2071 | * if the client closes first. |
| 2072 | */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2073 | if (!tv_add_ifset(&req->cex, &now, &t->be->contimeout)) |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 2074 | req->cex = now; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2075 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2076 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2077 | /* OK let's go on with the BODY now */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2078 | goto process_data; |
| 2079 | |
| 2080 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 2081 | txn->req.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2082 | txn->status = 400; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2083 | client_retnclose(t, error_message(t, HTTP_ERR_400)); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 2084 | t->fe->failed_req++; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2085 | return_prx_cond: |
| 2086 | if (!(t->flags & SN_ERR_MASK)) |
| 2087 | t->flags |= SN_ERR_PRXCOND; |
| 2088 | if (!(t->flags & SN_FINST_MASK)) |
| 2089 | t->flags |= SN_FINST_R; |
| 2090 | return 1; |
| 2091 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2092 | } |
| 2093 | else if (c == CL_STDATA) { |
| 2094 | process_data: |
| 2095 | /* FIXME: this error handling is partly buggy because we always report |
| 2096 | * a 'DATA' phase while we don't know if the server was in IDLE, CONN |
| 2097 | * or HEADER phase. BTW, it's not logical to expire the client while |
| 2098 | * we're waiting for the server to connect. |
| 2099 | */ |
| 2100 | /* read or write error */ |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2101 | if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2102 | buffer_shutr(req); |
| 2103 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2104 | fd_delete(t->cli_fd); |
| 2105 | t->cli_state = CL_STCLOSE; |
| 2106 | if (!(t->flags & SN_ERR_MASK)) |
| 2107 | t->flags |= SN_ERR_CLICL; |
| 2108 | if (!(t->flags & SN_FINST_MASK)) { |
| 2109 | if (t->pend_pos) |
| 2110 | t->flags |= SN_FINST_Q; |
| 2111 | else if (s == SV_STCONN) |
| 2112 | t->flags |= SN_FINST_C; |
| 2113 | else |
| 2114 | t->flags |= SN_FINST_D; |
| 2115 | } |
| 2116 | return 1; |
| 2117 | } |
| 2118 | /* last read, or end of server write */ |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2119 | 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] | 2120 | EV_FD_CLR(t->cli_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2121 | buffer_shutr(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2122 | t->cli_state = CL_STSHUTR; |
| 2123 | return 1; |
| 2124 | } |
| 2125 | /* last server read and buffer empty */ |
| 2126 | else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2127 | EV_FD_CLR(t->cli_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2128 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2129 | shutdown(t->cli_fd, SHUT_WR); |
| 2130 | /* We must ensure that the read part is still alive when switching |
| 2131 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2132 | EV_FD_SET(t->cli_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2133 | tv_add_ifset(&req->rex, &now, &t->fe->clitimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2134 | t->cli_state = CL_STSHUTW; |
| 2135 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 2136 | return 1; |
| 2137 | } |
| 2138 | /* read timeout */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2139 | else if (tv_isle(&req->rex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2140 | EV_FD_CLR(t->cli_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2141 | buffer_shutr(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2142 | t->cli_state = CL_STSHUTR; |
| 2143 | if (!(t->flags & SN_ERR_MASK)) |
| 2144 | t->flags |= SN_ERR_CLITO; |
| 2145 | if (!(t->flags & SN_FINST_MASK)) { |
| 2146 | if (t->pend_pos) |
| 2147 | t->flags |= SN_FINST_Q; |
| 2148 | else if (s == SV_STCONN) |
| 2149 | t->flags |= SN_FINST_C; |
| 2150 | else |
| 2151 | t->flags |= SN_FINST_D; |
| 2152 | } |
| 2153 | return 1; |
| 2154 | } |
| 2155 | /* write timeout */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2156 | else if (tv_isle(&rep->wex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2157 | EV_FD_CLR(t->cli_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2158 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2159 | shutdown(t->cli_fd, SHUT_WR); |
| 2160 | /* We must ensure that the read part is still alive when switching |
| 2161 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2162 | EV_FD_SET(t->cli_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2163 | tv_add_ifset(&req->rex, &now, &t->fe->clitimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2164 | |
| 2165 | t->cli_state = CL_STSHUTW; |
| 2166 | if (!(t->flags & SN_ERR_MASK)) |
| 2167 | t->flags |= SN_ERR_CLITO; |
| 2168 | if (!(t->flags & SN_FINST_MASK)) { |
| 2169 | if (t->pend_pos) |
| 2170 | t->flags |= SN_FINST_Q; |
| 2171 | else if (s == SV_STCONN) |
| 2172 | t->flags |= SN_FINST_C; |
| 2173 | else |
| 2174 | t->flags |= SN_FINST_D; |
| 2175 | } |
| 2176 | return 1; |
| 2177 | } |
| 2178 | |
| 2179 | if (req->l >= req->rlim - req->data) { |
| 2180 | /* no room to read more data */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2181 | if (EV_FD_COND_C(t->cli_fd, DIR_RD)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2182 | /* stop reading until we get some space */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2183 | tv_eternity(&req->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2184 | } |
| 2185 | } else { |
| 2186 | /* there's still some space in the buffer */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2187 | if (EV_FD_COND_S(t->cli_fd, DIR_RD)) { |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 2188 | if (!tv_isset(&t->fe->clitimeout) || |
| 2189 | (t->srv_state < SV_STDATA && tv_isset(&t->be->srvtimeout))) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2190 | /* If the client has no timeout, or if the server not ready yet, and we |
| 2191 | * know for sure that it can expire, then it's cleaner to disable the |
| 2192 | * timeout on the client side so that too low values cannot make the |
| 2193 | * sessions abort too early. |
| 2194 | */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2195 | tv_eternity(&req->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2196 | else |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 2197 | tv_add(&req->rex, &now, &t->fe->clitimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2198 | } |
| 2199 | } |
| 2200 | |
| 2201 | if ((rep->l == 0) || |
| 2202 | ((s < SV_STDATA) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2203 | if (EV_FD_COND_C(t->cli_fd, DIR_WR)) { |
| 2204 | /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2205 | tv_eternity(&rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2206 | } |
| 2207 | } else { |
| 2208 | /* buffer not empty */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2209 | if (EV_FD_COND_S(t->cli_fd, DIR_WR)) { |
| 2210 | /* restart writing */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2211 | if (tv_add_ifset(&rep->wex, &now, &t->fe->clitimeout)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2212 | /* FIXME: to prevent the client from expiring read timeouts during writes, |
| 2213 | * we refresh it. */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2214 | req->rex = rep->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2215 | } |
| 2216 | else |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2217 | tv_eternity(&rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2218 | } |
| 2219 | } |
| 2220 | return 0; /* other cases change nothing */ |
| 2221 | } |
| 2222 | else if (c == CL_STSHUTR) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2223 | if (rep->flags & BF_WRITE_ERROR) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2224 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2225 | fd_delete(t->cli_fd); |
| 2226 | t->cli_state = CL_STCLOSE; |
| 2227 | if (!(t->flags & SN_ERR_MASK)) |
| 2228 | t->flags |= SN_ERR_CLICL; |
| 2229 | if (!(t->flags & SN_FINST_MASK)) { |
| 2230 | if (t->pend_pos) |
| 2231 | t->flags |= SN_FINST_Q; |
| 2232 | else if (s == SV_STCONN) |
| 2233 | t->flags |= SN_FINST_C; |
| 2234 | else |
| 2235 | t->flags |= SN_FINST_D; |
| 2236 | } |
| 2237 | return 1; |
| 2238 | } |
| 2239 | else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0) |
| 2240 | && !(t->flags & SN_SELF_GEN)) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2241 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2242 | fd_delete(t->cli_fd); |
| 2243 | t->cli_state = CL_STCLOSE; |
| 2244 | return 1; |
| 2245 | } |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2246 | else if (tv_isle(&rep->wex, &now)) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2247 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2248 | fd_delete(t->cli_fd); |
| 2249 | t->cli_state = CL_STCLOSE; |
| 2250 | if (!(t->flags & SN_ERR_MASK)) |
| 2251 | t->flags |= SN_ERR_CLITO; |
| 2252 | if (!(t->flags & SN_FINST_MASK)) { |
| 2253 | if (t->pend_pos) |
| 2254 | t->flags |= SN_FINST_Q; |
| 2255 | else if (s == SV_STCONN) |
| 2256 | t->flags |= SN_FINST_C; |
| 2257 | else |
| 2258 | t->flags |= SN_FINST_D; |
| 2259 | } |
| 2260 | return 1; |
| 2261 | } |
| 2262 | |
| 2263 | if (t->flags & SN_SELF_GEN) { |
| 2264 | produce_content(t); |
| 2265 | if (rep->l == 0) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2266 | buffer_shutw(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2267 | fd_delete(t->cli_fd); |
| 2268 | t->cli_state = CL_STCLOSE; |
| 2269 | return 1; |
| 2270 | } |
| 2271 | } |
| 2272 | |
| 2273 | if ((rep->l == 0) |
| 2274 | || ((s == SV_STHEADERS) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2275 | if (EV_FD_COND_C(t->cli_fd, DIR_WR)) { |
| 2276 | /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2277 | tv_eternity(&rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2278 | } |
| 2279 | } else { |
| 2280 | /* buffer not empty */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2281 | if (EV_FD_COND_S(t->cli_fd, DIR_WR)) { |
| 2282 | /* restart writing */ |
Willy Tarreau | 33014d0 | 2007-06-03 15:25:37 +0200 | [diff] [blame] | 2283 | if (!tv_add_ifset(&rep->wex, &now, &t->fe->clitimeout)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2284 | tv_eternity(&rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2285 | } |
| 2286 | } |
| 2287 | return 0; |
| 2288 | } |
| 2289 | else if (c == CL_STSHUTW) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2290 | if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2291 | buffer_shutr(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2292 | fd_delete(t->cli_fd); |
| 2293 | t->cli_state = CL_STCLOSE; |
| 2294 | if (!(t->flags & SN_ERR_MASK)) |
| 2295 | t->flags |= SN_ERR_CLICL; |
| 2296 | if (!(t->flags & SN_FINST_MASK)) { |
| 2297 | if (t->pend_pos) |
| 2298 | t->flags |= SN_FINST_Q; |
| 2299 | else if (s == SV_STCONN) |
| 2300 | t->flags |= SN_FINST_C; |
| 2301 | else |
| 2302 | t->flags |= SN_FINST_D; |
| 2303 | } |
| 2304 | return 1; |
| 2305 | } |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2306 | 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] | 2307 | buffer_shutr(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2308 | fd_delete(t->cli_fd); |
| 2309 | t->cli_state = CL_STCLOSE; |
| 2310 | return 1; |
| 2311 | } |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2312 | else if (tv_isle(&req->rex, &now)) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2313 | buffer_shutr(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2314 | fd_delete(t->cli_fd); |
| 2315 | t->cli_state = CL_STCLOSE; |
| 2316 | if (!(t->flags & SN_ERR_MASK)) |
| 2317 | t->flags |= SN_ERR_CLITO; |
| 2318 | if (!(t->flags & SN_FINST_MASK)) { |
| 2319 | if (t->pend_pos) |
| 2320 | t->flags |= SN_FINST_Q; |
| 2321 | else if (s == SV_STCONN) |
| 2322 | t->flags |= SN_FINST_C; |
| 2323 | else |
| 2324 | t->flags |= SN_FINST_D; |
| 2325 | } |
| 2326 | return 1; |
| 2327 | } |
| 2328 | else if (req->l >= req->rlim - req->data) { |
| 2329 | /* no room to read more data */ |
| 2330 | |
| 2331 | /* FIXME-20050705: is it possible for a client to maintain a session |
| 2332 | * after the timeout by sending more data after it receives a close ? |
| 2333 | */ |
| 2334 | |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2335 | if (EV_FD_COND_C(t->cli_fd, DIR_RD)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2336 | /* stop reading until we get some space */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2337 | tv_eternity(&req->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2338 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 2339 | } |
| 2340 | } else { |
| 2341 | /* there's still some space in the buffer */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2342 | if (EV_FD_COND_S(t->cli_fd, DIR_RD)) { |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2343 | if (!tv_add_ifset(&req->rex, &now, &t->fe->clitimeout)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2344 | tv_eternity(&req->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2345 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 2346 | } |
| 2347 | } |
| 2348 | return 0; |
| 2349 | } |
| 2350 | else { /* CL_STCLOSE: nothing to do */ |
| 2351 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 2352 | int len; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2353 | 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] | 2354 | write(1, trash, len); |
| 2355 | } |
| 2356 | return 0; |
| 2357 | } |
| 2358 | return 0; |
| 2359 | } |
| 2360 | |
| 2361 | |
| 2362 | /* |
| 2363 | * manages the server FSM and its socket. It returns 1 if a state has changed |
| 2364 | * (and a resync may be needed), 0 else. |
| 2365 | */ |
| 2366 | int process_srv(struct session *t) |
| 2367 | { |
| 2368 | int s = t->srv_state; |
| 2369 | int c = t->cli_state; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2370 | struct http_txn *txn = &t->txn; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2371 | struct buffer *req = t->req; |
| 2372 | struct buffer *rep = t->rep; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2373 | int conn_err; |
| 2374 | |
| 2375 | #ifdef DEBUG_FULL |
| 2376 | fprintf(stderr,"process_srv: c=%s, s=%s\n", cli_stnames[c], srv_stnames[s]); |
| 2377 | #endif |
Willy Tarreau | ee99136 | 2007-05-14 14:37:50 +0200 | [diff] [blame] | 2378 | |
| 2379 | #if 0 |
| 2380 | fprintf(stderr,"%s:%d fe->clito=%d.%d, fe->conto=%d.%d, fe->srvto=%d.%d\n", |
| 2381 | __FUNCTION__, __LINE__, |
| 2382 | t->fe->clitimeout.tv_sec, t->fe->clitimeout.tv_usec, |
| 2383 | t->fe->contimeout.tv_sec, t->fe->contimeout.tv_usec, |
| 2384 | t->fe->srvtimeout.tv_sec, t->fe->srvtimeout.tv_usec); |
| 2385 | fprintf(stderr,"%s:%d be->clito=%d.%d, be->conto=%d.%d, be->srvto=%d.%d\n", |
| 2386 | __FUNCTION__, __LINE__, |
| 2387 | t->be->clitimeout.tv_sec, t->be->clitimeout.tv_usec, |
| 2388 | t->be->contimeout.tv_sec, t->be->contimeout.tv_usec, |
| 2389 | t->be->srvtimeout.tv_sec, t->be->srvtimeout.tv_usec); |
| 2390 | |
| 2391 | fprintf(stderr,"%s:%d req->cto=%d.%d, req->rto=%d.%d, req->wto=%d.%d\n", |
| 2392 | __FUNCTION__, __LINE__, |
| 2393 | req->cto.tv_sec, req->cto.tv_usec, |
| 2394 | req->rto.tv_sec, req->rto.tv_usec, |
| 2395 | req->wto.tv_sec, req->wto.tv_usec); |
| 2396 | |
| 2397 | fprintf(stderr,"%s:%d rep->cto=%d.%d, rep->rto=%d.%d, rep->wto=%d.%d\n", |
| 2398 | __FUNCTION__, __LINE__, |
| 2399 | rep->cto.tv_sec, rep->cto.tv_usec, |
| 2400 | rep->rto.tv_sec, rep->rto.tv_usec, |
| 2401 | rep->wto.tv_sec, rep->wto.tv_usec); |
| 2402 | #endif |
| 2403 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2404 | //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] | 2405 | //EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR), |
| 2406 | //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] | 2407 | //); |
| 2408 | if (s == SV_STIDLE) { |
| 2409 | if (c == CL_STHEADERS) |
| 2410 | return 0; /* stay in idle, waiting for data to reach the client side */ |
| 2411 | else if (c == CL_STCLOSE || c == CL_STSHUTW || |
| 2412 | (c == CL_STSHUTR && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2413 | (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] | 2414 | tv_eternity(&req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2415 | if (t->pend_pos) |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2416 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2417 | /* note that this must not return any error because it would be able to |
| 2418 | * overwrite the client_retnclose() output. |
| 2419 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2420 | if (txn->flags & TX_CLTARPIT) |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 2421 | 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] | 2422 | else |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 2423 | 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] | 2424 | |
| 2425 | return 1; |
| 2426 | } |
| 2427 | else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2428 | if (txn->flags & TX_CLTARPIT) { |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 2429 | /* This connection is being tarpitted. The CLIENT side has |
| 2430 | * already set the connect expiration date to the right |
| 2431 | * timeout. We just have to check that it has not expired. |
| 2432 | */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2433 | if (!tv_isle(&req->cex, &now)) |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 2434 | return 0; |
| 2435 | |
| 2436 | /* We will set the queue timer to the time spent, just for |
| 2437 | * logging purposes. We fake a 500 server error, so that the |
| 2438 | * attacker will not suspect his connection has been tarpitted. |
| 2439 | * It will not cause trouble to the logs because we can exclude |
| 2440 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 2441 | */ |
| 2442 | tv_eternity(&req->cex); |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2443 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 2444 | srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T, |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2445 | 500, error_message(t, HTTP_ERR_500)); |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 2446 | return 1; |
| 2447 | } |
| 2448 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2449 | /* Right now, we will need to create a connection to the server. |
| 2450 | * We might already have tried, and got a connection pending, in |
| 2451 | * which case we will not do anything till it's pending. It's up |
| 2452 | * to any other session to release it and wake us up again. |
| 2453 | */ |
| 2454 | if (t->pend_pos) { |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2455 | if (!tv_isle(&req->cex, &now)) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2456 | return 0; |
| 2457 | else { |
| 2458 | /* we've been waiting too long here */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2459 | tv_eternity(&req->cex); |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2460 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2461 | srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q, |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2462 | 503, error_message(t, HTTP_ERR_503)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2463 | if (t->srv) |
| 2464 | t->srv->failed_conns++; |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 2465 | t->fe->failed_conns++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2466 | return 1; |
| 2467 | } |
| 2468 | } |
| 2469 | |
| 2470 | do { |
| 2471 | /* first, get a connection */ |
| 2472 | if (srv_redispatch_connect(t)) |
| 2473 | return t->srv_state != SV_STIDLE; |
| 2474 | |
| 2475 | /* try to (re-)connect to the server, and fail if we expire the |
| 2476 | * number of retries. |
| 2477 | */ |
| 2478 | if (srv_retryable_connect(t)) { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2479 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2480 | return t->srv_state != SV_STIDLE; |
| 2481 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2482 | } while (1); |
| 2483 | } |
| 2484 | } |
| 2485 | else if (s == SV_STCONN) { /* connection in progress */ |
| 2486 | if (c == CL_STCLOSE || c == CL_STSHUTW || |
| 2487 | (c == CL_STSHUTR && |
Willy Tarreau | c9b654b | 2007-05-08 14:46:53 +0200 | [diff] [blame] | 2488 | ((t->req->l == 0 && !(req->flags & BF_WRITE_STATUS)) || |
| 2489 | t->be->options & PR_O_ABRT_CLOSE))) { /* give up */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2490 | tv_eternity(&req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2491 | fd_delete(t->srv_fd); |
| 2492 | if (t->srv) |
| 2493 | t->srv->cur_sess--; |
| 2494 | |
| 2495 | /* note that this must not return any error because it would be able to |
| 2496 | * overwrite the client_retnclose() output. |
| 2497 | */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 2498 | 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] | 2499 | return 1; |
| 2500 | } |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2501 | if (!(req->flags & BF_WRITE_STATUS) && !tv_isle(&req->cex, &now)) { |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2502 | //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] | 2503 | return 0; /* nothing changed */ |
| 2504 | } |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2505 | else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2506 | /* timeout, asynchronous connect error or first write error */ |
| 2507 | //fprintf(stderr,"2: c=%d, s=%d\n", c, s); |
| 2508 | |
| 2509 | fd_delete(t->srv_fd); |
| 2510 | if (t->srv) |
| 2511 | t->srv->cur_sess--; |
| 2512 | |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2513 | if (!(req->flags & BF_WRITE_STATUS)) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2514 | conn_err = SN_ERR_SRVTO; // it was a connect timeout. |
| 2515 | else |
| 2516 | conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error. |
| 2517 | |
| 2518 | /* ensure that we have enough retries left */ |
| 2519 | if (srv_count_retry_down(t, conn_err)) |
| 2520 | return 1; |
| 2521 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2522 | 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] | 2523 | /* We're on our last chance, and the REDISP option was specified. |
| 2524 | * We will ignore cookie and force to balance or use the dispatcher. |
| 2525 | */ |
| 2526 | /* let's try to offer this slot to anybody */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2527 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 2528 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 2529 | |
| 2530 | if (t->srv) |
| 2531 | t->srv->failed_conns++; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2532 | t->be->failed_conns++; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 2533 | |
| 2534 | t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
| 2535 | 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] | 2536 | http_flush_cookie_flags(txn); |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 2537 | |
| 2538 | /* first, get a connection */ |
| 2539 | if (srv_redispatch_connect(t)) |
| 2540 | return t->srv_state != SV_STIDLE; |
| 2541 | } |
| 2542 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2543 | do { |
| 2544 | /* Now we will try to either reconnect to the same server or |
| 2545 | * connect to another server. If the connection gets queued |
| 2546 | * because all servers are saturated, then we will go back to |
| 2547 | * the SV_STIDLE state. |
| 2548 | */ |
| 2549 | if (srv_retryable_connect(t)) { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2550 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2551 | return t->srv_state != SV_STCONN; |
| 2552 | } |
| 2553 | |
| 2554 | /* we need to redispatch the connection to another server */ |
| 2555 | if (srv_redispatch_connect(t)) |
| 2556 | return t->srv_state != SV_STCONN; |
| 2557 | } while (1); |
| 2558 | } |
| 2559 | else { /* no error or write 0 */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2560 | t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2561 | |
| 2562 | //fprintf(stderr,"3: c=%d, s=%d\n", c, s); |
| 2563 | if (req->l == 0) /* nothing to write */ { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2564 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2565 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2566 | } else /* need the right to write */ { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2567 | EV_FD_SET(t->srv_fd, DIR_WR); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2568 | if (tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2569 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 2570 | * we refresh it. */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2571 | rep->rex = req->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2572 | } |
| 2573 | else |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2574 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2575 | } |
| 2576 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2577 | 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] | 2578 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2579 | if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2580 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2581 | |
| 2582 | t->srv_state = SV_STDATA; |
| 2583 | if (t->srv) |
| 2584 | t->srv->cum_sess++; |
| 2585 | rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */ |
| 2586 | |
| 2587 | /* if the user wants to log as soon as possible, without counting |
| 2588 | bytes from the server, then this is the right moment. */ |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 2589 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2590 | 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] | 2591 | tcp_sess_log(t); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2592 | } |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 2593 | #ifdef CONFIG_HAP_TCPSPLICE |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2594 | if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 2595 | /* TCP splicing supported by both FE and BE */ |
| 2596 | tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0); |
| 2597 | } |
| 2598 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2599 | } |
| 2600 | else { |
| 2601 | t->srv_state = SV_STHEADERS; |
| 2602 | if (t->srv) |
| 2603 | t->srv->cum_sess++; |
| 2604 | rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2605 | t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE; |
| 2606 | /* reset hdr_idx which was already initialized by the request. |
| 2607 | * right now, the http parser does it. |
| 2608 | * hdr_idx_init(&t->txn.hdr_idx); |
| 2609 | */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2610 | } |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2611 | tv_eternity(&req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2612 | return 1; |
| 2613 | } |
| 2614 | } |
| 2615 | else if (s == SV_STHEADERS) { /* receiving server headers */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2616 | /* |
| 2617 | * Now parse the partial (or complete) lines. |
| 2618 | * We will check the response syntax, and also join multi-line |
| 2619 | * headers. An index of all the lines will be elaborated while |
| 2620 | * parsing. |
| 2621 | * |
| 2622 | * For the parsing, we use a 28 states FSM. |
| 2623 | * |
| 2624 | * Here is the information we currently have : |
| 2625 | * rep->data + req->som = beginning of response |
| 2626 | * rep->data + req->eoh = end of processed headers / start of current one |
| 2627 | * rep->data + req->eol = end of current header or line (LF or CRLF) |
| 2628 | * rep->lr = first non-visited byte |
| 2629 | * rep->r = end of data |
| 2630 | */ |
| 2631 | |
| 2632 | int cur_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2633 | struct http_msg *msg = &txn->rsp; |
| 2634 | struct proxy *cur_proxy; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2635 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2636 | if (likely(rep->lr < rep->r)) |
| 2637 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2638 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2639 | /* 1: we might have to print this header in debug mode */ |
| 2640 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2641 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
| 2642 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
| 2643 | char *eol, *sol; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2644 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2645 | sol = rep->data + msg->som; |
| 2646 | eol = sol + msg->sl.rq.l; |
| 2647 | debug_hdr("srvrep", t, sol, eol); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2648 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2649 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2650 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2651 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2652 | while (cur_idx) { |
| 2653 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2654 | debug_hdr("srvhdr", t, sol, eol); |
| 2655 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2656 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 2657 | } |
| 2658 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2659 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2660 | |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2661 | 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] | 2662 | /* fd in DIR_RD was disabled, perhaps because of a previous buffer |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2663 | * full. We cannot loop here since stream_sock_read will disable it only if |
| 2664 | * rep->l == rlim-data |
| 2665 | */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2666 | if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2667 | tv_eternity(&rep->rex); |
| 2668 | } |
| 2669 | |
| 2670 | |
| 2671 | /* |
| 2672 | * Now we quickly check if we have found a full valid response. |
| 2673 | * If not so, we check the FD and buffer states before leaving. |
| 2674 | * A full response is indicated by the fact that we have seen |
| 2675 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 2676 | * responses are checked first. |
| 2677 | * |
| 2678 | * Depending on whether the client is still there or not, we |
| 2679 | * may send an error response back or not. Note that normally |
| 2680 | * we should only check for HTTP status there, and check I/O |
| 2681 | * errors somewhere else. |
| 2682 | */ |
| 2683 | |
| 2684 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
| 2685 | |
| 2686 | /* Invalid response, or read error or write error */ |
| 2687 | if (unlikely((msg->msg_state == HTTP_MSG_ERROR) || |
| 2688 | (req->flags & BF_WRITE_ERROR) || |
| 2689 | (rep->flags & BF_READ_ERROR))) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2690 | buffer_shutr(rep); |
| 2691 | buffer_shutw(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2692 | fd_delete(t->srv_fd); |
| 2693 | if (t->srv) { |
| 2694 | t->srv->cur_sess--; |
| 2695 | t->srv->failed_resp++; |
| 2696 | } |
| 2697 | t->be->failed_resp++; |
| 2698 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2699 | txn->status = 502; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2700 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 2701 | if (!(t->flags & SN_ERR_MASK)) |
| 2702 | t->flags |= SN_ERR_SRVCL; |
| 2703 | if (!(t->flags & SN_FINST_MASK)) |
| 2704 | t->flags |= SN_FINST_H; |
| 2705 | /* We used to have a free connection slot. Since we'll never use it, |
| 2706 | * we have to inform the server that it may be used by another session. |
| 2707 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2708 | if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 2709 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2710 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2711 | return 1; |
| 2712 | } |
| 2713 | |
| 2714 | /* end of client write or end of server read. |
| 2715 | * since we are in header mode, if there's no space left for headers, we |
| 2716 | * won't be able to free more later, so the session will never terminate. |
| 2717 | */ |
| 2718 | else if (unlikely(rep->flags & BF_READ_NULL || |
| 2719 | c == CL_STSHUTW || c == CL_STCLOSE || |
| 2720 | rep->l >= rep->rlim - rep->data)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2721 | EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2722 | buffer_shutr(rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2723 | t->srv_state = SV_STSHUTR; |
| 2724 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 2725 | return 1; |
| 2726 | } |
| 2727 | |
| 2728 | /* read timeout : return a 504 to the client. |
| 2729 | */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2730 | else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_RD) && |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2731 | tv_isle(&rep->rex, &now))) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2732 | buffer_shutr(rep); |
| 2733 | buffer_shutw(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2734 | fd_delete(t->srv_fd); |
| 2735 | if (t->srv) { |
| 2736 | t->srv->cur_sess--; |
| 2737 | t->srv->failed_resp++; |
| 2738 | } |
| 2739 | t->be->failed_resp++; |
| 2740 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2741 | txn->status = 504; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2742 | client_return(t, error_message(t, HTTP_ERR_504)); |
| 2743 | if (!(t->flags & SN_ERR_MASK)) |
| 2744 | t->flags |= SN_ERR_SRVTO; |
| 2745 | if (!(t->flags & SN_FINST_MASK)) |
| 2746 | t->flags |= SN_FINST_H; |
| 2747 | /* We used to have a free connection slot. Since we'll never use it, |
| 2748 | * we have to inform the server that it may be used by another session. |
| 2749 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2750 | if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 2751 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2752 | return 1; |
| 2753 | } |
| 2754 | |
| 2755 | /* last client read and buffer empty */ |
| 2756 | /* FIXME!!! here, we don't want to switch to SHUTW if the |
| 2757 | * client shuts read too early, because we may still have |
| 2758 | * some work to do on the headers. |
| 2759 | * The side-effect is that if the client completely closes its |
| 2760 | * connection during SV_STHEADER, the connection to the server |
| 2761 | * is kept until a response comes back or the timeout is reached. |
| 2762 | */ |
| 2763 | else if (unlikely((/*c == CL_STSHUTR ||*/ c == CL_STCLOSE) && |
| 2764 | (req->l == 0))) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2765 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2766 | buffer_shutw(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2767 | |
| 2768 | /* We must ensure that the read part is still |
| 2769 | * alive when switching to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2770 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2771 | tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2772 | |
| 2773 | shutdown(t->srv_fd, SHUT_WR); |
| 2774 | t->srv_state = SV_STSHUTW; |
| 2775 | return 1; |
| 2776 | } |
| 2777 | |
| 2778 | /* write timeout */ |
| 2779 | /* FIXME!!! here, we don't want to switch to SHUTW if the |
| 2780 | * client shuts read too early, because we may still have |
| 2781 | * some work to do on the headers. |
| 2782 | */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2783 | else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_WR) && |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2784 | tv_isle(&req->wex, &now))) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2785 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2786 | buffer_shutw(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2787 | shutdown(t->srv_fd, SHUT_WR); |
| 2788 | /* We must ensure that the read part is still alive |
| 2789 | * when switching to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2790 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2791 | tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2792 | |
| 2793 | t->srv_state = SV_STSHUTW; |
| 2794 | if (!(t->flags & SN_ERR_MASK)) |
| 2795 | t->flags |= SN_ERR_SRVTO; |
| 2796 | if (!(t->flags & SN_FINST_MASK)) |
| 2797 | t->flags |= SN_FINST_H; |
| 2798 | return 1; |
| 2799 | } |
| 2800 | |
| 2801 | /* |
| 2802 | * And now the non-error cases. |
| 2803 | */ |
| 2804 | |
| 2805 | /* Data remaining in the request buffer. |
| 2806 | * This happens during the first pass here, and during |
| 2807 | * long posts. |
| 2808 | */ |
| 2809 | else if (likely(req->l)) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2810 | if (EV_FD_COND_S(t->srv_fd, DIR_WR)) { |
| 2811 | /* restart writing */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2812 | if (tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2813 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 2814 | * we refresh it. */ |
| 2815 | rep->rex = req->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2816 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2817 | else |
| 2818 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2819 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2820 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2821 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2822 | /* nothing left in the request buffer */ |
| 2823 | else { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2824 | if (EV_FD_COND_C(t->srv_fd, DIR_WR)) { |
| 2825 | /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2826 | tv_eternity(&req->wex); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2827 | } |
| 2828 | } |
| 2829 | |
| 2830 | return t->srv_state != SV_STHEADERS; |
| 2831 | } |
| 2832 | |
| 2833 | |
| 2834 | /***************************************************************** |
| 2835 | * More interesting part now : we know that we have a complete * |
| 2836 | * response which at least looks like HTTP. We have an indicator * |
| 2837 | * of each header's length, so we can parse them quickly. * |
| 2838 | ****************************************************************/ |
| 2839 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2840 | /* ensure we keep this pointer to the beginning of the message */ |
| 2841 | msg->sol = rep->data + msg->som; |
| 2842 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2843 | /* |
| 2844 | * 1: get the status code and check for cacheability. |
| 2845 | */ |
| 2846 | |
| 2847 | t->logs.logwait &= ~LW_RESP; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2848 | 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] | 2849 | |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2850 | switch (txn->status) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2851 | case 200: |
| 2852 | case 203: |
| 2853 | case 206: |
| 2854 | case 300: |
| 2855 | case 301: |
| 2856 | case 410: |
| 2857 | /* RFC2616 @13.4: |
| 2858 | * "A response received with a status code of |
| 2859 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 2860 | * by a cache (...) unless a cache-control |
| 2861 | * directive prohibits caching." |
| 2862 | * |
| 2863 | * RFC2616 @9.5: POST method : |
| 2864 | * "Responses to this method are not cacheable, |
| 2865 | * unless the response includes appropriate |
| 2866 | * Cache-Control or Expires header fields." |
| 2867 | */ |
| 2868 | if (likely(txn->meth != HTTP_METH_POST) && |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame^] | 2869 | (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2870 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2871 | break; |
| 2872 | default: |
| 2873 | break; |
| 2874 | } |
| 2875 | |
| 2876 | /* |
| 2877 | * 2: we may need to capture headers |
| 2878 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2879 | if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2880 | capture_headers(rep->data + msg->som, &txn->hdr_idx, |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2881 | txn->rsp.cap, t->fe->rsp_cap); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2882 | |
| 2883 | /* |
| 2884 | * 3: we will have to evaluate the filters. |
| 2885 | * As opposed to version 1.2, now they will be evaluated in the |
| 2886 | * filters order and not in the header order. This means that |
| 2887 | * each filter has to be validated among all headers. |
| 2888 | * |
| 2889 | * Filters are tried with ->be first, then with ->fe if it is |
| 2890 | * different from ->be. |
| 2891 | */ |
| 2892 | |
| 2893 | t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */ |
| 2894 | |
| 2895 | cur_proxy = t->be; |
| 2896 | while (1) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2897 | struct proxy *rule_set = cur_proxy; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2898 | |
| 2899 | /* try headers filters */ |
| 2900 | if (rule_set->rsp_exp != NULL) { |
| 2901 | if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) { |
| 2902 | return_bad_resp: |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2903 | if (t->srv) { |
| 2904 | t->srv->cur_sess--; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2905 | t->srv->failed_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2906 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2907 | cur_proxy->failed_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2908 | return_srv_prx_502: |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 2909 | buffer_shutr(rep); |
| 2910 | buffer_shutw(req); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2911 | fd_delete(t->srv_fd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2912 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2913 | txn->status = 502; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2914 | client_return(t, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2915 | if (!(t->flags & SN_ERR_MASK)) |
| 2916 | t->flags |= SN_ERR_PRXCOND; |
| 2917 | if (!(t->flags & SN_FINST_MASK)) |
| 2918 | t->flags |= SN_FINST_H; |
| 2919 | /* We used to have a free connection slot. Since we'll never use it, |
| 2920 | * we have to inform the server that it may be used by another session. |
| 2921 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2922 | if (t->srv && may_dequeue_tasks(t->srv, cur_proxy)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 2923 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2924 | return 1; |
| 2925 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2926 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2927 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2928 | /* has the response been denied ? */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2929 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2930 | if (t->srv) { |
| 2931 | t->srv->cur_sess--; |
| 2932 | t->srv->failed_secu++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2933 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2934 | cur_proxy->denied_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2935 | goto return_srv_prx_502; |
| 2936 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2937 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2938 | /* We might have to check for "Connection:" */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2939 | if (((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2940 | !(t->flags & SN_CONN_CLOSED)) { |
| 2941 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2942 | int cur_idx, old_idx, delta, val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2943 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2944 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2945 | cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 2946 | old_idx = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2947 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2948 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 2949 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 2950 | cur_ptr = cur_next; |
| 2951 | cur_end = cur_ptr + cur_hdr->len; |
| 2952 | cur_next = cur_end + cur_hdr->cr + 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2953 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2954 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 2955 | if (val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2956 | /* 3 possibilities : |
| 2957 | * - we have already set Connection: close, |
| 2958 | * so we remove this line. |
| 2959 | * - we have not yet set Connection: close, |
| 2960 | * but this line indicates close. We leave |
| 2961 | * it untouched and set the flag. |
| 2962 | * - we have not yet set Connection: close, |
| 2963 | * and this line indicates non-close. We |
| 2964 | * replace it. |
| 2965 | */ |
| 2966 | if (t->flags & SN_CONN_CLOSED) { |
| 2967 | delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0); |
| 2968 | txn->rsp.eoh += delta; |
| 2969 | cur_next += delta; |
| 2970 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 2971 | txn->hdr_idx.used--; |
| 2972 | cur_hdr->len = 0; |
| 2973 | } else { |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2974 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 2975 | delta = buffer_replace2(rep, cur_ptr + val, cur_end, |
| 2976 | "close", 5); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2977 | cur_next += delta; |
| 2978 | cur_hdr->len += delta; |
| 2979 | txn->rsp.eoh += delta; |
| 2980 | } |
| 2981 | t->flags |= SN_CONN_CLOSED; |
| 2982 | } |
| 2983 | } |
| 2984 | old_idx = cur_idx; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2985 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2986 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2987 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2988 | /* add response headers from the rule sets in the same order */ |
| 2989 | for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) { |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2990 | if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, |
| 2991 | rule_set->rsp_add[cur_idx])) < 0) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2992 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2993 | } |
| 2994 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2995 | /* check whether we're already working on the frontend */ |
| 2996 | if (cur_proxy == t->fe) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2997 | break; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2998 | cur_proxy = t->fe; |
| 2999 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3000 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3001 | /* |
| 3002 | * 4: check for server cookie. |
| 3003 | */ |
| 3004 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3005 | |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame^] | 3006 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3007 | /* |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame^] | 3008 | * 5: check for cache-control or pragma headers. |
| 3009 | */ |
| 3010 | check_response_for_cacheability(t, rep); |
| 3011 | |
| 3012 | |
| 3013 | /* |
| 3014 | * 6: add server cookie in the response if needed |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3015 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3016 | if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) && |
| 3017 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3018 | int len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3019 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3020 | /* the server is known, it's not the one the client requested, we have to |
| 3021 | * insert a set-cookie here, except if we want to insert only on POST |
| 3022 | * requests and this one isn't. Note that servers which don't have cookies |
| 3023 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3024 | */ |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3025 | len = sprintf(trash, "Set-Cookie: %s=%s; path=/", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3026 | t->be->cookie_name, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3027 | 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] | 3028 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3029 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3030 | trash, len)) < 0) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3031 | goto return_bad_resp; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3032 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3033 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3034 | /* Here, we will tell an eventual cache on the client side that we don't |
| 3035 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 3036 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 3037 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 3038 | */ |
Krzysztof Oledzki | 9198ab5 | 2007-10-11 18:56:27 +0200 | [diff] [blame^] | 3039 | if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) { |
| 3040 | |
| 3041 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3042 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3043 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3044 | "Cache-control: private", 22)) < 0) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3045 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3046 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3047 | } |
| 3048 | |
| 3049 | |
| 3050 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3051 | * 7: check if result will be cacheable with a cookie. |
| 3052 | * We'll block the response if security checks have caught |
| 3053 | * nasty things such as a cacheable cookie. |
| 3054 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3055 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) == |
| 3056 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3057 | (t->be->options & PR_O_CHK_CACHE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3058 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3059 | /* we're in presence of a cacheable response containing |
| 3060 | * a set-cookie header. We'll block it as requested by |
| 3061 | * the 'checkcache' option, and send an alert. |
| 3062 | */ |
| 3063 | if (t->srv) { |
| 3064 | t->srv->cur_sess--; |
| 3065 | t->srv->failed_secu++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3066 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3067 | t->be->denied_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3068 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3069 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3070 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3071 | send_log(t->be, LOG_ALERT, |
| 3072 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3073 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3074 | goto return_srv_prx_502; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3075 | } |
| 3076 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3077 | /* |
| 3078 | * 8: add "Connection: close" if needed and not yet set. |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 3079 | * 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] | 3080 | */ |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 3081 | if (!(t->flags & SN_CONN_CLOSED) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3082 | ((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE)) { |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 3083 | if ((unlikely(msg->sl.st.v_l != 8) || |
| 3084 | unlikely(req->data[msg->som + 7] != '0')) && |
| 3085 | unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3086 | "Connection: close", 17)) < 0) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3087 | goto return_bad_resp; |
| 3088 | t->flags |= SN_CONN_CLOSED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3089 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3090 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3091 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3092 | /************************************************************* |
| 3093 | * OK, that's finished for the headers. We have done what we * |
| 3094 | * could. Let's switch to the DATA state. * |
| 3095 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3096 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3097 | t->srv_state = SV_STDATA; |
| 3098 | rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 3099 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3100 | |
| 3101 | /* client connection already closed or option 'forceclose' required : |
| 3102 | * we close the server's outgoing connection right now. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3103 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3104 | if ((req->l == 0) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3105 | (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] | 3106 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3107 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3108 | |
| 3109 | /* We must ensure that the read part is still alive when switching |
| 3110 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3111 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3112 | tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3113 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3114 | shutdown(t->srv_fd, SHUT_WR); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3115 | t->srv_state = SV_STSHUTW; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3116 | } |
| 3117 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3118 | #ifdef CONFIG_HAP_TCPSPLICE |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3119 | if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3120 | /* TCP splicing supported by both FE and BE */ |
| 3121 | tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3122 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3123 | #endif |
| 3124 | /* if the user wants to log as soon as possible, without counting |
| 3125 | bytes from the server, then this is the right moment. */ |
| 3126 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 3127 | 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] | 3128 | t->logs.bytes_in = txn->rsp.eoh; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 3129 | if (t->fe->to_log & LW_REQ) |
| 3130 | http_sess_log(t); |
| 3131 | else |
| 3132 | tcp_sess_log(t); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3133 | } |
| 3134 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3135 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 3136 | * otherwise we would not let the client side wake up. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3137 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3138 | |
| 3139 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3140 | } |
| 3141 | else if (s == SV_STDATA) { |
| 3142 | /* read or write error */ |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3143 | if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) { |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3144 | buffer_shutr(rep); |
| 3145 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3146 | fd_delete(t->srv_fd); |
| 3147 | if (t->srv) { |
| 3148 | t->srv->cur_sess--; |
| 3149 | t->srv->failed_resp++; |
| 3150 | } |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 3151 | t->be->failed_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3152 | t->srv_state = SV_STCLOSE; |
| 3153 | if (!(t->flags & SN_ERR_MASK)) |
| 3154 | t->flags |= SN_ERR_SRVCL; |
| 3155 | if (!(t->flags & SN_FINST_MASK)) |
| 3156 | t->flags |= SN_FINST_D; |
| 3157 | /* We used to have a free connection slot. Since we'll never use it, |
| 3158 | * we have to inform the server that it may be used by another session. |
| 3159 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3160 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3161 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3162 | |
| 3163 | return 1; |
| 3164 | } |
| 3165 | /* last read, or end of client write */ |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3166 | 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] | 3167 | EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3168 | buffer_shutr(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3169 | t->srv_state = SV_STSHUTR; |
| 3170 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 3171 | return 1; |
| 3172 | } |
| 3173 | /* end of client read and no more data to send */ |
| 3174 | else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3175 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3176 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3177 | shutdown(t->srv_fd, SHUT_WR); |
| 3178 | /* We must ensure that the read part is still alive when switching |
| 3179 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3180 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3181 | tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3182 | |
| 3183 | t->srv_state = SV_STSHUTW; |
| 3184 | return 1; |
| 3185 | } |
| 3186 | /* read timeout */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3187 | else if (tv_isle(&rep->rex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3188 | EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3189 | buffer_shutr(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3190 | t->srv_state = SV_STSHUTR; |
| 3191 | if (!(t->flags & SN_ERR_MASK)) |
| 3192 | t->flags |= SN_ERR_SRVTO; |
| 3193 | if (!(t->flags & SN_FINST_MASK)) |
| 3194 | t->flags |= SN_FINST_D; |
| 3195 | return 1; |
| 3196 | } |
| 3197 | /* write timeout */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3198 | else if (tv_isle(&req->wex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3199 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3200 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3201 | shutdown(t->srv_fd, SHUT_WR); |
| 3202 | /* We must ensure that the read part is still alive when switching |
| 3203 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3204 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3205 | tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3206 | t->srv_state = SV_STSHUTW; |
| 3207 | if (!(t->flags & SN_ERR_MASK)) |
| 3208 | t->flags |= SN_ERR_SRVTO; |
| 3209 | if (!(t->flags & SN_FINST_MASK)) |
| 3210 | t->flags |= SN_FINST_D; |
| 3211 | return 1; |
| 3212 | } |
| 3213 | |
| 3214 | /* recompute request time-outs */ |
| 3215 | if (req->l == 0) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3216 | if (EV_FD_COND_C(t->srv_fd, DIR_WR)) { |
| 3217 | /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3218 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3219 | } |
| 3220 | } |
| 3221 | else { /* buffer not empty, there are still data to be transferred */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3222 | if (EV_FD_COND_S(t->srv_fd, DIR_WR)) { |
| 3223 | /* restart writing */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3224 | if (tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3225 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 3226 | * we refresh it. */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3227 | rep->rex = req->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3228 | } |
| 3229 | else |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3230 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3231 | } |
| 3232 | } |
| 3233 | |
| 3234 | /* recompute response time-outs */ |
| 3235 | if (rep->l == BUFSIZE) { /* no room to read more data */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3236 | if (EV_FD_COND_C(t->srv_fd, DIR_RD)) { |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3237 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3238 | } |
| 3239 | } |
| 3240 | else { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3241 | if (EV_FD_COND_S(t->srv_fd, DIR_RD)) { |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3242 | if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3243 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3244 | } |
| 3245 | } |
| 3246 | |
| 3247 | return 0; /* other cases change nothing */ |
| 3248 | } |
| 3249 | else if (s == SV_STSHUTR) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3250 | if (req->flags & BF_WRITE_ERROR) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3251 | //EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3252 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3253 | fd_delete(t->srv_fd); |
| 3254 | if (t->srv) { |
| 3255 | t->srv->cur_sess--; |
| 3256 | t->srv->failed_resp++; |
| 3257 | } |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 3258 | t->be->failed_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3259 | //close(t->srv_fd); |
| 3260 | t->srv_state = SV_STCLOSE; |
| 3261 | if (!(t->flags & SN_ERR_MASK)) |
| 3262 | t->flags |= SN_ERR_SRVCL; |
| 3263 | if (!(t->flags & SN_FINST_MASK)) |
| 3264 | t->flags |= SN_FINST_D; |
| 3265 | /* We used to have a free connection slot. Since we'll never use it, |
| 3266 | * we have to inform the server that it may be used by another session. |
| 3267 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3268 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3269 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3270 | |
| 3271 | return 1; |
| 3272 | } |
| 3273 | else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3274 | //EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3275 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3276 | fd_delete(t->srv_fd); |
| 3277 | if (t->srv) |
| 3278 | t->srv->cur_sess--; |
| 3279 | //close(t->srv_fd); |
| 3280 | t->srv_state = SV_STCLOSE; |
| 3281 | /* We used to have a free connection slot. Since we'll never use it, |
| 3282 | * we have to inform the server that it may be used by another session. |
| 3283 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3284 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3285 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3286 | |
| 3287 | return 1; |
| 3288 | } |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3289 | else if (tv_isle(&req->wex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3290 | //EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3291 | buffer_shutw(req); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3292 | fd_delete(t->srv_fd); |
| 3293 | if (t->srv) |
| 3294 | t->srv->cur_sess--; |
| 3295 | //close(t->srv_fd); |
| 3296 | t->srv_state = SV_STCLOSE; |
| 3297 | if (!(t->flags & SN_ERR_MASK)) |
| 3298 | t->flags |= SN_ERR_SRVTO; |
| 3299 | if (!(t->flags & SN_FINST_MASK)) |
| 3300 | t->flags |= SN_FINST_D; |
| 3301 | /* We used to have a free connection slot. Since we'll never use it, |
| 3302 | * we have to inform the server that it may be used by another session. |
| 3303 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3304 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3305 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3306 | |
| 3307 | return 1; |
| 3308 | } |
| 3309 | else if (req->l == 0) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3310 | if (EV_FD_COND_C(t->srv_fd, DIR_WR)) { |
| 3311 | /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3312 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3313 | } |
| 3314 | } |
| 3315 | else { /* buffer not empty */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3316 | if (EV_FD_COND_S(t->srv_fd, DIR_WR)) { |
| 3317 | /* restart writing */ |
Willy Tarreau | 33014d0 | 2007-06-03 15:25:37 +0200 | [diff] [blame] | 3318 | if (!tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3319 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3320 | } |
| 3321 | } |
| 3322 | return 0; |
| 3323 | } |
| 3324 | else if (s == SV_STSHUTW) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3325 | if (rep->flags & BF_READ_ERROR) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3326 | //EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3327 | buffer_shutr(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3328 | fd_delete(t->srv_fd); |
| 3329 | if (t->srv) { |
| 3330 | t->srv->cur_sess--; |
| 3331 | t->srv->failed_resp++; |
| 3332 | } |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 3333 | t->be->failed_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3334 | //close(t->srv_fd); |
| 3335 | t->srv_state = SV_STCLOSE; |
| 3336 | if (!(t->flags & SN_ERR_MASK)) |
| 3337 | t->flags |= SN_ERR_SRVCL; |
| 3338 | if (!(t->flags & SN_FINST_MASK)) |
| 3339 | t->flags |= SN_FINST_D; |
| 3340 | /* We used to have a free connection slot. Since we'll never use it, |
| 3341 | * we have to inform the server that it may be used by another session. |
| 3342 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3343 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3344 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3345 | |
| 3346 | return 1; |
| 3347 | } |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3348 | 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] | 3349 | //EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3350 | buffer_shutr(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3351 | fd_delete(t->srv_fd); |
| 3352 | if (t->srv) |
| 3353 | t->srv->cur_sess--; |
| 3354 | //close(t->srv_fd); |
| 3355 | t->srv_state = SV_STCLOSE; |
| 3356 | /* We used to have a free connection slot. Since we'll never use it, |
| 3357 | * we have to inform the server that it may be used by another session. |
| 3358 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3359 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3360 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3361 | |
| 3362 | return 1; |
| 3363 | } |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3364 | else if (tv_isle(&rep->rex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3365 | //EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | fa64558 | 2007-06-03 15:59:52 +0200 | [diff] [blame] | 3366 | buffer_shutr(rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3367 | fd_delete(t->srv_fd); |
| 3368 | if (t->srv) |
| 3369 | t->srv->cur_sess--; |
| 3370 | //close(t->srv_fd); |
| 3371 | t->srv_state = SV_STCLOSE; |
| 3372 | if (!(t->flags & SN_ERR_MASK)) |
| 3373 | t->flags |= SN_ERR_SRVTO; |
| 3374 | if (!(t->flags & SN_FINST_MASK)) |
| 3375 | t->flags |= SN_FINST_D; |
| 3376 | /* We used to have a free connection slot. Since we'll never use it, |
| 3377 | * we have to inform the server that it may be used by another session. |
| 3378 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3379 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3380 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3381 | |
| 3382 | return 1; |
| 3383 | } |
| 3384 | else if (rep->l == BUFSIZE) { /* no room to read more data */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3385 | if (EV_FD_COND_C(t->srv_fd, DIR_RD)) { |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3386 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3387 | } |
| 3388 | } |
| 3389 | else { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3390 | if (EV_FD_COND_S(t->srv_fd, DIR_RD)) { |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3391 | if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3392 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3393 | } |
| 3394 | } |
| 3395 | return 0; |
| 3396 | } |
| 3397 | else { /* SV_STCLOSE : nothing to do */ |
| 3398 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 3399 | int len; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3400 | len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3401 | 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] | 3402 | write(1, trash, len); |
| 3403 | } |
| 3404 | return 0; |
| 3405 | } |
| 3406 | return 0; |
| 3407 | } |
| 3408 | |
| 3409 | |
| 3410 | /* |
| 3411 | * Produces data for the session <s> depending on its source. Expects to be |
| 3412 | * called with s->cli_state == CL_STSHUTR. Right now, only statistics can be |
| 3413 | * produced. It stops by itself by unsetting the SN_SELF_GEN flag from the |
| 3414 | * session, which it uses to keep on being called when there is free space in |
| 3415 | * the buffer, of simply by letting an empty buffer upon return. It returns 1 |
| 3416 | * if it changes the session state from CL_STSHUTR, otherwise 0. |
| 3417 | */ |
| 3418 | int produce_content(struct session *s) |
| 3419 | { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3420 | if (s->data_source == DATA_SRC_NONE) { |
| 3421 | s->flags &= ~SN_SELF_GEN; |
| 3422 | return 1; |
| 3423 | } |
| 3424 | else if (s->data_source == DATA_SRC_STATS) { |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3425 | /* dump server statistics */ |
| 3426 | return produce_content_stats(s); |
| 3427 | } |
| 3428 | else { |
| 3429 | /* unknown data source */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 3430 | s->txn.status = 500; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3431 | client_retnclose(s, error_message(s, HTTP_ERR_500)); |
| 3432 | if (!(s->flags & SN_ERR_MASK)) |
| 3433 | s->flags |= SN_ERR_PRXCOND; |
| 3434 | if (!(s->flags & SN_FINST_MASK)) |
| 3435 | s->flags |= SN_FINST_R; |
| 3436 | s->flags &= ~SN_SELF_GEN; |
| 3437 | return 1; |
| 3438 | } |
| 3439 | } |
| 3440 | |
| 3441 | |
| 3442 | /* |
| 3443 | * Produces statistics data for the session <s>. Expects to be called with |
| 3444 | * s->cli_state == CL_STSHUTR. It stops by itself by unsetting the SN_SELF_GEN |
| 3445 | * flag from the session, which it uses to keep on being called when there is |
| 3446 | * free space in the buffer, of simply by letting an empty buffer upon return. |
| 3447 | * It returns 1 if it changes the session state from CL_STSHUTR, otherwise 0. |
| 3448 | */ |
| 3449 | int produce_content_stats(struct session *s) |
| 3450 | { |
| 3451 | struct buffer *rep = s->rep; |
| 3452 | struct proxy *px; |
| 3453 | struct chunk msg; |
| 3454 | unsigned int up; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3455 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3456 | msg.len = 0; |
| 3457 | msg.str = trash; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3458 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3459 | switch (s->data_state) { |
| 3460 | case DATA_ST_INIT: |
| 3461 | /* the function had not been called yet */ |
| 3462 | s->flags |= SN_SELF_GEN; // more data will follow |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3463 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3464 | chunk_printf(&msg, sizeof(trash), |
| 3465 | "HTTP/1.0 200 OK\r\n" |
| 3466 | "Cache-Control: no-cache\r\n" |
| 3467 | "Connection: close\r\n" |
Willy Tarreau | bbd4212 | 2007-07-25 07:26:38 +0200 | [diff] [blame] | 3468 | "Content-Type: text/html\r\n"); |
| 3469 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 3470 | if (s->be->uri_auth->refresh > 0 && !(s->flags & SN_STAT_NORFRSH)) |
Willy Tarreau | bbd4212 | 2007-07-25 07:26:38 +0200 | [diff] [blame] | 3471 | chunk_printf(&msg, sizeof(trash), "Refresh: %d\r\n", |
| 3472 | s->be->uri_auth->refresh); |
| 3473 | |
| 3474 | chunk_printf(&msg, sizeof(trash), "\r\n"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3475 | |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 3476 | s->txn.status = 200; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3477 | client_retnclose(s, &msg); // send the start of the response. |
| 3478 | msg.len = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3479 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3480 | if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is |
| 3481 | s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy |
| 3482 | if (!(s->flags & SN_FINST_MASK)) |
| 3483 | s->flags |= SN_FINST_R; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3484 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 3485 | if (s->txn.meth == HTTP_METH_HEAD) { |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 3486 | /* that's all we return in case of HEAD request */ |
| 3487 | s->data_state = DATA_ST_FIN; |
| 3488 | s->flags &= ~SN_SELF_GEN; |
| 3489 | return 1; |
| 3490 | } |
| 3491 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3492 | s->data_state = DATA_ST_HEAD; /* let's start producing data */ |
| 3493 | /* fall through */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3494 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3495 | case DATA_ST_HEAD: |
| 3496 | /* WARNING! This must fit in the first buffer !!! */ |
| 3497 | chunk_printf(&msg, sizeof(trash), |
| 3498 | "<html><head><title>Statistics Report for " PRODUCT_NAME "</title>\n" |
| 3499 | "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n" |
| 3500 | "<style type=\"text/css\"><!--\n" |
| 3501 | "body {" |
| 3502 | " font-family: helvetica, arial;" |
| 3503 | " font-size: 12px;" |
| 3504 | " font-weight: normal;" |
| 3505 | " color: black;" |
| 3506 | " background: white;" |
| 3507 | "}\n" |
| 3508 | "th,td {" |
| 3509 | " font-size: 0.8em;" |
| 3510 | " align: center;" |
Willy Tarreau | f2b74c2 | 2007-03-25 22:44:08 +0200 | [diff] [blame] | 3511 | "}\n" |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3512 | "h1 {" |
| 3513 | " font-size: xx-large;" |
| 3514 | " margin-bottom: 0.5em;" |
| 3515 | "}\n" |
| 3516 | "h2 {" |
| 3517 | " font-family: helvetica, arial;" |
| 3518 | " font-size: x-large;" |
| 3519 | " font-weight: bold;" |
| 3520 | " font-style: italic;" |
| 3521 | " color: #6020a0;" |
| 3522 | " margin-top: 0em;" |
| 3523 | " margin-bottom: 0em;" |
| 3524 | "}\n" |
| 3525 | "h3 {" |
| 3526 | " font-family: helvetica, arial;" |
| 3527 | " font-size: 16px;" |
| 3528 | " font-weight: bold;" |
| 3529 | " color: #b00040;" |
| 3530 | " background: #e8e8d0;" |
| 3531 | " margin-top: 0em;" |
| 3532 | " margin-bottom: 0em;" |
| 3533 | "}\n" |
| 3534 | "li {" |
| 3535 | " margin-top: 0.25em;" |
| 3536 | " margin-right: 2em;" |
| 3537 | "}\n" |
| 3538 | ".hr {margin-top: 0.25em;" |
| 3539 | " border-color: black;" |
| 3540 | " border-bottom-style: solid;" |
| 3541 | "}\n" |
| 3542 | ".pxname {background: #b00040;color: #ffff40;font-weight: bold;}\n" |
| 3543 | ".titre {background: #20D0D0;color: #000000;font-weight: bold;}\n" |
| 3544 | ".total {background: #20D0D0;color: #ffff80;}\n" |
| 3545 | ".frontend {background: #e8e8d0;}\n" |
| 3546 | ".backend {background: #e8e8d0;}\n" |
| 3547 | ".active0 {background: #ff9090;}\n" |
| 3548 | ".active1 {background: #ffd020;}\n" |
| 3549 | ".active2 {background: #ffffa0;}\n" |
| 3550 | ".active3 {background: #c0ffc0;}\n" |
| 3551 | ".active4 {background: #e0e0e0;}\n" |
| 3552 | ".backup0 {background: #ff9090;}\n" |
| 3553 | ".backup1 {background: #ff80ff;}\n" |
| 3554 | ".backup2 {background: #c060ff;}\n" |
| 3555 | ".backup3 {background: #b0d0ff;}\n" |
| 3556 | ".backup4 {background: #e0e0e0;}\n" |
| 3557 | "table.tbl { border-collapse: collapse; border-style: none;}\n" |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 3558 | "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] | 3559 | "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n" |
| 3560 | "table.tbl th.empty { border-style: none; empty-cells: hide;}\n" |
| 3561 | "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n" |
| 3562 | "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n" |
| 3563 | "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] | 3564 | "-->\n" |
| 3565 | "</style></head>\n"); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3566 | |
| 3567 | if (buffer_write_chunk(rep, &msg) != 0) |
| 3568 | return 0; |
| 3569 | |
| 3570 | s->data_state = DATA_ST_INFO; |
| 3571 | /* fall through */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3572 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3573 | case DATA_ST_INFO: |
| 3574 | up = (now.tv_sec - start_date.tv_sec); |
| 3575 | |
| 3576 | /* WARNING! this has to fit the first packet too. |
| 3577 | * We are around 3.5 kB, add adding entries will |
| 3578 | * become tricky if we want to support 4kB buffers ! |
| 3579 | */ |
| 3580 | chunk_printf(&msg, sizeof(trash), |
| 3581 | "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">" |
| 3582 | PRODUCT_NAME "</a></h1>\n" |
| 3583 | "<h2>Statistics Report for pid %d</h2>\n" |
| 3584 | "<hr width=\"100%%\" class=\"hr\">\n" |
| 3585 | "<h3>> General process information</h3>\n" |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 3586 | "<table border=0 cols=4><tr><td align=\"left\" nowrap width=\"1%%\">\n" |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3587 | "<p><b>pid = </b> %d (nbproc = %d)<br>\n" |
| 3588 | "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n" |
| 3589 | "<b>system limits :</b> memmax = %s%s ; ulimit-n = %d<br>\n" |
| 3590 | "<b>maxsock = </b> %d<br>\n" |
| 3591 | "<b>maxconn = </b> %d (current conns = %d)<br>\n" |
| 3592 | "</td><td align=\"center\" nowrap>\n" |
Willy Tarreau | f2b74c2 | 2007-03-25 22:44:08 +0200 | [diff] [blame] | 3593 | "<table class=\"lgd\"><tr>\n" |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3594 | "<td class=\"active3\"> </td><td class=\"noborder\">active UP </td>" |
| 3595 | "<td class=\"backup3\"> </td><td class=\"noborder\">backup UP </td>" |
Willy Tarreau | f2b74c2 | 2007-03-25 22:44:08 +0200 | [diff] [blame] | 3596 | "</tr><tr>\n" |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3597 | "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>" |
| 3598 | "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>" |
Willy Tarreau | f2b74c2 | 2007-03-25 22:44:08 +0200 | [diff] [blame] | 3599 | "</tr><tr>\n" |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3600 | "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>" |
| 3601 | "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>" |
Willy Tarreau | f2b74c2 | 2007-03-25 22:44:08 +0200 | [diff] [blame] | 3602 | "</tr><tr>\n" |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3603 | "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN </td>" |
| 3604 | "<td class=\"active4\"></td><td class=\"noborder\">not checked </td>" |
| 3605 | "</tr></table>\n" |
| 3606 | "</td>" |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 3607 | "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">" |
| 3608 | "<b>Display option:</b><ul style=\"margin-top: 0.25em;\">" |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3609 | "", |
| 3610 | pid, pid, global.nbproc, |
| 3611 | up / 86400, (up % 86400) / 3600, |
| 3612 | (up % 3600) / 60, (up % 60), |
| 3613 | global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited", |
| 3614 | global.rlimit_memmax ? " MB" : "", |
| 3615 | global.rlimit_nofile, |
| 3616 | global.maxsock, |
| 3617 | global.maxconn, |
| 3618 | actconn |
| 3619 | ); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3620 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 3621 | if (s->flags & SN_STAT_HIDEDWN) |
| 3622 | chunk_printf(&msg, sizeof(trash), |
| 3623 | "<li><a href=\"%s%s%s\">Show all servers</a><br>\n", |
| 3624 | s->be->uri_auth->uri_prefix, |
| 3625 | "", |
| 3626 | (s->flags & SN_STAT_NORFRSH) ? ";norefresh" : ""); |
| 3627 | else |
| 3628 | chunk_printf(&msg, sizeof(trash), |
| 3629 | "<li><a href=\"%s%s%s\">Hide 'DOWN' servers</a><br>\n", |
| 3630 | s->be->uri_auth->uri_prefix, |
| 3631 | ";up", |
| 3632 | (s->flags & SN_STAT_NORFRSH) ? ";norefresh" : ""); |
| 3633 | |
| 3634 | if (s->be->uri_auth->refresh > 0) { |
| 3635 | if (s->flags & SN_STAT_NORFRSH) |
| 3636 | chunk_printf(&msg, sizeof(trash), |
| 3637 | "<li><a href=\"%s%s%s\">Enable refresh</a><br>\n", |
| 3638 | s->be->uri_auth->uri_prefix, |
| 3639 | (s->flags & SN_STAT_HIDEDWN) ? ";up" : "", |
| 3640 | ""); |
| 3641 | else |
| 3642 | chunk_printf(&msg, sizeof(trash), |
| 3643 | "<li><a href=\"%s%s%s\">Disable refresh</a><br>\n", |
| 3644 | s->be->uri_auth->uri_prefix, |
| 3645 | (s->flags & SN_STAT_HIDEDWN) ? ";up" : "", |
| 3646 | ";norefresh"); |
| 3647 | } |
| 3648 | |
| 3649 | chunk_printf(&msg, sizeof(trash), |
| 3650 | "<li><a href=\"%s%s%s\">Refresh now</a><br>\n", |
| 3651 | s->be->uri_auth->uri_prefix, |
| 3652 | (s->flags & SN_STAT_HIDEDWN) ? ";up" : "", |
| 3653 | (s->flags & SN_STAT_NORFRSH) ? ";norefresh" : ""); |
| 3654 | |
| 3655 | chunk_printf(&msg, sizeof(trash), |
| 3656 | "</td>" |
| 3657 | "<td align=\"left\" valign=\"top\" nowrap width=\"1%%\">" |
| 3658 | "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n" |
| 3659 | "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n" |
| 3660 | "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n" |
| 3661 | "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n" |
| 3662 | "</ul>" |
| 3663 | "</td>" |
| 3664 | "</tr></table>\n" |
| 3665 | "" |
| 3666 | ); |
| 3667 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3668 | if (buffer_write_chunk(rep, &msg) != 0) |
| 3669 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3670 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3671 | memset(&s->data_ctx, 0, sizeof(s->data_ctx)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3672 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3673 | s->data_ctx.stats.px = proxy; |
| 3674 | s->data_ctx.stats.px_st = DATA_ST_PX_INIT; |
| 3675 | s->data_state = DATA_ST_LIST; |
| 3676 | /* fall through */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3677 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3678 | case DATA_ST_LIST: |
| 3679 | /* dump proxies */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3680 | while (s->data_ctx.stats.px) { |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3681 | px = s->data_ctx.stats.px; |
| 3682 | /* skip the disabled proxies and non-networked ones */ |
| 3683 | if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE))) |
| 3684 | if (produce_content_stats_proxy(s, px) == 0) |
| 3685 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3686 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3687 | s->data_ctx.stats.px = px->next; |
| 3688 | s->data_ctx.stats.px_st = DATA_ST_PX_INIT; |
| 3689 | } |
| 3690 | /* here, we just have reached the last proxy */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3691 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3692 | s->data_state = DATA_ST_END; |
| 3693 | /* fall through */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3694 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3695 | case DATA_ST_END: |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 3696 | chunk_printf(&msg, sizeof(trash), "</body></html>\n"); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3697 | if (buffer_write_chunk(rep, &msg) != 0) |
| 3698 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3699 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3700 | s->data_state = DATA_ST_FIN; |
| 3701 | /* fall through */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3702 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3703 | case DATA_ST_FIN: |
| 3704 | s->flags &= ~SN_SELF_GEN; |
| 3705 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3706 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3707 | default: |
| 3708 | /* unknown state ! */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 3709 | s->txn.status = 500; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3710 | client_retnclose(s, error_message(s, HTTP_ERR_500)); |
| 3711 | if (!(s->flags & SN_ERR_MASK)) |
| 3712 | s->flags |= SN_ERR_PRXCOND; |
| 3713 | if (!(s->flags & SN_FINST_MASK)) |
| 3714 | s->flags |= SN_FINST_R; |
| 3715 | s->flags &= ~SN_SELF_GEN; |
| 3716 | return 1; |
| 3717 | } |
| 3718 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3719 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3720 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3721 | /* |
| 3722 | * Dumps statistics for a proxy. |
| 3723 | * Returns 0 if it had to stop dumping data because of lack of buffer space, |
| 3724 | * ot non-zero if everything completed. |
| 3725 | */ |
| 3726 | int produce_content_stats_proxy(struct session *s, struct proxy *px) |
| 3727 | { |
| 3728 | struct buffer *rep = s->rep; |
| 3729 | struct server *sv; |
| 3730 | struct chunk msg; |
| 3731 | |
| 3732 | msg.len = 0; |
| 3733 | msg.str = trash; |
| 3734 | |
| 3735 | switch (s->data_ctx.stats.px_st) { |
| 3736 | case DATA_ST_PX_INIT: |
| 3737 | /* we are on a new proxy */ |
| 3738 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3739 | if (s->be->uri_auth && s->be->uri_auth->scope) { |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3740 | /* we have a limited scope, we have to check the proxy name */ |
| 3741 | struct stat_scope *scope; |
| 3742 | int len; |
| 3743 | |
| 3744 | len = strlen(px->id); |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3745 | scope = s->be->uri_auth->scope; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3746 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3747 | while (scope) { |
| 3748 | /* match exact proxy name */ |
| 3749 | if (scope->px_len == len && !memcmp(px->id, scope->px_id, len)) |
| 3750 | break; |
| 3751 | |
| 3752 | /* match '.' which means 'self' proxy */ |
| 3753 | if (!strcmp(scope->px_id, ".") && px == s->fe) |
| 3754 | break; |
| 3755 | scope = scope->next; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3756 | } |
| 3757 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3758 | /* proxy name not found : don't dump anything */ |
| 3759 | if (scope == NULL) |
| 3760 | return 1; |
| 3761 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3762 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3763 | s->data_ctx.stats.px_st = DATA_ST_PX_TH; |
| 3764 | /* fall through */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3765 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3766 | case DATA_ST_PX_TH: |
| 3767 | /* print a new table */ |
| 3768 | chunk_printf(&msg, sizeof(trash), |
| 3769 | "<table cols=\"20\" class=\"tbl\" width=\"100%%\">\n" |
| 3770 | "<tr align=\"center\" class=\"titre\">" |
| 3771 | "<th colspan=2 class=\"pxname\">%s</th>" |
| 3772 | "<th colspan=18 class=\"empty\"></th>" |
| 3773 | "</tr>\n" |
| 3774 | "<tr align=\"center\" class=\"titre\">" |
| 3775 | "<th rowspan=2></th>" |
| 3776 | "<th colspan=2>Queue</th><th colspan=4>Sessions</th>" |
| 3777 | "<th colspan=2>Bytes</th><th colspan=2>Denied</th>" |
| 3778 | "<th colspan=3>Errors</th><th colspan=6>Server</th>" |
| 3779 | "</tr>\n" |
| 3780 | "<tr align=\"center\" class=\"titre\">" |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 3781 | "<th>Cur</th><th>Max</th><th>Cur</th><th>Max</th>" |
| 3782 | "<th>Limit</th><th>Cumul</th><th>In</th><th>Out</th>" |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3783 | "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>" |
| 3784 | "<th>Resp</th><th>Status</th><th>Weight</th><th>Act</th>" |
| 3785 | "<th>Bck</th><th>Check</th><th>Down</th></tr>\n" |
| 3786 | "", |
| 3787 | px->id); |
| 3788 | |
| 3789 | if (buffer_write_chunk(rep, &msg) != 0) |
| 3790 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3791 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3792 | s->data_ctx.stats.px_st = DATA_ST_PX_FE; |
| 3793 | /* fall through */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3794 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3795 | case DATA_ST_PX_FE: |
| 3796 | /* print the frontend */ |
| 3797 | if (px->cap & PR_CAP_FE) { |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3798 | chunk_printf(&msg, sizeof(trash), |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3799 | /* name, queue */ |
| 3800 | "<tr align=center class=\"frontend\"><td>Frontend</td><td colspan=2></td>" |
| 3801 | /* sessions : current, max, limit, cumul. */ |
| 3802 | "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>" |
| 3803 | /* bytes : in, out */ |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 3804 | "<td align=right>%lld</td><td align=right>%lld</td>" |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3805 | /* denied: req, resp */ |
| 3806 | "<td align=right>%d</td><td align=right>%d</td>" |
| 3807 | /* errors : request, connect, response */ |
| 3808 | "<td align=right>%d</td><td align=right></td><td align=right></td>" |
| 3809 | /* server status : reflect backend status */ |
| 3810 | "<td align=center>%s</td>" |
| 3811 | /* rest of server: nothing */ |
| 3812 | "<td align=center colspan=5></td></tr>" |
| 3813 | "", |
| 3814 | px->feconn, px->feconn_max, px->maxconn, px->cum_feconn, |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 3815 | px->bytes_in, px->bytes_out, |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3816 | px->denied_req, px->denied_resp, |
| 3817 | px->failed_req, |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3818 | px->state == PR_STRUN ? "OPEN" : |
| 3819 | px->state == PR_STIDLE ? "FULL" : "STOP"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3820 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3821 | if (buffer_write_chunk(rep, &msg) != 0) |
| 3822 | return 0; |
| 3823 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3824 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3825 | s->data_ctx.stats.sv = px->srv; /* may be NULL */ |
| 3826 | s->data_ctx.stats.px_st = DATA_ST_PX_SV; |
| 3827 | /* fall through */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3828 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3829 | case DATA_ST_PX_SV: |
| 3830 | /* stats.sv has been initialized above */ |
| 3831 | while (s->data_ctx.stats.sv != NULL) { |
| 3832 | static char *srv_hlt_st[5] = { "DOWN", "DN %d/%d ↑", "UP %d/%d ↓", "UP", "<i>no check</i>" }; |
| 3833 | 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] | 3834 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3835 | sv = s->data_ctx.stats.sv; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3836 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3837 | /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */ |
| 3838 | if (!(sv->state & SRV_CHECKED)) |
| 3839 | sv_state = 4; |
| 3840 | else if (sv->state & SRV_RUNNING) |
| 3841 | if (sv->health == sv->rise + sv->fall - 1) |
| 3842 | sv_state = 3; /* UP */ |
| 3843 | else |
| 3844 | sv_state = 2; /* going down */ |
| 3845 | else |
| 3846 | if (sv->health) |
| 3847 | sv_state = 1; /* going up */ |
| 3848 | else |
| 3849 | sv_state = 0; /* DOWN */ |
| 3850 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 3851 | if ((sv_state == 0) && (s->flags & SN_STAT_HIDEDWN)) { |
| 3852 | /* do not report servers which are DOWN */ |
| 3853 | s->data_ctx.stats.sv = sv->next; |
| 3854 | continue; |
| 3855 | } |
| 3856 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3857 | chunk_printf(&msg, sizeof(trash), |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3858 | /* name */ |
| 3859 | "<tr align=\"center\" class=\"%s%d\"><td>%s</td>" |
| 3860 | /* queue : current, max */ |
| 3861 | "<td align=right>%d</td><td align=right>%d</td>" |
| 3862 | /* sessions : current, max, limit, cumul */ |
| 3863 | "<td align=right>%d</td><td align=right>%d</td><td align=right>%s</td><td align=right>%d</td>" |
| 3864 | /* bytes : in, out */ |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 3865 | "<td align=right>%lld</td><td align=right>%lld</td>" |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3866 | /* denied: req, resp */ |
| 3867 | "<td align=right></td><td align=right>%d</td>" |
| 3868 | /* errors : request, connect, response */ |
| 3869 | "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n" |
| 3870 | "", |
Willy Tarreau | 368e96a | 2007-01-07 00:16:15 +0100 | [diff] [blame] | 3871 | (sv->state & SRV_BACKUP) ? "backup" : "active", |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3872 | sv_state, sv->id, |
| 3873 | sv->nbpend, sv->nbpend_max, |
| 3874 | 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] | 3875 | sv->bytes_in, sv->bytes_out, |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3876 | sv->failed_secu, |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3877 | sv->failed_conns, sv->failed_resp); |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3878 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3879 | /* status */ |
| 3880 | chunk_printf(&msg, sizeof(trash), "<td nowrap>"); |
| 3881 | chunk_printf(&msg, sizeof(trash), |
| 3882 | srv_hlt_st[sv_state], |
| 3883 | (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health), |
| 3884 | (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise)); |
| 3885 | |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3886 | chunk_printf(&msg, sizeof(trash), |
| 3887 | /* weight */ |
| 3888 | "</td><td>%d</td>" |
| 3889 | /* act, bck */ |
| 3890 | "<td>%s</td><td>%s</td>" |
| 3891 | "", |
Willy Tarreau | 417fae0 | 2007-03-25 21:16:40 +0200 | [diff] [blame] | 3892 | sv->uweight, |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3893 | (sv->state & SRV_BACKUP) ? "-" : "Y", |
| 3894 | (sv->state & SRV_BACKUP) ? "Y" : "-"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3895 | |
| 3896 | /* check failures : unique, fatal */ |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3897 | if (sv->state & SRV_CHECKED) |
| 3898 | chunk_printf(&msg, sizeof(trash), |
| 3899 | "<td align=right>%d</td><td align=right>%d</td></tr>\n", |
| 3900 | sv->failed_checks, sv->down_trans); |
| 3901 | else |
| 3902 | chunk_printf(&msg, sizeof(trash), |
| 3903 | "<td colspan=2></td></tr>\n"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3904 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3905 | if (buffer_write_chunk(rep, &msg) != 0) |
| 3906 | return 0; |
| 3907 | |
| 3908 | s->data_ctx.stats.sv = sv->next; |
| 3909 | } /* while sv */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3910 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3911 | s->data_ctx.stats.px_st = DATA_ST_PX_BE; |
| 3912 | /* fall through */ |
| 3913 | |
| 3914 | case DATA_ST_PX_BE: |
| 3915 | /* print the backend */ |
| 3916 | if (px->cap & PR_CAP_BE) { |
Willy Tarreau | 4b946c8 | 2007-07-25 00:28:06 +0200 | [diff] [blame] | 3917 | int gcd = 1; |
| 3918 | |
Willy Tarreau | 5af3a69 | 2007-07-24 23:32:33 +0200 | [diff] [blame] | 3919 | if (px->map_state & PR_MAP_RECALC) |
| 3920 | recalc_server_map(px); |
| 3921 | |
Willy Tarreau | 4b946c8 | 2007-07-25 00:28:06 +0200 | [diff] [blame] | 3922 | /* The GCD which was computed causes the total effective |
| 3923 | * weight to appear lower than all weights. Let's |
| 3924 | * recompute it. |
| 3925 | */ |
| 3926 | if (px->srv && px->srv->eweight) |
| 3927 | gcd = px->srv->uweight / px->srv->eweight; |
| 3928 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3929 | chunk_printf(&msg, sizeof(trash), |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3930 | /* name */ |
| 3931 | "<tr align=center class=\"backend\"><td>Backend</td>" |
| 3932 | /* queue : current, max */ |
| 3933 | "<td align=right>%d</td><td align=right>%d</td>" |
| 3934 | /* sessions : current, max, limit, cumul. */ |
| 3935 | "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>" |
| 3936 | /* bytes : in, out */ |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 3937 | "<td align=right>%lld</td><td align=right>%lld</td>" |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3938 | /* denied: req, resp */ |
| 3939 | "<td align=right>%d</td><td align=right>%d</td>" |
| 3940 | /* errors : request, connect, response */ |
| 3941 | "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n" |
| 3942 | /* server status : reflect backend status (up/down) : we display UP |
| 3943 | * if the backend has known working servers or if it has no server at |
| 3944 | * all (eg: for stats). Tthen we display the total weight, number of |
| 3945 | * active and backups. */ |
| 3946 | "<td align=center>%s</td><td align=center>%d</td>" |
| 3947 | "<td align=center>%d</td><td align=center>%d</td>" |
| 3948 | /* rest of server: nothing */ |
| 3949 | "<td align=center colspan=2></td></tr>" |
| 3950 | "", |
| 3951 | px->nbpend /* or px->totpend ? */, px->nbpend_max, |
| 3952 | px->beconn, px->beconn_max, px->fullconn, px->cum_beconn, |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 3953 | px->bytes_in, px->bytes_out, |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3954 | px->denied_req, px->denied_resp, |
| 3955 | px->failed_conns, px->failed_resp, |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3956 | (px->srv_map_sz > 0 || !px->srv) ? "UP" : "DOWN", |
Willy Tarreau | 4b946c8 | 2007-07-25 00:28:06 +0200 | [diff] [blame] | 3957 | px->srv_map_sz * gcd, px->srv_act, px->srv_bck); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3958 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3959 | if (buffer_write_chunk(rep, &msg) != 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3960 | return 0; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3961 | } |
| 3962 | |
| 3963 | s->data_ctx.stats.px_st = DATA_ST_PX_END; |
| 3964 | /* fall through */ |
| 3965 | |
| 3966 | case DATA_ST_PX_END: |
| 3967 | chunk_printf(&msg, sizeof(trash), "</table><p>\n"); |
| 3968 | |
| 3969 | if (buffer_write_chunk(rep, &msg) != 0) |
| 3970 | return 0; |
| 3971 | |
| 3972 | s->data_ctx.stats.px_st = DATA_ST_PX_FIN; |
| 3973 | /* fall through */ |
| 3974 | |
| 3975 | case DATA_ST_PX_FIN: |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3976 | return 1; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3977 | |
| 3978 | default: |
| 3979 | /* unknown state, we should put an abort() here ! */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3980 | return 1; |
| 3981 | } |
| 3982 | } |
| 3983 | |
| 3984 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3985 | /* Iterate the same filter through all request headers. |
| 3986 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3987 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 3988 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3989 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3990 | 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] | 3991 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3992 | char term; |
| 3993 | char *cur_ptr, *cur_end, *cur_next; |
| 3994 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3995 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3996 | struct hdr_idx_elem *cur_hdr; |
| 3997 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 3998 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3999 | last_hdr = 0; |
| 4000 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4001 | 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] | 4002 | old_idx = 0; |
| 4003 | |
| 4004 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4005 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4006 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4007 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4008 | (exp->action == ACT_ALLOW || |
| 4009 | exp->action == ACT_DENY || |
| 4010 | exp->action == ACT_TARPIT)) |
| 4011 | return 0; |
| 4012 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4013 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4014 | if (!cur_idx) |
| 4015 | break; |
| 4016 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4017 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4018 | cur_ptr = cur_next; |
| 4019 | cur_end = cur_ptr + cur_hdr->len; |
| 4020 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4021 | |
| 4022 | /* Now we have one header between cur_ptr and cur_end, |
| 4023 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4024 | */ |
| 4025 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4026 | /* The annoying part is that pattern matching needs |
| 4027 | * that we modify the contents to null-terminate all |
| 4028 | * strings before testing them. |
| 4029 | */ |
| 4030 | |
| 4031 | term = *cur_end; |
| 4032 | *cur_end = '\0'; |
| 4033 | |
| 4034 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4035 | switch (exp->action) { |
| 4036 | case ACT_SETBE: |
| 4037 | /* It is not possible to jump a second time. |
| 4038 | * FIXME: should we return an HTTP/500 here so that |
| 4039 | * the admin knows there's a problem ? |
| 4040 | */ |
| 4041 | if (t->be != t->fe) |
| 4042 | break; |
| 4043 | |
| 4044 | /* Swithing Proxy */ |
| 4045 | t->be = (struct proxy *) exp->replace; |
| 4046 | |
| 4047 | /* right now, the backend switch is not too much complicated |
| 4048 | * because we have associated req_cap and rsp_cap to the |
| 4049 | * frontend, and the beconn will be updated later. |
| 4050 | */ |
| 4051 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4052 | t->rep->rto = t->req->wto = t->be->srvtimeout; |
| 4053 | t->req->cto = t->be->contimeout; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 4054 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4055 | last_hdr = 1; |
| 4056 | break; |
| 4057 | |
| 4058 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4059 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4060 | last_hdr = 1; |
| 4061 | break; |
| 4062 | |
| 4063 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4064 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4065 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4066 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4067 | break; |
| 4068 | |
| 4069 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4070 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4071 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4072 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4073 | break; |
| 4074 | |
| 4075 | case ACT_REPLACE: |
| 4076 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4077 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 4078 | /* FIXME: if the user adds a newline in the replacement, the |
| 4079 | * index will not be recalculated for now, and the new line |
| 4080 | * will not be counted as a new header. |
| 4081 | */ |
| 4082 | |
| 4083 | cur_end += delta; |
| 4084 | cur_next += delta; |
| 4085 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4086 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4087 | break; |
| 4088 | |
| 4089 | case ACT_REMOVE: |
| 4090 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 4091 | cur_next += delta; |
| 4092 | |
| 4093 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4094 | txn->req.eoh += delta; |
| 4095 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4096 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4097 | cur_hdr->len = 0; |
| 4098 | cur_end = NULL; /* null-term has been rewritten */ |
| 4099 | break; |
| 4100 | |
| 4101 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4102 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4103 | if (cur_end) |
| 4104 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4105 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4106 | /* keep the link from this header to next one in case of later |
| 4107 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4108 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4109 | old_idx = cur_idx; |
| 4110 | } |
| 4111 | return 0; |
| 4112 | } |
| 4113 | |
| 4114 | |
| 4115 | /* Apply the filter to the request line. |
| 4116 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 4117 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4118 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 4119 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4120 | */ |
| 4121 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 4122 | { |
| 4123 | char term; |
| 4124 | char *cur_ptr, *cur_end; |
| 4125 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4126 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4127 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4128 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4129 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4130 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4131 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4132 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4133 | (exp->action == ACT_ALLOW || |
| 4134 | exp->action == ACT_DENY || |
| 4135 | exp->action == ACT_TARPIT)) |
| 4136 | return 0; |
| 4137 | else if (exp->action == ACT_REMOVE) |
| 4138 | return 0; |
| 4139 | |
| 4140 | done = 0; |
| 4141 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4142 | 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] | 4143 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4144 | |
| 4145 | /* Now we have the request line between cur_ptr and cur_end */ |
| 4146 | |
| 4147 | /* The annoying part is that pattern matching needs |
| 4148 | * that we modify the contents to null-terminate all |
| 4149 | * strings before testing them. |
| 4150 | */ |
| 4151 | |
| 4152 | term = *cur_end; |
| 4153 | *cur_end = '\0'; |
| 4154 | |
| 4155 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4156 | switch (exp->action) { |
| 4157 | case ACT_SETBE: |
| 4158 | /* It is not possible to jump a second time. |
| 4159 | * FIXME: should we return an HTTP/500 here so that |
| 4160 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4161 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4162 | if (t->be != t->fe) |
| 4163 | break; |
| 4164 | |
| 4165 | /* Swithing Proxy */ |
| 4166 | t->be = (struct proxy *) exp->replace; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4167 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4168 | /* right now, the backend switch is not too much complicated |
| 4169 | * because we have associated req_cap and rsp_cap to the |
| 4170 | * frontend, and the beconn will be updated later. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4171 | */ |
| 4172 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4173 | t->rep->rto = t->req->wto = t->be->srvtimeout; |
| 4174 | t->req->cto = t->be->contimeout; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 4175 | t->conn_retries = t->be->conn_retries; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4176 | done = 1; |
| 4177 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4178 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4179 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4180 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4181 | done = 1; |
| 4182 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4183 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4184 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4185 | txn->flags |= TX_CLDENY; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4186 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4187 | done = 1; |
| 4188 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4189 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4190 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4191 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4192 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4193 | done = 1; |
| 4194 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4195 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4196 | case ACT_REPLACE: |
| 4197 | *cur_end = term; /* restore the string terminator */ |
| 4198 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4199 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 4200 | /* FIXME: if the user adds a newline in the replacement, the |
| 4201 | * index will not be recalculated for now, and the new line |
| 4202 | * will not be counted as a new header. |
| 4203 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4204 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4205 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4206 | cur_end += delta; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4207 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4208 | 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] | 4209 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4210 | HTTP_MSG_RQMETH, |
| 4211 | cur_ptr, cur_end + 1, |
| 4212 | NULL, NULL); |
| 4213 | if (unlikely(!cur_end)) |
| 4214 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4215 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4216 | /* we have a full request and we know that we have either a CR |
| 4217 | * or an LF at <ptr>. |
| 4218 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4219 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 4220 | 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] | 4221 | /* there is no point trying this regex on headers */ |
| 4222 | return 1; |
| 4223 | } |
| 4224 | } |
| 4225 | *cur_end = term; /* restore the string terminator */ |
| 4226 | return done; |
| 4227 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 4228 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4229 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4230 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4231 | /* |
| 4232 | * Apply all the req filters <exp> to all headers in buffer <req> of session <t>. |
| 4233 | * 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] | 4234 | * unparsable request. Since it can manage the switch to another backend, it |
| 4235 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4236 | */ |
| 4237 | int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 4238 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4239 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4240 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4241 | while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4242 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4243 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4244 | /* |
| 4245 | * The interleaving of transformations and verdicts |
| 4246 | * makes it difficult to decide to continue or stop |
| 4247 | * the evaluation. |
| 4248 | */ |
| 4249 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4250 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4251 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 4252 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) { |
| 4253 | exp = exp->next; |
| 4254 | continue; |
| 4255 | } |
| 4256 | |
| 4257 | /* Apply the filter to the request line. */ |
| 4258 | ret = apply_filter_to_req_line(t, req, exp); |
| 4259 | if (unlikely(ret < 0)) |
| 4260 | return -1; |
| 4261 | |
| 4262 | if (likely(ret == 0)) { |
| 4263 | /* The filter did not match the request, it can be |
| 4264 | * iterated through all headers. |
| 4265 | */ |
| 4266 | apply_filter_to_req_headers(t, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4267 | } |
| 4268 | exp = exp->next; |
| 4269 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4270 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4271 | } |
| 4272 | |
| 4273 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4274 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4275 | /* |
| 4276 | * Manager client-side cookie |
| 4277 | */ |
| 4278 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 4279 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4280 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4281 | char *p1, *p2, *p3, *p4; |
| 4282 | char *del_colon, *del_cookie, *colon; |
| 4283 | int app_cookies; |
| 4284 | |
| 4285 | appsess *asession_temp = NULL; |
| 4286 | appsess local_asession; |
| 4287 | |
| 4288 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4289 | int cur_idx, old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4290 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4291 | if (t->be->cookie_name == NULL && |
| 4292 | t->be->appsession_name == NULL && |
| 4293 | t->be->capture_name == NULL) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4294 | return; |
| 4295 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4296 | /* Iterate through the headers. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4297 | * we start with the start line. |
| 4298 | */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 4299 | old_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4300 | 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] | 4301 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4302 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4303 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4304 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4305 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4306 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4307 | cur_ptr = cur_next; |
| 4308 | cur_end = cur_ptr + cur_hdr->len; |
| 4309 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4310 | |
| 4311 | /* We have one full header between cur_ptr and cur_end, and the |
| 4312 | * next header starts at cur_next. We're only interested in |
| 4313 | * "Cookie:" headers. |
| 4314 | */ |
| 4315 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4316 | val = http_header_match2(cur_ptr, cur_end, "Cookie", 6); |
| 4317 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4318 | old_idx = cur_idx; |
| 4319 | continue; |
| 4320 | } |
| 4321 | |
| 4322 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 4323 | * attributes whose name begin with a '$', and associate them with |
| 4324 | * the right cookie, if we want to delete this cookie. |
| 4325 | * So there are 3 cases for each cookie read : |
| 4326 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 4327 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 4328 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 4329 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 4330 | * "special" cookie. |
| 4331 | * At the end of loop, if a "special" cookie remains, we may have to |
| 4332 | * remove it. If no application cookie persists in the header, we |
| 4333 | * *MUST* delete it |
| 4334 | */ |
| 4335 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4336 | colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4337 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4338 | /* del_cookie == NULL => nothing to be deleted */ |
| 4339 | del_colon = del_cookie = NULL; |
| 4340 | app_cookies = 0; |
| 4341 | |
| 4342 | while (p1 < cur_end) { |
| 4343 | /* skip spaces and colons, but keep an eye on these ones */ |
| 4344 | while (p1 < cur_end) { |
| 4345 | if (*p1 == ';' || *p1 == ',') |
| 4346 | colon = p1; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4347 | else if (!isspace((unsigned char)*p1)) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4348 | break; |
| 4349 | p1++; |
| 4350 | } |
| 4351 | |
| 4352 | if (p1 == cur_end) |
| 4353 | break; |
| 4354 | |
| 4355 | /* p1 is at the beginning of the cookie name */ |
| 4356 | p2 = p1; |
| 4357 | while (p2 < cur_end && *p2 != '=') |
| 4358 | p2++; |
| 4359 | |
| 4360 | if (p2 == cur_end) |
| 4361 | break; |
| 4362 | |
| 4363 | p3 = p2 + 1; /* skips the '=' sign */ |
| 4364 | if (p3 == cur_end) |
| 4365 | break; |
| 4366 | |
| 4367 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4368 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4369 | p4++; |
| 4370 | |
| 4371 | /* here, we have the cookie name between p1 and p2, |
| 4372 | * and its value between p3 and p4. |
| 4373 | * we can process it : |
| 4374 | * |
| 4375 | * Cookie: NAME=VALUE; |
| 4376 | * | || || | |
| 4377 | * | || || +--> p4 |
| 4378 | * | || |+-------> p3 |
| 4379 | * | || +--------> p2 |
| 4380 | * | |+------------> p1 |
| 4381 | * | +-------------> colon |
| 4382 | * +--------------------> cur_ptr |
| 4383 | */ |
| 4384 | |
| 4385 | if (*p1 == '$') { |
| 4386 | /* skip this one */ |
| 4387 | } |
| 4388 | else { |
| 4389 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4390 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4391 | txn->cli_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4392 | (p4 - p1 >= t->fe->capture_namelen) && |
| 4393 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4394 | int log_len = p4 - p1; |
| 4395 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 4396 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4397 | Alert("HTTP logging : out of memory.\n"); |
| 4398 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4399 | if (log_len > t->fe->capture_len) |
| 4400 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4401 | memcpy(txn->cli_cookie, p1, log_len); |
| 4402 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4403 | } |
| 4404 | } |
| 4405 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4406 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 4407 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4408 | /* Cool... it's the right one */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4409 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4410 | char *delim; |
| 4411 | |
| 4412 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 4413 | * have the server ID betweek p3 and delim, and the original cookie between |
| 4414 | * delim+1 and p4. Otherwise, delim==p4 : |
| 4415 | * |
| 4416 | * Cookie: NAME=SRV~VALUE; |
| 4417 | * | || || | | |
| 4418 | * | || || | +--> p4 |
| 4419 | * | || || +--------> delim |
| 4420 | * | || |+-----------> p3 |
| 4421 | * | || +------------> p2 |
| 4422 | * | |+----------------> p1 |
| 4423 | * | +-----------------> colon |
| 4424 | * +------------------------> cur_ptr |
| 4425 | */ |
| 4426 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4427 | if (t->be->options & PR_O_COOK_PFX) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4428 | for (delim = p3; delim < p4; delim++) |
| 4429 | if (*delim == COOKIE_DELIM) |
| 4430 | break; |
| 4431 | } |
| 4432 | else |
| 4433 | delim = p4; |
| 4434 | |
| 4435 | |
| 4436 | /* Here, we'll look for the first running server which supports the cookie. |
| 4437 | * This allows to share a same cookie between several servers, for example |
| 4438 | * to dedicate backup servers to specific servers only. |
| 4439 | * However, to prevent clients from sticking to cookie-less backup server |
| 4440 | * when they have incidentely learned an empty cookie, we simply ignore |
| 4441 | * empty cookies and mark them as invalid. |
| 4442 | */ |
| 4443 | if (delim == p3) |
| 4444 | srv = NULL; |
| 4445 | |
| 4446 | while (srv) { |
Willy Tarreau | 92f2ab1 | 2007-02-02 22:14:47 +0100 | [diff] [blame] | 4447 | if (srv->cookie && (srv->cklen == delim - p3) && |
| 4448 | !memcmp(p3, srv->cookie, delim - p3)) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4449 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4450 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4451 | txn->flags &= ~TX_CK_MASK; |
| 4452 | txn->flags |= TX_CK_VALID; |
| 4453 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4454 | t->srv = srv; |
| 4455 | break; |
| 4456 | } else { |
| 4457 | /* we found a server, but it's down */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4458 | txn->flags &= ~TX_CK_MASK; |
| 4459 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4460 | } |
| 4461 | } |
| 4462 | srv = srv->next; |
| 4463 | } |
| 4464 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4465 | if (!srv && !(txn->flags & TX_CK_DOWN)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4466 | /* no server matched this cookie */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4467 | txn->flags &= ~TX_CK_MASK; |
| 4468 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4469 | } |
| 4470 | |
| 4471 | /* depending on the cookie mode, we may have to either : |
| 4472 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 4473 | * the server never sees it ; |
| 4474 | * - remove the server id from the cookie value, and tag the cookie as an |
| 4475 | * application cookie so that it does not get accidentely removed later, |
| 4476 | * if we're in cookie prefix mode |
| 4477 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4478 | if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4479 | int delta; /* negative */ |
| 4480 | |
| 4481 | delta = buffer_replace2(req, p3, delim + 1, NULL, 0); |
| 4482 | p4 += delta; |
| 4483 | cur_end += delta; |
| 4484 | cur_next += delta; |
| 4485 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4486 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4487 | |
| 4488 | del_cookie = del_colon = NULL; |
| 4489 | app_cookies++; /* protect the header from deletion */ |
| 4490 | } |
| 4491 | else if (del_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4492 | (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] | 4493 | del_cookie = p1; |
| 4494 | del_colon = colon; |
| 4495 | } |
| 4496 | } else { |
| 4497 | /* now we know that we must keep this cookie since it's |
| 4498 | * not ours. But if we wanted to delete our cookie |
| 4499 | * earlier, we cannot remove the complete header, but we |
| 4500 | * can remove the previous block itself. |
| 4501 | */ |
| 4502 | app_cookies++; |
| 4503 | |
| 4504 | if (del_cookie != NULL) { |
| 4505 | int delta; /* negative */ |
| 4506 | |
| 4507 | delta = buffer_replace2(req, del_cookie, p1, NULL, 0); |
| 4508 | p4 += delta; |
| 4509 | cur_end += delta; |
| 4510 | cur_next += delta; |
| 4511 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4512 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4513 | del_cookie = del_colon = NULL; |
| 4514 | } |
| 4515 | } |
| 4516 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4517 | if ((t->be->appsession_name != NULL) && |
| 4518 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4519 | /* first, let's see if the cookie is our appcookie*/ |
| 4520 | |
| 4521 | /* Cool... it's the right one */ |
| 4522 | |
| 4523 | asession_temp = &local_asession; |
| 4524 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4525 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4526 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 4527 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 4528 | return; |
| 4529 | } |
| 4530 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4531 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 4532 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4533 | asession_temp->serverid = NULL; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4534 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4535 | /* only do insert, if lookup fails */ |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4536 | asession_temp = appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid); |
| 4537 | if (asession_temp == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4538 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4539 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4540 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4541 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 4542 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 4543 | return; |
| 4544 | } |
| 4545 | |
| 4546 | asession_temp->sessid = local_asession.sessid; |
| 4547 | asession_temp->serverid = local_asession.serverid; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4548 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4549 | } else { |
| 4550 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4551 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4552 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4553 | if (asession_temp->serverid == NULL) { |
| 4554 | Alert("Found Application Session without matching server.\n"); |
| 4555 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4556 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4557 | while (srv) { |
| 4558 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4559 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4560 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4561 | txn->flags &= ~TX_CK_MASK; |
| 4562 | txn->flags |= TX_CK_VALID; |
| 4563 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4564 | t->srv = srv; |
| 4565 | break; |
| 4566 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4567 | txn->flags &= ~TX_CK_MASK; |
| 4568 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4569 | } |
| 4570 | } |
| 4571 | srv = srv->next; |
| 4572 | }/* end while(srv) */ |
| 4573 | }/* end else if server == NULL */ |
| 4574 | |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 4575 | tv_add(&asession_temp->expire, &now, &t->be->appsession_timeout); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4576 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 4577 | } |
| 4578 | |
| 4579 | /* we'll have to look for another cookie ... */ |
| 4580 | p1 = p4; |
| 4581 | } /* while (p1 < cur_end) */ |
| 4582 | |
| 4583 | /* There's no more cookie on this line. |
| 4584 | * We may have marked the last one(s) for deletion. |
| 4585 | * We must do this now in two ways : |
| 4586 | * - if there is no app cookie, we simply delete the header ; |
| 4587 | * - if there are app cookies, we must delete the end of the |
| 4588 | * string properly, including the colon/semi-colon before |
| 4589 | * the cookie name. |
| 4590 | */ |
| 4591 | if (del_cookie != NULL) { |
| 4592 | int delta; |
| 4593 | if (app_cookies) { |
| 4594 | delta = buffer_replace2(req, del_colon, cur_end, NULL, 0); |
| 4595 | cur_end = del_colon; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4596 | cur_hdr->len += delta; |
| 4597 | } else { |
| 4598 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4599 | |
| 4600 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4601 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4602 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4603 | cur_hdr->len = 0; |
| 4604 | } |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 4605 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4606 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4607 | } |
| 4608 | |
| 4609 | /* keep the link from this header to next one */ |
| 4610 | old_idx = cur_idx; |
| 4611 | } /* end of cookie processing on this header */ |
| 4612 | } |
| 4613 | |
| 4614 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4615 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 4616 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 4617 | */ |
| 4618 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4619 | { |
| 4620 | char term; |
| 4621 | char *cur_ptr, *cur_end, *cur_next; |
| 4622 | int cur_idx, old_idx, last_hdr; |
| 4623 | struct http_txn *txn = &t->txn; |
| 4624 | struct hdr_idx_elem *cur_hdr; |
| 4625 | int len, delta; |
| 4626 | |
| 4627 | last_hdr = 0; |
| 4628 | |
| 4629 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4630 | old_idx = 0; |
| 4631 | |
| 4632 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4633 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4634 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4635 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4636 | (exp->action == ACT_ALLOW || |
| 4637 | exp->action == ACT_DENY)) |
| 4638 | return 0; |
| 4639 | |
| 4640 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 4641 | if (!cur_idx) |
| 4642 | break; |
| 4643 | |
| 4644 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4645 | cur_ptr = cur_next; |
| 4646 | cur_end = cur_ptr + cur_hdr->len; |
| 4647 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4648 | |
| 4649 | /* Now we have one header between cur_ptr and cur_end, |
| 4650 | * and the next header starts at cur_next. |
| 4651 | */ |
| 4652 | |
| 4653 | /* The annoying part is that pattern matching needs |
| 4654 | * that we modify the contents to null-terminate all |
| 4655 | * strings before testing them. |
| 4656 | */ |
| 4657 | |
| 4658 | term = *cur_end; |
| 4659 | *cur_end = '\0'; |
| 4660 | |
| 4661 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4662 | switch (exp->action) { |
| 4663 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4664 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4665 | last_hdr = 1; |
| 4666 | break; |
| 4667 | |
| 4668 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4669 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4670 | last_hdr = 1; |
| 4671 | break; |
| 4672 | |
| 4673 | case ACT_REPLACE: |
| 4674 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4675 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 4676 | /* FIXME: if the user adds a newline in the replacement, the |
| 4677 | * index will not be recalculated for now, and the new line |
| 4678 | * will not be counted as a new header. |
| 4679 | */ |
| 4680 | |
| 4681 | cur_end += delta; |
| 4682 | cur_next += delta; |
| 4683 | cur_hdr->len += delta; |
| 4684 | txn->rsp.eoh += delta; |
| 4685 | break; |
| 4686 | |
| 4687 | case ACT_REMOVE: |
| 4688 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 4689 | cur_next += delta; |
| 4690 | |
| 4691 | /* FIXME: this should be a separate function */ |
| 4692 | txn->rsp.eoh += delta; |
| 4693 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4694 | txn->hdr_idx.used--; |
| 4695 | cur_hdr->len = 0; |
| 4696 | cur_end = NULL; /* null-term has been rewritten */ |
| 4697 | break; |
| 4698 | |
| 4699 | } |
| 4700 | } |
| 4701 | if (cur_end) |
| 4702 | *cur_end = term; /* restore the string terminator */ |
| 4703 | |
| 4704 | /* keep the link from this header to next one in case of later |
| 4705 | * removal of next header. |
| 4706 | */ |
| 4707 | old_idx = cur_idx; |
| 4708 | } |
| 4709 | return 0; |
| 4710 | } |
| 4711 | |
| 4712 | |
| 4713 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 4714 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 4715 | * or -1 if a replacement resulted in an invalid status line. |
| 4716 | */ |
| 4717 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4718 | { |
| 4719 | char term; |
| 4720 | char *cur_ptr, *cur_end; |
| 4721 | int done; |
| 4722 | struct http_txn *txn = &t->txn; |
| 4723 | int len, delta; |
| 4724 | |
| 4725 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4726 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4727 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4728 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4729 | (exp->action == ACT_ALLOW || |
| 4730 | exp->action == ACT_DENY)) |
| 4731 | return 0; |
| 4732 | else if (exp->action == ACT_REMOVE) |
| 4733 | return 0; |
| 4734 | |
| 4735 | done = 0; |
| 4736 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4737 | 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] | 4738 | cur_end = cur_ptr + txn->rsp.sl.rq.l; |
| 4739 | |
| 4740 | /* Now we have the status line between cur_ptr and cur_end */ |
| 4741 | |
| 4742 | /* The annoying part is that pattern matching needs |
| 4743 | * that we modify the contents to null-terminate all |
| 4744 | * strings before testing them. |
| 4745 | */ |
| 4746 | |
| 4747 | term = *cur_end; |
| 4748 | *cur_end = '\0'; |
| 4749 | |
| 4750 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4751 | switch (exp->action) { |
| 4752 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4753 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4754 | done = 1; |
| 4755 | break; |
| 4756 | |
| 4757 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4758 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4759 | done = 1; |
| 4760 | break; |
| 4761 | |
| 4762 | case ACT_REPLACE: |
| 4763 | *cur_end = term; /* restore the string terminator */ |
| 4764 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4765 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 4766 | /* FIXME: if the user adds a newline in the replacement, the |
| 4767 | * index will not be recalculated for now, and the new line |
| 4768 | * will not be counted as a new header. |
| 4769 | */ |
| 4770 | |
| 4771 | txn->rsp.eoh += delta; |
| 4772 | cur_end += delta; |
| 4773 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4774 | 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] | 4775 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 4776 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4777 | cur_ptr, cur_end + 1, |
| 4778 | NULL, NULL); |
| 4779 | if (unlikely(!cur_end)) |
| 4780 | return -1; |
| 4781 | |
| 4782 | /* we have a full respnse and we know that we have either a CR |
| 4783 | * or an LF at <ptr>. |
| 4784 | */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4785 | 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] | 4786 | hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r'); |
| 4787 | /* there is no point trying this regex on headers */ |
| 4788 | return 1; |
| 4789 | } |
| 4790 | } |
| 4791 | *cur_end = term; /* restore the string terminator */ |
| 4792 | return done; |
| 4793 | } |
| 4794 | |
| 4795 | |
| 4796 | |
| 4797 | /* |
| 4798 | * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>. |
| 4799 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 4800 | * unparsable response. |
| 4801 | */ |
| 4802 | int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4803 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4804 | struct http_txn *txn = &t->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4805 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4806 | while (exp && !(txn->flags & TX_SVDENY)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4807 | int ret; |
| 4808 | |
| 4809 | /* |
| 4810 | * The interleaving of transformations and verdicts |
| 4811 | * makes it difficult to decide to continue or stop |
| 4812 | * the evaluation. |
| 4813 | */ |
| 4814 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4815 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4816 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 4817 | exp->action == ACT_PASS)) { |
| 4818 | exp = exp->next; |
| 4819 | continue; |
| 4820 | } |
| 4821 | |
| 4822 | /* Apply the filter to the status line. */ |
| 4823 | ret = apply_filter_to_sts_line(t, rtr, exp); |
| 4824 | if (unlikely(ret < 0)) |
| 4825 | return -1; |
| 4826 | |
| 4827 | if (likely(ret == 0)) { |
| 4828 | /* The filter did not match the response, it can be |
| 4829 | * iterated through all headers. |
| 4830 | */ |
| 4831 | apply_filter_to_resp_headers(t, rtr, exp); |
| 4832 | } |
| 4833 | exp = exp->next; |
| 4834 | } |
| 4835 | return 0; |
| 4836 | } |
| 4837 | |
| 4838 | |
| 4839 | |
| 4840 | /* |
| 4841 | * Manager server-side cookies |
| 4842 | */ |
| 4843 | void manage_server_side_cookies(struct session *t, struct buffer *rtr) |
| 4844 | { |
| 4845 | struct http_txn *txn = &t->txn; |
| 4846 | char *p1, *p2, *p3, *p4; |
| 4847 | |
| 4848 | appsess *asession_temp = NULL; |
| 4849 | appsess local_asession; |
| 4850 | |
| 4851 | char *cur_ptr, *cur_end, *cur_next; |
| 4852 | int cur_idx, old_idx, delta; |
| 4853 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4854 | if (t->be->cookie_name == NULL && |
| 4855 | t->be->appsession_name == NULL && |
| 4856 | t->be->capture_name == NULL && |
| 4857 | !(t->be->options & PR_O_CHK_CACHE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4858 | return; |
| 4859 | |
| 4860 | /* Iterate through the headers. |
| 4861 | * we start with the start line. |
| 4862 | */ |
| 4863 | old_idx = 0; |
| 4864 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4865 | |
| 4866 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 4867 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4868 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4869 | |
| 4870 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4871 | cur_ptr = cur_next; |
| 4872 | cur_end = cur_ptr + cur_hdr->len; |
| 4873 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4874 | |
| 4875 | /* We have one full header between cur_ptr and cur_end, and the |
| 4876 | * next header starts at cur_next. We're only interested in |
| 4877 | * "Cookie:" headers. |
| 4878 | */ |
| 4879 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4880 | val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10); |
| 4881 | if (!val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4882 | old_idx = cur_idx; |
| 4883 | continue; |
| 4884 | } |
| 4885 | |
| 4886 | /* 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] | 4887 | txn->flags |= TX_SCK_ANY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4888 | |
| 4889 | |
| 4890 | /* maybe we only wanted to see if there was a set-cookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4891 | if (t->be->cookie_name == NULL && |
| 4892 | t->be->appsession_name == NULL && |
| 4893 | t->be->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4894 | return; |
| 4895 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4896 | p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4897 | |
| 4898 | 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] | 4899 | if (p1 == cur_end || *p1 == ';') /* end of cookie */ |
| 4900 | break; |
| 4901 | |
| 4902 | /* p1 is at the beginning of the cookie name */ |
| 4903 | p2 = p1; |
| 4904 | |
| 4905 | while (p2 < cur_end && *p2 != '=' && *p2 != ';') |
| 4906 | p2++; |
| 4907 | |
| 4908 | if (p2 == cur_end || *p2 == ';') /* next cookie */ |
| 4909 | break; |
| 4910 | |
| 4911 | p3 = p2 + 1; /* skip the '=' sign */ |
| 4912 | if (p3 == cur_end) |
| 4913 | break; |
| 4914 | |
| 4915 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4916 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';') |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4917 | p4++; |
| 4918 | |
| 4919 | /* here, we have the cookie name between p1 and p2, |
| 4920 | * and its value between p3 and p4. |
| 4921 | * we can process it. |
| 4922 | */ |
| 4923 | |
| 4924 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4925 | if (t->be->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4926 | txn->srv_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4927 | (p4 - p1 >= t->be->capture_namelen) && |
| 4928 | memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4929 | int log_len = p4 - p1; |
| 4930 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 4931 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4932 | Alert("HTTP logging : out of memory.\n"); |
| 4933 | } |
| 4934 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4935 | if (log_len > t->be->capture_len) |
| 4936 | log_len = t->be->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4937 | memcpy(txn->srv_cookie, p1, log_len); |
| 4938 | txn->srv_cookie[log_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4939 | } |
| 4940 | |
| 4941 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4942 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 4943 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4944 | /* Cool... it's the right one */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4945 | txn->flags |= TX_SCK_SEEN; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4946 | |
| 4947 | /* If the cookie is in insert mode on a known server, we'll delete |
| 4948 | * this occurrence because we'll insert another one later. |
| 4949 | * We'll delete it too if the "indirect" option is set and we're in |
| 4950 | * a direct access. */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4951 | if (((t->srv) && (t->be->options & PR_O_COOK_INS)) || |
| 4952 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4953 | /* this header must be deleted */ |
| 4954 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 4955 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4956 | txn->hdr_idx.used--; |
| 4957 | cur_hdr->len = 0; |
| 4958 | cur_next += delta; |
| 4959 | txn->rsp.eoh += delta; |
| 4960 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4961 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4962 | } |
| 4963 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4964 | (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4965 | /* replace bytes p3->p4 with the cookie name associated |
| 4966 | * with this server since we know it. |
| 4967 | */ |
| 4968 | delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen); |
| 4969 | cur_hdr->len += delta; |
| 4970 | cur_next += delta; |
| 4971 | txn->rsp.eoh += delta; |
| 4972 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4973 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4974 | } |
| 4975 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4976 | (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4977 | /* insert the cookie name associated with this server |
| 4978 | * before existing cookie, and insert a delimitor between them.. |
| 4979 | */ |
| 4980 | delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1); |
| 4981 | cur_hdr->len += delta; |
| 4982 | cur_next += delta; |
| 4983 | txn->rsp.eoh += delta; |
| 4984 | |
| 4985 | p3[t->srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4986 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4987 | } |
| 4988 | } |
| 4989 | /* next, let's see if the cookie is our appcookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4990 | else if ((t->be->appsession_name != NULL) && |
| 4991 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4992 | |
| 4993 | /* Cool... it's the right one */ |
| 4994 | |
| 4995 | size_t server_id_len = strlen(t->srv->id) + 1; |
| 4996 | asession_temp = &local_asession; |
| 4997 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4998 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4999 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5000 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5001 | return; |
| 5002 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5003 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 5004 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5005 | asession_temp->serverid = NULL; |
| 5006 | |
| 5007 | /* only do insert, if lookup fails */ |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5008 | if (appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid) == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5009 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5010 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 5011 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 5012 | return; |
| 5013 | } |
| 5014 | asession_temp->sessid = local_asession.sessid; |
| 5015 | asession_temp->serverid = local_asession.serverid; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5016 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
| 5017 | } else { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5018 | /* free wasted memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5019 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5020 | } |
| 5021 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5022 | if (asession_temp->serverid == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5023 | if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5024 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5025 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5026 | return; |
| 5027 | } |
| 5028 | asession_temp->serverid[0] = '\0'; |
| 5029 | } |
| 5030 | |
| 5031 | if (asession_temp->serverid[0] == '\0') |
| 5032 | memcpy(asession_temp->serverid, t->srv->id, server_id_len); |
| 5033 | |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 5034 | tv_add(&asession_temp->expire, &now, &t->be->appsession_timeout); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5035 | |
| 5036 | #if defined(DEBUG_HASH) |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5037 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5038 | #endif |
| 5039 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 5040 | break; /* we don't want to loop again since there cannot be another cookie on the same line */ |
| 5041 | } /* we're now at the end of the cookie value */ |
| 5042 | |
| 5043 | /* keep the link from this header to next one */ |
| 5044 | old_idx = cur_idx; |
| 5045 | } /* end of cookie processing on this header */ |
| 5046 | } |
| 5047 | |
| 5048 | |
| 5049 | |
| 5050 | /* |
| 5051 | * Check if response is cacheable or not. Updates t->flags. |
| 5052 | */ |
| 5053 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 5054 | { |
| 5055 | struct http_txn *txn = &t->txn; |
| 5056 | char *p1, *p2; |
| 5057 | |
| 5058 | char *cur_ptr, *cur_end, *cur_next; |
| 5059 | int cur_idx; |
| 5060 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5061 | if (!txn->flags & TX_CACHEABLE) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5062 | return; |
| 5063 | |
| 5064 | /* Iterate through the headers. |
| 5065 | * we start with the start line. |
| 5066 | */ |
| 5067 | cur_idx = 0; |
| 5068 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 5069 | |
| 5070 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 5071 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5072 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5073 | |
| 5074 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 5075 | cur_ptr = cur_next; |
| 5076 | cur_end = cur_ptr + cur_hdr->len; |
| 5077 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5078 | |
| 5079 | /* We have one full header between cur_ptr and cur_end, and the |
| 5080 | * next header starts at cur_next. We're only interested in |
| 5081 | * "Cookie:" headers. |
| 5082 | */ |
| 5083 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5084 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 5085 | if (val) { |
| 5086 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 5087 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 5088 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 5089 | return; |
| 5090 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5091 | } |
| 5092 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5093 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 5094 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5095 | continue; |
| 5096 | |
| 5097 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 5098 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5099 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5100 | |
| 5101 | if (p1 >= cur_end) /* no more info */ |
| 5102 | continue; |
| 5103 | |
| 5104 | /* p1 is at the beginning of the value */ |
| 5105 | p2 = p1; |
| 5106 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 5107 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5108 | p2++; |
| 5109 | |
| 5110 | /* we have a complete value between p1 and p2 */ |
| 5111 | if (p2 < cur_end && *p2 == '=') { |
| 5112 | /* we have something of the form no-cache="set-cookie" */ |
| 5113 | if ((cur_end - p1 >= 21) && |
| 5114 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 5115 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5116 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5117 | continue; |
| 5118 | } |
| 5119 | |
| 5120 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 5121 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 5122 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 5123 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 5124 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5125 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5126 | return; |
| 5127 | } |
| 5128 | |
| 5129 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5130 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5131 | continue; |
| 5132 | } |
| 5133 | } |
| 5134 | } |
| 5135 | |
| 5136 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5137 | /* |
| 5138 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 5139 | * If the server is found, it's assigned to the session. |
| 5140 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5141 | 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] | 5142 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5143 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5144 | appsess *asession_temp = NULL; |
| 5145 | appsess local_asession; |
| 5146 | char *request_line; |
| 5147 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5148 | if (t->be->appsession_name == NULL || |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 5149 | (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) || |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5150 | (request_line = memchr(begin, ';', len)) == NULL || |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5151 | ((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] | 5152 | return; |
| 5153 | |
| 5154 | /* skip ';' */ |
| 5155 | request_line++; |
| 5156 | |
| 5157 | /* look if we have a jsessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5158 | 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] | 5159 | return; |
| 5160 | |
| 5161 | /* skip jsessionid= */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5162 | request_line += t->be->appsession_name_len + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5163 | |
| 5164 | /* First try if we already have an appsession */ |
| 5165 | asession_temp = &local_asession; |
| 5166 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5167 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5168 | Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n"); |
| 5169 | send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n"); |
| 5170 | return; |
| 5171 | } |
| 5172 | |
| 5173 | /* Copy the sessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5174 | memcpy(asession_temp->sessid, request_line, t->be->appsession_len); |
| 5175 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5176 | asession_temp->serverid = NULL; |
| 5177 | |
| 5178 | /* only do insert, if lookup fails */ |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5179 | if (appsession_hash_lookup(&(t->be->htbl_proxy), asession_temp->sessid) == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5180 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5181 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5182 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5183 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 5184 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 5185 | return; |
| 5186 | } |
| 5187 | asession_temp->sessid = local_asession.sessid; |
| 5188 | asession_temp->serverid = local_asession.serverid; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5189 | appsession_hash_insert(&(t->be->htbl_proxy), asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5190 | } |
| 5191 | else { |
| 5192 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5193 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5194 | } |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5195 | |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 5196 | tv_add(&asession_temp->expire, &now, &t->be->appsession_timeout); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5197 | asession_temp->request_count++; |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5198 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5199 | #if defined(DEBUG_HASH) |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 5200 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5201 | #endif |
| 5202 | if (asession_temp->serverid == NULL) { |
| 5203 | Alert("Found Application Session without matching server.\n"); |
| 5204 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5205 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5206 | while (srv) { |
| 5207 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5208 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5209 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5210 | txn->flags &= ~TX_CK_MASK; |
| 5211 | txn->flags |= TX_CK_VALID; |
| 5212 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5213 | t->srv = srv; |
| 5214 | break; |
| 5215 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5216 | txn->flags &= ~TX_CK_MASK; |
| 5217 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5218 | } |
| 5219 | } |
| 5220 | srv = srv->next; |
| 5221 | } |
| 5222 | } |
| 5223 | } |
| 5224 | |
| 5225 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5226 | /* |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5227 | * In a GET or HEAD request, check if the requested URI matches the stats uri |
| 5228 | * 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] | 5229 | * |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5230 | * 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] | 5231 | * 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] | 5232 | * produce_content() can be called to start sending data. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5233 | * |
| 5234 | * Returns 1 if the session's state changes, otherwise 0. |
| 5235 | */ |
| 5236 | int stats_check_uri_auth(struct session *t, struct proxy *backend) |
| 5237 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5238 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5239 | struct uri_auth *uri_auth = backend->uri_auth; |
| 5240 | struct user_auth *user; |
| 5241 | int authenticated, cur_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5242 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5243 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5244 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5245 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5246 | return 0; |
| 5247 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5248 | h = t->req->data + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5249 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5250 | /* the URI is in h */ |
| 5251 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5252 | return 0; |
| 5253 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 5254 | h += uri_auth->uri_len; |
| 5255 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) { |
| 5256 | if (memcmp(h, ";up", 3) == 0) { |
| 5257 | t->flags |= SN_STAT_HIDEDWN; |
| 5258 | break; |
| 5259 | } |
| 5260 | h++; |
| 5261 | } |
| 5262 | |
| 5263 | if (uri_auth->refresh) { |
| 5264 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 5265 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) { |
| 5266 | if (memcmp(h, ";norefresh", 10) == 0) { |
| 5267 | t->flags |= SN_STAT_NORFRSH; |
| 5268 | break; |
| 5269 | } |
| 5270 | h++; |
| 5271 | } |
| 5272 | } |
| 5273 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5274 | /* we are in front of a interceptable URI. Let's check |
| 5275 | * if there's an authentication and if it's valid. |
| 5276 | */ |
| 5277 | user = uri_auth->users; |
| 5278 | if (!user) { |
| 5279 | /* no user auth required, it's OK */ |
| 5280 | authenticated = 1; |
| 5281 | } else { |
| 5282 | authenticated = 0; |
| 5283 | |
| 5284 | /* a user list is defined, we have to check. |
| 5285 | * skip 21 chars for "Authorization: Basic ". |
| 5286 | */ |
| 5287 | |
| 5288 | /* FIXME: this should move to an earlier place */ |
| 5289 | cur_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5290 | h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 5291 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 5292 | int len = txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5293 | if (len > 14 && |
| 5294 | !strncasecmp("Authorization:", h, 14)) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5295 | txn->auth_hdr.str = h; |
| 5296 | txn->auth_hdr.len = len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5297 | break; |
| 5298 | } |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5299 | h += len + txn->hdr_idx.v[cur_idx].cr + 1; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5300 | } |
| 5301 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5302 | if (txn->auth_hdr.len < 21 || |
| 5303 | memcmp(txn->auth_hdr.str + 14, " Basic ", 7)) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5304 | user = NULL; |
| 5305 | |
| 5306 | while (user) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5307 | if ((txn->auth_hdr.len == user->user_len + 14 + 7) |
| 5308 | && !memcmp(txn->auth_hdr.str + 14 + 7, |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5309 | user->user_pwd, user->user_len)) { |
| 5310 | authenticated = 1; |
| 5311 | break; |
| 5312 | } |
| 5313 | user = user->next; |
| 5314 | } |
| 5315 | } |
| 5316 | |
| 5317 | if (!authenticated) { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5318 | struct chunk msg; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5319 | |
| 5320 | /* no need to go further */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5321 | msg.str = trash; |
| 5322 | msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm); |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5323 | txn->status = 401; |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5324 | client_retnclose(t, &msg); |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5325 | if (!(t->flags & SN_ERR_MASK)) |
| 5326 | t->flags |= SN_ERR_PRXCOND; |
| 5327 | if (!(t->flags & SN_FINST_MASK)) |
| 5328 | t->flags |= SN_FINST_R; |
| 5329 | return 1; |
| 5330 | } |
| 5331 | |
| 5332 | /* The request is valid, the user is authenticate. Let's start sending |
| 5333 | * data. |
| 5334 | */ |
| 5335 | t->cli_state = CL_STSHUTR; |
| 5336 | t->req->rlim = t->req->data + BUFSIZE; /* no more rewrite needed */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 5337 | t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5338 | t->data_source = DATA_SRC_STATS; |
| 5339 | t->data_state = DATA_ST_INIT; |
| 5340 | produce_content(t); |
| 5341 | return 1; |
| 5342 | } |
| 5343 | |
| 5344 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5345 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5346 | * Print a debug line with a header |
| 5347 | */ |
| 5348 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 5349 | { |
| 5350 | int len, max; |
| 5351 | len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id, |
| 5352 | dir, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd); |
| 5353 | max = end - start; |
| 5354 | UBOUND(max, sizeof(trash) - len - 1); |
| 5355 | len += strlcpy2(trash + len, start, max + 1); |
| 5356 | trash[len++] = '\n'; |
| 5357 | write(1, trash, len); |
| 5358 | } |
| 5359 | |
| 5360 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5361 | /************************************************************************/ |
| 5362 | /* The code below is dedicated to ACL parsing and matching */ |
| 5363 | /************************************************************************/ |
| 5364 | |
| 5365 | |
| 5366 | |
| 5367 | |
| 5368 | /* 1. Check on METHOD |
| 5369 | * We use the pre-parsed method if it is known, and store its number as an |
| 5370 | * integer. If it is unknown, we use the pointer and the length. |
| 5371 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5372 | 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] | 5373 | { |
| 5374 | int len, meth; |
| 5375 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5376 | len = strlen(*text); |
| 5377 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5378 | |
| 5379 | pattern->val.i = meth; |
| 5380 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5381 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5382 | if (!pattern->ptr.str) |
| 5383 | return 0; |
| 5384 | pattern->len = len; |
| 5385 | } |
| 5386 | return 1; |
| 5387 | } |
| 5388 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5389 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5390 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5391 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5392 | { |
| 5393 | int meth; |
| 5394 | struct http_txn *txn = l7; |
| 5395 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5396 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5397 | return 0; |
| 5398 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5399 | meth = txn->meth; |
| 5400 | test->i = meth; |
| 5401 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5402 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5403 | /* ensure the indexes are not affected */ |
| 5404 | return 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5405 | test->len = txn->req.sl.rq.m_l; |
| 5406 | test->ptr = txn->req.sol; |
| 5407 | } |
| 5408 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5409 | return 1; |
| 5410 | } |
| 5411 | |
| 5412 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 5413 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 5414 | int icase; |
| 5415 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5416 | if (test->i != pattern->val.i) |
| 5417 | return 0; |
| 5418 | |
| 5419 | if (test->i != HTTP_METH_OTHER) |
| 5420 | return 1; |
| 5421 | |
| 5422 | /* Other method, we must compare the strings */ |
| 5423 | if (pattern->len != test->len) |
| 5424 | return 0; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 5425 | |
| 5426 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 5427 | if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) || |
| 5428 | (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0)) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5429 | return 0; |
| 5430 | return 1; |
| 5431 | } |
| 5432 | |
| 5433 | /* 2. Check on Request/Status Version |
| 5434 | * We simply compare strings here. |
| 5435 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5436 | 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] | 5437 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5438 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5439 | if (!pattern->ptr.str) |
| 5440 | return 0; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5441 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5442 | return 1; |
| 5443 | } |
| 5444 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5445 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5446 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5447 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5448 | { |
| 5449 | struct http_txn *txn = l7; |
| 5450 | char *ptr; |
| 5451 | int len; |
| 5452 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5453 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5454 | return 0; |
| 5455 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5456 | len = txn->req.sl.rq.v_l; |
| 5457 | ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som; |
| 5458 | |
| 5459 | while ((len-- > 0) && (*ptr++ != '/')); |
| 5460 | if (len <= 0) |
| 5461 | return 0; |
| 5462 | |
| 5463 | test->ptr = ptr; |
| 5464 | test->len = len; |
| 5465 | |
| 5466 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5467 | return 1; |
| 5468 | } |
| 5469 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5470 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5471 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5472 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5473 | { |
| 5474 | struct http_txn *txn = l7; |
| 5475 | char *ptr; |
| 5476 | int len; |
| 5477 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5478 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5479 | return 0; |
| 5480 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5481 | len = txn->rsp.sl.st.v_l; |
| 5482 | ptr = txn->rsp.sol; |
| 5483 | |
| 5484 | while ((len-- > 0) && (*ptr++ != '/')); |
| 5485 | if (len <= 0) |
| 5486 | return 0; |
| 5487 | |
| 5488 | test->ptr = ptr; |
| 5489 | test->len = len; |
| 5490 | |
| 5491 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5492 | return 1; |
| 5493 | } |
| 5494 | |
| 5495 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5496 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5497 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5498 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5499 | { |
| 5500 | struct http_txn *txn = l7; |
| 5501 | char *ptr; |
| 5502 | int len; |
| 5503 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5504 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5505 | return 0; |
| 5506 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5507 | len = txn->rsp.sl.st.c_l; |
| 5508 | ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som; |
| 5509 | |
| 5510 | test->i = __strl2ui(ptr, len); |
| 5511 | test->flags = ACL_TEST_F_VOL_1ST; |
| 5512 | return 1; |
| 5513 | } |
| 5514 | |
| 5515 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 5516 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 5517 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5518 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5519 | { |
| 5520 | struct http_txn *txn = l7; |
| 5521 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5522 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5523 | return 0; |
| 5524 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5525 | /* ensure the indexes are not affected */ |
| 5526 | return 0; |
| 5527 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5528 | test->len = txn->req.sl.rq.u_l; |
| 5529 | test->ptr = txn->req.sol + txn->req.sl.rq.u; |
| 5530 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 5531 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 5532 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5533 | return 1; |
| 5534 | } |
| 5535 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5536 | /* 5. Check on HTTP header. A pointer to the beginning of the value is returned. |
| 5537 | * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr(). |
| 5538 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5539 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5540 | acl_fetch_hdr(struct proxy *px, struct session *l4, void *l7, char *sol, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5541 | struct acl_expr *expr, struct acl_test *test) |
| 5542 | { |
| 5543 | struct http_txn *txn = l7; |
| 5544 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5545 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5546 | |
| 5547 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 5548 | /* search for header from the beginning */ |
| 5549 | ctx->idx = 0; |
| 5550 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5551 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 5552 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 5553 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5554 | test->len = ctx->vlen; |
| 5555 | test->ptr = (char *)ctx->line + ctx->val; |
| 5556 | return 1; |
| 5557 | } |
| 5558 | |
| 5559 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 5560 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5561 | return 0; |
| 5562 | } |
| 5563 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5564 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5565 | acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5566 | struct acl_expr *expr, struct acl_test *test) |
| 5567 | { |
| 5568 | struct http_txn *txn = l7; |
| 5569 | |
| 5570 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5571 | return 0; |
| 5572 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5573 | /* ensure the indexes are not affected */ |
| 5574 | return 0; |
| 5575 | |
| 5576 | return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test); |
| 5577 | } |
| 5578 | |
| 5579 | static int |
| 5580 | acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5581 | struct acl_expr *expr, struct acl_test *test) |
| 5582 | { |
| 5583 | struct http_txn *txn = l7; |
| 5584 | |
| 5585 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5586 | return 0; |
| 5587 | |
| 5588 | return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test); |
| 5589 | } |
| 5590 | |
| 5591 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 5592 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 5593 | */ |
| 5594 | static int |
| 5595 | acl_fetch_hdr_cnt(struct proxy *px, struct session *l4, void *l7, char *sol, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5596 | struct acl_expr *expr, struct acl_test *test) |
| 5597 | { |
| 5598 | struct http_txn *txn = l7; |
| 5599 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5600 | struct hdr_ctx ctx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5601 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5602 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5603 | ctx.idx = 0; |
| 5604 | cnt = 0; |
| 5605 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx)) |
| 5606 | cnt++; |
| 5607 | |
| 5608 | test->i = cnt; |
| 5609 | test->flags = ACL_TEST_F_VOL_HDR; |
| 5610 | return 1; |
| 5611 | } |
| 5612 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5613 | static int |
| 5614 | acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5615 | struct acl_expr *expr, struct acl_test *test) |
| 5616 | { |
| 5617 | struct http_txn *txn = l7; |
| 5618 | |
| 5619 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5620 | return 0; |
| 5621 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5622 | /* ensure the indexes are not affected */ |
| 5623 | return 0; |
| 5624 | |
| 5625 | return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test); |
| 5626 | } |
| 5627 | |
| 5628 | static int |
| 5629 | acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5630 | struct acl_expr *expr, struct acl_test *test) |
| 5631 | { |
| 5632 | struct http_txn *txn = l7; |
| 5633 | |
| 5634 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5635 | return 0; |
| 5636 | |
| 5637 | return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test); |
| 5638 | } |
| 5639 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5640 | /* 7. Check on HTTP header's integer value. The integer value is returned. |
| 5641 | * FIXME: the type is 'int', it may not be appropriate for everything. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5642 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5643 | */ |
| 5644 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5645 | acl_fetch_hdr_val(struct proxy *px, struct session *l4, void *l7, char *sol, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5646 | struct acl_expr *expr, struct acl_test *test) |
| 5647 | { |
| 5648 | struct http_txn *txn = l7; |
| 5649 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5650 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5651 | |
| 5652 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 5653 | /* search for header from the beginning */ |
| 5654 | ctx->idx = 0; |
| 5655 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5656 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 5657 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 5658 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5659 | test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen); |
| 5660 | return 1; |
| 5661 | } |
| 5662 | |
| 5663 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 5664 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5665 | return 0; |
| 5666 | } |
| 5667 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5668 | static int |
| 5669 | acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5670 | struct acl_expr *expr, struct acl_test *test) |
| 5671 | { |
| 5672 | struct http_txn *txn = l7; |
| 5673 | |
| 5674 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5675 | return 0; |
| 5676 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5677 | /* ensure the indexes are not affected */ |
| 5678 | return 0; |
| 5679 | |
| 5680 | return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test); |
| 5681 | } |
| 5682 | |
| 5683 | static int |
| 5684 | acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5685 | struct acl_expr *expr, struct acl_test *test) |
| 5686 | { |
| 5687 | struct http_txn *txn = l7; |
| 5688 | |
| 5689 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5690 | return 0; |
| 5691 | |
| 5692 | return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test); |
| 5693 | } |
| 5694 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5695 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 5696 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 5697 | */ |
| 5698 | static int |
| 5699 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5700 | struct acl_expr *expr, struct acl_test *test) |
| 5701 | { |
| 5702 | struct http_txn *txn = l7; |
| 5703 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5704 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5705 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5706 | return 0; |
| 5707 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5708 | /* ensure the indexes are not affected */ |
| 5709 | return 0; |
| 5710 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5711 | ptr = txn->req.sol + txn->req.sl.rq.u; |
| 5712 | end = ptr + txn->req.sl.rq.u_l; |
| 5713 | |
| 5714 | if (ptr >= end) |
| 5715 | return 0; |
| 5716 | |
| 5717 | /* RFC2616, par. 5.1.2 : |
| 5718 | * Request-URI = "*" | absuri | abspath | authority |
| 5719 | */ |
| 5720 | |
| 5721 | if (*ptr == '*') |
| 5722 | return 0; |
| 5723 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 5724 | if (isalpha((unsigned char)*ptr)) { |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5725 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 5726 | ptr++; |
| 5727 | while (ptr < end && |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 5728 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5729 | ptr++; |
| 5730 | /* skip '://' */ |
| 5731 | if (ptr == end || *ptr++ != ':') |
| 5732 | return 0; |
| 5733 | if (ptr == end || *ptr++ != '/') |
| 5734 | return 0; |
| 5735 | if (ptr == end || *ptr++ != '/') |
| 5736 | return 0; |
| 5737 | } |
| 5738 | /* skip [user[:passwd]@]host[:[port]] */ |
| 5739 | |
| 5740 | while (ptr < end && *ptr != '/') |
| 5741 | ptr++; |
| 5742 | |
| 5743 | if (ptr == end) |
| 5744 | return 0; |
| 5745 | |
| 5746 | /* OK, we got the '/' ! */ |
| 5747 | test->ptr = ptr; |
| 5748 | |
| 5749 | while (ptr < end && *ptr != '?') |
| 5750 | ptr++; |
| 5751 | |
| 5752 | test->len = ptr - test->ptr; |
| 5753 | |
| 5754 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 5755 | test->flags = ACL_TEST_F_VOL_1ST; |
| 5756 | return 1; |
| 5757 | } |
| 5758 | |
| 5759 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5760 | |
| 5761 | /************************************************************************/ |
| 5762 | /* All supported keywords must be declared here. */ |
| 5763 | /************************************************************************/ |
| 5764 | |
| 5765 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 5766 | static struct acl_kw_list acl_kws = {{ },{ |
| 5767 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth }, |
| 5768 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str }, |
| 5769 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str }, |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5770 | { "status", acl_parse_int, acl_fetch_stcode, acl_match_int }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5771 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 5772 | { "url", acl_parse_str, acl_fetch_url, acl_match_str }, |
| 5773 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg }, |
| 5774 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end }, |
| 5775 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub }, |
| 5776 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir }, |
| 5777 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom }, |
| 5778 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5779 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5780 | { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str }, |
| 5781 | { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg }, |
| 5782 | { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg }, |
| 5783 | { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end }, |
| 5784 | { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub }, |
| 5785 | { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir }, |
| 5786 | { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom }, |
| 5787 | { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int }, |
| 5788 | { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int }, |
| 5789 | |
| 5790 | { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str }, |
| 5791 | { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg }, |
| 5792 | { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg }, |
| 5793 | { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end }, |
| 5794 | { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub }, |
| 5795 | { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir }, |
| 5796 | { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom }, |
| 5797 | { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int }, |
| 5798 | { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int }, |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5799 | |
| 5800 | { "path", acl_parse_str, acl_fetch_path, acl_match_str }, |
| 5801 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg }, |
| 5802 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg }, |
| 5803 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end }, |
| 5804 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub }, |
| 5805 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir }, |
| 5806 | { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom }, |
| 5807 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 5808 | { NULL, NULL, NULL, NULL }, |
| 5809 | |
| 5810 | #if 0 |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5811 | { "line", acl_parse_str, acl_fetch_line, acl_match_str }, |
| 5812 | { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg }, |
| 5813 | { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg }, |
| 5814 | { "line_end", acl_parse_str, acl_fetch_line, acl_match_end }, |
| 5815 | { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub }, |
| 5816 | { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir }, |
| 5817 | { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom }, |
| 5818 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5819 | { "cook", acl_parse_str, acl_fetch_cook, acl_match_str }, |
| 5820 | { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg }, |
| 5821 | { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg }, |
| 5822 | { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end }, |
| 5823 | { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub }, |
| 5824 | { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir }, |
| 5825 | { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom }, |
| 5826 | { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst }, |
| 5827 | |
| 5828 | { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str }, |
| 5829 | { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg }, |
| 5830 | { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str }, |
| 5831 | { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 }, |
| 5832 | { NULL, NULL, NULL, NULL }, |
| 5833 | #endif |
| 5834 | }}; |
| 5835 | |
| 5836 | |
| 5837 | __attribute__((constructor)) |
| 5838 | static void __http_protocol_init(void) |
| 5839 | { |
| 5840 | acl_register_keywords(&acl_kws); |
| 5841 | } |
| 5842 | |
| 5843 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5844 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5845 | * Local variables: |
| 5846 | * c-indent-level: 8 |
| 5847 | * c-basic-offset: 8 |
| 5848 | * End: |
| 5849 | */ |