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 | |
| 436 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 437 | * returns a message to the client ; the connection is shut down for read, |
| 438 | * and the request is cleared so that no server connection can be initiated. |
| 439 | * The client must be in a valid state for this (HEADER, DATA ...). |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 440 | * Nothing is performed on the server side. The message is contained in a |
| 441 | * "chunk". If it is null, then an empty message is used. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 442 | * The reply buffer doesn't need to be empty before this. |
| 443 | */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 444 | void client_retnclose(struct session *s, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 445 | { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 446 | EV_FD_CLR(s->cli_fd, DIR_RD); |
| 447 | EV_FD_SET(s->cli_fd, DIR_WR); |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 448 | tv_eternity(&s->req->rex); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 449 | if (!tv_add_ifset(&s->rep->wex, &now, &s->fe->clitimeout)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 450 | tv_eternity(&s->rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 451 | s->cli_state = CL_STSHUTR; |
| 452 | buffer_flush(s->rep); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 453 | if (msg->len) |
| 454 | buffer_write(s->rep, msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 455 | s->req->l = 0; |
| 456 | } |
| 457 | |
| 458 | |
| 459 | /* |
| 460 | * returns a message into the rep buffer, and flushes the req buffer. |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 461 | * The reply buffer doesn't need to be empty before this. The message |
| 462 | * is contained in a "chunk". If it is null, then an empty message is |
| 463 | * used. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 464 | */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 465 | void client_return(struct session *s, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 466 | { |
| 467 | buffer_flush(s->rep); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 468 | if (msg->len) |
| 469 | buffer_write(s->rep, msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 470 | s->req->l = 0; |
| 471 | } |
| 472 | |
| 473 | |
| 474 | /* This function turns the server state into the SV_STCLOSE, and sets |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 475 | * indicators accordingly. Note that if <status> is 0, or if the message |
| 476 | * pointer is NULL, then no message is returned. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 477 | */ |
| 478 | void srv_close_with_err(struct session *t, int err, int finst, |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 479 | int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 480 | { |
| 481 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 482 | if (status > 0 && msg) { |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 483 | t->txn.status = status; |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 484 | if (t->fe->mode == PR_MODE_HTTP) |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 485 | client_return(t, msg); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 486 | } |
| 487 | if (!(t->flags & SN_ERR_MASK)) |
| 488 | t->flags |= err; |
| 489 | if (!(t->flags & SN_FINST_MASK)) |
| 490 | t->flags |= finst; |
| 491 | } |
| 492 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 493 | /* This function returns the appropriate error location for the given session |
| 494 | * and message. |
| 495 | */ |
| 496 | |
| 497 | struct chunk *error_message(struct session *s, int msgnum) |
| 498 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 499 | if (s->be->errmsg[msgnum].str) |
| 500 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 501 | else if (s->fe->errmsg[msgnum].str) |
| 502 | return &s->fe->errmsg[msgnum]; |
| 503 | else |
| 504 | return &http_err_chunks[msgnum]; |
| 505 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 506 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 507 | /* |
| 508 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 509 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 510 | */ |
| 511 | static http_meth_t find_http_meth(const char *str, const int len) |
| 512 | { |
| 513 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 514 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 515 | |
| 516 | m = ((unsigned)*str - 'A'); |
| 517 | |
| 518 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 519 | for (h = http_methods[m]; h->len > 0; h++) { |
| 520 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 521 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 522 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 523 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 524 | }; |
| 525 | return HTTP_METH_OTHER; |
| 526 | } |
| 527 | return HTTP_METH_NONE; |
| 528 | |
| 529 | } |
| 530 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 531 | /* Processes the client and server jobs of a session task, then |
| 532 | * puts it back to the wait queue in a clean state, or |
| 533 | * cleans up its resources if it must be deleted. Returns |
| 534 | * the time the task accepts to wait, or TIME_ETERNITY for |
| 535 | * infinity. |
| 536 | */ |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 537 | void process_session(struct task *t, struct timeval *next) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 538 | { |
| 539 | struct session *s = t->context; |
| 540 | int fsm_resync = 0; |
| 541 | |
| 542 | do { |
| 543 | fsm_resync = 0; |
| 544 | //fprintf(stderr,"before_cli:cli=%d, srv=%d\n", s->cli_state, s->srv_state); |
| 545 | fsm_resync |= process_cli(s); |
| 546 | //fprintf(stderr,"cli/srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state); |
| 547 | fsm_resync |= process_srv(s); |
| 548 | //fprintf(stderr,"after_srv:cli=%d, srv=%d\n", s->cli_state, s->srv_state); |
| 549 | } while (fsm_resync); |
| 550 | |
Willy Tarreau | f41d4b1 | 2007-04-28 23:26:14 +0200 | [diff] [blame] | 551 | if (likely(s->cli_state != CL_STCLOSE || s->srv_state != SV_STCLOSE)) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 552 | s->req->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE; |
| 553 | s->rep->flags &= BF_CLEAR_READ & BF_CLEAR_WRITE; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 554 | |
Willy Tarreau | a6a6a93 | 2007-04-28 22:40:08 +0200 | [diff] [blame] | 555 | t->expire = s->req->rex; |
| 556 | tv_min(&t->expire, &s->req->rex, &s->req->wex); |
| 557 | tv_bound(&t->expire, &s->req->cex); |
| 558 | tv_bound(&t->expire, &s->rep->rex); |
| 559 | tv_bound(&t->expire, &s->rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 560 | |
| 561 | /* restore t to its place in the task list */ |
| 562 | task_queue(t); |
| 563 | |
| 564 | #ifdef DEBUG_FULL |
| 565 | /* DEBUG code : this should never ever happen, otherwise it indicates |
| 566 | * that a task still has something to do and will provoke a quick loop. |
| 567 | */ |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 568 | if (tv_remain2(&now, &t->expire) <= 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 569 | exit(100); |
| 570 | #endif |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 571 | *next = t->expire; |
| 572 | return; /* nothing more to do */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 573 | } |
| 574 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 575 | s->fe->feconn--; |
| 576 | if (s->flags & SN_BE_ASSIGNED) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 577 | s->be->beconn--; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 578 | actconn--; |
| 579 | |
Willy Tarreau | f41d4b1 | 2007-04-28 23:26:14 +0200 | [diff] [blame] | 580 | if (unlikely((global.mode & MODE_DEBUG) && |
| 581 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 582 | int len; |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 583 | len = sprintf(trash, "%08x:%s.closed[%04x:%04x]\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 584 | s->uniq_id, s->be->id, |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 585 | (unsigned short)s->cli_fd, (unsigned short)s->srv_fd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 586 | write(1, trash, len); |
| 587 | } |
| 588 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 589 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 590 | if (s->req != NULL) |
| 591 | s->logs.bytes_in = s->req->total; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 592 | if (s->rep != NULL) |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 593 | s->logs.bytes_out = s->rep->total; |
| 594 | |
| 595 | s->fe->bytes_in += s->logs.bytes_in; |
| 596 | s->fe->bytes_out += s->logs.bytes_out; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 597 | if (s->be != s->fe) { |
| 598 | s->be->bytes_in += s->logs.bytes_in; |
| 599 | s->be->bytes_out += s->logs.bytes_out; |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 600 | } |
| 601 | if (s->srv) { |
| 602 | s->srv->bytes_in += s->logs.bytes_in; |
| 603 | s->srv->bytes_out += s->logs.bytes_out; |
| 604 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 605 | |
| 606 | /* let's do a final log if we need it */ |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 607 | if (s->logs.logwait && |
| 608 | !(s->flags & SN_MONITOR) && |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 609 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 610 | if (s->fe->to_log & LW_REQ) |
| 611 | http_sess_log(s); |
| 612 | else |
| 613 | tcp_sess_log(s); |
| 614 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 615 | |
| 616 | /* the task MUST not be in the run queue anymore */ |
| 617 | task_delete(t); |
| 618 | session_free(s); |
| 619 | task_free(t); |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 620 | tv_eternity(next); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 624 | extern const char sess_term_cond[8]; |
| 625 | extern const char sess_fin_state[8]; |
| 626 | extern const char *monthname[12]; |
| 627 | const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */ |
| 628 | const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown, |
| 629 | Set-cookie seen and left unchanged (passive), Set-cookie Deleted, |
| 630 | unknown, Set-cookie Rewritten */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 631 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 632 | struct pool_head *pool2_capture; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 633 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 634 | /* |
| 635 | * send a log for the session when we have enough info about it. |
| 636 | * Will not log if the frontend has no log defined. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 637 | */ |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 638 | static void http_sess_log(struct session *s) |
| 639 | { |
| 640 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 641 | struct proxy *fe = s->fe; |
| 642 | struct proxy *be = s->be; |
| 643 | struct proxy *prx_log; |
| 644 | struct http_txn *txn = &s->txn; |
| 645 | int tolog; |
| 646 | char *uri, *h; |
| 647 | char *svid; |
| 648 | struct tm *tm; |
| 649 | static char tmpline[MAX_SYSLOG_LEN]; |
| 650 | int hdr; |
| 651 | |
| 652 | if (fe->logfac1 < 0 && fe->logfac2 < 0) |
| 653 | return; |
| 654 | prx_log = fe; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 655 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 656 | if (s->cli_addr.ss_family == AF_INET) |
| 657 | inet_ntop(AF_INET, |
| 658 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 659 | pn, sizeof(pn)); |
| 660 | else |
| 661 | inet_ntop(AF_INET6, |
| 662 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 663 | pn, sizeof(pn)); |
| 664 | |
| 665 | tm = localtime((time_t *)&s->logs.tv_accept.tv_sec); |
| 666 | |
| 667 | |
| 668 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 669 | tolog = fe->to_log; |
| 670 | |
| 671 | h = tmpline; |
| 672 | if (fe->to_log & LW_REQHDR && |
| 673 | txn->req.cap && |
| 674 | (h < tmpline + sizeof(tmpline) - 10)) { |
| 675 | *(h++) = ' '; |
| 676 | *(h++) = '{'; |
| 677 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 678 | if (hdr) |
| 679 | *(h++) = '|'; |
| 680 | if (txn->req.cap[hdr] != NULL) |
| 681 | h = encode_string(h, tmpline + sizeof(tmpline) - 7, |
| 682 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 683 | } |
| 684 | *(h++) = '}'; |
| 685 | } |
| 686 | |
| 687 | if (fe->to_log & LW_RSPHDR && |
| 688 | txn->rsp.cap && |
| 689 | (h < tmpline + sizeof(tmpline) - 7)) { |
| 690 | *(h++) = ' '; |
| 691 | *(h++) = '{'; |
| 692 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 693 | if (hdr) |
| 694 | *(h++) = '|'; |
| 695 | if (txn->rsp.cap[hdr] != NULL) |
| 696 | h = encode_string(h, tmpline + sizeof(tmpline) - 4, |
| 697 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 698 | } |
| 699 | *(h++) = '}'; |
| 700 | } |
| 701 | |
| 702 | if (h < tmpline + sizeof(tmpline) - 4) { |
| 703 | *(h++) = ' '; |
| 704 | *(h++) = '"'; |
| 705 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 706 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 707 | '#', url_encode_map, uri); |
| 708 | *(h++) = '"'; |
| 709 | } |
| 710 | *h = '\0'; |
| 711 | |
| 712 | svid = (tolog & LW_SVID) ? |
| 713 | (s->data_source != DATA_SRC_STATS) ? |
| 714 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 715 | |
| 716 | send_log(prx_log, LOG_INFO, |
| 717 | "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]" |
| 718 | " %s %s/%s %d/%d/%d/%d/%s%d %d %s%lld" |
| 719 | " %s %s %c%c%c%c %d/%d/%d/%d %d/%d%s\n", |
| 720 | pn, |
| 721 | (s->cli_addr.ss_family == AF_INET) ? |
| 722 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 723 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
| 724 | tm->tm_mday, monthname[tm->tm_mon], tm->tm_year+1900, |
| 725 | tm->tm_hour, tm->tm_min, tm->tm_sec, s->logs.tv_accept.tv_usec/1000, |
| 726 | fe->id, be->id, svid, |
| 727 | s->logs.t_request, |
| 728 | (s->logs.t_queue >= 0) ? s->logs.t_queue - s->logs.t_request : -1, |
| 729 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 730 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 731 | (tolog & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 732 | txn->status, |
| 733 | (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_in, |
| 734 | txn->cli_cookie ? txn->cli_cookie : "-", |
| 735 | txn->srv_cookie ? txn->srv_cookie : "-", |
| 736 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 737 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 738 | (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 739 | (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-', |
| 740 | actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0, |
| 741 | s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline); |
| 742 | |
| 743 | s->logs.logwait = 0; |
| 744 | } |
| 745 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 746 | |
| 747 | /* |
| 748 | * Capture headers from message starting at <som> according to header list |
| 749 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 750 | */ |
| 751 | void capture_headers(char *som, struct hdr_idx *idx, |
| 752 | char **cap, struct cap_hdr *cap_hdr) |
| 753 | { |
| 754 | char *eol, *sol, *col, *sov; |
| 755 | int cur_idx; |
| 756 | struct cap_hdr *h; |
| 757 | int len; |
| 758 | |
| 759 | sol = som + hdr_idx_first_pos(idx); |
| 760 | cur_idx = hdr_idx_first_idx(idx); |
| 761 | |
| 762 | while (cur_idx) { |
| 763 | eol = sol + idx->v[cur_idx].len; |
| 764 | |
| 765 | col = sol; |
| 766 | while (col < eol && *col != ':') |
| 767 | col++; |
| 768 | |
| 769 | sov = col + 1; |
| 770 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 771 | sov++; |
| 772 | |
| 773 | for (h = cap_hdr; h; h = h->next) { |
| 774 | if ((h->namelen == col - sol) && |
| 775 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 776 | if (cap[h->index] == NULL) |
| 777 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 778 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 779 | |
| 780 | if (cap[h->index] == NULL) { |
| 781 | Alert("HTTP capture : out of memory.\n"); |
| 782 | continue; |
| 783 | } |
| 784 | |
| 785 | len = eol - sov; |
| 786 | if (len > h->len) |
| 787 | len = h->len; |
| 788 | |
| 789 | memcpy(cap[h->index], sov, len); |
| 790 | cap[h->index][len]=0; |
| 791 | } |
| 792 | } |
| 793 | sol = eol + idx->v[cur_idx].cr + 1; |
| 794 | cur_idx = idx->v[cur_idx].next; |
| 795 | } |
| 796 | } |
| 797 | |
| 798 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 799 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 800 | */ |
| 801 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 802 | |
| 803 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 804 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 805 | */ |
| 806 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 807 | ptr++; \ |
| 808 | if (likely(ptr < end)) \ |
| 809 | goto good; \ |
| 810 | else { \ |
| 811 | state = (st); \ |
| 812 | goto http_msg_ood; \ |
| 813 | } \ |
| 814 | } while (0) |
| 815 | |
| 816 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 817 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 818 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 819 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 820 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 821 | * will give undefined results. |
| 822 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 823 | * and that msg->sol points to the beginning of the response. |
| 824 | * If a complete line is found (which implies that at least one CR or LF is |
| 825 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 826 | * returned indicating an incomplete line (which does not mean that parts have |
| 827 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 828 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 829 | * upon next call. |
| 830 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 831 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 832 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 833 | * 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] | 834 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 835 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 836 | 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] | 837 | const char *ptr, const char *end, |
| 838 | char **ret_ptr, int *ret_state) |
| 839 | { |
| 840 | __label__ |
| 841 | http_msg_rpver, |
| 842 | http_msg_rpver_sp, |
| 843 | http_msg_rpcode, |
| 844 | http_msg_rpcode_sp, |
| 845 | http_msg_rpreason, |
| 846 | http_msg_rpline_eol, |
| 847 | http_msg_ood, /* out of data */ |
| 848 | http_msg_invalid; |
| 849 | |
| 850 | switch (state) { |
| 851 | http_msg_rpver: |
| 852 | case HTTP_MSG_RPVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 853 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 854 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 855 | |
| 856 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 857 | msg->sl.st.v_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 858 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 859 | } |
| 860 | goto http_msg_invalid; |
| 861 | |
| 862 | http_msg_rpver_sp: |
| 863 | case HTTP_MSG_RPVER_SP: |
| 864 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 865 | msg->sl.st.c = ptr - msg_buf; |
| 866 | goto http_msg_rpcode; |
| 867 | } |
| 868 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 869 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 870 | /* so it's a CR/LF, this is invalid */ |
| 871 | goto http_msg_invalid; |
| 872 | |
| 873 | http_msg_rpcode: |
| 874 | case HTTP_MSG_RPCODE: |
| 875 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 876 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 877 | |
| 878 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 879 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 880 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 881 | } |
| 882 | |
| 883 | /* so it's a CR/LF, so there is no reason phrase */ |
| 884 | msg->sl.st.c_l = (ptr - msg_buf) - msg->sl.st.c; |
| 885 | http_msg_rsp_reason: |
| 886 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
| 887 | msg->sl.st.r = ptr - msg_buf; |
| 888 | msg->sl.st.r_l = 0; |
| 889 | goto http_msg_rpline_eol; |
| 890 | |
| 891 | http_msg_rpcode_sp: |
| 892 | case HTTP_MSG_RPCODE_SP: |
| 893 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 894 | msg->sl.st.r = ptr - msg_buf; |
| 895 | goto http_msg_rpreason; |
| 896 | } |
| 897 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 898 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 899 | /* so it's a CR/LF, so there is no reason phrase */ |
| 900 | goto http_msg_rsp_reason; |
| 901 | |
| 902 | http_msg_rpreason: |
| 903 | case HTTP_MSG_RPREASON: |
| 904 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 905 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
| 906 | msg->sl.st.r_l = (ptr - msg_buf) - msg->sl.st.r; |
| 907 | http_msg_rpline_eol: |
| 908 | /* We have seen the end of line. Note that we do not |
| 909 | * necessarily have the \n yet, but at least we know that we |
| 910 | * have EITHER \r OR \n, otherwise the response would not be |
| 911 | * complete. We can then record the response length and return |
| 912 | * to the caller which will be able to register it. |
| 913 | */ |
| 914 | msg->sl.st.l = ptr - msg->sol; |
| 915 | return ptr; |
| 916 | |
| 917 | #ifdef DEBUG_FULL |
| 918 | default: |
| 919 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 920 | exit(1); |
| 921 | #endif |
| 922 | } |
| 923 | |
| 924 | http_msg_ood: |
| 925 | /* out of data */ |
| 926 | if (ret_state) |
| 927 | *ret_state = state; |
| 928 | if (ret_ptr) |
| 929 | *ret_ptr = (char *)ptr; |
| 930 | return NULL; |
| 931 | |
| 932 | http_msg_invalid: |
| 933 | /* invalid message */ |
| 934 | if (ret_state) |
| 935 | *ret_state = HTTP_MSG_ERROR; |
| 936 | return NULL; |
| 937 | } |
| 938 | |
| 939 | |
| 940 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 941 | * This function parses a request line between <ptr> and <end>, starting with |
| 942 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 943 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 944 | * will give undefined results. |
| 945 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 946 | * and that msg->sol points to the beginning of the request. |
| 947 | * If a complete line is found (which implies that at least one CR or LF is |
| 948 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 949 | * returned indicating an incomplete line (which does not mean that parts have |
| 950 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 951 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 952 | * upon next call. |
| 953 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 954 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 955 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 956 | * 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] | 957 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 958 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 959 | 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] | 960 | const char *ptr, const char *end, |
| 961 | char **ret_ptr, int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 962 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 963 | __label__ |
| 964 | http_msg_rqmeth, |
| 965 | http_msg_rqmeth_sp, |
| 966 | http_msg_rquri, |
| 967 | http_msg_rquri_sp, |
| 968 | http_msg_rqver, |
| 969 | http_msg_rqline_eol, |
| 970 | http_msg_ood, /* out of data */ |
| 971 | http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 972 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 973 | switch (state) { |
| 974 | http_msg_rqmeth: |
| 975 | case HTTP_MSG_RQMETH: |
| 976 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 977 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 978 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 979 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 980 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 981 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 982 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 983 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 984 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 985 | /* HTTP 0.9 request */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 986 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 987 | http_msg_req09_uri: |
| 988 | msg->sl.rq.u = ptr - msg_buf; |
| 989 | http_msg_req09_uri_e: |
| 990 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 991 | http_msg_req09_ver: |
| 992 | msg->sl.rq.v = ptr - msg_buf; |
| 993 | msg->sl.rq.v_l = 0; |
| 994 | goto http_msg_rqline_eol; |
| 995 | } |
| 996 | goto http_msg_invalid; |
| 997 | |
| 998 | http_msg_rqmeth_sp: |
| 999 | case HTTP_MSG_RQMETH_SP: |
| 1000 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1001 | msg->sl.rq.u = ptr - msg_buf; |
| 1002 | goto http_msg_rquri; |
| 1003 | } |
| 1004 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1005 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1006 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1007 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1008 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1009 | http_msg_rquri: |
| 1010 | case HTTP_MSG_RQURI: |
| 1011 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1012 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1013 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1014 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1015 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->sl.rq.u; |
| 1016 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1017 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1018 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1019 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1020 | goto http_msg_req09_uri_e; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1021 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1022 | http_msg_rquri_sp: |
| 1023 | case HTTP_MSG_RQURI_SP: |
| 1024 | if (likely(!HTTP_IS_LWS(*ptr))) { |
| 1025 | msg->sl.rq.v = ptr - msg_buf; |
| 1026 | goto http_msg_rqver; |
| 1027 | } |
| 1028 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1029 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1030 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1031 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1032 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1033 | http_msg_rqver: |
| 1034 | case HTTP_MSG_RQVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1035 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1036 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1037 | |
| 1038 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1039 | msg->sl.rq.v_l = (ptr - msg_buf) - msg->sl.rq.v; |
| 1040 | http_msg_rqline_eol: |
| 1041 | /* We have seen the end of line. Note that we do not |
| 1042 | * necessarily have the \n yet, but at least we know that we |
| 1043 | * have EITHER \r OR \n, otherwise the request would not be |
| 1044 | * complete. We can then record the request length and return |
| 1045 | * to the caller which will be able to register it. |
| 1046 | */ |
| 1047 | msg->sl.rq.l = ptr - msg->sol; |
| 1048 | return ptr; |
| 1049 | } |
| 1050 | |
| 1051 | /* neither an HTTP_VER token nor a CRLF */ |
| 1052 | goto http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1053 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1054 | #ifdef DEBUG_FULL |
| 1055 | default: |
| 1056 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1057 | exit(1); |
| 1058 | #endif |
| 1059 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1060 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1061 | http_msg_ood: |
| 1062 | /* out of data */ |
| 1063 | if (ret_state) |
| 1064 | *ret_state = state; |
| 1065 | if (ret_ptr) |
| 1066 | *ret_ptr = (char *)ptr; |
| 1067 | return NULL; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1068 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1069 | http_msg_invalid: |
| 1070 | /* invalid message */ |
| 1071 | if (ret_state) |
| 1072 | *ret_state = HTTP_MSG_ERROR; |
| 1073 | return NULL; |
| 1074 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1075 | |
| 1076 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1077 | /* |
| 1078 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1079 | * depending on the initial msg->msg_state. It can be preempted everywhere |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1080 | * when data are missing and recalled at the exact same location with no |
| 1081 | * information loss. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1082 | * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other |
| 1083 | * fields. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1084 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1085 | void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx) |
| 1086 | { |
| 1087 | __label__ |
| 1088 | http_msg_rqbefore, |
| 1089 | http_msg_rqbefore_cr, |
| 1090 | http_msg_rqmeth, |
| 1091 | http_msg_rqline_end, |
| 1092 | http_msg_hdr_first, |
| 1093 | http_msg_hdr_name, |
| 1094 | http_msg_hdr_l1_sp, |
| 1095 | http_msg_hdr_l1_lf, |
| 1096 | http_msg_hdr_l1_lws, |
| 1097 | http_msg_hdr_val, |
| 1098 | http_msg_hdr_l2_lf, |
| 1099 | http_msg_hdr_l2_lws, |
| 1100 | http_msg_complete_header, |
| 1101 | http_msg_last_lf, |
| 1102 | http_msg_ood, /* out of data */ |
| 1103 | http_msg_invalid; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1104 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1105 | int state; /* updated only when leaving the FSM */ |
| 1106 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1107 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1108 | state = msg->msg_state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1109 | ptr = buf->lr; |
| 1110 | end = buf->r; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1111 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1112 | if (unlikely(ptr >= end)) |
| 1113 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1114 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1115 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1116 | /* |
| 1117 | * First, states that are specific to the response only. |
| 1118 | * We check them first so that request and headers are |
| 1119 | * closer to each other (accessed more often). |
| 1120 | */ |
| 1121 | http_msg_rpbefore: |
| 1122 | case HTTP_MSG_RPBEFORE: |
| 1123 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1124 | if (likely(ptr == buf->data)) { |
| 1125 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1126 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1127 | } else { |
| 1128 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1129 | /* only skip empty leading lines, don't remove them */ |
| 1130 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1131 | msg->som = ptr - buf->data; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1132 | #else |
| 1133 | /* Remove empty leading lines, as recommended by |
| 1134 | * RFC2616. This takes a lot of time because we |
| 1135 | * must move all the buffer backwards, but this |
| 1136 | * is rarely needed. The method above will be |
| 1137 | * cleaner when we'll be able to start sending |
| 1138 | * the request from any place in the buffer. |
| 1139 | */ |
| 1140 | buf->lr = ptr; |
| 1141 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1142 | msg->som = 0; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1143 | msg->sol = buf->data; |
| 1144 | ptr = buf->data; |
| 1145 | end = buf->r; |
| 1146 | #endif |
| 1147 | } |
| 1148 | hdr_idx_init(idx); |
| 1149 | state = HTTP_MSG_RPVER; |
| 1150 | goto http_msg_rpver; |
| 1151 | } |
| 1152 | |
| 1153 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1154 | goto http_msg_invalid; |
| 1155 | |
| 1156 | if (unlikely(*ptr == '\n')) |
| 1157 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1158 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1159 | /* stop here */ |
| 1160 | |
| 1161 | http_msg_rpbefore_cr: |
| 1162 | case HTTP_MSG_RPBEFORE_CR: |
| 1163 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1164 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1165 | /* stop here */ |
| 1166 | |
| 1167 | http_msg_rpver: |
| 1168 | case HTTP_MSG_RPVER: |
| 1169 | case HTTP_MSG_RPVER_SP: |
| 1170 | case HTTP_MSG_RPCODE: |
| 1171 | case HTTP_MSG_RPCODE_SP: |
| 1172 | case HTTP_MSG_RPREASON: |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1173 | ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1174 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1175 | if (unlikely(!ptr)) |
| 1176 | return; |
| 1177 | |
| 1178 | /* we have a full response and we know that we have either a CR |
| 1179 | * or an LF at <ptr>. |
| 1180 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1181 | //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] | 1182 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1183 | |
| 1184 | msg->sol = ptr; |
| 1185 | if (likely(*ptr == '\r')) |
| 1186 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1187 | goto http_msg_rpline_end; |
| 1188 | |
| 1189 | http_msg_rpline_end: |
| 1190 | case HTTP_MSG_RPLINE_END: |
| 1191 | /* msg->sol must point to the first of CR or LF. */ |
| 1192 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1193 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1194 | /* stop here */ |
| 1195 | |
| 1196 | /* |
| 1197 | * Second, states that are specific to the request only |
| 1198 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1199 | http_msg_rqbefore: |
| 1200 | case HTTP_MSG_RQBEFORE: |
| 1201 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1202 | if (likely(ptr == buf->data)) { |
| 1203 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1204 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1205 | } else { |
| 1206 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1207 | /* only skip empty leading lines, don't remove them */ |
| 1208 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1209 | msg->som = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1210 | #else |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1211 | /* Remove empty leading lines, as recommended by |
| 1212 | * RFC2616. This takes a lot of time because we |
| 1213 | * must move all the buffer backwards, but this |
| 1214 | * is rarely needed. The method above will be |
| 1215 | * cleaner when we'll be able to start sending |
| 1216 | * the request from any place in the buffer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1217 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1218 | buf->lr = ptr; |
| 1219 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1220 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1221 | msg->sol = buf->data; |
| 1222 | ptr = buf->data; |
| 1223 | end = buf->r; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1224 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1225 | } |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1226 | /* we will need this when keep-alive will be supported |
| 1227 | hdr_idx_init(idx); |
| 1228 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1229 | state = HTTP_MSG_RQMETH; |
| 1230 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1231 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1232 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1233 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1234 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1235 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1236 | if (unlikely(*ptr == '\n')) |
| 1237 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1238 | 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] | 1239 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1240 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1241 | http_msg_rqbefore_cr: |
| 1242 | case HTTP_MSG_RQBEFORE_CR: |
| 1243 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1244 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1245 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1246 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1247 | http_msg_rqmeth: |
| 1248 | case HTTP_MSG_RQMETH: |
| 1249 | case HTTP_MSG_RQMETH_SP: |
| 1250 | case HTTP_MSG_RQURI: |
| 1251 | case HTTP_MSG_RQURI_SP: |
| 1252 | case HTTP_MSG_RQVER: |
| 1253 | ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1254 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1255 | if (unlikely(!ptr)) |
| 1256 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1257 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1258 | /* we have a full request and we know that we have either a CR |
| 1259 | * or an LF at <ptr>. |
| 1260 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1261 | //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] | 1262 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1263 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1264 | msg->sol = ptr; |
| 1265 | if (likely(*ptr == '\r')) |
| 1266 | 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] | 1267 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1268 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1269 | http_msg_rqline_end: |
| 1270 | case HTTP_MSG_RQLINE_END: |
| 1271 | /* check for HTTP/0.9 request : no version information available. |
| 1272 | * msg->sol must point to the first of CR or LF. |
| 1273 | */ |
| 1274 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1275 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1276 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1277 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1278 | 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] | 1279 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1280 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1281 | /* |
| 1282 | * Common states below |
| 1283 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1284 | http_msg_hdr_first: |
| 1285 | case HTTP_MSG_HDR_FIRST: |
| 1286 | msg->sol = ptr; |
| 1287 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1288 | goto http_msg_hdr_name; |
| 1289 | } |
| 1290 | |
| 1291 | if (likely(*ptr == '\r')) |
| 1292 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1293 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1294 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1295 | http_msg_hdr_name: |
| 1296 | case HTTP_MSG_HDR_NAME: |
| 1297 | /* assumes msg->sol points to the first char */ |
| 1298 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1299 | 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] | 1300 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1301 | if (likely(*ptr == ':')) { |
| 1302 | msg->col = ptr - buf->data; |
| 1303 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| 1304 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1305 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1306 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1307 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1308 | http_msg_hdr_l1_sp: |
| 1309 | case HTTP_MSG_HDR_L1_SP: |
| 1310 | /* assumes msg->sol points to the first char and msg->col to the colon */ |
| 1311 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1312 | 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] | 1313 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1314 | /* header value can be basically anything except CR/LF */ |
| 1315 | msg->sov = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1316 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1317 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1318 | goto http_msg_hdr_val; |
| 1319 | } |
| 1320 | |
| 1321 | if (likely(*ptr == '\r')) |
| 1322 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1323 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1324 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1325 | http_msg_hdr_l1_lf: |
| 1326 | case HTTP_MSG_HDR_L1_LF: |
| 1327 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1328 | 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] | 1329 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1330 | http_msg_hdr_l1_lws: |
| 1331 | case HTTP_MSG_HDR_L1_LWS: |
| 1332 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1333 | /* replace HT,CR,LF with spaces */ |
| 1334 | for (; buf->data+msg->sov < ptr; msg->sov++) |
| 1335 | buf->data[msg->sov] = ' '; |
| 1336 | goto http_msg_hdr_l1_sp; |
| 1337 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1338 | /* we had a header consisting only in spaces ! */ |
| 1339 | msg->eol = buf->data + msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1340 | goto http_msg_complete_header; |
| 1341 | |
| 1342 | http_msg_hdr_val: |
| 1343 | case HTTP_MSG_HDR_VAL: |
| 1344 | /* assumes msg->sol points to the first char, msg->col to the |
| 1345 | * colon, and msg->sov points to the first character of the |
| 1346 | * value. |
| 1347 | */ |
| 1348 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1349 | 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] | 1350 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1351 | msg->eol = ptr; |
| 1352 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1353 | * real header end in case it ends with lots of LWS, but is this |
| 1354 | * really needed ? |
| 1355 | */ |
| 1356 | if (likely(*ptr == '\r')) |
| 1357 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1358 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1359 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1360 | http_msg_hdr_l2_lf: |
| 1361 | case HTTP_MSG_HDR_L2_LF: |
| 1362 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1363 | 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] | 1364 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1365 | http_msg_hdr_l2_lws: |
| 1366 | case HTTP_MSG_HDR_L2_LWS: |
| 1367 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1368 | /* LWS: replace HT,CR,LF with spaces */ |
| 1369 | for (; msg->eol < ptr; msg->eol++) |
| 1370 | *msg->eol = ' '; |
| 1371 | goto http_msg_hdr_val; |
| 1372 | } |
| 1373 | http_msg_complete_header: |
| 1374 | /* |
| 1375 | * It was a new header, so the last one is finished. |
| 1376 | * Assumes msg->sol points to the first char, msg->col to the |
| 1377 | * colon, msg->sov points to the first character of the value |
| 1378 | * and msg->eol to the first CR or LF so we know how the line |
| 1379 | * ends. We insert last header into the index. |
| 1380 | */ |
| 1381 | /* |
| 1382 | fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol); |
| 1383 | write(2, msg->sol, msg->eol-msg->sol); |
| 1384 | fprintf(stderr,"\n"); |
| 1385 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1386 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1387 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r', |
| 1388 | idx, idx->tail) < 0)) |
| 1389 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1390 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1391 | msg->sol = ptr; |
| 1392 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1393 | goto http_msg_hdr_name; |
| 1394 | } |
| 1395 | |
| 1396 | if (likely(*ptr == '\r')) |
| 1397 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1398 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1399 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1400 | http_msg_last_lf: |
| 1401 | case HTTP_MSG_LAST_LF: |
| 1402 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1403 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1404 | ptr++; |
| 1405 | buf->lr = ptr; |
| 1406 | msg->eoh = msg->sol - buf->data; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1407 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1408 | return; |
| 1409 | #ifdef DEBUG_FULL |
| 1410 | default: |
| 1411 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1412 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1413 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1414 | } |
| 1415 | http_msg_ood: |
| 1416 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1417 | msg->msg_state = state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1418 | buf->lr = ptr; |
| 1419 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1420 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1421 | http_msg_invalid: |
| 1422 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1423 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1424 | return; |
| 1425 | } |
| 1426 | |
| 1427 | /* |
| 1428 | * manages the client FSM and its socket. BTW, it also tries to handle the |
| 1429 | * cookie. It returns 1 if a state has changed (and a resync may be needed), |
| 1430 | * 0 else. |
| 1431 | */ |
| 1432 | int process_cli(struct session *t) |
| 1433 | { |
| 1434 | int s = t->srv_state; |
| 1435 | int c = t->cli_state; |
| 1436 | struct buffer *req = t->req; |
| 1437 | struct buffer *rep = t->rep; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1438 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1439 | DPRINTF(stderr,"process_cli: c=%s s=%s set(r,w)=%d,%d exp(r,w)=%d.%d,%d.%d\n", |
| 1440 | cli_stnames[c], srv_stnames[s], |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 1441 | 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] | 1442 | req->rex.tv_sec, req->rex.tv_usec, |
| 1443 | rep->wex.tv_sec, rep->wex.tv_usec); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1444 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1445 | if (c == CL_STHEADERS) { |
| 1446 | /* |
| 1447 | * Now parse the partial (or complete) lines. |
| 1448 | * We will check the request syntax, and also join multi-line |
| 1449 | * headers. An index of all the lines will be elaborated while |
| 1450 | * parsing. |
| 1451 | * |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1452 | * For the parsing, we use a 28 states FSM. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1453 | * |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1454 | * Here is the information we currently have : |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1455 | * req->data + req->som = beginning of request |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1456 | * req->data + req->eoh = end of processed headers / start of current one |
| 1457 | * req->data + req->eol = end of current header or line (LF or CRLF) |
| 1458 | * req->lr = first non-visited byte |
| 1459 | * req->r = end of data |
| 1460 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1461 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1462 | int cur_idx; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1463 | struct http_txn *txn = &t->txn; |
| 1464 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1465 | struct proxy *cur_proxy; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1466 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1467 | if (likely(req->lr < req->r)) |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1468 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1469 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1470 | /* 1: we might have to print this header in debug mode */ |
| 1471 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1472 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1473 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1474 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1475 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1476 | sol = req->data + msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1477 | eol = sol + msg->sl.rq.l; |
| 1478 | debug_hdr("clireq", t, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 1479 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1480 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 1481 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1482 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1483 | while (cur_idx) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1484 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1485 | debug_hdr("clihdr", t, sol, eol); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1486 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 1487 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1488 | } |
| 1489 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1490 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1491 | |
| 1492 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1493 | * Now we quickly check if we have found a full valid request. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1494 | * If not so, we check the FD and buffer states before leaving. |
| 1495 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1496 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 1497 | * requests are checked first. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1498 | * |
| 1499 | */ |
| 1500 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1501 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1502 | /* |
| 1503 | * First, let's catch bad requests. |
| 1504 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1505 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1506 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1507 | |
| 1508 | /* 1: Since we are in header mode, if there's no space |
| 1509 | * left for headers, we won't be able to free more |
| 1510 | * later, so the session will never terminate. We |
| 1511 | * must terminate it now. |
| 1512 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1513 | if (unlikely(req->l >= req->rlim - req->data)) { |
| 1514 | /* FIXME: check if URI is set and return Status |
| 1515 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1516 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1517 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | /* 2: have we encountered a read error or a close ? */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1521 | else if (unlikely(req->flags & (BF_READ_ERROR | BF_READ_NULL))) { |
| 1522 | /* read error, or last read : give up. */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1523 | tv_eternity(&req->rex); |
| 1524 | fd_delete(t->cli_fd); |
| 1525 | t->cli_state = CL_STCLOSE; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 1526 | t->fe->failed_req++; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1527 | if (!(t->flags & SN_ERR_MASK)) |
| 1528 | t->flags |= SN_ERR_CLICL; |
| 1529 | if (!(t->flags & SN_FINST_MASK)) |
| 1530 | t->flags |= SN_FINST_R; |
| 1531 | return 1; |
| 1532 | } |
| 1533 | |
| 1534 | /* 3: has the read timeout expired ? */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 1535 | else if (unlikely(tv_isle(&req->rex, &now))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1536 | /* read timeout : give up with an error message. */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1537 | txn->status = 408; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 1538 | client_retnclose(t, error_message(t, HTTP_ERR_408)); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 1539 | t->fe->failed_req++; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1540 | if (!(t->flags & SN_ERR_MASK)) |
| 1541 | t->flags |= SN_ERR_CLITO; |
| 1542 | if (!(t->flags & SN_FINST_MASK)) |
| 1543 | t->flags |= SN_FINST_R; |
| 1544 | return 1; |
| 1545 | } |
| 1546 | |
| 1547 | /* 4: do we need to re-enable the read socket ? */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 1548 | else if (unlikely(EV_FD_COND_S(t->cli_fd, DIR_RD))) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 1549 | /* fd in DIR_RD was disabled, perhaps because of a previous buffer |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1550 | * full. We cannot loop here since stream_sock_read will disable it only if |
| 1551 | * req->l == rlim-data |
| 1552 | */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 1553 | if (!tv_add_ifset(&req->rex, &now, &t->fe->clitimeout)) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1554 | tv_eternity(&req->rex); |
| 1555 | } |
| 1556 | return t->cli_state != CL_STHEADERS; |
| 1557 | } |
| 1558 | |
| 1559 | |
| 1560 | /**************************************************************** |
| 1561 | * More interesting part now : we know that we have a complete * |
| 1562 | * request which at least looks like HTTP. We have an indicator * |
| 1563 | * of each header's length, so we can parse them quickly. * |
| 1564 | ****************************************************************/ |
| 1565 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1566 | /* ensure we keep this pointer to the beginning of the message */ |
| 1567 | msg->sol = req->data + msg->som; |
| 1568 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1569 | /* |
| 1570 | * 1: identify the method |
| 1571 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1572 | 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] | 1573 | |
| 1574 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1575 | * 2: check if the URI matches the monitor_uri. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1576 | * We have to do this for every request which gets in, because |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1577 | * the monitor-uri is defined by the frontend. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1578 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1579 | if (unlikely((t->fe->monitor_uri_len != 0) && |
| 1580 | (t->fe->monitor_uri_len == msg->sl.rq.u_l) && |
| 1581 | !memcmp(&req->data[msg->sl.rq.u], |
| 1582 | t->fe->monitor_uri, |
| 1583 | t->fe->monitor_uri_len))) { |
| 1584 | /* |
| 1585 | * We have found the monitor URI |
| 1586 | */ |
| 1587 | t->flags |= SN_MONITOR; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1588 | txn->status = 200; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1589 | client_retnclose(t, &http_200_chunk); |
| 1590 | goto return_prx_cond; |
| 1591 | } |
| 1592 | |
| 1593 | /* |
| 1594 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 1595 | * Note: we cannot log anymore if the request has been |
| 1596 | * classified as invalid. |
| 1597 | */ |
| 1598 | if (unlikely(t->logs.logwait & LW_REQ)) { |
| 1599 | /* we have a complete HTTP request that we must log */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 1600 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1601 | int urilen = msg->sl.rq.l; |
| 1602 | |
| 1603 | if (urilen >= REQURI_LEN) |
| 1604 | urilen = REQURI_LEN - 1; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1605 | memcpy(txn->uri, &req->data[msg->som], urilen); |
| 1606 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1607 | |
| 1608 | if (!(t->logs.logwait &= ~LW_REQ)) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1609 | http_sess_log(t); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1610 | } else { |
| 1611 | Alert("HTTP logging : out of memory.\n"); |
| 1612 | } |
| 1613 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1614 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1615 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1616 | /* 4. We may have to convert HTTP/0.9 requests to HTTP/1.0 */ |
| 1617 | if (unlikely(msg->sl.rq.v_l == 0)) { |
| 1618 | int delta; |
| 1619 | char *cur_end; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1620 | msg->sol = req->data + msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1621 | cur_end = msg->sol + msg->sl.rq.l; |
| 1622 | delta = 0; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1623 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1624 | if (msg->sl.rq.u_l == 0) { |
| 1625 | /* if no URI was set, add "/" */ |
| 1626 | delta = buffer_replace2(req, cur_end, cur_end, " /", 2); |
| 1627 | cur_end += delta; |
| 1628 | msg->eoh += delta; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1629 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1630 | /* add HTTP version */ |
| 1631 | delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11); |
| 1632 | msg->eoh += delta; |
| 1633 | cur_end += delta; |
| 1634 | cur_end = (char *)http_parse_reqline(msg, req->data, |
| 1635 | HTTP_MSG_RQMETH, |
| 1636 | msg->sol, cur_end + 1, |
| 1637 | NULL, NULL); |
| 1638 | if (unlikely(!cur_end)) |
| 1639 | goto return_bad_req; |
| 1640 | |
| 1641 | /* we have a full HTTP/1.0 request now and we know that |
| 1642 | * we have either a CR or an LF at <ptr>. |
| 1643 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1644 | 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] | 1645 | } |
| 1646 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1647 | |
| 1648 | /* 5: we may need to capture headers */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1649 | if (unlikely((t->logs.logwait & LW_REQHDR) && t->fe->req_cap)) |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1650 | capture_headers(req->data + msg->som, &txn->hdr_idx, |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1651 | txn->req.cap, t->fe->req_cap); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1652 | |
| 1653 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1654 | * 6: we will have to evaluate the filters. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1655 | * As opposed to version 1.2, now they will be evaluated in the |
| 1656 | * filters order and not in the header order. This means that |
| 1657 | * each filter has to be validated among all headers. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1658 | * |
| 1659 | * We can now check whether we want to switch to another |
| 1660 | * backend, in which case we will re-check the backend's |
| 1661 | * filters and various options. In order to support 3-level |
| 1662 | * switching, here's how we should proceed : |
| 1663 | * |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1664 | * a) run be. |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1665 | * if (switch) then switch ->be to the new backend. |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1666 | * b) run be if (be != fe). |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1667 | * There cannot be any switch from there, so ->be cannot be |
| 1668 | * changed anymore. |
| 1669 | * |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1670 | * => filters always apply to ->be, then ->be may change. |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1671 | * |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1672 | * The response path will be able to apply either ->be, or |
| 1673 | * ->be then ->fe filters in order to match the reverse of |
| 1674 | * the forward sequence. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1675 | */ |
| 1676 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1677 | do { |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 1678 | struct acl_cond *cond; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1679 | struct proxy *rule_set = t->be; |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1680 | cur_proxy = t->be; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1681 | |
Willy Tarreau | 5c8e3e0 | 2007-05-07 00:58:25 +0200 | [diff] [blame] | 1682 | /* first check whether we have some ACLs set to block this request */ |
| 1683 | list_for_each_entry(cond, &cur_proxy->block_cond, list) { |
| 1684 | int ret = acl_exec_cond(cond, cur_proxy, t, txn); |
| 1685 | if (cond->pol == ACL_COND_UNLESS) |
| 1686 | ret = !ret; |
| 1687 | |
| 1688 | if (ret) { |
| 1689 | txn->status = 403; |
| 1690 | /* let's log the request time */ |
| 1691 | t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now); |
| 1692 | client_retnclose(t, error_message(t, HTTP_ERR_403)); |
| 1693 | goto return_prx_cond; |
| 1694 | } |
| 1695 | } |
| 1696 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1697 | /* try headers filters */ |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 1698 | if (rule_set->req_exp != NULL) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1699 | if (apply_filters_to_request(t, req, rule_set->req_exp) < 0) |
| 1700 | goto return_bad_req; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 1701 | } |
| 1702 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 1703 | if (!(t->flags & SN_BE_ASSIGNED) && (t->be != cur_proxy)) { |
| 1704 | /* to ensure correct connection accounting on |
| 1705 | * the backend, we count the connection for the |
| 1706 | * one managing the queue. |
| 1707 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1708 | t->be->beconn++; |
| 1709 | if (t->be->beconn > t->be->beconn_max) |
| 1710 | t->be->beconn_max = t->be->beconn; |
| 1711 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 1712 | t->flags |= SN_BE_ASSIGNED; |
| 1713 | } |
| 1714 | |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1715 | /* has the request been denied ? */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 1716 | if (txn->flags & TX_CLDENY) { |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1717 | /* no need to go further */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1718 | txn->status = 403; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1719 | /* let's log the request time */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 1720 | t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 1721 | client_retnclose(t, error_message(t, HTTP_ERR_403)); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1722 | goto return_prx_cond; |
| 1723 | } |
| 1724 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1725 | /* We might have to check for "Connection:" */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1726 | if (((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1727 | !(t->flags & SN_CONN_CLOSED)) { |
| 1728 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1729 | int cur_idx, old_idx, delta, val; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1730 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1731 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1732 | 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] | 1733 | old_idx = 0; |
| 1734 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1735 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 1736 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1737 | cur_ptr = cur_next; |
| 1738 | cur_end = cur_ptr + cur_hdr->len; |
| 1739 | cur_next = cur_end + cur_hdr->cr + 1; |
| 1740 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1741 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 1742 | if (val) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1743 | /* 3 possibilities : |
| 1744 | * - we have already set Connection: close, |
| 1745 | * so we remove this line. |
| 1746 | * - we have not yet set Connection: close, |
| 1747 | * but this line indicates close. We leave |
| 1748 | * it untouched and set the flag. |
| 1749 | * - we have not yet set Connection: close, |
| 1750 | * and this line indicates non-close. We |
| 1751 | * replace it. |
| 1752 | */ |
| 1753 | if (t->flags & SN_CONN_CLOSED) { |
| 1754 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1755 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1756 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1757 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 1758 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1759 | cur_hdr->len = 0; |
| 1760 | } else { |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1761 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 1762 | delta = buffer_replace2(req, cur_ptr + val, cur_end, |
| 1763 | "close", 5); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1764 | cur_next += delta; |
| 1765 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1766 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1767 | } |
| 1768 | t->flags |= SN_CONN_CLOSED; |
| 1769 | } |
| 1770 | } |
| 1771 | old_idx = cur_idx; |
| 1772 | } |
Willy Tarreau | f2f0ee8 | 2007-03-30 12:02:43 +0200 | [diff] [blame] | 1773 | } |
| 1774 | /* add request headers from the rule sets in the same order */ |
| 1775 | for (cur_idx = 0; cur_idx < rule_set->nb_reqadd; cur_idx++) { |
| 1776 | if (unlikely(http_header_add_tail(req, |
| 1777 | &txn->req, |
| 1778 | &txn->hdr_idx, |
| 1779 | rule_set->req_add[cur_idx])) < 0) |
| 1780 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1781 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 1782 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1783 | /* check if stats URI was requested, and if an auth is needed */ |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 1784 | if (rule_set->uri_auth != NULL && |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1785 | (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) { |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 1786 | /* we have to check the URI and auth for this request */ |
| 1787 | if (stats_check_uri_auth(t, rule_set)) |
| 1788 | return 1; |
| 1789 | } |
| 1790 | |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 1791 | if (!(t->flags & SN_BE_ASSIGNED) && cur_proxy->defbe.be) { |
| 1792 | /* No backend was set, but there was a default |
| 1793 | * backend set in the frontend, so we use it and |
| 1794 | * loop again. |
| 1795 | */ |
| 1796 | t->be = cur_proxy->defbe.be; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1797 | t->be->beconn++; |
| 1798 | if (t->be->beconn > t->be->beconn_max) |
| 1799 | t->be->beconn_max = t->be->beconn; |
| 1800 | t->be->cum_beconn++; |
Willy Tarreau | 5fdfb91 | 2007-01-01 23:11:07 +0100 | [diff] [blame] | 1801 | t->flags |= SN_BE_ASSIGNED; |
| 1802 | } |
| 1803 | } 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] | 1804 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1805 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 1806 | if (!(t->flags & SN_BE_ASSIGNED)) { |
| 1807 | /* To ensure correct connection accounting on |
| 1808 | * the backend, we count the connection for the |
| 1809 | * one managing the queue. |
| 1810 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1811 | t->be->beconn++; |
| 1812 | if (t->be->beconn > t->be->beconn_max) |
| 1813 | t->be->beconn_max = t->be->beconn; |
| 1814 | t->be->cum_beconn++; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 1815 | t->flags |= SN_BE_ASSIGNED; |
| 1816 | } |
| 1817 | |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1818 | /* |
| 1819 | * Right now, we know that we have processed the entire headers |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1820 | * and that unwanted requests have been filtered out. We can do |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1821 | * whatever we want with the remaining request. Also, now we |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 1822 | * may have separate values for ->fe, ->be. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1823 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1824 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1825 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1826 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1827 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1828 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1829 | * 7: the appsession cookie was looked up very early in 1.2, |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1830 | * so let's do the same now. |
| 1831 | */ |
| 1832 | |
| 1833 | /* It needs to look into the URI */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1834 | if (t->be->appsession_name) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1835 | 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] | 1836 | } |
| 1837 | |
| 1838 | |
| 1839 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1840 | * 8: Now we can work with the cookies. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1841 | * Note that doing so might move headers in the request, but |
| 1842 | * the fields will stay coherent and the URI will not move. |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1843 | * This should only be performed in the backend. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1844 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 1845 | if (!(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1846 | manage_client_side_cookies(t, req); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1847 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1848 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1849 | /* |
Willy Tarreau | bb046ac | 2007-03-03 19:17:03 +0100 | [diff] [blame] | 1850 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 1851 | * asks for it. |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1852 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1853 | if ((t->fe->options | t->be->options) & PR_O_FWDFOR) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1854 | if (t->cli_addr.ss_family == AF_INET) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 1855 | /* Add an X-Forwarded-For header unless the source IP is |
| 1856 | * in the 'except' network range. |
| 1857 | */ |
| 1858 | if ((!t->fe->except_mask.s_addr || |
| 1859 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->fe->except_mask.s_addr) |
| 1860 | != t->fe->except_net.s_addr) && |
| 1861 | (!t->be->except_mask.s_addr || |
| 1862 | (((struct sockaddr_in *)&t->cli_addr)->sin_addr.s_addr & t->be->except_mask.s_addr) |
| 1863 | != t->be->except_net.s_addr)) { |
| 1864 | int len; |
| 1865 | unsigned char *pn; |
| 1866 | pn = (unsigned char *)&((struct sockaddr_in *)&t->cli_addr)->sin_addr; |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 1867 | |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 1868 | len = sprintf(trash, "X-Forwarded-For: %d.%d.%d.%d", |
| 1869 | pn[0], pn[1], pn[2], pn[3]); |
| 1870 | |
| 1871 | if (unlikely(http_header_add_tail2(req, &txn->req, |
| 1872 | &txn->hdr_idx, trash, len)) < 0) |
| 1873 | goto return_bad_req; |
| 1874 | } |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1875 | } |
| 1876 | else if (t->cli_addr.ss_family == AF_INET6) { |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 1877 | /* FIXME: for the sake of completeness, we should also support |
| 1878 | * 'except' here, although it is mostly useless in this case. |
| 1879 | */ |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1880 | int len; |
| 1881 | char pn[INET6_ADDRSTRLEN]; |
| 1882 | inet_ntop(AF_INET6, |
| 1883 | (const void *)&((struct sockaddr_in6 *)(&t->cli_addr))->sin6_addr, |
| 1884 | pn, sizeof(pn)); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 1885 | len = sprintf(trash, "X-Forwarded-For: %s", pn); |
| 1886 | if (unlikely(http_header_add_tail2(req, &txn->req, |
| 1887 | &txn->hdr_idx, trash, len)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1888 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1889 | } |
| 1890 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1891 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1892 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1893 | * 10: add "Connection: close" if needed and not yet set. |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 1894 | * 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] | 1895 | */ |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 1896 | if (!(t->flags & SN_CONN_CLOSED) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 1897 | ((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE)) { |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 1898 | if ((unlikely(msg->sl.rq.v_l != 8) || |
| 1899 | unlikely(req->data[msg->som + msg->sl.rq.v + 7] != '0')) && |
| 1900 | unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx, |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 1901 | "Connection: close", 17)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1902 | goto return_bad_req; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1903 | t->flags |= SN_CONN_CLOSED; |
Willy Tarreau | e15d913 | 2006-12-14 22:26:42 +0100 | [diff] [blame] | 1904 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1905 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1906 | /************************************************************* |
| 1907 | * OK, that's finished for the headers. We have done what we * |
| 1908 | * could. Let's switch to the DATA state. * |
| 1909 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1910 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1911 | t->cli_state = CL_STDATA; |
| 1912 | req->rlim = req->data + BUFSIZE; /* no more rewrite needed */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1913 | |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 1914 | t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1915 | |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 1916 | if (!tv_isset(&t->fe->clitimeout) || |
| 1917 | (t->srv_state < SV_STDATA && tv_isset(&t->be->srvtimeout))) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1918 | /* If the client has no timeout, or if the server is not ready yet, |
| 1919 | * and we know for sure that it can expire, then it's cleaner to |
| 1920 | * disable the timeout on the client side so that too low values |
| 1921 | * cannot make the sessions abort too early. |
| 1922 | * |
| 1923 | * FIXME-20050705: the server needs a way to re-enable this time-out |
| 1924 | * when it switches its state, otherwise a client can stay connected |
| 1925 | * indefinitely. This now seems to be OK. |
| 1926 | */ |
| 1927 | tv_eternity(&req->rex); |
| 1928 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1929 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1930 | /* When a connection is tarpitted, we use the queue timeout for the |
| 1931 | * tarpit delay, which currently happens to be the server's connect |
| 1932 | * timeout. If unset, then set it to zero because we really want it |
| 1933 | * to expire at one moment. |
| 1934 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 1935 | if (txn->flags & TX_CLTARPIT) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1936 | t->req->l = 0; |
| 1937 | /* flush the request so that we can drop the connection early |
| 1938 | * if the client closes first. |
| 1939 | */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 1940 | if (!tv_add_ifset(&req->cex, &now, &t->be->contimeout)) |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 1941 | req->cex = now; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 1942 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1943 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1944 | /* OK let's go on with the BODY now */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1945 | goto process_data; |
| 1946 | |
| 1947 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 1948 | txn->req.msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 1949 | txn->status = 400; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 1950 | client_retnclose(t, error_message(t, HTTP_ERR_400)); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 1951 | t->fe->failed_req++; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1952 | return_prx_cond: |
| 1953 | if (!(t->flags & SN_ERR_MASK)) |
| 1954 | t->flags |= SN_ERR_PRXCOND; |
| 1955 | if (!(t->flags & SN_FINST_MASK)) |
| 1956 | t->flags |= SN_FINST_R; |
| 1957 | return 1; |
| 1958 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1959 | } |
| 1960 | else if (c == CL_STDATA) { |
| 1961 | process_data: |
| 1962 | /* FIXME: this error handling is partly buggy because we always report |
| 1963 | * a 'DATA' phase while we don't know if the server was in IDLE, CONN |
| 1964 | * or HEADER phase. BTW, it's not logical to expire the client while |
| 1965 | * we're waiting for the server to connect. |
| 1966 | */ |
| 1967 | /* read or write error */ |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 1968 | if (rep->flags & BF_WRITE_ERROR || req->flags & BF_READ_ERROR) { |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 1969 | tv_eternity(&req->rex); |
| 1970 | tv_eternity(&rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1971 | fd_delete(t->cli_fd); |
| 1972 | t->cli_state = CL_STCLOSE; |
| 1973 | if (!(t->flags & SN_ERR_MASK)) |
| 1974 | t->flags |= SN_ERR_CLICL; |
| 1975 | if (!(t->flags & SN_FINST_MASK)) { |
| 1976 | if (t->pend_pos) |
| 1977 | t->flags |= SN_FINST_Q; |
| 1978 | else if (s == SV_STCONN) |
| 1979 | t->flags |= SN_FINST_C; |
| 1980 | else |
| 1981 | t->flags |= SN_FINST_D; |
| 1982 | } |
| 1983 | return 1; |
| 1984 | } |
| 1985 | /* last read, or end of server write */ |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 1986 | 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] | 1987 | EV_FD_CLR(t->cli_fd, DIR_RD); |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 1988 | tv_eternity(&req->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1989 | t->cli_state = CL_STSHUTR; |
| 1990 | return 1; |
| 1991 | } |
| 1992 | /* last server read and buffer empty */ |
| 1993 | else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 1994 | EV_FD_CLR(t->cli_fd, DIR_WR); |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 1995 | tv_eternity(&rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1996 | shutdown(t->cli_fd, SHUT_WR); |
| 1997 | /* We must ensure that the read part is still alive when switching |
| 1998 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 1999 | EV_FD_SET(t->cli_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2000 | tv_add_ifset(&req->rex, &now, &t->fe->clitimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2001 | t->cli_state = CL_STSHUTW; |
| 2002 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 2003 | return 1; |
| 2004 | } |
| 2005 | /* read timeout */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2006 | else if (tv_isle(&req->rex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2007 | EV_FD_CLR(t->cli_fd, DIR_RD); |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2008 | tv_eternity(&req->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2009 | t->cli_state = CL_STSHUTR; |
| 2010 | if (!(t->flags & SN_ERR_MASK)) |
| 2011 | t->flags |= SN_ERR_CLITO; |
| 2012 | if (!(t->flags & SN_FINST_MASK)) { |
| 2013 | if (t->pend_pos) |
| 2014 | t->flags |= SN_FINST_Q; |
| 2015 | else if (s == SV_STCONN) |
| 2016 | t->flags |= SN_FINST_C; |
| 2017 | else |
| 2018 | t->flags |= SN_FINST_D; |
| 2019 | } |
| 2020 | return 1; |
| 2021 | } |
| 2022 | /* write timeout */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2023 | else if (tv_isle(&rep->wex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2024 | EV_FD_CLR(t->cli_fd, DIR_WR); |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2025 | tv_eternity(&rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2026 | shutdown(t->cli_fd, SHUT_WR); |
| 2027 | /* We must ensure that the read part is still alive when switching |
| 2028 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2029 | EV_FD_SET(t->cli_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2030 | tv_add_ifset(&req->rex, &now, &t->fe->clitimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2031 | |
| 2032 | t->cli_state = CL_STSHUTW; |
| 2033 | if (!(t->flags & SN_ERR_MASK)) |
| 2034 | t->flags |= SN_ERR_CLITO; |
| 2035 | if (!(t->flags & SN_FINST_MASK)) { |
| 2036 | if (t->pend_pos) |
| 2037 | t->flags |= SN_FINST_Q; |
| 2038 | else if (s == SV_STCONN) |
| 2039 | t->flags |= SN_FINST_C; |
| 2040 | else |
| 2041 | t->flags |= SN_FINST_D; |
| 2042 | } |
| 2043 | return 1; |
| 2044 | } |
| 2045 | |
| 2046 | if (req->l >= req->rlim - req->data) { |
| 2047 | /* no room to read more data */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2048 | if (EV_FD_COND_C(t->cli_fd, DIR_RD)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2049 | /* stop reading until we get some space */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2050 | tv_eternity(&req->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2051 | } |
| 2052 | } else { |
| 2053 | /* there's still some space in the buffer */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2054 | if (EV_FD_COND_S(t->cli_fd, DIR_RD)) { |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 2055 | if (!tv_isset(&t->fe->clitimeout) || |
| 2056 | (t->srv_state < SV_STDATA && tv_isset(&t->be->srvtimeout))) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2057 | /* If the client has no timeout, or if the server not ready yet, and we |
| 2058 | * know for sure that it can expire, then it's cleaner to disable the |
| 2059 | * timeout on the client side so that too low values cannot make the |
| 2060 | * sessions abort too early. |
| 2061 | */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2062 | tv_eternity(&req->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2063 | else |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 2064 | tv_add(&req->rex, &now, &t->fe->clitimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2065 | } |
| 2066 | } |
| 2067 | |
| 2068 | if ((rep->l == 0) || |
| 2069 | ((s < SV_STDATA) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2070 | if (EV_FD_COND_C(t->cli_fd, DIR_WR)) { |
| 2071 | /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2072 | tv_eternity(&rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2073 | } |
| 2074 | } else { |
| 2075 | /* buffer not empty */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2076 | if (EV_FD_COND_S(t->cli_fd, DIR_WR)) { |
| 2077 | /* restart writing */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2078 | if (tv_add_ifset(&rep->wex, &now, &t->fe->clitimeout)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2079 | /* FIXME: to prevent the client from expiring read timeouts during writes, |
| 2080 | * we refresh it. */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2081 | req->rex = rep->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2082 | } |
| 2083 | else |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2084 | tv_eternity(&rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2085 | } |
| 2086 | } |
| 2087 | return 0; /* other cases change nothing */ |
| 2088 | } |
| 2089 | else if (c == CL_STSHUTR) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2090 | if (rep->flags & BF_WRITE_ERROR) { |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2091 | tv_eternity(&rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2092 | fd_delete(t->cli_fd); |
| 2093 | t->cli_state = CL_STCLOSE; |
| 2094 | if (!(t->flags & SN_ERR_MASK)) |
| 2095 | t->flags |= SN_ERR_CLICL; |
| 2096 | if (!(t->flags & SN_FINST_MASK)) { |
| 2097 | if (t->pend_pos) |
| 2098 | t->flags |= SN_FINST_Q; |
| 2099 | else if (s == SV_STCONN) |
| 2100 | t->flags |= SN_FINST_C; |
| 2101 | else |
| 2102 | t->flags |= SN_FINST_D; |
| 2103 | } |
| 2104 | return 1; |
| 2105 | } |
| 2106 | else if ((s == SV_STSHUTR || s == SV_STCLOSE) && (rep->l == 0) |
| 2107 | && !(t->flags & SN_SELF_GEN)) { |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2108 | tv_eternity(&rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2109 | fd_delete(t->cli_fd); |
| 2110 | t->cli_state = CL_STCLOSE; |
| 2111 | return 1; |
| 2112 | } |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2113 | else if (tv_isle(&rep->wex, &now)) { |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2114 | tv_eternity(&rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2115 | fd_delete(t->cli_fd); |
| 2116 | t->cli_state = CL_STCLOSE; |
| 2117 | if (!(t->flags & SN_ERR_MASK)) |
| 2118 | t->flags |= SN_ERR_CLITO; |
| 2119 | if (!(t->flags & SN_FINST_MASK)) { |
| 2120 | if (t->pend_pos) |
| 2121 | t->flags |= SN_FINST_Q; |
| 2122 | else if (s == SV_STCONN) |
| 2123 | t->flags |= SN_FINST_C; |
| 2124 | else |
| 2125 | t->flags |= SN_FINST_D; |
| 2126 | } |
| 2127 | return 1; |
| 2128 | } |
| 2129 | |
| 2130 | if (t->flags & SN_SELF_GEN) { |
| 2131 | produce_content(t); |
| 2132 | if (rep->l == 0) { |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2133 | tv_eternity(&rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2134 | fd_delete(t->cli_fd); |
| 2135 | t->cli_state = CL_STCLOSE; |
| 2136 | return 1; |
| 2137 | } |
| 2138 | } |
| 2139 | |
| 2140 | if ((rep->l == 0) |
| 2141 | || ((s == SV_STHEADERS) /* FIXME: this may be optimized && (rep->w == rep->h)*/)) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2142 | if (EV_FD_COND_C(t->cli_fd, DIR_WR)) { |
| 2143 | /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2144 | tv_eternity(&rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2145 | } |
| 2146 | } else { |
| 2147 | /* buffer not empty */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2148 | if (EV_FD_COND_S(t->cli_fd, DIR_WR)) { |
| 2149 | /* restart writing */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2150 | if (tv_add_ifset(&rep->wex, &now, &t->fe->clitimeout)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2151 | /* FIXME: to prevent the client from expiring read timeouts during writes, |
| 2152 | * we refresh it. */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2153 | req->rex = rep->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2154 | } |
| 2155 | else |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2156 | tv_eternity(&rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2157 | } |
| 2158 | } |
| 2159 | return 0; |
| 2160 | } |
| 2161 | else if (c == CL_STSHUTW) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2162 | if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2163 | tv_eternity(&req->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2164 | fd_delete(t->cli_fd); |
| 2165 | t->cli_state = CL_STCLOSE; |
| 2166 | if (!(t->flags & SN_ERR_MASK)) |
| 2167 | t->flags |= SN_ERR_CLICL; |
| 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 | } |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2178 | else if (req->flags & BF_READ_NULL || s == SV_STSHUTW || s == SV_STCLOSE) { |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2179 | tv_eternity(&req->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2180 | fd_delete(t->cli_fd); |
| 2181 | t->cli_state = CL_STCLOSE; |
| 2182 | return 1; |
| 2183 | } |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2184 | else if (tv_isle(&req->rex, &now)) { |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2185 | tv_eternity(&req->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2186 | fd_delete(t->cli_fd); |
| 2187 | t->cli_state = CL_STCLOSE; |
| 2188 | if (!(t->flags & SN_ERR_MASK)) |
| 2189 | t->flags |= SN_ERR_CLITO; |
| 2190 | if (!(t->flags & SN_FINST_MASK)) { |
| 2191 | if (t->pend_pos) |
| 2192 | t->flags |= SN_FINST_Q; |
| 2193 | else if (s == SV_STCONN) |
| 2194 | t->flags |= SN_FINST_C; |
| 2195 | else |
| 2196 | t->flags |= SN_FINST_D; |
| 2197 | } |
| 2198 | return 1; |
| 2199 | } |
| 2200 | else if (req->l >= req->rlim - req->data) { |
| 2201 | /* no room to read more data */ |
| 2202 | |
| 2203 | /* FIXME-20050705: is it possible for a client to maintain a session |
| 2204 | * after the timeout by sending more data after it receives a close ? |
| 2205 | */ |
| 2206 | |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2207 | if (EV_FD_COND_C(t->cli_fd, DIR_RD)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2208 | /* stop reading until we get some space */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2209 | tv_eternity(&req->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2210 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 2211 | } |
| 2212 | } else { |
| 2213 | /* there's still some space in the buffer */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2214 | if (EV_FD_COND_S(t->cli_fd, DIR_RD)) { |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2215 | if (!tv_add_ifset(&req->rex, &now, &t->fe->clitimeout)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2216 | tv_eternity(&req->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2217 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 2218 | } |
| 2219 | } |
| 2220 | return 0; |
| 2221 | } |
| 2222 | else { /* CL_STCLOSE: nothing to do */ |
| 2223 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 2224 | int len; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2225 | 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] | 2226 | write(1, trash, len); |
| 2227 | } |
| 2228 | return 0; |
| 2229 | } |
| 2230 | return 0; |
| 2231 | } |
| 2232 | |
| 2233 | |
| 2234 | /* |
| 2235 | * manages the server FSM and its socket. It returns 1 if a state has changed |
| 2236 | * (and a resync may be needed), 0 else. |
| 2237 | */ |
| 2238 | int process_srv(struct session *t) |
| 2239 | { |
| 2240 | int s = t->srv_state; |
| 2241 | int c = t->cli_state; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2242 | struct http_txn *txn = &t->txn; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2243 | struct buffer *req = t->req; |
| 2244 | struct buffer *rep = t->rep; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2245 | int conn_err; |
| 2246 | |
| 2247 | #ifdef DEBUG_FULL |
| 2248 | fprintf(stderr,"process_srv: c=%s, s=%s\n", cli_stnames[c], srv_stnames[s]); |
| 2249 | #endif |
Willy Tarreau | ee99136 | 2007-05-14 14:37:50 +0200 | [diff] [blame] | 2250 | |
| 2251 | #if 0 |
| 2252 | fprintf(stderr,"%s:%d fe->clito=%d.%d, fe->conto=%d.%d, fe->srvto=%d.%d\n", |
| 2253 | __FUNCTION__, __LINE__, |
| 2254 | t->fe->clitimeout.tv_sec, t->fe->clitimeout.tv_usec, |
| 2255 | t->fe->contimeout.tv_sec, t->fe->contimeout.tv_usec, |
| 2256 | t->fe->srvtimeout.tv_sec, t->fe->srvtimeout.tv_usec); |
| 2257 | fprintf(stderr,"%s:%d be->clito=%d.%d, be->conto=%d.%d, be->srvto=%d.%d\n", |
| 2258 | __FUNCTION__, __LINE__, |
| 2259 | t->be->clitimeout.tv_sec, t->be->clitimeout.tv_usec, |
| 2260 | t->be->contimeout.tv_sec, t->be->contimeout.tv_usec, |
| 2261 | t->be->srvtimeout.tv_sec, t->be->srvtimeout.tv_usec); |
| 2262 | |
| 2263 | fprintf(stderr,"%s:%d req->cto=%d.%d, req->rto=%d.%d, req->wto=%d.%d\n", |
| 2264 | __FUNCTION__, __LINE__, |
| 2265 | req->cto.tv_sec, req->cto.tv_usec, |
| 2266 | req->rto.tv_sec, req->rto.tv_usec, |
| 2267 | req->wto.tv_sec, req->wto.tv_usec); |
| 2268 | |
| 2269 | fprintf(stderr,"%s:%d rep->cto=%d.%d, rep->rto=%d.%d, rep->wto=%d.%d\n", |
| 2270 | __FUNCTION__, __LINE__, |
| 2271 | rep->cto.tv_sec, rep->cto.tv_usec, |
| 2272 | rep->rto.tv_sec, rep->rto.tv_usec, |
| 2273 | rep->wto.tv_sec, rep->wto.tv_usec); |
| 2274 | #endif |
| 2275 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2276 | //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] | 2277 | //EV_FD_ISSET(t->cli_fd, DIR_RD), EV_FD_ISSET(t->cli_fd, DIR_WR), |
| 2278 | //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] | 2279 | //); |
| 2280 | if (s == SV_STIDLE) { |
| 2281 | if (c == CL_STHEADERS) |
| 2282 | return 0; /* stay in idle, waiting for data to reach the client side */ |
| 2283 | else if (c == CL_STCLOSE || c == CL_STSHUTW || |
| 2284 | (c == CL_STSHUTR && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2285 | (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] | 2286 | tv_eternity(&req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2287 | if (t->pend_pos) |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2288 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2289 | /* note that this must not return any error because it would be able to |
| 2290 | * overwrite the client_retnclose() output. |
| 2291 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2292 | if (txn->flags & TX_CLTARPIT) |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 2293 | 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] | 2294 | else |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 2295 | 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] | 2296 | |
| 2297 | return 1; |
| 2298 | } |
| 2299 | else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2300 | if (txn->flags & TX_CLTARPIT) { |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 2301 | /* This connection is being tarpitted. The CLIENT side has |
| 2302 | * already set the connect expiration date to the right |
| 2303 | * timeout. We just have to check that it has not expired. |
| 2304 | */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2305 | if (!tv_isle(&req->cex, &now)) |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 2306 | return 0; |
| 2307 | |
| 2308 | /* We will set the queue timer to the time spent, just for |
| 2309 | * logging purposes. We fake a 500 server error, so that the |
| 2310 | * attacker will not suspect his connection has been tarpitted. |
| 2311 | * It will not cause trouble to the logs because we can exclude |
| 2312 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 2313 | */ |
| 2314 | tv_eternity(&req->cex); |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2315 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 2316 | srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T, |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2317 | 500, error_message(t, HTTP_ERR_500)); |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 2318 | return 1; |
| 2319 | } |
| 2320 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2321 | /* Right now, we will need to create a connection to the server. |
| 2322 | * We might already have tried, and got a connection pending, in |
| 2323 | * which case we will not do anything till it's pending. It's up |
| 2324 | * to any other session to release it and wake us up again. |
| 2325 | */ |
| 2326 | if (t->pend_pos) { |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2327 | if (!tv_isle(&req->cex, &now)) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2328 | return 0; |
| 2329 | else { |
| 2330 | /* we've been waiting too long here */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2331 | tv_eternity(&req->cex); |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2332 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2333 | srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q, |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2334 | 503, error_message(t, HTTP_ERR_503)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2335 | if (t->srv) |
| 2336 | t->srv->failed_conns++; |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 2337 | t->fe->failed_conns++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2338 | return 1; |
| 2339 | } |
| 2340 | } |
| 2341 | |
| 2342 | do { |
| 2343 | /* first, get a connection */ |
| 2344 | if (srv_redispatch_connect(t)) |
| 2345 | return t->srv_state != SV_STIDLE; |
| 2346 | |
| 2347 | /* try to (re-)connect to the server, and fail if we expire the |
| 2348 | * number of retries. |
| 2349 | */ |
| 2350 | if (srv_retryable_connect(t)) { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2351 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2352 | return t->srv_state != SV_STIDLE; |
| 2353 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2354 | } while (1); |
| 2355 | } |
| 2356 | } |
| 2357 | else if (s == SV_STCONN) { /* connection in progress */ |
| 2358 | if (c == CL_STCLOSE || c == CL_STSHUTW || |
| 2359 | (c == CL_STSHUTR && |
Willy Tarreau | c9b654b | 2007-05-08 14:46:53 +0200 | [diff] [blame] | 2360 | ((t->req->l == 0 && !(req->flags & BF_WRITE_STATUS)) || |
| 2361 | t->be->options & PR_O_ABRT_CLOSE))) { /* give up */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2362 | tv_eternity(&req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2363 | fd_delete(t->srv_fd); |
| 2364 | if (t->srv) |
| 2365 | t->srv->cur_sess--; |
| 2366 | |
| 2367 | /* note that this must not return any error because it would be able to |
| 2368 | * overwrite the client_retnclose() output. |
| 2369 | */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 2370 | 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] | 2371 | return 1; |
| 2372 | } |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2373 | if (!(req->flags & BF_WRITE_STATUS) && !tv_isle(&req->cex, &now)) { |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2374 | //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] | 2375 | return 0; /* nothing changed */ |
| 2376 | } |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2377 | else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2378 | /* timeout, asynchronous connect error or first write error */ |
| 2379 | //fprintf(stderr,"2: c=%d, s=%d\n", c, s); |
| 2380 | |
| 2381 | fd_delete(t->srv_fd); |
| 2382 | if (t->srv) |
| 2383 | t->srv->cur_sess--; |
| 2384 | |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2385 | if (!(req->flags & BF_WRITE_STATUS)) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2386 | conn_err = SN_ERR_SRVTO; // it was a connect timeout. |
| 2387 | else |
| 2388 | conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error. |
| 2389 | |
| 2390 | /* ensure that we have enough retries left */ |
| 2391 | if (srv_count_retry_down(t, conn_err)) |
| 2392 | return 1; |
| 2393 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2394 | 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] | 2395 | /* We're on our last chance, and the REDISP option was specified. |
| 2396 | * We will ignore cookie and force to balance or use the dispatcher. |
| 2397 | */ |
| 2398 | /* let's try to offer this slot to anybody */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2399 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 2400 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 2401 | |
| 2402 | if (t->srv) |
| 2403 | t->srv->failed_conns++; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2404 | t->be->failed_conns++; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 2405 | |
| 2406 | t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
| 2407 | 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] | 2408 | http_flush_cookie_flags(txn); |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 2409 | |
| 2410 | /* first, get a connection */ |
| 2411 | if (srv_redispatch_connect(t)) |
| 2412 | return t->srv_state != SV_STIDLE; |
| 2413 | } |
| 2414 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2415 | do { |
| 2416 | /* Now we will try to either reconnect to the same server or |
| 2417 | * connect to another server. If the connection gets queued |
| 2418 | * because all servers are saturated, then we will go back to |
| 2419 | * the SV_STIDLE state. |
| 2420 | */ |
| 2421 | if (srv_retryable_connect(t)) { |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2422 | t->logs.t_queue = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2423 | return t->srv_state != SV_STCONN; |
| 2424 | } |
| 2425 | |
| 2426 | /* we need to redispatch the connection to another server */ |
| 2427 | if (srv_redispatch_connect(t)) |
| 2428 | return t->srv_state != SV_STCONN; |
| 2429 | } while (1); |
| 2430 | } |
| 2431 | else { /* no error or write 0 */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2432 | t->logs.t_connect = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2433 | |
| 2434 | //fprintf(stderr,"3: c=%d, s=%d\n", c, s); |
| 2435 | if (req->l == 0) /* nothing to write */ { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2436 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2437 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2438 | } else /* need the right to write */ { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2439 | EV_FD_SET(t->srv_fd, DIR_WR); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2440 | if (tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2441 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 2442 | * we refresh it. */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2443 | rep->rex = req->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2444 | } |
| 2445 | else |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2446 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2447 | } |
| 2448 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2449 | 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] | 2450 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2451 | if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2452 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2453 | |
| 2454 | t->srv_state = SV_STDATA; |
| 2455 | if (t->srv) |
| 2456 | t->srv->cum_sess++; |
| 2457 | rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */ |
| 2458 | |
| 2459 | /* if the user wants to log as soon as possible, without counting |
| 2460 | bytes from the server, then this is the right moment. */ |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 2461 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2462 | 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] | 2463 | tcp_sess_log(t); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2464 | } |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 2465 | #ifdef CONFIG_HAP_TCPSPLICE |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2466 | if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 2467 | /* TCP splicing supported by both FE and BE */ |
| 2468 | tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0); |
| 2469 | } |
| 2470 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2471 | } |
| 2472 | else { |
| 2473 | t->srv_state = SV_STHEADERS; |
| 2474 | if (t->srv) |
| 2475 | t->srv->cum_sess++; |
| 2476 | rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2477 | t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE; |
| 2478 | /* reset hdr_idx which was already initialized by the request. |
| 2479 | * right now, the http parser does it. |
| 2480 | * hdr_idx_init(&t->txn.hdr_idx); |
| 2481 | */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2482 | } |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2483 | tv_eternity(&req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2484 | return 1; |
| 2485 | } |
| 2486 | } |
| 2487 | else if (s == SV_STHEADERS) { /* receiving server headers */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2488 | /* |
| 2489 | * Now parse the partial (or complete) lines. |
| 2490 | * We will check the response syntax, and also join multi-line |
| 2491 | * headers. An index of all the lines will be elaborated while |
| 2492 | * parsing. |
| 2493 | * |
| 2494 | * For the parsing, we use a 28 states FSM. |
| 2495 | * |
| 2496 | * Here is the information we currently have : |
| 2497 | * rep->data + req->som = beginning of response |
| 2498 | * rep->data + req->eoh = end of processed headers / start of current one |
| 2499 | * rep->data + req->eol = end of current header or line (LF or CRLF) |
| 2500 | * rep->lr = first non-visited byte |
| 2501 | * rep->r = end of data |
| 2502 | */ |
| 2503 | |
| 2504 | int cur_idx; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2505 | struct http_msg *msg = &txn->rsp; |
| 2506 | struct proxy *cur_proxy; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2507 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2508 | if (likely(rep->lr < rep->r)) |
| 2509 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2510 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2511 | /* 1: we might have to print this header in debug mode */ |
| 2512 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2513 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
| 2514 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
| 2515 | char *eol, *sol; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2516 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2517 | sol = rep->data + msg->som; |
| 2518 | eol = sol + msg->sl.rq.l; |
| 2519 | debug_hdr("srvrep", t, sol, eol); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2520 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2521 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2522 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2523 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2524 | while (cur_idx) { |
| 2525 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2526 | debug_hdr("srvhdr", t, sol, eol); |
| 2527 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2528 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 2529 | } |
| 2530 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2531 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2532 | |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2533 | 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] | 2534 | /* fd in DIR_RD was disabled, perhaps because of a previous buffer |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2535 | * full. We cannot loop here since stream_sock_read will disable it only if |
| 2536 | * rep->l == rlim-data |
| 2537 | */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2538 | if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2539 | tv_eternity(&rep->rex); |
| 2540 | } |
| 2541 | |
| 2542 | |
| 2543 | /* |
| 2544 | * Now we quickly check if we have found a full valid response. |
| 2545 | * If not so, we check the FD and buffer states before leaving. |
| 2546 | * A full response is indicated by the fact that we have seen |
| 2547 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 2548 | * responses are checked first. |
| 2549 | * |
| 2550 | * Depending on whether the client is still there or not, we |
| 2551 | * may send an error response back or not. Note that normally |
| 2552 | * we should only check for HTTP status there, and check I/O |
| 2553 | * errors somewhere else. |
| 2554 | */ |
| 2555 | |
| 2556 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
| 2557 | |
| 2558 | /* Invalid response, or read error or write error */ |
| 2559 | if (unlikely((msg->msg_state == HTTP_MSG_ERROR) || |
| 2560 | (req->flags & BF_WRITE_ERROR) || |
| 2561 | (rep->flags & BF_READ_ERROR))) { |
| 2562 | tv_eternity(&rep->rex); |
| 2563 | tv_eternity(&req->wex); |
| 2564 | fd_delete(t->srv_fd); |
| 2565 | if (t->srv) { |
| 2566 | t->srv->cur_sess--; |
| 2567 | t->srv->failed_resp++; |
| 2568 | } |
| 2569 | t->be->failed_resp++; |
| 2570 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2571 | txn->status = 502; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2572 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 2573 | if (!(t->flags & SN_ERR_MASK)) |
| 2574 | t->flags |= SN_ERR_SRVCL; |
| 2575 | if (!(t->flags & SN_FINST_MASK)) |
| 2576 | t->flags |= SN_FINST_H; |
| 2577 | /* We used to have a free connection slot. Since we'll never use it, |
| 2578 | * we have to inform the server that it may be used by another session. |
| 2579 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2580 | if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 2581 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2582 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2583 | return 1; |
| 2584 | } |
| 2585 | |
| 2586 | /* end of client write or end of server read. |
| 2587 | * since we are in header mode, if there's no space left for headers, we |
| 2588 | * won't be able to free more later, so the session will never terminate. |
| 2589 | */ |
| 2590 | else if (unlikely(rep->flags & BF_READ_NULL || |
| 2591 | c == CL_STSHUTW || c == CL_STCLOSE || |
| 2592 | rep->l >= rep->rlim - rep->data)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2593 | EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2594 | tv_eternity(&rep->rex); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2595 | t->srv_state = SV_STSHUTR; |
| 2596 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 2597 | return 1; |
| 2598 | } |
| 2599 | |
| 2600 | /* read timeout : return a 504 to the client. |
| 2601 | */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2602 | else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_RD) && |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2603 | tv_isle(&rep->rex, &now))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2604 | tv_eternity(&rep->rex); |
| 2605 | tv_eternity(&req->wex); |
| 2606 | fd_delete(t->srv_fd); |
| 2607 | if (t->srv) { |
| 2608 | t->srv->cur_sess--; |
| 2609 | t->srv->failed_resp++; |
| 2610 | } |
| 2611 | t->be->failed_resp++; |
| 2612 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2613 | txn->status = 504; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2614 | client_return(t, error_message(t, HTTP_ERR_504)); |
| 2615 | if (!(t->flags & SN_ERR_MASK)) |
| 2616 | t->flags |= SN_ERR_SRVTO; |
| 2617 | if (!(t->flags & SN_FINST_MASK)) |
| 2618 | t->flags |= SN_FINST_H; |
| 2619 | /* We used to have a free connection slot. Since we'll never use it, |
| 2620 | * we have to inform the server that it may be used by another session. |
| 2621 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2622 | if (t->srv && may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 2623 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2624 | return 1; |
| 2625 | } |
| 2626 | |
| 2627 | /* last client read and buffer empty */ |
| 2628 | /* FIXME!!! here, we don't want to switch to SHUTW if the |
| 2629 | * client shuts read too early, because we may still have |
| 2630 | * some work to do on the headers. |
| 2631 | * The side-effect is that if the client completely closes its |
| 2632 | * connection during SV_STHEADER, the connection to the server |
| 2633 | * is kept until a response comes back or the timeout is reached. |
| 2634 | */ |
| 2635 | else if (unlikely((/*c == CL_STSHUTR ||*/ c == CL_STCLOSE) && |
| 2636 | (req->l == 0))) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2637 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2638 | tv_eternity(&req->wex); |
| 2639 | |
| 2640 | /* We must ensure that the read part is still |
| 2641 | * alive when switching to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2642 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2643 | tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2644 | |
| 2645 | shutdown(t->srv_fd, SHUT_WR); |
| 2646 | t->srv_state = SV_STSHUTW; |
| 2647 | return 1; |
| 2648 | } |
| 2649 | |
| 2650 | /* write timeout */ |
| 2651 | /* FIXME!!! here, we don't want to switch to SHUTW if the |
| 2652 | * client shuts read too early, because we may still have |
| 2653 | * some work to do on the headers. |
| 2654 | */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2655 | else if (unlikely(EV_FD_ISSET(t->srv_fd, DIR_WR) && |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2656 | tv_isle(&req->wex, &now))) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2657 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2658 | tv_eternity(&req->wex); |
| 2659 | shutdown(t->srv_fd, SHUT_WR); |
| 2660 | /* We must ensure that the read part is still alive |
| 2661 | * when switching to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2662 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2663 | tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2664 | |
| 2665 | t->srv_state = SV_STSHUTW; |
| 2666 | if (!(t->flags & SN_ERR_MASK)) |
| 2667 | t->flags |= SN_ERR_SRVTO; |
| 2668 | if (!(t->flags & SN_FINST_MASK)) |
| 2669 | t->flags |= SN_FINST_H; |
| 2670 | return 1; |
| 2671 | } |
| 2672 | |
| 2673 | /* |
| 2674 | * And now the non-error cases. |
| 2675 | */ |
| 2676 | |
| 2677 | /* Data remaining in the request buffer. |
| 2678 | * This happens during the first pass here, and during |
| 2679 | * long posts. |
| 2680 | */ |
| 2681 | else if (likely(req->l)) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2682 | if (EV_FD_COND_S(t->srv_fd, DIR_WR)) { |
| 2683 | /* restart writing */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2684 | if (tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2685 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 2686 | * we refresh it. */ |
| 2687 | rep->rex = req->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2688 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2689 | else |
| 2690 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2691 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2692 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2693 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2694 | /* nothing left in the request buffer */ |
| 2695 | else { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 2696 | if (EV_FD_COND_C(t->srv_fd, DIR_WR)) { |
| 2697 | /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2698 | tv_eternity(&req->wex); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2699 | } |
| 2700 | } |
| 2701 | |
| 2702 | return t->srv_state != SV_STHEADERS; |
| 2703 | } |
| 2704 | |
| 2705 | |
| 2706 | /***************************************************************** |
| 2707 | * More interesting part now : we know that we have a complete * |
| 2708 | * response which at least looks like HTTP. We have an indicator * |
| 2709 | * of each header's length, so we can parse them quickly. * |
| 2710 | ****************************************************************/ |
| 2711 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2712 | /* ensure we keep this pointer to the beginning of the message */ |
| 2713 | msg->sol = rep->data + msg->som; |
| 2714 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2715 | /* |
| 2716 | * 1: get the status code and check for cacheability. |
| 2717 | */ |
| 2718 | |
| 2719 | t->logs.logwait &= ~LW_RESP; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2720 | 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] | 2721 | |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2722 | switch (txn->status) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2723 | case 200: |
| 2724 | case 203: |
| 2725 | case 206: |
| 2726 | case 300: |
| 2727 | case 301: |
| 2728 | case 410: |
| 2729 | /* RFC2616 @13.4: |
| 2730 | * "A response received with a status code of |
| 2731 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 2732 | * by a cache (...) unless a cache-control |
| 2733 | * directive prohibits caching." |
| 2734 | * |
| 2735 | * RFC2616 @9.5: POST method : |
| 2736 | * "Responses to this method are not cacheable, |
| 2737 | * unless the response includes appropriate |
| 2738 | * Cache-Control or Expires header fields." |
| 2739 | */ |
| 2740 | if (likely(txn->meth != HTTP_METH_POST) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2741 | unlikely(t->be->options & PR_O_CHK_CACHE)) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2742 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2743 | break; |
| 2744 | default: |
| 2745 | break; |
| 2746 | } |
| 2747 | |
| 2748 | /* |
| 2749 | * 2: we may need to capture headers |
| 2750 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2751 | if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2752 | capture_headers(rep->data + msg->som, &txn->hdr_idx, |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2753 | txn->rsp.cap, t->fe->rsp_cap); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2754 | |
| 2755 | /* |
| 2756 | * 3: we will have to evaluate the filters. |
| 2757 | * As opposed to version 1.2, now they will be evaluated in the |
| 2758 | * filters order and not in the header order. This means that |
| 2759 | * each filter has to be validated among all headers. |
| 2760 | * |
| 2761 | * Filters are tried with ->be first, then with ->fe if it is |
| 2762 | * different from ->be. |
| 2763 | */ |
| 2764 | |
| 2765 | t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */ |
| 2766 | |
| 2767 | cur_proxy = t->be; |
| 2768 | while (1) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2769 | struct proxy *rule_set = cur_proxy; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2770 | |
| 2771 | /* try headers filters */ |
| 2772 | if (rule_set->rsp_exp != NULL) { |
| 2773 | if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) { |
| 2774 | return_bad_resp: |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2775 | if (t->srv) { |
| 2776 | t->srv->cur_sess--; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2777 | t->srv->failed_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2778 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2779 | cur_proxy->failed_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2780 | return_srv_prx_502: |
| 2781 | tv_eternity(&rep->rex); |
| 2782 | tv_eternity(&req->wex); |
| 2783 | fd_delete(t->srv_fd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2784 | t->srv_state = SV_STCLOSE; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 2785 | txn->status = 502; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2786 | client_return(t, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2787 | if (!(t->flags & SN_ERR_MASK)) |
| 2788 | t->flags |= SN_ERR_PRXCOND; |
| 2789 | if (!(t->flags & SN_FINST_MASK)) |
| 2790 | t->flags |= SN_FINST_H; |
| 2791 | /* We used to have a free connection slot. Since we'll never use it, |
| 2792 | * we have to inform the server that it may be used by another session. |
| 2793 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2794 | if (t->srv && may_dequeue_tasks(t->srv, cur_proxy)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 2795 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2796 | return 1; |
| 2797 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2798 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2799 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2800 | /* has the response been denied ? */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2801 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2802 | if (t->srv) { |
| 2803 | t->srv->cur_sess--; |
| 2804 | t->srv->failed_secu++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2805 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2806 | cur_proxy->denied_resp++; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2807 | goto return_srv_prx_502; |
| 2808 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2809 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2810 | /* We might have to check for "Connection:" */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2811 | if (((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2812 | !(t->flags & SN_CONN_CLOSED)) { |
| 2813 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2814 | int cur_idx, old_idx, delta, val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2815 | struct hdr_idx_elem *cur_hdr; |
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 | cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 2818 | old_idx = 0; |
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 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 2821 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 2822 | cur_ptr = cur_next; |
| 2823 | cur_end = cur_ptr + cur_hdr->len; |
| 2824 | cur_next = cur_end + cur_hdr->cr + 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2825 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2826 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 2827 | if (val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2828 | /* 3 possibilities : |
| 2829 | * - we have already set Connection: close, |
| 2830 | * so we remove this line. |
| 2831 | * - we have not yet set Connection: close, |
| 2832 | * but this line indicates close. We leave |
| 2833 | * it untouched and set the flag. |
| 2834 | * - we have not yet set Connection: close, |
| 2835 | * and this line indicates non-close. We |
| 2836 | * replace it. |
| 2837 | */ |
| 2838 | if (t->flags & SN_CONN_CLOSED) { |
| 2839 | delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0); |
| 2840 | txn->rsp.eoh += delta; |
| 2841 | cur_next += delta; |
| 2842 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 2843 | txn->hdr_idx.used--; |
| 2844 | cur_hdr->len = 0; |
| 2845 | } else { |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 2846 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 2847 | delta = buffer_replace2(rep, cur_ptr + val, cur_end, |
| 2848 | "close", 5); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2849 | cur_next += delta; |
| 2850 | cur_hdr->len += delta; |
| 2851 | txn->rsp.eoh += delta; |
| 2852 | } |
| 2853 | t->flags |= SN_CONN_CLOSED; |
| 2854 | } |
| 2855 | } |
| 2856 | old_idx = cur_idx; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2857 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2858 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2859 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2860 | /* add response headers from the rule sets in the same order */ |
| 2861 | for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) { |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2862 | if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, |
| 2863 | rule_set->rsp_add[cur_idx])) < 0) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2864 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2865 | } |
| 2866 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2867 | /* check whether we're already working on the frontend */ |
| 2868 | if (cur_proxy == t->fe) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2869 | break; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2870 | cur_proxy = t->fe; |
| 2871 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2872 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2873 | /* |
| 2874 | * 4: check for server cookie. |
| 2875 | */ |
| 2876 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2877 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2878 | /* |
| 2879 | * 5: add server cookie in the response if needed |
| 2880 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2881 | if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) && |
| 2882 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2883 | int len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2884 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2885 | /* the server is known, it's not the one the client requested, we have to |
| 2886 | * insert a set-cookie here, except if we want to insert only on POST |
| 2887 | * requests and this one isn't. Note that servers which don't have cookies |
| 2888 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2889 | */ |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2890 | len = sprintf(trash, "Set-Cookie: %s=%s; path=/", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2891 | t->be->cookie_name, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2892 | 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] | 2893 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2894 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 2895 | trash, len)) < 0) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2896 | goto return_bad_resp; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2897 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2898 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2899 | /* Here, we will tell an eventual cache on the client side that we don't |
| 2900 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 2901 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 2902 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 2903 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2904 | if (t->be->options & PR_O_COOK_NOC) { |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2905 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 2906 | "Cache-control: private", 22)) < 0) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2907 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2908 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2909 | } |
| 2910 | |
| 2911 | |
| 2912 | /* |
| 2913 | * 6: check for cache-control or pragma headers. |
| 2914 | */ |
| 2915 | check_response_for_cacheability(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2916 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2917 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2918 | /* |
| 2919 | * 7: check if result will be cacheable with a cookie. |
| 2920 | * We'll block the response if security checks have caught |
| 2921 | * nasty things such as a cacheable cookie. |
| 2922 | */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 2923 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) == |
| 2924 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2925 | (t->be->options & PR_O_CHK_CACHE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2926 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2927 | /* we're in presence of a cacheable response containing |
| 2928 | * a set-cookie header. We'll block it as requested by |
| 2929 | * the 'checkcache' option, and send an alert. |
| 2930 | */ |
| 2931 | if (t->srv) { |
| 2932 | t->srv->cur_sess--; |
| 2933 | t->srv->failed_secu++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2934 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2935 | t->be->denied_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2936 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2937 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2938 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2939 | send_log(t->be, LOG_ALERT, |
| 2940 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2941 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2942 | goto return_srv_prx_502; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2943 | } |
| 2944 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2945 | /* |
| 2946 | * 8: add "Connection: close" if needed and not yet set. |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2947 | * 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] | 2948 | */ |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2949 | if (!(t->flags & SN_CONN_CLOSED) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2950 | ((t->fe->options | t->be->options) & PR_O_HTTP_CLOSE)) { |
Willy Tarreau | 2807efd | 2007-03-25 23:47:23 +0200 | [diff] [blame] | 2951 | if ((unlikely(msg->sl.st.v_l != 8) || |
| 2952 | unlikely(req->data[msg->som + 7] != '0')) && |
| 2953 | unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2954 | "Connection: close", 17)) < 0) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2955 | goto return_bad_resp; |
| 2956 | t->flags |= SN_CONN_CLOSED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2957 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2958 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2959 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2960 | /************************************************************* |
| 2961 | * OK, that's finished for the headers. We have done what we * |
| 2962 | * could. Let's switch to the DATA state. * |
| 2963 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2964 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2965 | t->srv_state = SV_STDATA; |
| 2966 | rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 2967 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2968 | |
| 2969 | /* client connection already closed or option 'forceclose' required : |
| 2970 | * we close the server's outgoing connection right now. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2971 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2972 | if ((req->l == 0) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2973 | (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] | 2974 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2975 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2976 | |
| 2977 | /* We must ensure that the read part is still alive when switching |
| 2978 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 2979 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 2980 | tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2981 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2982 | shutdown(t->srv_fd, SHUT_WR); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2983 | t->srv_state = SV_STSHUTW; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2984 | } |
| 2985 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2986 | #ifdef CONFIG_HAP_TCPSPLICE |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 2987 | if ((t->fe->options & t->be->options) & PR_O_TCPSPLICE) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2988 | /* TCP splicing supported by both FE and BE */ |
| 2989 | tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2990 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2991 | #endif |
| 2992 | /* if the user wants to log as soon as possible, without counting |
| 2993 | bytes from the server, then this is the right moment. */ |
| 2994 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 2995 | 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] | 2996 | t->logs.bytes_in = txn->rsp.eoh; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 2997 | if (t->fe->to_log & LW_REQ) |
| 2998 | http_sess_log(t); |
| 2999 | else |
| 3000 | tcp_sess_log(t); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3001 | } |
| 3002 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3003 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 3004 | * otherwise we would not let the client side wake up. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3005 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3006 | |
| 3007 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3008 | } |
| 3009 | else if (s == SV_STDATA) { |
| 3010 | /* read or write error */ |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3011 | if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) { |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3012 | tv_eternity(&rep->rex); |
| 3013 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3014 | fd_delete(t->srv_fd); |
| 3015 | if (t->srv) { |
| 3016 | t->srv->cur_sess--; |
| 3017 | t->srv->failed_resp++; |
| 3018 | } |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 3019 | t->be->failed_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3020 | t->srv_state = SV_STCLOSE; |
| 3021 | if (!(t->flags & SN_ERR_MASK)) |
| 3022 | t->flags |= SN_ERR_SRVCL; |
| 3023 | if (!(t->flags & SN_FINST_MASK)) |
| 3024 | t->flags |= SN_FINST_D; |
| 3025 | /* We used to have a free connection slot. Since we'll never use it, |
| 3026 | * we have to inform the server that it may be used by another session. |
| 3027 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3028 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3029 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3030 | |
| 3031 | return 1; |
| 3032 | } |
| 3033 | /* last read, or end of client write */ |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3034 | 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] | 3035 | EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3036 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3037 | t->srv_state = SV_STSHUTR; |
| 3038 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 3039 | return 1; |
| 3040 | } |
| 3041 | /* end of client read and no more data to send */ |
| 3042 | else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3043 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3044 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3045 | shutdown(t->srv_fd, SHUT_WR); |
| 3046 | /* We must ensure that the read part is still alive when switching |
| 3047 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3048 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3049 | tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3050 | |
| 3051 | t->srv_state = SV_STSHUTW; |
| 3052 | return 1; |
| 3053 | } |
| 3054 | /* read timeout */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3055 | else if (tv_isle(&rep->rex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3056 | EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3057 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3058 | t->srv_state = SV_STSHUTR; |
| 3059 | if (!(t->flags & SN_ERR_MASK)) |
| 3060 | t->flags |= SN_ERR_SRVTO; |
| 3061 | if (!(t->flags & SN_FINST_MASK)) |
| 3062 | t->flags |= SN_FINST_D; |
| 3063 | return 1; |
| 3064 | } |
| 3065 | /* write timeout */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3066 | else if (tv_isle(&req->wex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3067 | EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3068 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3069 | shutdown(t->srv_fd, SHUT_WR); |
| 3070 | /* We must ensure that the read part is still alive when switching |
| 3071 | * to shutw */ |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3072 | EV_FD_SET(t->srv_fd, DIR_RD); |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3073 | tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3074 | t->srv_state = SV_STSHUTW; |
| 3075 | if (!(t->flags & SN_ERR_MASK)) |
| 3076 | t->flags |= SN_ERR_SRVTO; |
| 3077 | if (!(t->flags & SN_FINST_MASK)) |
| 3078 | t->flags |= SN_FINST_D; |
| 3079 | return 1; |
| 3080 | } |
| 3081 | |
| 3082 | /* recompute request time-outs */ |
| 3083 | if (req->l == 0) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3084 | if (EV_FD_COND_C(t->srv_fd, DIR_WR)) { |
| 3085 | /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3086 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3087 | } |
| 3088 | } |
| 3089 | else { /* buffer not empty, there are still data to be transferred */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3090 | if (EV_FD_COND_S(t->srv_fd, DIR_WR)) { |
| 3091 | /* restart writing */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3092 | if (tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3093 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 3094 | * we refresh it. */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3095 | rep->rex = req->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3096 | } |
| 3097 | else |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3098 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3099 | } |
| 3100 | } |
| 3101 | |
| 3102 | /* recompute response time-outs */ |
| 3103 | if (rep->l == BUFSIZE) { /* no room to read more data */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3104 | if (EV_FD_COND_C(t->srv_fd, DIR_RD)) { |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3105 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3106 | } |
| 3107 | } |
| 3108 | else { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3109 | if (EV_FD_COND_S(t->srv_fd, DIR_RD)) { |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3110 | if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3111 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3112 | } |
| 3113 | } |
| 3114 | |
| 3115 | return 0; /* other cases change nothing */ |
| 3116 | } |
| 3117 | else if (s == SV_STSHUTR) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3118 | if (req->flags & BF_WRITE_ERROR) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3119 | //EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3120 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3121 | fd_delete(t->srv_fd); |
| 3122 | if (t->srv) { |
| 3123 | t->srv->cur_sess--; |
| 3124 | t->srv->failed_resp++; |
| 3125 | } |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 3126 | t->be->failed_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3127 | //close(t->srv_fd); |
| 3128 | t->srv_state = SV_STCLOSE; |
| 3129 | if (!(t->flags & SN_ERR_MASK)) |
| 3130 | t->flags |= SN_ERR_SRVCL; |
| 3131 | if (!(t->flags & SN_FINST_MASK)) |
| 3132 | t->flags |= SN_FINST_D; |
| 3133 | /* We used to have a free connection slot. Since we'll never use it, |
| 3134 | * we have to inform the server that it may be used by another session. |
| 3135 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3136 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3137 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3138 | |
| 3139 | return 1; |
| 3140 | } |
| 3141 | else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3142 | //EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3143 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3144 | fd_delete(t->srv_fd); |
| 3145 | if (t->srv) |
| 3146 | t->srv->cur_sess--; |
| 3147 | //close(t->srv_fd); |
| 3148 | t->srv_state = SV_STCLOSE; |
| 3149 | /* We used to have a free connection slot. Since we'll never use it, |
| 3150 | * we have to inform the server that it may be used by another session. |
| 3151 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3152 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3153 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3154 | |
| 3155 | return 1; |
| 3156 | } |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3157 | else if (tv_isle(&req->wex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3158 | //EV_FD_CLR(t->srv_fd, DIR_WR); |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3159 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3160 | fd_delete(t->srv_fd); |
| 3161 | if (t->srv) |
| 3162 | t->srv->cur_sess--; |
| 3163 | //close(t->srv_fd); |
| 3164 | t->srv_state = SV_STCLOSE; |
| 3165 | if (!(t->flags & SN_ERR_MASK)) |
| 3166 | t->flags |= SN_ERR_SRVTO; |
| 3167 | if (!(t->flags & SN_FINST_MASK)) |
| 3168 | t->flags |= SN_FINST_D; |
| 3169 | /* We used to have a free connection slot. Since we'll never use it, |
| 3170 | * we have to inform the server that it may be used by another session. |
| 3171 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3172 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3173 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3174 | |
| 3175 | return 1; |
| 3176 | } |
| 3177 | else if (req->l == 0) { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3178 | if (EV_FD_COND_C(t->srv_fd, DIR_WR)) { |
| 3179 | /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3180 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3181 | } |
| 3182 | } |
| 3183 | else { /* buffer not empty */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3184 | if (EV_FD_COND_S(t->srv_fd, DIR_WR)) { |
| 3185 | /* restart writing */ |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3186 | if (tv_add_ifset(&req->wex, &now, &t->be->srvtimeout)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3187 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 3188 | * we refresh it. */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3189 | rep->rex = req->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3190 | } |
| 3191 | else |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3192 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3193 | } |
| 3194 | } |
| 3195 | return 0; |
| 3196 | } |
| 3197 | else if (s == SV_STSHUTW) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3198 | if (rep->flags & BF_READ_ERROR) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3199 | //EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3200 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3201 | fd_delete(t->srv_fd); |
| 3202 | if (t->srv) { |
| 3203 | t->srv->cur_sess--; |
| 3204 | t->srv->failed_resp++; |
| 3205 | } |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 3206 | t->be->failed_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3207 | //close(t->srv_fd); |
| 3208 | t->srv_state = SV_STCLOSE; |
| 3209 | if (!(t->flags & SN_ERR_MASK)) |
| 3210 | t->flags |= SN_ERR_SRVCL; |
| 3211 | if (!(t->flags & SN_FINST_MASK)) |
| 3212 | t->flags |= SN_FINST_D; |
| 3213 | /* We used to have a free connection slot. Since we'll never use it, |
| 3214 | * we have to inform the server that it may be used by another session. |
| 3215 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3216 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3217 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3218 | |
| 3219 | return 1; |
| 3220 | } |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 3221 | 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] | 3222 | //EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3223 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3224 | fd_delete(t->srv_fd); |
| 3225 | if (t->srv) |
| 3226 | t->srv->cur_sess--; |
| 3227 | //close(t->srv_fd); |
| 3228 | t->srv_state = SV_STCLOSE; |
| 3229 | /* We used to have a free connection slot. Since we'll never use it, |
| 3230 | * we have to inform the server that it may be used by another session. |
| 3231 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3232 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3233 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3234 | |
| 3235 | return 1; |
| 3236 | } |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3237 | else if (tv_isle(&rep->rex, &now)) { |
Willy Tarreau | f161a34 | 2007-04-08 16:59:42 +0200 | [diff] [blame] | 3238 | //EV_FD_CLR(t->srv_fd, DIR_RD); |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3239 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3240 | fd_delete(t->srv_fd); |
| 3241 | if (t->srv) |
| 3242 | t->srv->cur_sess--; |
| 3243 | //close(t->srv_fd); |
| 3244 | t->srv_state = SV_STCLOSE; |
| 3245 | if (!(t->flags & SN_ERR_MASK)) |
| 3246 | t->flags |= SN_ERR_SRVTO; |
| 3247 | if (!(t->flags & SN_FINST_MASK)) |
| 3248 | t->flags |= SN_FINST_D; |
| 3249 | /* We used to have a free connection slot. Since we'll never use it, |
| 3250 | * we have to inform the server that it may be used by another session. |
| 3251 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3252 | if (may_dequeue_tasks(t->srv, t->be)) |
Willy Tarreau | 96bcfd7 | 2007-04-29 10:41:56 +0200 | [diff] [blame] | 3253 | task_wakeup(t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3254 | |
| 3255 | return 1; |
| 3256 | } |
| 3257 | else if (rep->l == BUFSIZE) { /* no room to read more data */ |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3258 | if (EV_FD_COND_C(t->srv_fd, DIR_RD)) { |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3259 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3260 | } |
| 3261 | } |
| 3262 | else { |
Willy Tarreau | 6631938 | 2007-04-08 17:17:37 +0200 | [diff] [blame] | 3263 | if (EV_FD_COND_S(t->srv_fd, DIR_RD)) { |
Willy Tarreau | a8b55e3 | 2007-05-13 16:08:19 +0200 | [diff] [blame] | 3264 | if (!tv_add_ifset(&rep->rex, &now, &t->be->srvtimeout)) |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3265 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3266 | } |
| 3267 | } |
| 3268 | return 0; |
| 3269 | } |
| 3270 | else { /* SV_STCLOSE : nothing to do */ |
| 3271 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 3272 | int len; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3273 | len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n", |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3274 | 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] | 3275 | write(1, trash, len); |
| 3276 | } |
| 3277 | return 0; |
| 3278 | } |
| 3279 | return 0; |
| 3280 | } |
| 3281 | |
| 3282 | |
| 3283 | /* |
| 3284 | * Produces data for the session <s> depending on its source. Expects to be |
| 3285 | * called with s->cli_state == CL_STSHUTR. Right now, only statistics can be |
| 3286 | * produced. It stops by itself by unsetting the SN_SELF_GEN flag from the |
| 3287 | * session, which it uses to keep on being called when there is free space in |
| 3288 | * the buffer, of simply by letting an empty buffer upon return. It returns 1 |
| 3289 | * if it changes the session state from CL_STSHUTR, otherwise 0. |
| 3290 | */ |
| 3291 | int produce_content(struct session *s) |
| 3292 | { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3293 | if (s->data_source == DATA_SRC_NONE) { |
| 3294 | s->flags &= ~SN_SELF_GEN; |
| 3295 | return 1; |
| 3296 | } |
| 3297 | else if (s->data_source == DATA_SRC_STATS) { |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3298 | /* dump server statistics */ |
| 3299 | return produce_content_stats(s); |
| 3300 | } |
| 3301 | else { |
| 3302 | /* unknown data source */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 3303 | s->txn.status = 500; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3304 | client_retnclose(s, error_message(s, HTTP_ERR_500)); |
| 3305 | if (!(s->flags & SN_ERR_MASK)) |
| 3306 | s->flags |= SN_ERR_PRXCOND; |
| 3307 | if (!(s->flags & SN_FINST_MASK)) |
| 3308 | s->flags |= SN_FINST_R; |
| 3309 | s->flags &= ~SN_SELF_GEN; |
| 3310 | return 1; |
| 3311 | } |
| 3312 | } |
| 3313 | |
| 3314 | |
| 3315 | /* |
| 3316 | * Produces statistics data for the session <s>. Expects to be called with |
| 3317 | * s->cli_state == CL_STSHUTR. It stops by itself by unsetting the SN_SELF_GEN |
| 3318 | * flag from the session, which it uses to keep on being called when there is |
| 3319 | * free space in the buffer, of simply by letting an empty buffer upon return. |
| 3320 | * It returns 1 if it changes the session state from CL_STSHUTR, otherwise 0. |
| 3321 | */ |
| 3322 | int produce_content_stats(struct session *s) |
| 3323 | { |
| 3324 | struct buffer *rep = s->rep; |
| 3325 | struct proxy *px; |
| 3326 | struct chunk msg; |
| 3327 | unsigned int up; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3328 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3329 | msg.len = 0; |
| 3330 | msg.str = trash; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3331 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3332 | switch (s->data_state) { |
| 3333 | case DATA_ST_INIT: |
| 3334 | /* the function had not been called yet */ |
| 3335 | s->flags |= SN_SELF_GEN; // more data will follow |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3336 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3337 | chunk_printf(&msg, sizeof(trash), |
| 3338 | "HTTP/1.0 200 OK\r\n" |
| 3339 | "Cache-Control: no-cache\r\n" |
| 3340 | "Connection: close\r\n" |
| 3341 | "Content-Type: text/html\r\n" |
| 3342 | "\r\n"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3343 | |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 3344 | s->txn.status = 200; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3345 | client_retnclose(s, &msg); // send the start of the response. |
| 3346 | msg.len = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3347 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3348 | if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is |
| 3349 | s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy |
| 3350 | if (!(s->flags & SN_FINST_MASK)) |
| 3351 | s->flags |= SN_FINST_R; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3352 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 3353 | if (s->txn.meth == HTTP_METH_HEAD) { |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 3354 | /* that's all we return in case of HEAD request */ |
| 3355 | s->data_state = DATA_ST_FIN; |
| 3356 | s->flags &= ~SN_SELF_GEN; |
| 3357 | return 1; |
| 3358 | } |
| 3359 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3360 | s->data_state = DATA_ST_HEAD; /* let's start producing data */ |
| 3361 | /* fall through */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3362 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3363 | case DATA_ST_HEAD: |
| 3364 | /* WARNING! This must fit in the first buffer !!! */ |
| 3365 | chunk_printf(&msg, sizeof(trash), |
| 3366 | "<html><head><title>Statistics Report for " PRODUCT_NAME "</title>\n" |
| 3367 | "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n" |
| 3368 | "<style type=\"text/css\"><!--\n" |
| 3369 | "body {" |
| 3370 | " font-family: helvetica, arial;" |
| 3371 | " font-size: 12px;" |
| 3372 | " font-weight: normal;" |
| 3373 | " color: black;" |
| 3374 | " background: white;" |
| 3375 | "}\n" |
| 3376 | "th,td {" |
| 3377 | " font-size: 0.8em;" |
| 3378 | " align: center;" |
Willy Tarreau | f2b74c2 | 2007-03-25 22:44:08 +0200 | [diff] [blame] | 3379 | "}\n" |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3380 | "h1 {" |
| 3381 | " font-size: xx-large;" |
| 3382 | " margin-bottom: 0.5em;" |
| 3383 | "}\n" |
| 3384 | "h2 {" |
| 3385 | " font-family: helvetica, arial;" |
| 3386 | " font-size: x-large;" |
| 3387 | " font-weight: bold;" |
| 3388 | " font-style: italic;" |
| 3389 | " color: #6020a0;" |
| 3390 | " margin-top: 0em;" |
| 3391 | " margin-bottom: 0em;" |
| 3392 | "}\n" |
| 3393 | "h3 {" |
| 3394 | " font-family: helvetica, arial;" |
| 3395 | " font-size: 16px;" |
| 3396 | " font-weight: bold;" |
| 3397 | " color: #b00040;" |
| 3398 | " background: #e8e8d0;" |
| 3399 | " margin-top: 0em;" |
| 3400 | " margin-bottom: 0em;" |
| 3401 | "}\n" |
| 3402 | "li {" |
| 3403 | " margin-top: 0.25em;" |
| 3404 | " margin-right: 2em;" |
| 3405 | "}\n" |
| 3406 | ".hr {margin-top: 0.25em;" |
| 3407 | " border-color: black;" |
| 3408 | " border-bottom-style: solid;" |
| 3409 | "}\n" |
| 3410 | ".pxname {background: #b00040;color: #ffff40;font-weight: bold;}\n" |
| 3411 | ".titre {background: #20D0D0;color: #000000;font-weight: bold;}\n" |
| 3412 | ".total {background: #20D0D0;color: #ffff80;}\n" |
| 3413 | ".frontend {background: #e8e8d0;}\n" |
| 3414 | ".backend {background: #e8e8d0;}\n" |
| 3415 | ".active0 {background: #ff9090;}\n" |
| 3416 | ".active1 {background: #ffd020;}\n" |
| 3417 | ".active2 {background: #ffffa0;}\n" |
| 3418 | ".active3 {background: #c0ffc0;}\n" |
| 3419 | ".active4 {background: #e0e0e0;}\n" |
| 3420 | ".backup0 {background: #ff9090;}\n" |
| 3421 | ".backup1 {background: #ff80ff;}\n" |
| 3422 | ".backup2 {background: #c060ff;}\n" |
| 3423 | ".backup3 {background: #b0d0ff;}\n" |
| 3424 | ".backup4 {background: #e0e0e0;}\n" |
| 3425 | "table.tbl { border-collapse: collapse; border-style: none;}\n" |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 3426 | "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] | 3427 | "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n" |
| 3428 | "table.tbl th.empty { border-style: none; empty-cells: hide;}\n" |
| 3429 | "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n" |
| 3430 | "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n" |
| 3431 | "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] | 3432 | "-->\n" |
| 3433 | "</style></head>\n"); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3434 | |
| 3435 | if (buffer_write_chunk(rep, &msg) != 0) |
| 3436 | return 0; |
| 3437 | |
| 3438 | s->data_state = DATA_ST_INFO; |
| 3439 | /* fall through */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3440 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3441 | case DATA_ST_INFO: |
| 3442 | up = (now.tv_sec - start_date.tv_sec); |
| 3443 | |
| 3444 | /* WARNING! this has to fit the first packet too. |
| 3445 | * We are around 3.5 kB, add adding entries will |
| 3446 | * become tricky if we want to support 4kB buffers ! |
| 3447 | */ |
| 3448 | chunk_printf(&msg, sizeof(trash), |
| 3449 | "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">" |
| 3450 | PRODUCT_NAME "</a></h1>\n" |
| 3451 | "<h2>Statistics Report for pid %d</h2>\n" |
| 3452 | "<hr width=\"100%%\" class=\"hr\">\n" |
| 3453 | "<h3>> General process information</h3>\n" |
| 3454 | "<table border=0 cols=3><tr><td align=\"left\" nowrap width=\"1%%\">\n" |
| 3455 | "<p><b>pid = </b> %d (nbproc = %d)<br>\n" |
| 3456 | "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n" |
| 3457 | "<b>system limits :</b> memmax = %s%s ; ulimit-n = %d<br>\n" |
| 3458 | "<b>maxsock = </b> %d<br>\n" |
| 3459 | "<b>maxconn = </b> %d (current conns = %d)<br>\n" |
| 3460 | "</td><td align=\"center\" nowrap>\n" |
Willy Tarreau | f2b74c2 | 2007-03-25 22:44:08 +0200 | [diff] [blame] | 3461 | "<table class=\"lgd\"><tr>\n" |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3462 | "<td class=\"active3\"> </td><td class=\"noborder\">active UP </td>" |
| 3463 | "<td class=\"backup3\"> </td><td class=\"noborder\">backup UP </td>" |
Willy Tarreau | f2b74c2 | 2007-03-25 22:44:08 +0200 | [diff] [blame] | 3464 | "</tr><tr>\n" |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3465 | "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>" |
| 3466 | "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>" |
Willy Tarreau | f2b74c2 | 2007-03-25 22:44:08 +0200 | [diff] [blame] | 3467 | "</tr><tr>\n" |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3468 | "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>" |
| 3469 | "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>" |
Willy Tarreau | f2b74c2 | 2007-03-25 22:44:08 +0200 | [diff] [blame] | 3470 | "</tr><tr>\n" |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3471 | "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN </td>" |
| 3472 | "<td class=\"active4\"></td><td class=\"noborder\">not checked </td>" |
| 3473 | "</tr></table>\n" |
| 3474 | "</td>" |
| 3475 | "<td align=\"left\" nowrap width=\"1%%\">" |
Willy Tarreau | f2b74c2 | 2007-03-25 22:44:08 +0200 | [diff] [blame] | 3476 | "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">\n" |
| 3477 | "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>\n" |
| 3478 | "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>\n" |
| 3479 | "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>\n" |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3480 | "</ul>" |
| 3481 | "</td>" |
| 3482 | "</tr></table>\n" |
| 3483 | "", |
| 3484 | pid, pid, global.nbproc, |
| 3485 | up / 86400, (up % 86400) / 3600, |
| 3486 | (up % 3600) / 60, (up % 60), |
| 3487 | global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited", |
| 3488 | global.rlimit_memmax ? " MB" : "", |
| 3489 | global.rlimit_nofile, |
| 3490 | global.maxsock, |
| 3491 | global.maxconn, |
| 3492 | actconn |
| 3493 | ); |
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 | if (buffer_write_chunk(rep, &msg) != 0) |
| 3496 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3497 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3498 | memset(&s->data_ctx, 0, sizeof(s->data_ctx)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3499 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3500 | s->data_ctx.stats.px = proxy; |
| 3501 | s->data_ctx.stats.px_st = DATA_ST_PX_INIT; |
| 3502 | s->data_state = DATA_ST_LIST; |
| 3503 | /* fall through */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3504 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3505 | case DATA_ST_LIST: |
| 3506 | /* dump proxies */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3507 | while (s->data_ctx.stats.px) { |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3508 | px = s->data_ctx.stats.px; |
| 3509 | /* skip the disabled proxies and non-networked ones */ |
| 3510 | if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE))) |
| 3511 | if (produce_content_stats_proxy(s, px) == 0) |
| 3512 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3513 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3514 | s->data_ctx.stats.px = px->next; |
| 3515 | s->data_ctx.stats.px_st = DATA_ST_PX_INIT; |
| 3516 | } |
| 3517 | /* here, we just have reached the last proxy */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3518 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3519 | s->data_state = DATA_ST_END; |
| 3520 | /* fall through */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3521 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3522 | case DATA_ST_END: |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 3523 | chunk_printf(&msg, sizeof(trash), "</body></html>\n"); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3524 | if (buffer_write_chunk(rep, &msg) != 0) |
| 3525 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3526 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3527 | s->data_state = DATA_ST_FIN; |
| 3528 | /* fall through */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3529 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3530 | case DATA_ST_FIN: |
| 3531 | s->flags &= ~SN_SELF_GEN; |
| 3532 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3533 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3534 | default: |
| 3535 | /* unknown state ! */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 3536 | s->txn.status = 500; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3537 | client_retnclose(s, error_message(s, HTTP_ERR_500)); |
| 3538 | if (!(s->flags & SN_ERR_MASK)) |
| 3539 | s->flags |= SN_ERR_PRXCOND; |
| 3540 | if (!(s->flags & SN_FINST_MASK)) |
| 3541 | s->flags |= SN_FINST_R; |
| 3542 | s->flags &= ~SN_SELF_GEN; |
| 3543 | return 1; |
| 3544 | } |
| 3545 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3546 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3547 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3548 | /* |
| 3549 | * Dumps statistics for a proxy. |
| 3550 | * Returns 0 if it had to stop dumping data because of lack of buffer space, |
| 3551 | * ot non-zero if everything completed. |
| 3552 | */ |
| 3553 | int produce_content_stats_proxy(struct session *s, struct proxy *px) |
| 3554 | { |
| 3555 | struct buffer *rep = s->rep; |
| 3556 | struct server *sv; |
| 3557 | struct chunk msg; |
| 3558 | |
| 3559 | msg.len = 0; |
| 3560 | msg.str = trash; |
| 3561 | |
| 3562 | switch (s->data_ctx.stats.px_st) { |
| 3563 | case DATA_ST_PX_INIT: |
| 3564 | /* we are on a new proxy */ |
| 3565 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3566 | if (s->be->uri_auth && s->be->uri_auth->scope) { |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3567 | /* we have a limited scope, we have to check the proxy name */ |
| 3568 | struct stat_scope *scope; |
| 3569 | int len; |
| 3570 | |
| 3571 | len = strlen(px->id); |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3572 | scope = s->be->uri_auth->scope; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3573 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3574 | while (scope) { |
| 3575 | /* match exact proxy name */ |
| 3576 | if (scope->px_len == len && !memcmp(px->id, scope->px_id, len)) |
| 3577 | break; |
| 3578 | |
| 3579 | /* match '.' which means 'self' proxy */ |
| 3580 | if (!strcmp(scope->px_id, ".") && px == s->fe) |
| 3581 | break; |
| 3582 | scope = scope->next; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3583 | } |
| 3584 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3585 | /* proxy name not found : don't dump anything */ |
| 3586 | if (scope == NULL) |
| 3587 | return 1; |
| 3588 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3589 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3590 | s->data_ctx.stats.px_st = DATA_ST_PX_TH; |
| 3591 | /* fall through */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3592 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3593 | case DATA_ST_PX_TH: |
| 3594 | /* print a new table */ |
| 3595 | chunk_printf(&msg, sizeof(trash), |
| 3596 | "<table cols=\"20\" class=\"tbl\" width=\"100%%\">\n" |
| 3597 | "<tr align=\"center\" class=\"titre\">" |
| 3598 | "<th colspan=2 class=\"pxname\">%s</th>" |
| 3599 | "<th colspan=18 class=\"empty\"></th>" |
| 3600 | "</tr>\n" |
| 3601 | "<tr align=\"center\" class=\"titre\">" |
| 3602 | "<th rowspan=2></th>" |
| 3603 | "<th colspan=2>Queue</th><th colspan=4>Sessions</th>" |
| 3604 | "<th colspan=2>Bytes</th><th colspan=2>Denied</th>" |
| 3605 | "<th colspan=3>Errors</th><th colspan=6>Server</th>" |
| 3606 | "</tr>\n" |
| 3607 | "<tr align=\"center\" class=\"titre\">" |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 3608 | "<th>Cur</th><th>Max</th><th>Cur</th><th>Max</th>" |
| 3609 | "<th>Limit</th><th>Cumul</th><th>In</th><th>Out</th>" |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3610 | "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>" |
| 3611 | "<th>Resp</th><th>Status</th><th>Weight</th><th>Act</th>" |
| 3612 | "<th>Bck</th><th>Check</th><th>Down</th></tr>\n" |
| 3613 | "", |
| 3614 | px->id); |
| 3615 | |
| 3616 | if (buffer_write_chunk(rep, &msg) != 0) |
| 3617 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3618 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3619 | s->data_ctx.stats.px_st = DATA_ST_PX_FE; |
| 3620 | /* fall through */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3621 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3622 | case DATA_ST_PX_FE: |
| 3623 | /* print the frontend */ |
| 3624 | if (px->cap & PR_CAP_FE) { |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3625 | chunk_printf(&msg, sizeof(trash), |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3626 | /* name, queue */ |
| 3627 | "<tr align=center class=\"frontend\"><td>Frontend</td><td colspan=2></td>" |
| 3628 | /* sessions : current, max, limit, cumul. */ |
| 3629 | "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>" |
| 3630 | /* bytes : in, out */ |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 3631 | "<td align=right>%lld</td><td align=right>%lld</td>" |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3632 | /* denied: req, resp */ |
| 3633 | "<td align=right>%d</td><td align=right>%d</td>" |
| 3634 | /* errors : request, connect, response */ |
| 3635 | "<td align=right>%d</td><td align=right></td><td align=right></td>" |
| 3636 | /* server status : reflect backend status */ |
| 3637 | "<td align=center>%s</td>" |
| 3638 | /* rest of server: nothing */ |
| 3639 | "<td align=center colspan=5></td></tr>" |
| 3640 | "", |
| 3641 | px->feconn, px->feconn_max, px->maxconn, px->cum_feconn, |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 3642 | px->bytes_in, px->bytes_out, |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3643 | px->denied_req, px->denied_resp, |
| 3644 | px->failed_req, |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3645 | px->state == PR_STRUN ? "OPEN" : |
| 3646 | px->state == PR_STIDLE ? "FULL" : "STOP"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3647 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3648 | if (buffer_write_chunk(rep, &msg) != 0) |
| 3649 | return 0; |
| 3650 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3651 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3652 | s->data_ctx.stats.sv = px->srv; /* may be NULL */ |
| 3653 | s->data_ctx.stats.px_st = DATA_ST_PX_SV; |
| 3654 | /* fall through */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3655 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3656 | case DATA_ST_PX_SV: |
| 3657 | /* stats.sv has been initialized above */ |
| 3658 | while (s->data_ctx.stats.sv != NULL) { |
| 3659 | static char *srv_hlt_st[5] = { "DOWN", "DN %d/%d ↑", "UP %d/%d ↓", "UP", "<i>no check</i>" }; |
| 3660 | 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] | 3661 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3662 | sv = s->data_ctx.stats.sv; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3663 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3664 | /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */ |
| 3665 | if (!(sv->state & SRV_CHECKED)) |
| 3666 | sv_state = 4; |
| 3667 | else if (sv->state & SRV_RUNNING) |
| 3668 | if (sv->health == sv->rise + sv->fall - 1) |
| 3669 | sv_state = 3; /* UP */ |
| 3670 | else |
| 3671 | sv_state = 2; /* going down */ |
| 3672 | else |
| 3673 | if (sv->health) |
| 3674 | sv_state = 1; /* going up */ |
| 3675 | else |
| 3676 | sv_state = 0; /* DOWN */ |
| 3677 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3678 | chunk_printf(&msg, sizeof(trash), |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3679 | /* name */ |
| 3680 | "<tr align=\"center\" class=\"%s%d\"><td>%s</td>" |
| 3681 | /* queue : current, max */ |
| 3682 | "<td align=right>%d</td><td align=right>%d</td>" |
| 3683 | /* sessions : current, max, limit, cumul */ |
| 3684 | "<td align=right>%d</td><td align=right>%d</td><td align=right>%s</td><td align=right>%d</td>" |
| 3685 | /* bytes : in, out */ |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 3686 | "<td align=right>%lld</td><td align=right>%lld</td>" |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3687 | /* denied: req, resp */ |
| 3688 | "<td align=right></td><td align=right>%d</td>" |
| 3689 | /* errors : request, connect, response */ |
| 3690 | "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n" |
| 3691 | "", |
Willy Tarreau | 368e96a | 2007-01-07 00:16:15 +0100 | [diff] [blame] | 3692 | (sv->state & SRV_BACKUP) ? "backup" : "active", |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3693 | sv_state, sv->id, |
| 3694 | sv->nbpend, sv->nbpend_max, |
| 3695 | 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] | 3696 | sv->bytes_in, sv->bytes_out, |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3697 | sv->failed_secu, |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3698 | sv->failed_conns, sv->failed_resp); |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3699 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3700 | /* status */ |
| 3701 | chunk_printf(&msg, sizeof(trash), "<td nowrap>"); |
| 3702 | chunk_printf(&msg, sizeof(trash), |
| 3703 | srv_hlt_st[sv_state], |
| 3704 | (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health), |
| 3705 | (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise)); |
| 3706 | |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3707 | chunk_printf(&msg, sizeof(trash), |
| 3708 | /* weight */ |
| 3709 | "</td><td>%d</td>" |
| 3710 | /* act, bck */ |
| 3711 | "<td>%s</td><td>%s</td>" |
| 3712 | "", |
Willy Tarreau | 417fae0 | 2007-03-25 21:16:40 +0200 | [diff] [blame] | 3713 | sv->uweight, |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3714 | (sv->state & SRV_BACKUP) ? "-" : "Y", |
| 3715 | (sv->state & SRV_BACKUP) ? "Y" : "-"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3716 | |
| 3717 | /* check failures : unique, fatal */ |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3718 | if (sv->state & SRV_CHECKED) |
| 3719 | chunk_printf(&msg, sizeof(trash), |
| 3720 | "<td align=right>%d</td><td align=right>%d</td></tr>\n", |
| 3721 | sv->failed_checks, sv->down_trans); |
| 3722 | else |
| 3723 | chunk_printf(&msg, sizeof(trash), |
| 3724 | "<td colspan=2></td></tr>\n"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3725 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3726 | if (buffer_write_chunk(rep, &msg) != 0) |
| 3727 | return 0; |
| 3728 | |
| 3729 | s->data_ctx.stats.sv = sv->next; |
| 3730 | } /* while sv */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3731 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3732 | s->data_ctx.stats.px_st = DATA_ST_PX_BE; |
| 3733 | /* fall through */ |
| 3734 | |
| 3735 | case DATA_ST_PX_BE: |
| 3736 | /* print the backend */ |
| 3737 | if (px->cap & PR_CAP_BE) { |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3738 | chunk_printf(&msg, sizeof(trash), |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3739 | /* name */ |
| 3740 | "<tr align=center class=\"backend\"><td>Backend</td>" |
| 3741 | /* queue : current, max */ |
| 3742 | "<td align=right>%d</td><td align=right>%d</td>" |
| 3743 | /* sessions : current, max, limit, cumul. */ |
| 3744 | "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>" |
| 3745 | /* bytes : in, out */ |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 3746 | "<td align=right>%lld</td><td align=right>%lld</td>" |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3747 | /* denied: req, resp */ |
| 3748 | "<td align=right>%d</td><td align=right>%d</td>" |
| 3749 | /* errors : request, connect, response */ |
| 3750 | "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n" |
| 3751 | /* server status : reflect backend status (up/down) : we display UP |
| 3752 | * if the backend has known working servers or if it has no server at |
| 3753 | * all (eg: for stats). Tthen we display the total weight, number of |
| 3754 | * active and backups. */ |
| 3755 | "<td align=center>%s</td><td align=center>%d</td>" |
| 3756 | "<td align=center>%d</td><td align=center>%d</td>" |
| 3757 | /* rest of server: nothing */ |
| 3758 | "<td align=center colspan=2></td></tr>" |
| 3759 | "", |
| 3760 | px->nbpend /* or px->totpend ? */, px->nbpend_max, |
| 3761 | px->beconn, px->beconn_max, px->fullconn, px->cum_beconn, |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 3762 | px->bytes_in, px->bytes_out, |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3763 | px->denied_req, px->denied_resp, |
| 3764 | px->failed_conns, px->failed_resp, |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3765 | (px->srv_map_sz > 0 || !px->srv) ? "UP" : "DOWN", |
| 3766 | px->srv_map_sz, px->srv_act, px->srv_bck); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3767 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3768 | if (buffer_write_chunk(rep, &msg) != 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3769 | return 0; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3770 | } |
| 3771 | |
| 3772 | s->data_ctx.stats.px_st = DATA_ST_PX_END; |
| 3773 | /* fall through */ |
| 3774 | |
| 3775 | case DATA_ST_PX_END: |
| 3776 | chunk_printf(&msg, sizeof(trash), "</table><p>\n"); |
| 3777 | |
| 3778 | if (buffer_write_chunk(rep, &msg) != 0) |
| 3779 | return 0; |
| 3780 | |
| 3781 | s->data_ctx.stats.px_st = DATA_ST_PX_FIN; |
| 3782 | /* fall through */ |
| 3783 | |
| 3784 | case DATA_ST_PX_FIN: |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3785 | return 1; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3786 | |
| 3787 | default: |
| 3788 | /* unknown state, we should put an abort() here ! */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3789 | return 1; |
| 3790 | } |
| 3791 | } |
| 3792 | |
| 3793 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3794 | /* Iterate the same filter through all request headers. |
| 3795 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3796 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 3797 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3798 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3799 | 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] | 3800 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3801 | char term; |
| 3802 | char *cur_ptr, *cur_end, *cur_next; |
| 3803 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3804 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3805 | struct hdr_idx_elem *cur_hdr; |
| 3806 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 3807 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3808 | last_hdr = 0; |
| 3809 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3810 | 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] | 3811 | old_idx = 0; |
| 3812 | |
| 3813 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3814 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3815 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3816 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3817 | (exp->action == ACT_ALLOW || |
| 3818 | exp->action == ACT_DENY || |
| 3819 | exp->action == ACT_TARPIT)) |
| 3820 | return 0; |
| 3821 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3822 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3823 | if (!cur_idx) |
| 3824 | break; |
| 3825 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3826 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3827 | cur_ptr = cur_next; |
| 3828 | cur_end = cur_ptr + cur_hdr->len; |
| 3829 | cur_next = cur_end + cur_hdr->cr + 1; |
| 3830 | |
| 3831 | /* Now we have one header between cur_ptr and cur_end, |
| 3832 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3833 | */ |
| 3834 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3835 | /* The annoying part is that pattern matching needs |
| 3836 | * that we modify the contents to null-terminate all |
| 3837 | * strings before testing them. |
| 3838 | */ |
| 3839 | |
| 3840 | term = *cur_end; |
| 3841 | *cur_end = '\0'; |
| 3842 | |
| 3843 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 3844 | switch (exp->action) { |
| 3845 | case ACT_SETBE: |
| 3846 | /* It is not possible to jump a second time. |
| 3847 | * FIXME: should we return an HTTP/500 here so that |
| 3848 | * the admin knows there's a problem ? |
| 3849 | */ |
| 3850 | if (t->be != t->fe) |
| 3851 | break; |
| 3852 | |
| 3853 | /* Swithing Proxy */ |
| 3854 | t->be = (struct proxy *) exp->replace; |
| 3855 | |
| 3856 | /* right now, the backend switch is not too much complicated |
| 3857 | * because we have associated req_cap and rsp_cap to the |
| 3858 | * frontend, and the beconn will be updated later. |
| 3859 | */ |
| 3860 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3861 | t->rep->rto = t->req->wto = t->be->srvtimeout; |
| 3862 | t->req->cto = t->be->contimeout; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3863 | last_hdr = 1; |
| 3864 | break; |
| 3865 | |
| 3866 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3867 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3868 | last_hdr = 1; |
| 3869 | break; |
| 3870 | |
| 3871 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3872 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3873 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3874 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3875 | break; |
| 3876 | |
| 3877 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3878 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3879 | last_hdr = 1; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3880 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3881 | break; |
| 3882 | |
| 3883 | case ACT_REPLACE: |
| 3884 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 3885 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 3886 | /* FIXME: if the user adds a newline in the replacement, the |
| 3887 | * index will not be recalculated for now, and the new line |
| 3888 | * will not be counted as a new header. |
| 3889 | */ |
| 3890 | |
| 3891 | cur_end += delta; |
| 3892 | cur_next += delta; |
| 3893 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3894 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3895 | break; |
| 3896 | |
| 3897 | case ACT_REMOVE: |
| 3898 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 3899 | cur_next += delta; |
| 3900 | |
| 3901 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3902 | txn->req.eoh += delta; |
| 3903 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 3904 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3905 | cur_hdr->len = 0; |
| 3906 | cur_end = NULL; /* null-term has been rewritten */ |
| 3907 | break; |
| 3908 | |
| 3909 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3910 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3911 | if (cur_end) |
| 3912 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3913 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3914 | /* keep the link from this header to next one in case of later |
| 3915 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3916 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3917 | old_idx = cur_idx; |
| 3918 | } |
| 3919 | return 0; |
| 3920 | } |
| 3921 | |
| 3922 | |
| 3923 | /* Apply the filter to the request line. |
| 3924 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 3925 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3926 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 3927 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3928 | */ |
| 3929 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 3930 | { |
| 3931 | char term; |
| 3932 | char *cur_ptr, *cur_end; |
| 3933 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3934 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3935 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3936 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3937 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3938 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3939 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3940 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3941 | (exp->action == ACT_ALLOW || |
| 3942 | exp->action == ACT_DENY || |
| 3943 | exp->action == ACT_TARPIT)) |
| 3944 | return 0; |
| 3945 | else if (exp->action == ACT_REMOVE) |
| 3946 | return 0; |
| 3947 | |
| 3948 | done = 0; |
| 3949 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 3950 | 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] | 3951 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3952 | |
| 3953 | /* Now we have the request line between cur_ptr and cur_end */ |
| 3954 | |
| 3955 | /* The annoying part is that pattern matching needs |
| 3956 | * that we modify the contents to null-terminate all |
| 3957 | * strings before testing them. |
| 3958 | */ |
| 3959 | |
| 3960 | term = *cur_end; |
| 3961 | *cur_end = '\0'; |
| 3962 | |
| 3963 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 3964 | switch (exp->action) { |
| 3965 | case ACT_SETBE: |
| 3966 | /* It is not possible to jump a second time. |
| 3967 | * FIXME: should we return an HTTP/500 here so that |
| 3968 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3969 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3970 | if (t->be != t->fe) |
| 3971 | break; |
| 3972 | |
| 3973 | /* Swithing Proxy */ |
| 3974 | t->be = (struct proxy *) exp->replace; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3975 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3976 | /* right now, the backend switch is not too much complicated |
| 3977 | * because we have associated req_cap and rsp_cap to the |
| 3978 | * frontend, and the beconn will be updated later. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3979 | */ |
| 3980 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3981 | t->rep->rto = t->req->wto = t->be->srvtimeout; |
| 3982 | t->req->cto = t->be->contimeout; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3983 | done = 1; |
| 3984 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3985 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3986 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3987 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3988 | done = 1; |
| 3989 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3990 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3991 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3992 | txn->flags |= TX_CLDENY; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3993 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3994 | done = 1; |
| 3995 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3996 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3997 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3998 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3999 | t->be->denied_req++; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4000 | done = 1; |
| 4001 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4002 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4003 | case ACT_REPLACE: |
| 4004 | *cur_end = term; /* restore the string terminator */ |
| 4005 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4006 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 4007 | /* FIXME: if the user adds a newline in the replacement, the |
| 4008 | * index will not be recalculated for now, and the new line |
| 4009 | * will not be counted as a new header. |
| 4010 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4011 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4012 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4013 | cur_end += delta; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4014 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4015 | 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] | 4016 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4017 | HTTP_MSG_RQMETH, |
| 4018 | cur_ptr, cur_end + 1, |
| 4019 | NULL, NULL); |
| 4020 | if (unlikely(!cur_end)) |
| 4021 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 4022 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4023 | /* we have a full request and we know that we have either a CR |
| 4024 | * or an LF at <ptr>. |
| 4025 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4026 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 4027 | 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] | 4028 | /* there is no point trying this regex on headers */ |
| 4029 | return 1; |
| 4030 | } |
| 4031 | } |
| 4032 | *cur_end = term; /* restore the string terminator */ |
| 4033 | return done; |
| 4034 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 4035 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4036 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4037 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4038 | /* |
| 4039 | * Apply all the req filters <exp> to all headers in buffer <req> of session <t>. |
| 4040 | * 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] | 4041 | * unparsable request. Since it can manage the switch to another backend, it |
| 4042 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4043 | */ |
| 4044 | int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 4045 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4046 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4047 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4048 | while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4049 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4050 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4051 | /* |
| 4052 | * The interleaving of transformations and verdicts |
| 4053 | * makes it difficult to decide to continue or stop |
| 4054 | * the evaluation. |
| 4055 | */ |
| 4056 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4057 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4058 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 4059 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) { |
| 4060 | exp = exp->next; |
| 4061 | continue; |
| 4062 | } |
| 4063 | |
| 4064 | /* Apply the filter to the request line. */ |
| 4065 | ret = apply_filter_to_req_line(t, req, exp); |
| 4066 | if (unlikely(ret < 0)) |
| 4067 | return -1; |
| 4068 | |
| 4069 | if (likely(ret == 0)) { |
| 4070 | /* The filter did not match the request, it can be |
| 4071 | * iterated through all headers. |
| 4072 | */ |
| 4073 | apply_filter_to_req_headers(t, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4074 | } |
| 4075 | exp = exp->next; |
| 4076 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4077 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4078 | } |
| 4079 | |
| 4080 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4081 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4082 | /* |
| 4083 | * Manager client-side cookie |
| 4084 | */ |
| 4085 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 4086 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4087 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4088 | char *p1, *p2, *p3, *p4; |
| 4089 | char *del_colon, *del_cookie, *colon; |
| 4090 | int app_cookies; |
| 4091 | |
| 4092 | appsess *asession_temp = NULL; |
| 4093 | appsess local_asession; |
| 4094 | |
| 4095 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4096 | int cur_idx, old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4097 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4098 | if (t->be->cookie_name == NULL && |
| 4099 | t->be->appsession_name == NULL && |
| 4100 | t->be->capture_name == NULL) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4101 | return; |
| 4102 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 4103 | /* Iterate through the headers. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4104 | * we start with the start line. |
| 4105 | */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 4106 | old_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4107 | 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] | 4108 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4109 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4110 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4111 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4112 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4113 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4114 | cur_ptr = cur_next; |
| 4115 | cur_end = cur_ptr + cur_hdr->len; |
| 4116 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4117 | |
| 4118 | /* We have one full header between cur_ptr and cur_end, and the |
| 4119 | * next header starts at cur_next. We're only interested in |
| 4120 | * "Cookie:" headers. |
| 4121 | */ |
| 4122 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4123 | val = http_header_match2(cur_ptr, cur_end, "Cookie", 6); |
| 4124 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4125 | old_idx = cur_idx; |
| 4126 | continue; |
| 4127 | } |
| 4128 | |
| 4129 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 4130 | * attributes whose name begin with a '$', and associate them with |
| 4131 | * the right cookie, if we want to delete this cookie. |
| 4132 | * So there are 3 cases for each cookie read : |
| 4133 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 4134 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 4135 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 4136 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 4137 | * "special" cookie. |
| 4138 | * At the end of loop, if a "special" cookie remains, we may have to |
| 4139 | * remove it. If no application cookie persists in the header, we |
| 4140 | * *MUST* delete it |
| 4141 | */ |
| 4142 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4143 | colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4144 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4145 | /* del_cookie == NULL => nothing to be deleted */ |
| 4146 | del_colon = del_cookie = NULL; |
| 4147 | app_cookies = 0; |
| 4148 | |
| 4149 | while (p1 < cur_end) { |
| 4150 | /* skip spaces and colons, but keep an eye on these ones */ |
| 4151 | while (p1 < cur_end) { |
| 4152 | if (*p1 == ';' || *p1 == ',') |
| 4153 | colon = p1; |
| 4154 | else if (!isspace((int)*p1)) |
| 4155 | break; |
| 4156 | p1++; |
| 4157 | } |
| 4158 | |
| 4159 | if (p1 == cur_end) |
| 4160 | break; |
| 4161 | |
| 4162 | /* p1 is at the beginning of the cookie name */ |
| 4163 | p2 = p1; |
| 4164 | while (p2 < cur_end && *p2 != '=') |
| 4165 | p2++; |
| 4166 | |
| 4167 | if (p2 == cur_end) |
| 4168 | break; |
| 4169 | |
| 4170 | p3 = p2 + 1; /* skips the '=' sign */ |
| 4171 | if (p3 == cur_end) |
| 4172 | break; |
| 4173 | |
| 4174 | p4 = p3; |
| 4175 | while (p4 < cur_end && !isspace((int)*p4) && *p4 != ';' && *p4 != ',') |
| 4176 | p4++; |
| 4177 | |
| 4178 | /* here, we have the cookie name between p1 and p2, |
| 4179 | * and its value between p3 and p4. |
| 4180 | * we can process it : |
| 4181 | * |
| 4182 | * Cookie: NAME=VALUE; |
| 4183 | * | || || | |
| 4184 | * | || || +--> p4 |
| 4185 | * | || |+-------> p3 |
| 4186 | * | || +--------> p2 |
| 4187 | * | |+------------> p1 |
| 4188 | * | +-------------> colon |
| 4189 | * +--------------------> cur_ptr |
| 4190 | */ |
| 4191 | |
| 4192 | if (*p1 == '$') { |
| 4193 | /* skip this one */ |
| 4194 | } |
| 4195 | else { |
| 4196 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4197 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4198 | txn->cli_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4199 | (p4 - p1 >= t->fe->capture_namelen) && |
| 4200 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4201 | int log_len = p4 - p1; |
| 4202 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 4203 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4204 | Alert("HTTP logging : out of memory.\n"); |
| 4205 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4206 | if (log_len > t->fe->capture_len) |
| 4207 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4208 | memcpy(txn->cli_cookie, p1, log_len); |
| 4209 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4210 | } |
| 4211 | } |
| 4212 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4213 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 4214 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4215 | /* Cool... it's the right one */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4216 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4217 | char *delim; |
| 4218 | |
| 4219 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 4220 | * have the server ID betweek p3 and delim, and the original cookie between |
| 4221 | * delim+1 and p4. Otherwise, delim==p4 : |
| 4222 | * |
| 4223 | * Cookie: NAME=SRV~VALUE; |
| 4224 | * | || || | | |
| 4225 | * | || || | +--> p4 |
| 4226 | * | || || +--------> delim |
| 4227 | * | || |+-----------> p3 |
| 4228 | * | || +------------> p2 |
| 4229 | * | |+----------------> p1 |
| 4230 | * | +-----------------> colon |
| 4231 | * +------------------------> cur_ptr |
| 4232 | */ |
| 4233 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4234 | if (t->be->options & PR_O_COOK_PFX) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4235 | for (delim = p3; delim < p4; delim++) |
| 4236 | if (*delim == COOKIE_DELIM) |
| 4237 | break; |
| 4238 | } |
| 4239 | else |
| 4240 | delim = p4; |
| 4241 | |
| 4242 | |
| 4243 | /* Here, we'll look for the first running server which supports the cookie. |
| 4244 | * This allows to share a same cookie between several servers, for example |
| 4245 | * to dedicate backup servers to specific servers only. |
| 4246 | * However, to prevent clients from sticking to cookie-less backup server |
| 4247 | * when they have incidentely learned an empty cookie, we simply ignore |
| 4248 | * empty cookies and mark them as invalid. |
| 4249 | */ |
| 4250 | if (delim == p3) |
| 4251 | srv = NULL; |
| 4252 | |
| 4253 | while (srv) { |
Willy Tarreau | 92f2ab1 | 2007-02-02 22:14:47 +0100 | [diff] [blame] | 4254 | if (srv->cookie && (srv->cklen == delim - p3) && |
| 4255 | !memcmp(p3, srv->cookie, delim - p3)) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4256 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4257 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4258 | txn->flags &= ~TX_CK_MASK; |
| 4259 | txn->flags |= TX_CK_VALID; |
| 4260 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4261 | t->srv = srv; |
| 4262 | break; |
| 4263 | } else { |
| 4264 | /* we found a server, but it's down */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4265 | txn->flags &= ~TX_CK_MASK; |
| 4266 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4267 | } |
| 4268 | } |
| 4269 | srv = srv->next; |
| 4270 | } |
| 4271 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4272 | if (!srv && !(txn->flags & TX_CK_DOWN)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4273 | /* no server matched this cookie */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4274 | txn->flags &= ~TX_CK_MASK; |
| 4275 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4276 | } |
| 4277 | |
| 4278 | /* depending on the cookie mode, we may have to either : |
| 4279 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 4280 | * the server never sees it ; |
| 4281 | * - remove the server id from the cookie value, and tag the cookie as an |
| 4282 | * application cookie so that it does not get accidentely removed later, |
| 4283 | * if we're in cookie prefix mode |
| 4284 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4285 | if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4286 | int delta; /* negative */ |
| 4287 | |
| 4288 | delta = buffer_replace2(req, p3, delim + 1, NULL, 0); |
| 4289 | p4 += delta; |
| 4290 | cur_end += delta; |
| 4291 | cur_next += delta; |
| 4292 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4293 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4294 | |
| 4295 | del_cookie = del_colon = NULL; |
| 4296 | app_cookies++; /* protect the header from deletion */ |
| 4297 | } |
| 4298 | else if (del_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4299 | (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] | 4300 | del_cookie = p1; |
| 4301 | del_colon = colon; |
| 4302 | } |
| 4303 | } else { |
| 4304 | /* now we know that we must keep this cookie since it's |
| 4305 | * not ours. But if we wanted to delete our cookie |
| 4306 | * earlier, we cannot remove the complete header, but we |
| 4307 | * can remove the previous block itself. |
| 4308 | */ |
| 4309 | app_cookies++; |
| 4310 | |
| 4311 | if (del_cookie != NULL) { |
| 4312 | int delta; /* negative */ |
| 4313 | |
| 4314 | delta = buffer_replace2(req, del_cookie, p1, NULL, 0); |
| 4315 | p4 += delta; |
| 4316 | cur_end += delta; |
| 4317 | cur_next += delta; |
| 4318 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4319 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4320 | del_cookie = del_colon = NULL; |
| 4321 | } |
| 4322 | } |
| 4323 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4324 | if ((t->be->appsession_name != NULL) && |
| 4325 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4326 | /* first, let's see if the cookie is our appcookie*/ |
| 4327 | |
| 4328 | /* Cool... it's the right one */ |
| 4329 | |
| 4330 | asession_temp = &local_asession; |
| 4331 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4332 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4333 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 4334 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 4335 | return; |
| 4336 | } |
| 4337 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4338 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 4339 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4340 | asession_temp->serverid = NULL; |
| 4341 | |
| 4342 | /* only do insert, if lookup fails */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4343 | if (chtbl_lookup(&(t->be->htbl_proxy), (void *) &asession_temp) != 0) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4344 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4345 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4346 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4347 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 4348 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 4349 | return; |
| 4350 | } |
| 4351 | |
| 4352 | asession_temp->sessid = local_asession.sessid; |
| 4353 | asession_temp->serverid = local_asession.serverid; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4354 | chtbl_insert(&(t->be->htbl_proxy), (void *) asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4355 | } else { |
| 4356 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4357 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4358 | } |
| 4359 | |
| 4360 | if (asession_temp->serverid == NULL) { |
| 4361 | Alert("Found Application Session without matching server.\n"); |
| 4362 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4363 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4364 | while (srv) { |
| 4365 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4366 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4367 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4368 | txn->flags &= ~TX_CK_MASK; |
| 4369 | txn->flags |= TX_CK_VALID; |
| 4370 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4371 | t->srv = srv; |
| 4372 | break; |
| 4373 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4374 | txn->flags &= ~TX_CK_MASK; |
| 4375 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4376 | } |
| 4377 | } |
| 4378 | srv = srv->next; |
| 4379 | }/* end while(srv) */ |
| 4380 | }/* end else if server == NULL */ |
| 4381 | |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 4382 | tv_add(&asession_temp->expire, &now, &t->be->appsession_timeout); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4383 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 4384 | } |
| 4385 | |
| 4386 | /* we'll have to look for another cookie ... */ |
| 4387 | p1 = p4; |
| 4388 | } /* while (p1 < cur_end) */ |
| 4389 | |
| 4390 | /* There's no more cookie on this line. |
| 4391 | * We may have marked the last one(s) for deletion. |
| 4392 | * We must do this now in two ways : |
| 4393 | * - if there is no app cookie, we simply delete the header ; |
| 4394 | * - if there are app cookies, we must delete the end of the |
| 4395 | * string properly, including the colon/semi-colon before |
| 4396 | * the cookie name. |
| 4397 | */ |
| 4398 | if (del_cookie != NULL) { |
| 4399 | int delta; |
| 4400 | if (app_cookies) { |
| 4401 | delta = buffer_replace2(req, del_colon, cur_end, NULL, 0); |
| 4402 | cur_end = del_colon; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4403 | cur_hdr->len += delta; |
| 4404 | } else { |
| 4405 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4406 | |
| 4407 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4408 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4409 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4410 | cur_hdr->len = 0; |
| 4411 | } |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 4412 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4413 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4414 | } |
| 4415 | |
| 4416 | /* keep the link from this header to next one */ |
| 4417 | old_idx = cur_idx; |
| 4418 | } /* end of cookie processing on this header */ |
| 4419 | } |
| 4420 | |
| 4421 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4422 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 4423 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 4424 | */ |
| 4425 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4426 | { |
| 4427 | char term; |
| 4428 | char *cur_ptr, *cur_end, *cur_next; |
| 4429 | int cur_idx, old_idx, last_hdr; |
| 4430 | struct http_txn *txn = &t->txn; |
| 4431 | struct hdr_idx_elem *cur_hdr; |
| 4432 | int len, delta; |
| 4433 | |
| 4434 | last_hdr = 0; |
| 4435 | |
| 4436 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4437 | old_idx = 0; |
| 4438 | |
| 4439 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4440 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4441 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4442 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4443 | (exp->action == ACT_ALLOW || |
| 4444 | exp->action == ACT_DENY)) |
| 4445 | return 0; |
| 4446 | |
| 4447 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 4448 | if (!cur_idx) |
| 4449 | break; |
| 4450 | |
| 4451 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4452 | cur_ptr = cur_next; |
| 4453 | cur_end = cur_ptr + cur_hdr->len; |
| 4454 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4455 | |
| 4456 | /* Now we have one header between cur_ptr and cur_end, |
| 4457 | * and the next header starts at cur_next. |
| 4458 | */ |
| 4459 | |
| 4460 | /* The annoying part is that pattern matching needs |
| 4461 | * that we modify the contents to null-terminate all |
| 4462 | * strings before testing them. |
| 4463 | */ |
| 4464 | |
| 4465 | term = *cur_end; |
| 4466 | *cur_end = '\0'; |
| 4467 | |
| 4468 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4469 | switch (exp->action) { |
| 4470 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4471 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4472 | last_hdr = 1; |
| 4473 | break; |
| 4474 | |
| 4475 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4476 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4477 | last_hdr = 1; |
| 4478 | break; |
| 4479 | |
| 4480 | case ACT_REPLACE: |
| 4481 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4482 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 4483 | /* FIXME: if the user adds a newline in the replacement, the |
| 4484 | * index will not be recalculated for now, and the new line |
| 4485 | * will not be counted as a new header. |
| 4486 | */ |
| 4487 | |
| 4488 | cur_end += delta; |
| 4489 | cur_next += delta; |
| 4490 | cur_hdr->len += delta; |
| 4491 | txn->rsp.eoh += delta; |
| 4492 | break; |
| 4493 | |
| 4494 | case ACT_REMOVE: |
| 4495 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 4496 | cur_next += delta; |
| 4497 | |
| 4498 | /* FIXME: this should be a separate function */ |
| 4499 | txn->rsp.eoh += delta; |
| 4500 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4501 | txn->hdr_idx.used--; |
| 4502 | cur_hdr->len = 0; |
| 4503 | cur_end = NULL; /* null-term has been rewritten */ |
| 4504 | break; |
| 4505 | |
| 4506 | } |
| 4507 | } |
| 4508 | if (cur_end) |
| 4509 | *cur_end = term; /* restore the string terminator */ |
| 4510 | |
| 4511 | /* keep the link from this header to next one in case of later |
| 4512 | * removal of next header. |
| 4513 | */ |
| 4514 | old_idx = cur_idx; |
| 4515 | } |
| 4516 | return 0; |
| 4517 | } |
| 4518 | |
| 4519 | |
| 4520 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 4521 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 4522 | * or -1 if a replacement resulted in an invalid status line. |
| 4523 | */ |
| 4524 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4525 | { |
| 4526 | char term; |
| 4527 | char *cur_ptr, *cur_end; |
| 4528 | int done; |
| 4529 | struct http_txn *txn = &t->txn; |
| 4530 | int len, delta; |
| 4531 | |
| 4532 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4533 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4534 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4535 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4536 | (exp->action == ACT_ALLOW || |
| 4537 | exp->action == ACT_DENY)) |
| 4538 | return 0; |
| 4539 | else if (exp->action == ACT_REMOVE) |
| 4540 | return 0; |
| 4541 | |
| 4542 | done = 0; |
| 4543 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4544 | 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] | 4545 | cur_end = cur_ptr + txn->rsp.sl.rq.l; |
| 4546 | |
| 4547 | /* Now we have the status line between cur_ptr and cur_end */ |
| 4548 | |
| 4549 | /* The annoying part is that pattern matching needs |
| 4550 | * that we modify the contents to null-terminate all |
| 4551 | * strings before testing them. |
| 4552 | */ |
| 4553 | |
| 4554 | term = *cur_end; |
| 4555 | *cur_end = '\0'; |
| 4556 | |
| 4557 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4558 | switch (exp->action) { |
| 4559 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4560 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4561 | done = 1; |
| 4562 | break; |
| 4563 | |
| 4564 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4565 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4566 | done = 1; |
| 4567 | break; |
| 4568 | |
| 4569 | case ACT_REPLACE: |
| 4570 | *cur_end = term; /* restore the string terminator */ |
| 4571 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4572 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 4573 | /* FIXME: if the user adds a newline in the replacement, the |
| 4574 | * index will not be recalculated for now, and the new line |
| 4575 | * will not be counted as a new header. |
| 4576 | */ |
| 4577 | |
| 4578 | txn->rsp.eoh += delta; |
| 4579 | cur_end += delta; |
| 4580 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4581 | 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] | 4582 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 4583 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4584 | cur_ptr, cur_end + 1, |
| 4585 | NULL, NULL); |
| 4586 | if (unlikely(!cur_end)) |
| 4587 | return -1; |
| 4588 | |
| 4589 | /* we have a full respnse and we know that we have either a CR |
| 4590 | * or an LF at <ptr>. |
| 4591 | */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4592 | 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] | 4593 | hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r'); |
| 4594 | /* there is no point trying this regex on headers */ |
| 4595 | return 1; |
| 4596 | } |
| 4597 | } |
| 4598 | *cur_end = term; /* restore the string terminator */ |
| 4599 | return done; |
| 4600 | } |
| 4601 | |
| 4602 | |
| 4603 | |
| 4604 | /* |
| 4605 | * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>. |
| 4606 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 4607 | * unparsable response. |
| 4608 | */ |
| 4609 | int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4610 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4611 | struct http_txn *txn = &t->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4612 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4613 | while (exp && !(txn->flags & TX_SVDENY)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4614 | int ret; |
| 4615 | |
| 4616 | /* |
| 4617 | * The interleaving of transformations and verdicts |
| 4618 | * makes it difficult to decide to continue or stop |
| 4619 | * the evaluation. |
| 4620 | */ |
| 4621 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4622 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4623 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 4624 | exp->action == ACT_PASS)) { |
| 4625 | exp = exp->next; |
| 4626 | continue; |
| 4627 | } |
| 4628 | |
| 4629 | /* Apply the filter to the status line. */ |
| 4630 | ret = apply_filter_to_sts_line(t, rtr, exp); |
| 4631 | if (unlikely(ret < 0)) |
| 4632 | return -1; |
| 4633 | |
| 4634 | if (likely(ret == 0)) { |
| 4635 | /* The filter did not match the response, it can be |
| 4636 | * iterated through all headers. |
| 4637 | */ |
| 4638 | apply_filter_to_resp_headers(t, rtr, exp); |
| 4639 | } |
| 4640 | exp = exp->next; |
| 4641 | } |
| 4642 | return 0; |
| 4643 | } |
| 4644 | |
| 4645 | |
| 4646 | |
| 4647 | /* |
| 4648 | * Manager server-side cookies |
| 4649 | */ |
| 4650 | void manage_server_side_cookies(struct session *t, struct buffer *rtr) |
| 4651 | { |
| 4652 | struct http_txn *txn = &t->txn; |
| 4653 | char *p1, *p2, *p3, *p4; |
| 4654 | |
| 4655 | appsess *asession_temp = NULL; |
| 4656 | appsess local_asession; |
| 4657 | |
| 4658 | char *cur_ptr, *cur_end, *cur_next; |
| 4659 | int cur_idx, old_idx, delta; |
| 4660 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4661 | if (t->be->cookie_name == NULL && |
| 4662 | t->be->appsession_name == NULL && |
| 4663 | t->be->capture_name == NULL && |
| 4664 | !(t->be->options & PR_O_CHK_CACHE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4665 | return; |
| 4666 | |
| 4667 | /* Iterate through the headers. |
| 4668 | * we start with the start line. |
| 4669 | */ |
| 4670 | old_idx = 0; |
| 4671 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4672 | |
| 4673 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 4674 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4675 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4676 | |
| 4677 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4678 | cur_ptr = cur_next; |
| 4679 | cur_end = cur_ptr + cur_hdr->len; |
| 4680 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4681 | |
| 4682 | /* We have one full header between cur_ptr and cur_end, and the |
| 4683 | * next header starts at cur_next. We're only interested in |
| 4684 | * "Cookie:" headers. |
| 4685 | */ |
| 4686 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4687 | val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10); |
| 4688 | if (!val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4689 | old_idx = cur_idx; |
| 4690 | continue; |
| 4691 | } |
| 4692 | |
| 4693 | /* 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] | 4694 | txn->flags |= TX_SCK_ANY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4695 | |
| 4696 | |
| 4697 | /* maybe we only wanted to see if there was a set-cookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4698 | if (t->be->cookie_name == NULL && |
| 4699 | t->be->appsession_name == NULL && |
| 4700 | t->be->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4701 | return; |
| 4702 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4703 | p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4704 | |
| 4705 | 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] | 4706 | if (p1 == cur_end || *p1 == ';') /* end of cookie */ |
| 4707 | break; |
| 4708 | |
| 4709 | /* p1 is at the beginning of the cookie name */ |
| 4710 | p2 = p1; |
| 4711 | |
| 4712 | while (p2 < cur_end && *p2 != '=' && *p2 != ';') |
| 4713 | p2++; |
| 4714 | |
| 4715 | if (p2 == cur_end || *p2 == ';') /* next cookie */ |
| 4716 | break; |
| 4717 | |
| 4718 | p3 = p2 + 1; /* skip the '=' sign */ |
| 4719 | if (p3 == cur_end) |
| 4720 | break; |
| 4721 | |
| 4722 | p4 = p3; |
| 4723 | while (p4 < cur_end && !isspace((int)*p4) && *p4 != ';') |
| 4724 | p4++; |
| 4725 | |
| 4726 | /* here, we have the cookie name between p1 and p2, |
| 4727 | * and its value between p3 and p4. |
| 4728 | * we can process it. |
| 4729 | */ |
| 4730 | |
| 4731 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4732 | if (t->be->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4733 | txn->srv_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4734 | (p4 - p1 >= t->be->capture_namelen) && |
| 4735 | memcmp(p1, t->be->capture_name, t->be->capture_namelen) == 0) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4736 | int log_len = p4 - p1; |
| 4737 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 4738 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4739 | Alert("HTTP logging : out of memory.\n"); |
| 4740 | } |
| 4741 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4742 | if (log_len > t->be->capture_len) |
| 4743 | log_len = t->be->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4744 | memcpy(txn->srv_cookie, p1, log_len); |
| 4745 | txn->srv_cookie[log_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4746 | } |
| 4747 | |
| 4748 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4749 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 4750 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4751 | /* Cool... it's the right one */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4752 | txn->flags |= TX_SCK_SEEN; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4753 | |
| 4754 | /* If the cookie is in insert mode on a known server, we'll delete |
| 4755 | * this occurrence because we'll insert another one later. |
| 4756 | * We'll delete it too if the "indirect" option is set and we're in |
| 4757 | * a direct access. */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4758 | if (((t->srv) && (t->be->options & PR_O_COOK_INS)) || |
| 4759 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4760 | /* this header must be deleted */ |
| 4761 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 4762 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4763 | txn->hdr_idx.used--; |
| 4764 | cur_hdr->len = 0; |
| 4765 | cur_next += delta; |
| 4766 | txn->rsp.eoh += delta; |
| 4767 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4768 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4769 | } |
| 4770 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4771 | (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4772 | /* replace bytes p3->p4 with the cookie name associated |
| 4773 | * with this server since we know it. |
| 4774 | */ |
| 4775 | delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen); |
| 4776 | cur_hdr->len += delta; |
| 4777 | cur_next += delta; |
| 4778 | txn->rsp.eoh += delta; |
| 4779 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4780 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4781 | } |
| 4782 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4783 | (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4784 | /* insert the cookie name associated with this server |
| 4785 | * before existing cookie, and insert a delimitor between them.. |
| 4786 | */ |
| 4787 | delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1); |
| 4788 | cur_hdr->len += delta; |
| 4789 | cur_next += delta; |
| 4790 | txn->rsp.eoh += delta; |
| 4791 | |
| 4792 | p3[t->srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4793 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4794 | } |
| 4795 | } |
| 4796 | /* next, let's see if the cookie is our appcookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4797 | else if ((t->be->appsession_name != NULL) && |
| 4798 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4799 | |
| 4800 | /* Cool... it's the right one */ |
| 4801 | |
| 4802 | size_t server_id_len = strlen(t->srv->id) + 1; |
| 4803 | asession_temp = &local_asession; |
| 4804 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4805 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4806 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4807 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4808 | return; |
| 4809 | } |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4810 | memcpy(asession_temp->sessid, p3, t->be->appsession_len); |
| 4811 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4812 | asession_temp->serverid = NULL; |
| 4813 | |
| 4814 | /* only do insert, if lookup fails */ |
| 4815 | if (chtbl_lookup(&(t->be->htbl_proxy), (void *) &asession_temp) != 0) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4816 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4817 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 4818 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 4819 | return; |
| 4820 | } |
| 4821 | asession_temp->sessid = local_asession.sessid; |
| 4822 | asession_temp->serverid = local_asession.serverid; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4823 | chtbl_insert(&(t->be->htbl_proxy), (void *) asession_temp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4824 | }/* end if (chtbl_lookup()) */ |
| 4825 | else { |
| 4826 | /* free wasted memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4827 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4828 | } /* end else from if (chtbl_lookup()) */ |
| 4829 | |
| 4830 | if (asession_temp->serverid == NULL) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4831 | if ((asession_temp->serverid = pool_alloc2(apools.serverid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4832 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4833 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4834 | return; |
| 4835 | } |
| 4836 | asession_temp->serverid[0] = '\0'; |
| 4837 | } |
| 4838 | |
| 4839 | if (asession_temp->serverid[0] == '\0') |
| 4840 | memcpy(asession_temp->serverid, t->srv->id, server_id_len); |
| 4841 | |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 4842 | tv_add(&asession_temp->expire, &now, &t->be->appsession_timeout); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4843 | |
| 4844 | #if defined(DEBUG_HASH) |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4845 | print_table(&(t->be->htbl_proxy)); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4846 | #endif |
| 4847 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 4848 | break; /* we don't want to loop again since there cannot be another cookie on the same line */ |
| 4849 | } /* we're now at the end of the cookie value */ |
| 4850 | |
| 4851 | /* keep the link from this header to next one */ |
| 4852 | old_idx = cur_idx; |
| 4853 | } /* end of cookie processing on this header */ |
| 4854 | } |
| 4855 | |
| 4856 | |
| 4857 | |
| 4858 | /* |
| 4859 | * Check if response is cacheable or not. Updates t->flags. |
| 4860 | */ |
| 4861 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 4862 | { |
| 4863 | struct http_txn *txn = &t->txn; |
| 4864 | char *p1, *p2; |
| 4865 | |
| 4866 | char *cur_ptr, *cur_end, *cur_next; |
| 4867 | int cur_idx; |
| 4868 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4869 | if (!txn->flags & TX_CACHEABLE) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4870 | return; |
| 4871 | |
| 4872 | /* Iterate through the headers. |
| 4873 | * we start with the start line. |
| 4874 | */ |
| 4875 | cur_idx = 0; |
| 4876 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4877 | |
| 4878 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 4879 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4880 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4881 | |
| 4882 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4883 | cur_ptr = cur_next; |
| 4884 | cur_end = cur_ptr + cur_hdr->len; |
| 4885 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4886 | |
| 4887 | /* We have one full header between cur_ptr and cur_end, and the |
| 4888 | * next header starts at cur_next. We're only interested in |
| 4889 | * "Cookie:" headers. |
| 4890 | */ |
| 4891 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4892 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 4893 | if (val) { |
| 4894 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 4895 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 4896 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 4897 | return; |
| 4898 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4899 | } |
| 4900 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4901 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 4902 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4903 | continue; |
| 4904 | |
| 4905 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 4906 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4907 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4908 | |
| 4909 | if (p1 >= cur_end) /* no more info */ |
| 4910 | continue; |
| 4911 | |
| 4912 | /* p1 is at the beginning of the value */ |
| 4913 | p2 = p1; |
| 4914 | |
| 4915 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((int)*p2)) |
| 4916 | p2++; |
| 4917 | |
| 4918 | /* we have a complete value between p1 and p2 */ |
| 4919 | if (p2 < cur_end && *p2 == '=') { |
| 4920 | /* we have something of the form no-cache="set-cookie" */ |
| 4921 | if ((cur_end - p1 >= 21) && |
| 4922 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 4923 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4924 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4925 | continue; |
| 4926 | } |
| 4927 | |
| 4928 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 4929 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 4930 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 4931 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 4932 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4933 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4934 | return; |
| 4935 | } |
| 4936 | |
| 4937 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4938 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4939 | continue; |
| 4940 | } |
| 4941 | } |
| 4942 | } |
| 4943 | |
| 4944 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4945 | /* |
| 4946 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 4947 | * If the server is found, it's assigned to the session. |
| 4948 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4949 | 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] | 4950 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4951 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4952 | appsess *asession_temp = NULL; |
| 4953 | appsess local_asession; |
| 4954 | char *request_line; |
| 4955 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4956 | if (t->be->appsession_name == NULL || |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 4957 | (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) || |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4958 | (request_line = memchr(begin, ';', len)) == NULL || |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4959 | ((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] | 4960 | return; |
| 4961 | |
| 4962 | /* skip ';' */ |
| 4963 | request_line++; |
| 4964 | |
| 4965 | /* look if we have a jsessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4966 | 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] | 4967 | return; |
| 4968 | |
| 4969 | /* skip jsessionid= */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4970 | request_line += t->be->appsession_name_len + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4971 | |
| 4972 | /* First try if we already have an appsession */ |
| 4973 | asession_temp = &local_asession; |
| 4974 | |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4975 | if ((asession_temp->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4976 | Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n"); |
| 4977 | send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n"); |
| 4978 | return; |
| 4979 | } |
| 4980 | |
| 4981 | /* Copy the sessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4982 | memcpy(asession_temp->sessid, request_line, t->be->appsession_len); |
| 4983 | asession_temp->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4984 | asession_temp->serverid = NULL; |
| 4985 | |
| 4986 | /* only do insert, if lookup fails */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4987 | if (chtbl_lookup(&(t->be->htbl_proxy), (void *)&asession_temp)) { |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4988 | if ((asession_temp = pool_alloc2(pool2_appsess)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4989 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 4990 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4991 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 4992 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 4993 | return; |
| 4994 | } |
| 4995 | asession_temp->sessid = local_asession.sessid; |
| 4996 | asession_temp->serverid = local_asession.serverid; |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4997 | chtbl_insert(&(t->be->htbl_proxy), (void *) asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4998 | } |
| 4999 | else { |
| 5000 | /* free previously allocated memory */ |
Willy Tarreau | 63963c6 | 2007-05-13 21:29:55 +0200 | [diff] [blame] | 5001 | pool_free2(apools.sessid, local_asession.sessid); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5002 | } |
| 5003 | |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 5004 | tv_add(&asession_temp->expire, &now, &t->be->appsession_timeout); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5005 | asession_temp->request_count++; |
| 5006 | |
| 5007 | #if defined(DEBUG_HASH) |
| 5008 | print_table(&(t->proxy->htbl_proxy)); |
| 5009 | #endif |
| 5010 | if (asession_temp->serverid == NULL) { |
| 5011 | Alert("Found Application Session without matching server.\n"); |
| 5012 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5013 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5014 | while (srv) { |
| 5015 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5016 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5017 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5018 | txn->flags &= ~TX_CK_MASK; |
| 5019 | txn->flags |= TX_CK_VALID; |
| 5020 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5021 | t->srv = srv; |
| 5022 | break; |
| 5023 | } else { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5024 | txn->flags &= ~TX_CK_MASK; |
| 5025 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5026 | } |
| 5027 | } |
| 5028 | srv = srv->next; |
| 5029 | } |
| 5030 | } |
| 5031 | } |
| 5032 | |
| 5033 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5034 | /* |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5035 | * In a GET or HEAD request, check if the requested URI matches the stats uri |
| 5036 | * 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] | 5037 | * |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5038 | * 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] | 5039 | * 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] | 5040 | * produce_content() can be called to start sending data. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5041 | * |
| 5042 | * Returns 1 if the session's state changes, otherwise 0. |
| 5043 | */ |
| 5044 | int stats_check_uri_auth(struct session *t, struct proxy *backend) |
| 5045 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5046 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5047 | struct uri_auth *uri_auth = backend->uri_auth; |
| 5048 | struct user_auth *user; |
| 5049 | int authenticated, cur_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5050 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5051 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5052 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5053 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5054 | return 0; |
| 5055 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5056 | h = t->req->data + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5057 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 5058 | /* the URI is in h */ |
| 5059 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5060 | return 0; |
| 5061 | |
| 5062 | /* we are in front of a interceptable URI. Let's check |
| 5063 | * if there's an authentication and if it's valid. |
| 5064 | */ |
| 5065 | user = uri_auth->users; |
| 5066 | if (!user) { |
| 5067 | /* no user auth required, it's OK */ |
| 5068 | authenticated = 1; |
| 5069 | } else { |
| 5070 | authenticated = 0; |
| 5071 | |
| 5072 | /* a user list is defined, we have to check. |
| 5073 | * skip 21 chars for "Authorization: Basic ". |
| 5074 | */ |
| 5075 | |
| 5076 | /* FIXME: this should move to an earlier place */ |
| 5077 | cur_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5078 | h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 5079 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 5080 | int len = txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5081 | if (len > 14 && |
| 5082 | !strncasecmp("Authorization:", h, 14)) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5083 | txn->auth_hdr.str = h; |
| 5084 | txn->auth_hdr.len = len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5085 | break; |
| 5086 | } |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5087 | h += len + txn->hdr_idx.v[cur_idx].cr + 1; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5088 | } |
| 5089 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5090 | if (txn->auth_hdr.len < 21 || |
| 5091 | memcmp(txn->auth_hdr.str + 14, " Basic ", 7)) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5092 | user = NULL; |
| 5093 | |
| 5094 | while (user) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5095 | if ((txn->auth_hdr.len == user->user_len + 14 + 7) |
| 5096 | && !memcmp(txn->auth_hdr.str + 14 + 7, |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5097 | user->user_pwd, user->user_len)) { |
| 5098 | authenticated = 1; |
| 5099 | break; |
| 5100 | } |
| 5101 | user = user->next; |
| 5102 | } |
| 5103 | } |
| 5104 | |
| 5105 | if (!authenticated) { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5106 | struct chunk msg; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5107 | |
| 5108 | /* no need to go further */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5109 | msg.str = trash; |
| 5110 | msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm); |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5111 | txn->status = 401; |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 5112 | client_retnclose(t, &msg); |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5113 | if (!(t->flags & SN_ERR_MASK)) |
| 5114 | t->flags |= SN_ERR_PRXCOND; |
| 5115 | if (!(t->flags & SN_FINST_MASK)) |
| 5116 | t->flags |= SN_FINST_R; |
| 5117 | return 1; |
| 5118 | } |
| 5119 | |
| 5120 | /* The request is valid, the user is authenticate. Let's start sending |
| 5121 | * data. |
| 5122 | */ |
| 5123 | t->cli_state = CL_STSHUTR; |
| 5124 | t->req->rlim = t->req->data + BUFSIZE; /* no more rewrite needed */ |
Willy Tarreau | 42aae5c | 2007-04-29 17:43:56 +0200 | [diff] [blame] | 5125 | t->logs.t_request = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 5126 | t->data_source = DATA_SRC_STATS; |
| 5127 | t->data_state = DATA_ST_INIT; |
| 5128 | produce_content(t); |
| 5129 | return 1; |
| 5130 | } |
| 5131 | |
| 5132 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5133 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5134 | * Print a debug line with a header |
| 5135 | */ |
| 5136 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 5137 | { |
| 5138 | int len, max; |
| 5139 | len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id, |
| 5140 | dir, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd); |
| 5141 | max = end - start; |
| 5142 | UBOUND(max, sizeof(trash) - len - 1); |
| 5143 | len += strlcpy2(trash + len, start, max + 1); |
| 5144 | trash[len++] = '\n'; |
| 5145 | write(1, trash, len); |
| 5146 | } |
| 5147 | |
| 5148 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5149 | /************************************************************************/ |
| 5150 | /* The code below is dedicated to ACL parsing and matching */ |
| 5151 | /************************************************************************/ |
| 5152 | |
| 5153 | |
| 5154 | |
| 5155 | |
| 5156 | /* 1. Check on METHOD |
| 5157 | * We use the pre-parsed method if it is known, and store its number as an |
| 5158 | * integer. If it is unknown, we use the pointer and the length. |
| 5159 | */ |
| 5160 | static int acl_parse_meth(const char *text, struct acl_pattern *pattern) |
| 5161 | { |
| 5162 | int len, meth; |
| 5163 | |
| 5164 | len = strlen(text); |
| 5165 | meth = find_http_meth(text, len); |
| 5166 | |
| 5167 | pattern->val.i = meth; |
| 5168 | if (meth == HTTP_METH_OTHER) { |
| 5169 | pattern->ptr.str = strdup(text); |
| 5170 | if (!pattern->ptr.str) |
| 5171 | return 0; |
| 5172 | pattern->len = len; |
| 5173 | } |
| 5174 | return 1; |
| 5175 | } |
| 5176 | |
| 5177 | static int acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, void *arg, struct acl_test *test) |
| 5178 | { |
| 5179 | int meth; |
| 5180 | struct http_txn *txn = l7; |
| 5181 | |
| 5182 | meth = txn->meth; |
| 5183 | test->i = meth; |
| 5184 | if (meth == HTTP_METH_OTHER) { |
| 5185 | test->len = txn->req.sl.rq.m_l; |
| 5186 | test->ptr = txn->req.sol; |
| 5187 | } |
| 5188 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5189 | return 1; |
| 5190 | } |
| 5191 | |
| 5192 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 5193 | { |
| 5194 | if (test->i != pattern->val.i) |
| 5195 | return 0; |
| 5196 | |
| 5197 | if (test->i != HTTP_METH_OTHER) |
| 5198 | return 1; |
| 5199 | |
| 5200 | /* Other method, we must compare the strings */ |
| 5201 | if (pattern->len != test->len) |
| 5202 | return 0; |
| 5203 | if (strncmp(pattern->ptr.str, test->ptr, test->len) != 0) |
| 5204 | return 0; |
| 5205 | return 1; |
| 5206 | } |
| 5207 | |
| 5208 | /* 2. Check on Request/Status Version |
| 5209 | * We simply compare strings here. |
| 5210 | */ |
| 5211 | static int acl_parse_ver(const char *text, struct acl_pattern *pattern) |
| 5212 | { |
| 5213 | pattern->ptr.str = strdup(text); |
| 5214 | if (!pattern->ptr.str) |
| 5215 | return 0; |
| 5216 | pattern->len = strlen(text); |
| 5217 | return 1; |
| 5218 | } |
| 5219 | |
| 5220 | static int acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, void *arg, struct acl_test *test) |
| 5221 | { |
| 5222 | struct http_txn *txn = l7; |
| 5223 | char *ptr; |
| 5224 | int len; |
| 5225 | |
| 5226 | len = txn->req.sl.rq.v_l; |
| 5227 | ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som; |
| 5228 | |
| 5229 | while ((len-- > 0) && (*ptr++ != '/')); |
| 5230 | if (len <= 0) |
| 5231 | return 0; |
| 5232 | |
| 5233 | test->ptr = ptr; |
| 5234 | test->len = len; |
| 5235 | |
| 5236 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5237 | return 1; |
| 5238 | } |
| 5239 | |
| 5240 | static int acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, void *arg, struct acl_test *test) |
| 5241 | { |
| 5242 | struct http_txn *txn = l7; |
| 5243 | char *ptr; |
| 5244 | int len; |
| 5245 | |
| 5246 | len = txn->rsp.sl.st.v_l; |
| 5247 | ptr = txn->rsp.sol; |
| 5248 | |
| 5249 | while ((len-- > 0) && (*ptr++ != '/')); |
| 5250 | if (len <= 0) |
| 5251 | return 0; |
| 5252 | |
| 5253 | test->ptr = ptr; |
| 5254 | test->len = len; |
| 5255 | |
| 5256 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 5257 | return 1; |
| 5258 | } |
| 5259 | |
| 5260 | /* 3. Check on Status Code. We manipulate integers here. */ |
| 5261 | static int acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, void *arg, struct acl_test *test) |
| 5262 | { |
| 5263 | struct http_txn *txn = l7; |
| 5264 | char *ptr; |
| 5265 | int len; |
| 5266 | |
| 5267 | len = txn->rsp.sl.st.c_l; |
| 5268 | ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som; |
| 5269 | |
| 5270 | test->i = __strl2ui(ptr, len); |
| 5271 | test->flags = ACL_TEST_F_VOL_1ST; |
| 5272 | return 1; |
| 5273 | } |
| 5274 | |
| 5275 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
| 5276 | static int acl_fetch_url(struct proxy *px, struct session *l4, void *l7, void *arg, struct acl_test *test) |
| 5277 | { |
| 5278 | struct http_txn *txn = l7; |
| 5279 | |
| 5280 | test->len = txn->req.sl.rq.u_l; |
| 5281 | test->ptr = txn->req.sol + txn->req.sl.rq.u; |
| 5282 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 5283 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 5284 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5285 | return 1; |
| 5286 | } |
| 5287 | |
| 5288 | |
| 5289 | |
| 5290 | /************************************************************************/ |
| 5291 | /* All supported keywords must be declared here. */ |
| 5292 | /************************************************************************/ |
| 5293 | |
| 5294 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 5295 | static struct acl_kw_list acl_kws = {{ },{ |
| 5296 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth }, |
| 5297 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str }, |
| 5298 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str }, |
| 5299 | { "status", acl_parse_range, acl_fetch_stcode, acl_match_range }, |
| 5300 | |
| 5301 | { "url", acl_parse_str, acl_fetch_url, acl_match_str }, |
| 5302 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg }, |
| 5303 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end }, |
| 5304 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub }, |
| 5305 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir }, |
| 5306 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5307 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg }, |
| 5308 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 5309 | { NULL, NULL, NULL, NULL }, |
| 5310 | |
| 5311 | #if 0 |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5312 | { "line", acl_parse_str, acl_fetch_line, acl_match_str }, |
| 5313 | { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg }, |
| 5314 | { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg }, |
| 5315 | { "line_end", acl_parse_str, acl_fetch_line, acl_match_end }, |
| 5316 | { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub }, |
| 5317 | { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir }, |
| 5318 | { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom }, |
| 5319 | |
| 5320 | { "path", acl_parse_str, acl_fetch_path, acl_match_str }, |
| 5321 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg }, |
| 5322 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg }, |
| 5323 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end }, |
| 5324 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub }, |
| 5325 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir }, |
| 5326 | { "path_dom", acl_parse_str, acl_fetch_path, acl_match_dom }, |
| 5327 | |
| 5328 | { "hdr", acl_parse_str, acl_fetch_hdr, acl_match_str }, |
| 5329 | { "hdr_reg", acl_parse_reg, acl_fetch_hdr, acl_match_reg }, |
| 5330 | { "hdr_beg", acl_parse_str, acl_fetch_hdr, acl_match_beg }, |
| 5331 | { "hdr_end", acl_parse_str, acl_fetch_hdr, acl_match_end }, |
| 5332 | { "hdr_sub", acl_parse_str, acl_fetch_hdr, acl_match_sub }, |
| 5333 | { "hdr_dir", acl_parse_str, acl_fetch_hdr, acl_match_dir }, |
| 5334 | { "hdr_dom", acl_parse_str, acl_fetch_hdr, acl_match_dom }, |
| 5335 | { "hdr_pst", acl_parse_none, acl_fetch_hdr, acl_match_pst }, |
| 5336 | |
| 5337 | { "cook", acl_parse_str, acl_fetch_cook, acl_match_str }, |
| 5338 | { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg }, |
| 5339 | { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg }, |
| 5340 | { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end }, |
| 5341 | { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub }, |
| 5342 | { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir }, |
| 5343 | { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom }, |
| 5344 | { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst }, |
| 5345 | |
| 5346 | { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str }, |
| 5347 | { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg }, |
| 5348 | { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str }, |
| 5349 | { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 }, |
| 5350 | { NULL, NULL, NULL, NULL }, |
| 5351 | #endif |
| 5352 | }}; |
| 5353 | |
| 5354 | |
| 5355 | __attribute__((constructor)) |
| 5356 | static void __http_protocol_init(void) |
| 5357 | { |
| 5358 | acl_register_keywords(&acl_kws); |
| 5359 | } |
| 5360 | |
| 5361 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5362 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5363 | * Local variables: |
| 5364 | * c-indent-level: 8 |
| 5365 | * c-basic-offset: 8 |
| 5366 | * End: |
| 5367 | */ |