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