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