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