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