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