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 | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 365 | t->logs.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. */ |
| 1290 | t->logs.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; |
| 1341 | t->logs.status = 200; |
| 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 */ |
| 1353 | if ((t->logs.uri = pool_alloc(requri)) != NULL) { |
| 1354 | int urilen = msg->sl.rq.l; |
| 1355 | |
| 1356 | if (urilen >= REQURI_LEN) |
| 1357 | urilen = REQURI_LEN - 1; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1358 | memcpy(t->logs.uri, &req->data[msg->som], urilen); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1359 | t->logs.uri[urilen] = 0; |
| 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 ? */ |
| 1453 | if (t->flags & SN_CLDENY) { |
| 1454 | /* no need to go further */ |
| 1455 | t->logs.status = 403; |
| 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 | */ |
| 1588 | if (!(t->flags & (SN_CLDENY|SN_CLTARPIT))) |
| 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 | */ |
| 1670 | if (t->flags & SN_CLTARPIT) { |
| 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 | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1684 | t->logs.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; |
| 1989 | struct buffer *req = t->req; |
| 1990 | struct buffer *rep = t->rep; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1991 | int conn_err; |
| 1992 | |
| 1993 | #ifdef DEBUG_FULL |
| 1994 | fprintf(stderr,"process_srv: c=%s, s=%s\n", cli_stnames[c], srv_stnames[s]); |
| 1995 | #endif |
| 1996 | //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] | 1997 | //MY_FD_ISSET(t->cli_fd, StaticReadEvent), MY_FD_ISSET(t->cli_fd, StaticWriteEvent), |
| 1998 | //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] | 1999 | //); |
| 2000 | if (s == SV_STIDLE) { |
| 2001 | if (c == CL_STHEADERS) |
| 2002 | return 0; /* stay in idle, waiting for data to reach the client side */ |
| 2003 | else if (c == CL_STCLOSE || c == CL_STSHUTW || |
| 2004 | (c == CL_STSHUTR && |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2005 | (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] | 2006 | tv_eternity(&req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2007 | if (t->pend_pos) |
| 2008 | t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now); |
| 2009 | /* note that this must not return any error because it would be able to |
| 2010 | * overwrite the client_retnclose() output. |
| 2011 | */ |
Willy Tarreau | 08fa2e3 | 2006-09-03 10:47:37 +0200 | [diff] [blame] | 2012 | if (t->flags & SN_CLTARPIT) |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 2013 | 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] | 2014 | else |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 2015 | 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] | 2016 | |
| 2017 | return 1; |
| 2018 | } |
| 2019 | else { |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 2020 | if (t->flags & SN_CLTARPIT) { |
| 2021 | /* This connection is being tarpitted. The CLIENT side has |
| 2022 | * already set the connect expiration date to the right |
| 2023 | * timeout. We just have to check that it has not expired. |
| 2024 | */ |
| 2025 | if (tv_cmp2_ms(&req->cex, &now) > 0) |
| 2026 | return 0; |
| 2027 | |
| 2028 | /* We will set the queue timer to the time spent, just for |
| 2029 | * logging purposes. We fake a 500 server error, so that the |
| 2030 | * attacker will not suspect his connection has been tarpitted. |
| 2031 | * It will not cause trouble to the logs because we can exclude |
| 2032 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 2033 | */ |
| 2034 | tv_eternity(&req->cex); |
| 2035 | t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now); |
| 2036 | srv_close_with_err(t, SN_ERR_PRXCOND, SN_FINST_T, |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2037 | 500, error_message(t, HTTP_ERR_500)); |
Willy Tarreau | b8750a8 | 2006-09-03 09:56:00 +0200 | [diff] [blame] | 2038 | return 1; |
| 2039 | } |
| 2040 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2041 | /* Right now, we will need to create a connection to the server. |
| 2042 | * We might already have tried, and got a connection pending, in |
| 2043 | * which case we will not do anything till it's pending. It's up |
| 2044 | * to any other session to release it and wake us up again. |
| 2045 | */ |
| 2046 | if (t->pend_pos) { |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2047 | if (tv_cmp2_ms(&req->cex, &now) > 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2048 | return 0; |
| 2049 | else { |
| 2050 | /* we've been waiting too long here */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2051 | tv_eternity(&req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2052 | t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now); |
| 2053 | srv_close_with_err(t, SN_ERR_SRVTO, SN_FINST_Q, |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2054 | 503, error_message(t, HTTP_ERR_503)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2055 | if (t->srv) |
| 2056 | t->srv->failed_conns++; |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 2057 | t->fe->failed_conns++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2058 | return 1; |
| 2059 | } |
| 2060 | } |
| 2061 | |
| 2062 | do { |
| 2063 | /* first, get a connection */ |
| 2064 | if (srv_redispatch_connect(t)) |
| 2065 | return t->srv_state != SV_STIDLE; |
| 2066 | |
| 2067 | /* try to (re-)connect to the server, and fail if we expire the |
| 2068 | * number of retries. |
| 2069 | */ |
| 2070 | if (srv_retryable_connect(t)) { |
| 2071 | t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now); |
| 2072 | return t->srv_state != SV_STIDLE; |
| 2073 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2074 | } while (1); |
| 2075 | } |
| 2076 | } |
| 2077 | else if (s == SV_STCONN) { /* connection in progress */ |
| 2078 | if (c == CL_STCLOSE || c == CL_STSHUTW || |
| 2079 | (c == CL_STSHUTR && |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2080 | (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] | 2081 | tv_eternity(&req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2082 | fd_delete(t->srv_fd); |
| 2083 | if (t->srv) |
| 2084 | t->srv->cur_sess--; |
| 2085 | |
| 2086 | /* note that this must not return any error because it would be able to |
| 2087 | * overwrite the client_retnclose() output. |
| 2088 | */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 2089 | 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] | 2090 | return 1; |
| 2091 | } |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2092 | if (!(req->flags & BF_WRITE_STATUS) && tv_cmp2_ms(&req->cex, &now) > 0) { |
| 2093 | //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] | 2094 | return 0; /* nothing changed */ |
| 2095 | } |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2096 | else if (!(req->flags & BF_WRITE_STATUS) || (req->flags & BF_WRITE_ERROR)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2097 | /* timeout, asynchronous connect error or first write error */ |
| 2098 | //fprintf(stderr,"2: c=%d, s=%d\n", c, s); |
| 2099 | |
| 2100 | fd_delete(t->srv_fd); |
| 2101 | if (t->srv) |
| 2102 | t->srv->cur_sess--; |
| 2103 | |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2104 | if (!(req->flags & BF_WRITE_STATUS)) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2105 | conn_err = SN_ERR_SRVTO; // it was a connect timeout. |
| 2106 | else |
| 2107 | conn_err = SN_ERR_SRVCL; // it was an asynchronous connect error. |
| 2108 | |
| 2109 | /* ensure that we have enough retries left */ |
| 2110 | if (srv_count_retry_down(t, conn_err)) |
| 2111 | return 1; |
| 2112 | |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2113 | 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] | 2114 | /* We're on our last chance, and the REDISP option was specified. |
| 2115 | * We will ignore cookie and force to balance or use the dispatcher. |
| 2116 | */ |
| 2117 | /* let's try to offer this slot to anybody */ |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2118 | if (may_dequeue_tasks(t->srv, t->be->beprm)) |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 2119 | task_wakeup(&rq, t->srv->queue_mgt); |
| 2120 | |
| 2121 | if (t->srv) |
| 2122 | t->srv->failed_conns++; |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2123 | t->be->beprm->failed_conns++; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 2124 | |
| 2125 | t->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET); |
| 2126 | t->srv = NULL; /* it's left to the dispatcher to choose a server */ |
| 2127 | if ((t->flags & SN_CK_MASK) == SN_CK_VALID) { |
| 2128 | t->flags &= ~SN_CK_MASK; |
| 2129 | t->flags |= SN_CK_DOWN; |
| 2130 | } |
| 2131 | |
| 2132 | /* first, get a connection */ |
| 2133 | if (srv_redispatch_connect(t)) |
| 2134 | return t->srv_state != SV_STIDLE; |
| 2135 | } |
| 2136 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2137 | do { |
| 2138 | /* Now we will try to either reconnect to the same server or |
| 2139 | * connect to another server. If the connection gets queued |
| 2140 | * because all servers are saturated, then we will go back to |
| 2141 | * the SV_STIDLE state. |
| 2142 | */ |
| 2143 | if (srv_retryable_connect(t)) { |
| 2144 | t->logs.t_queue = tv_diff(&t->logs.tv_accept, &now); |
| 2145 | return t->srv_state != SV_STCONN; |
| 2146 | } |
| 2147 | |
| 2148 | /* we need to redispatch the connection to another server */ |
| 2149 | if (srv_redispatch_connect(t)) |
| 2150 | return t->srv_state != SV_STCONN; |
| 2151 | } while (1); |
| 2152 | } |
| 2153 | else { /* no error or write 0 */ |
| 2154 | t->logs.t_connect = tv_diff(&t->logs.tv_accept, &now); |
| 2155 | |
| 2156 | //fprintf(stderr,"3: c=%d, s=%d\n", c, s); |
| 2157 | if (req->l == 0) /* nothing to write */ { |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 2158 | MY_FD_CLR(t->srv_fd, StaticWriteEvent); |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2159 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2160 | } else /* need the right to write */ { |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 2161 | MY_FD_SET(t->srv_fd, StaticWriteEvent); |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2162 | if (t->be->beprm->srvtimeout) { |
| 2163 | tv_delayfrom(&req->wex, &now, t->be->beprm->srvtimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2164 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 2165 | * we refresh it. */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2166 | rep->rex = req->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2167 | } |
| 2168 | else |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2169 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2170 | } |
| 2171 | |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2172 | 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] | 2173 | MY_FD_SET(t->srv_fd, StaticReadEvent); |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2174 | if (t->be->beprm->srvtimeout) |
| 2175 | tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2176 | else |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2177 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2178 | |
| 2179 | t->srv_state = SV_STDATA; |
| 2180 | if (t->srv) |
| 2181 | t->srv->cum_sess++; |
| 2182 | rep->rlim = rep->data + BUFSIZE; /* no rewrite needed */ |
| 2183 | |
| 2184 | /* if the user wants to log as soon as possible, without counting |
| 2185 | bytes from the server, then this is the right moment. */ |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 2186 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2187 | t->logs.t_close = t->logs.t_connect; /* to get a valid end date */ |
| 2188 | sess_log(t); |
| 2189 | } |
Willy Tarreau | 6d1a988 | 2007-01-07 02:03:04 +0100 | [diff] [blame] | 2190 | #ifdef CONFIG_HAP_TCPSPLICE |
| 2191 | if ((t->fe->options & t->be->beprm->options) & PR_O_TCPSPLICE) { |
| 2192 | /* TCP splicing supported by both FE and BE */ |
| 2193 | tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0); |
| 2194 | } |
| 2195 | #endif |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2196 | } |
| 2197 | else { |
| 2198 | t->srv_state = SV_STHEADERS; |
| 2199 | if (t->srv) |
| 2200 | t->srv->cum_sess++; |
| 2201 | rep->rlim = rep->data + BUFSIZE - MAXREWRITE; /* rewrite needed */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2202 | t->txn.rsp.msg_state = HTTP_MSG_RPBEFORE; |
| 2203 | /* reset hdr_idx which was already initialized by the request. |
| 2204 | * right now, the http parser does it. |
| 2205 | * hdr_idx_init(&t->txn.hdr_idx); |
| 2206 | */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2207 | } |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2208 | tv_eternity(&req->cex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2209 | return 1; |
| 2210 | } |
| 2211 | } |
| 2212 | else if (s == SV_STHEADERS) { /* receiving server headers */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2213 | /* |
| 2214 | * Now parse the partial (or complete) lines. |
| 2215 | * We will check the response syntax, and also join multi-line |
| 2216 | * headers. An index of all the lines will be elaborated while |
| 2217 | * parsing. |
| 2218 | * |
| 2219 | * For the parsing, we use a 28 states FSM. |
| 2220 | * |
| 2221 | * Here is the information we currently have : |
| 2222 | * rep->data + req->som = beginning of response |
| 2223 | * rep->data + req->eoh = end of processed headers / start of current one |
| 2224 | * rep->data + req->eol = end of current header or line (LF or CRLF) |
| 2225 | * rep->lr = first non-visited byte |
| 2226 | * rep->r = end of data |
| 2227 | */ |
| 2228 | |
| 2229 | int cur_idx; |
| 2230 | struct http_txn *txn = &t->txn; |
| 2231 | struct http_msg *msg = &txn->rsp; |
| 2232 | struct proxy *cur_proxy; |
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 | if (likely(rep->lr < rep->r)) |
| 2235 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2236 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2237 | /* 1: we might have to print this header in debug mode */ |
| 2238 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2239 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
| 2240 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
| 2241 | char *eol, *sol; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2242 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2243 | sol = rep->data + msg->som; |
| 2244 | eol = sol + msg->sl.rq.l; |
| 2245 | debug_hdr("srvrep", t, sol, eol); |
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 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2248 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2249 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2250 | while (cur_idx) { |
| 2251 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2252 | debug_hdr("srvhdr", t, sol, eol); |
| 2253 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2254 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 2255 | } |
| 2256 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2257 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2258 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2259 | if ((rep->l < rep->rlim - rep->data) && ! MY_FD_ISSET(t->srv_fd, StaticReadEvent)) { |
| 2260 | /* fd in StaticReadEvent was disabled, perhaps because of a previous buffer |
| 2261 | * full. We cannot loop here since stream_sock_read will disable it only if |
| 2262 | * rep->l == rlim-data |
| 2263 | */ |
| 2264 | MY_FD_SET(t->srv_fd, StaticReadEvent); |
| 2265 | if (t->be->beprm->srvtimeout) |
| 2266 | tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout); |
| 2267 | else |
| 2268 | tv_eternity(&rep->rex); |
| 2269 | } |
| 2270 | |
| 2271 | |
| 2272 | /* |
| 2273 | * Now we quickly check if we have found a full valid response. |
| 2274 | * If not so, we check the FD and buffer states before leaving. |
| 2275 | * A full response is indicated by the fact that we have seen |
| 2276 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 2277 | * responses are checked first. |
| 2278 | * |
| 2279 | * Depending on whether the client is still there or not, we |
| 2280 | * may send an error response back or not. Note that normally |
| 2281 | * we should only check for HTTP status there, and check I/O |
| 2282 | * errors somewhere else. |
| 2283 | */ |
| 2284 | |
| 2285 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
| 2286 | |
| 2287 | /* Invalid response, or read error or write error */ |
| 2288 | if (unlikely((msg->msg_state == HTTP_MSG_ERROR) || |
| 2289 | (req->flags & BF_WRITE_ERROR) || |
| 2290 | (rep->flags & BF_READ_ERROR))) { |
| 2291 | tv_eternity(&rep->rex); |
| 2292 | tv_eternity(&req->wex); |
| 2293 | fd_delete(t->srv_fd); |
| 2294 | if (t->srv) { |
| 2295 | t->srv->cur_sess--; |
| 2296 | t->srv->failed_resp++; |
| 2297 | } |
| 2298 | t->be->failed_resp++; |
| 2299 | t->srv_state = SV_STCLOSE; |
| 2300 | t->logs.status = 502; |
| 2301 | client_return(t, error_message(t, HTTP_ERR_502)); |
| 2302 | if (!(t->flags & SN_ERR_MASK)) |
| 2303 | t->flags |= SN_ERR_SRVCL; |
| 2304 | if (!(t->flags & SN_FINST_MASK)) |
| 2305 | t->flags |= SN_FINST_H; |
| 2306 | /* We used to have a free connection slot. Since we'll never use it, |
| 2307 | * we have to inform the server that it may be used by another session. |
| 2308 | */ |
| 2309 | if (t->srv && may_dequeue_tasks(t->srv, t->be->beprm)) |
| 2310 | task_wakeup(&rq, t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2311 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2312 | return 1; |
| 2313 | } |
| 2314 | |
| 2315 | /* end of client write or end of server read. |
| 2316 | * since we are in header mode, if there's no space left for headers, we |
| 2317 | * won't be able to free more later, so the session will never terminate. |
| 2318 | */ |
| 2319 | else if (unlikely(rep->flags & BF_READ_NULL || |
| 2320 | c == CL_STSHUTW || c == CL_STCLOSE || |
| 2321 | rep->l >= rep->rlim - rep->data)) { |
| 2322 | MY_FD_CLR(t->srv_fd, StaticReadEvent); |
| 2323 | tv_eternity(&rep->rex); |
| 2324 | shutdown(t->srv_fd, SHUT_RD); |
| 2325 | t->srv_state = SV_STSHUTR; |
| 2326 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 2327 | return 1; |
| 2328 | } |
| 2329 | |
| 2330 | /* read timeout : return a 504 to the client. |
| 2331 | */ |
| 2332 | else if (unlikely(MY_FD_ISSET(t->srv_fd, StaticReadEvent) && |
| 2333 | tv_cmp2_ms(&rep->rex, &now) <= 0)) { |
| 2334 | tv_eternity(&rep->rex); |
| 2335 | tv_eternity(&req->wex); |
| 2336 | fd_delete(t->srv_fd); |
| 2337 | if (t->srv) { |
| 2338 | t->srv->cur_sess--; |
| 2339 | t->srv->failed_resp++; |
| 2340 | } |
| 2341 | t->be->failed_resp++; |
| 2342 | t->srv_state = SV_STCLOSE; |
| 2343 | t->logs.status = 504; |
| 2344 | client_return(t, error_message(t, HTTP_ERR_504)); |
| 2345 | if (!(t->flags & SN_ERR_MASK)) |
| 2346 | t->flags |= SN_ERR_SRVTO; |
| 2347 | if (!(t->flags & SN_FINST_MASK)) |
| 2348 | t->flags |= SN_FINST_H; |
| 2349 | /* We used to have a free connection slot. Since we'll never use it, |
| 2350 | * we have to inform the server that it may be used by another session. |
| 2351 | */ |
| 2352 | if (t->srv && may_dequeue_tasks(t->srv, t->be->beprm)) |
| 2353 | task_wakeup(&rq, t->srv->queue_mgt); |
| 2354 | return 1; |
| 2355 | } |
| 2356 | |
| 2357 | /* last client read and buffer empty */ |
| 2358 | /* FIXME!!! here, we don't want to switch to SHUTW if the |
| 2359 | * client shuts read too early, because we may still have |
| 2360 | * some work to do on the headers. |
| 2361 | * The side-effect is that if the client completely closes its |
| 2362 | * connection during SV_STHEADER, the connection to the server |
| 2363 | * is kept until a response comes back or the timeout is reached. |
| 2364 | */ |
| 2365 | else if (unlikely((/*c == CL_STSHUTR ||*/ c == CL_STCLOSE) && |
| 2366 | (req->l == 0))) { |
| 2367 | MY_FD_CLR(t->srv_fd, StaticWriteEvent); |
| 2368 | tv_eternity(&req->wex); |
| 2369 | |
| 2370 | /* We must ensure that the read part is still |
| 2371 | * alive when switching to shutw */ |
| 2372 | MY_FD_SET(t->srv_fd, StaticReadEvent); |
| 2373 | if (t->be->beprm->srvtimeout) |
| 2374 | tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout); |
| 2375 | |
| 2376 | shutdown(t->srv_fd, SHUT_WR); |
| 2377 | t->srv_state = SV_STSHUTW; |
| 2378 | return 1; |
| 2379 | } |
| 2380 | |
| 2381 | /* write timeout */ |
| 2382 | /* FIXME!!! here, we don't want to switch to SHUTW if the |
| 2383 | * client shuts read too early, because we may still have |
| 2384 | * some work to do on the headers. |
| 2385 | */ |
| 2386 | else if (unlikely(MY_FD_ISSET(t->srv_fd, StaticWriteEvent) && |
| 2387 | tv_cmp2_ms(&req->wex, &now) <= 0)) { |
| 2388 | MY_FD_CLR(t->srv_fd, StaticWriteEvent); |
| 2389 | tv_eternity(&req->wex); |
| 2390 | shutdown(t->srv_fd, SHUT_WR); |
| 2391 | /* We must ensure that the read part is still alive |
| 2392 | * when switching to shutw */ |
| 2393 | MY_FD_SET(t->srv_fd, StaticReadEvent); |
| 2394 | if (t->be->beprm->srvtimeout) |
| 2395 | tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout); |
| 2396 | |
| 2397 | t->srv_state = SV_STSHUTW; |
| 2398 | if (!(t->flags & SN_ERR_MASK)) |
| 2399 | t->flags |= SN_ERR_SRVTO; |
| 2400 | if (!(t->flags & SN_FINST_MASK)) |
| 2401 | t->flags |= SN_FINST_H; |
| 2402 | return 1; |
| 2403 | } |
| 2404 | |
| 2405 | /* |
| 2406 | * And now the non-error cases. |
| 2407 | */ |
| 2408 | |
| 2409 | /* Data remaining in the request buffer. |
| 2410 | * This happens during the first pass here, and during |
| 2411 | * long posts. |
| 2412 | */ |
| 2413 | else if (likely(req->l)) { |
| 2414 | if (! MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 2415 | MY_FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */ |
| 2416 | if (t->be->beprm->srvtimeout) { |
| 2417 | tv_delayfrom(&req->wex, &now, t->be->beprm->srvtimeout); |
| 2418 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 2419 | * we refresh it. */ |
| 2420 | rep->rex = 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 | else |
| 2423 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2424 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2425 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2426 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2427 | /* nothing left in the request buffer */ |
| 2428 | else { |
| 2429 | if (MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 2430 | MY_FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2431 | tv_eternity(&req->wex); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2432 | } |
| 2433 | } |
| 2434 | |
| 2435 | return t->srv_state != SV_STHEADERS; |
| 2436 | } |
| 2437 | |
| 2438 | |
| 2439 | /***************************************************************** |
| 2440 | * More interesting part now : we know that we have a complete * |
| 2441 | * response which at least looks like HTTP. We have an indicator * |
| 2442 | * of each header's length, so we can parse them quickly. * |
| 2443 | ****************************************************************/ |
| 2444 | |
| 2445 | /* |
| 2446 | * 1: get the status code and check for cacheability. |
| 2447 | */ |
| 2448 | |
| 2449 | t->logs.logwait &= ~LW_RESP; |
| 2450 | t->logs.status = strl2ui(rep->data + msg->sl.st.c, msg->sl.st.c_l); |
| 2451 | |
| 2452 | switch (t->logs.status) { |
| 2453 | case 200: |
| 2454 | case 203: |
| 2455 | case 206: |
| 2456 | case 300: |
| 2457 | case 301: |
| 2458 | case 410: |
| 2459 | /* RFC2616 @13.4: |
| 2460 | * "A response received with a status code of |
| 2461 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 2462 | * by a cache (...) unless a cache-control |
| 2463 | * directive prohibits caching." |
| 2464 | * |
| 2465 | * RFC2616 @9.5: POST method : |
| 2466 | * "Responses to this method are not cacheable, |
| 2467 | * unless the response includes appropriate |
| 2468 | * Cache-Control or Expires header fields." |
| 2469 | */ |
| 2470 | if (likely(txn->meth != HTTP_METH_POST) && |
| 2471 | unlikely(t->be->beprm->options & PR_O_CHK_CACHE)) |
| 2472 | t->flags |= SN_CACHEABLE | SN_CACHE_COOK; |
| 2473 | break; |
| 2474 | default: |
| 2475 | break; |
| 2476 | } |
| 2477 | |
| 2478 | /* |
| 2479 | * 2: we may need to capture headers |
| 2480 | */ |
| 2481 | if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->fiprm->rsp_cap)) |
| 2482 | capture_headers(rep->data + msg->som, &txn->hdr_idx, |
| 2483 | txn->rsp.cap, t->fe->fiprm->rsp_cap); |
| 2484 | |
| 2485 | /* |
| 2486 | * 3: we will have to evaluate the filters. |
| 2487 | * As opposed to version 1.2, now they will be evaluated in the |
| 2488 | * filters order and not in the header order. This means that |
| 2489 | * each filter has to be validated among all headers. |
| 2490 | * |
| 2491 | * Filters are tried with ->be first, then with ->fe if it is |
| 2492 | * different from ->be. |
| 2493 | */ |
| 2494 | |
| 2495 | t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */ |
| 2496 | |
| 2497 | cur_proxy = t->be; |
| 2498 | while (1) { |
| 2499 | struct proxy *rule_set = cur_proxy->fiprm; |
| 2500 | |
| 2501 | /* try headers filters */ |
| 2502 | if (rule_set->rsp_exp != NULL) { |
| 2503 | if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) { |
| 2504 | return_bad_resp: |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2505 | if (t->srv) { |
| 2506 | t->srv->cur_sess--; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2507 | t->srv->failed_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2508 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2509 | cur_proxy->beprm->failed_resp++; |
| 2510 | return_srv_prx_502: |
| 2511 | tv_eternity(&rep->rex); |
| 2512 | tv_eternity(&req->wex); |
| 2513 | fd_delete(t->srv_fd); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2514 | t->srv_state = SV_STCLOSE; |
| 2515 | t->logs.status = 502; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 2516 | client_return(t, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2517 | if (!(t->flags & SN_ERR_MASK)) |
| 2518 | t->flags |= SN_ERR_PRXCOND; |
| 2519 | if (!(t->flags & SN_FINST_MASK)) |
| 2520 | t->flags |= SN_FINST_H; |
| 2521 | /* We used to have a free connection slot. Since we'll never use it, |
| 2522 | * we have to inform the server that it may be used by another session. |
| 2523 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2524 | if (t->srv && may_dequeue_tasks(t->srv, cur_proxy->beprm)) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2525 | task_wakeup(&rq, t->srv->queue_mgt); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2526 | return 1; |
| 2527 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2528 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2529 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2530 | /* has the response been denied ? */ |
| 2531 | if (t->flags & SN_SVDENY) { |
| 2532 | if (t->srv) { |
| 2533 | t->srv->cur_sess--; |
| 2534 | t->srv->failed_secu++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2535 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2536 | cur_proxy->beprm->denied_resp++; |
| 2537 | goto return_srv_prx_502; |
| 2538 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2539 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2540 | /* We might have to check for "Connection:" */ |
| 2541 | if (((t->fe->options | t->be->beprm->options) & PR_O_HTTP_CLOSE) && |
| 2542 | !(t->flags & SN_CONN_CLOSED)) { |
| 2543 | char *cur_ptr, *cur_end, *cur_next; |
| 2544 | int cur_idx, old_idx, delta; |
| 2545 | struct hdr_idx_elem *cur_hdr; |
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 | cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 2548 | old_idx = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2549 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2550 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 2551 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 2552 | cur_ptr = cur_next; |
| 2553 | cur_end = cur_ptr + cur_hdr->len; |
| 2554 | cur_next = cur_end + cur_hdr->cr + 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2555 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2556 | if (strncasecmp(cur_ptr, "Connection:", 11) == 0) { |
| 2557 | /* 3 possibilities : |
| 2558 | * - we have already set Connection: close, |
| 2559 | * so we remove this line. |
| 2560 | * - we have not yet set Connection: close, |
| 2561 | * but this line indicates close. We leave |
| 2562 | * it untouched and set the flag. |
| 2563 | * - we have not yet set Connection: close, |
| 2564 | * and this line indicates non-close. We |
| 2565 | * replace it. |
| 2566 | */ |
| 2567 | if (t->flags & SN_CONN_CLOSED) { |
| 2568 | delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0); |
| 2569 | txn->rsp.eoh += delta; |
| 2570 | cur_next += delta; |
| 2571 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 2572 | txn->hdr_idx.used--; |
| 2573 | cur_hdr->len = 0; |
| 2574 | } else { |
| 2575 | if (cur_ptr + 17 > cur_end || |
| 2576 | !http_is_lws[(unsigned char)*(cur_ptr+17)] || |
| 2577 | strncasecmp(cur_ptr+11, " close", 6)) { |
| 2578 | delta = buffer_replace2(rep, cur_ptr+11, cur_end, |
| 2579 | " close", 6); |
| 2580 | cur_next += delta; |
| 2581 | cur_hdr->len += delta; |
| 2582 | txn->rsp.eoh += delta; |
| 2583 | } |
| 2584 | t->flags |= SN_CONN_CLOSED; |
| 2585 | } |
| 2586 | } |
| 2587 | old_idx = cur_idx; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2588 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2589 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2590 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2591 | /* add response headers from the rule sets in the same order */ |
| 2592 | for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) { |
| 2593 | int len; |
| 2594 | |
| 2595 | len = sprintf(trash, "%s\r\n", rule_set->rsp_add[cur_idx]); |
| 2596 | len = buffer_replace2(rep, rep->data + txn->rsp.eoh, |
| 2597 | rep->data + txn->rsp.eoh, trash, len); |
| 2598 | txn->rsp.eoh += len; |
| 2599 | |
| 2600 | if (unlikely(hdr_idx_add(len - 2, 1, &txn->hdr_idx, txn->hdr_idx.tail) < 0)) |
| 2601 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2602 | } |
| 2603 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2604 | /* check whether we're already working on the frontend */ |
| 2605 | if (cur_proxy == t->fe) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2606 | break; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2607 | cur_proxy = t->fe; |
| 2608 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2609 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2610 | /* |
| 2611 | * 4: check for server cookie. |
| 2612 | */ |
| 2613 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2614 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2615 | /* |
| 2616 | * 5: add server cookie in the response if needed |
| 2617 | */ |
| 2618 | if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->beprm->options & PR_O_COOK_INS) && |
| 2619 | (!(t->be->beprm->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) { |
| 2620 | int len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2621 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2622 | /* the server is known, it's not the one the client requested, we have to |
| 2623 | * insert a set-cookie here, except if we want to insert only on POST |
| 2624 | * requests and this one isn't. Note that servers which don't have cookies |
| 2625 | * (eg: some backup servers) will return a full cookie removal request. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2626 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2627 | len = sprintf(trash, "Set-Cookie: %s=%s; path=/\r\n", |
| 2628 | t->be->beprm->cookie_name, |
| 2629 | 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] | 2630 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2631 | len = buffer_replace2(rep, rep->data + txn->rsp.eoh, rep->data + txn->rsp.eoh, trash, len); |
| 2632 | txn->rsp.eoh += len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2633 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2634 | if (hdr_idx_add(len - 2, 1, &txn->hdr_idx, txn->hdr_idx.tail) < 0) |
| 2635 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2636 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2637 | t->flags |= SN_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2638 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2639 | /* Here, we will tell an eventual cache on the client side that we don't |
| 2640 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 2641 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 2642 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 2643 | */ |
| 2644 | if (t->be->beprm->options & PR_O_COOK_NOC) { |
| 2645 | //len += sprintf(newhdr + len, "Cache-control: no-cache=\"set-cookie\"\r\n"); |
| 2646 | len = sprintf(trash, "Cache-control: private\r\n"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2647 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2648 | len = buffer_replace2(rep, rep->data + txn->rsp.eoh, |
| 2649 | rep->data + txn->rsp.eoh, trash, len); |
| 2650 | txn->rsp.eoh += len; |
| 2651 | if (hdr_idx_add(len - 2, 1, &txn->hdr_idx, txn->hdr_idx.tail) < 0) |
| 2652 | goto return_bad_resp; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2653 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2654 | } |
| 2655 | |
| 2656 | |
| 2657 | /* |
| 2658 | * 6: check for cache-control or pragma headers. |
| 2659 | */ |
| 2660 | check_response_for_cacheability(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2661 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2662 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2663 | /* |
| 2664 | * 7: check if result will be cacheable with a cookie. |
| 2665 | * We'll block the response if security checks have caught |
| 2666 | * nasty things such as a cacheable cookie. |
| 2667 | */ |
| 2668 | if (((t->flags & (SN_CACHEABLE | SN_CACHE_COOK | SN_SCK_ANY)) == |
| 2669 | (SN_CACHEABLE | SN_CACHE_COOK | SN_SCK_ANY)) && |
| 2670 | (t->be->beprm->options & PR_O_CHK_CACHE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2671 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2672 | /* we're in presence of a cacheable response containing |
| 2673 | * a set-cookie header. We'll block it as requested by |
| 2674 | * the 'checkcache' option, and send an alert. |
| 2675 | */ |
| 2676 | if (t->srv) { |
| 2677 | t->srv->cur_sess--; |
| 2678 | t->srv->failed_secu++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2679 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2680 | t->be->beprm->denied_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2681 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2682 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 2683 | t->be->beprm->id, t->srv?t->srv->id:"<dispatch>"); |
| 2684 | send_log(t->be, LOG_ALERT, |
| 2685 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 2686 | t->be->beprm->id, t->srv?t->srv->id:"<dispatch>"); |
| 2687 | goto return_srv_prx_502; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2688 | } |
| 2689 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2690 | /* |
| 2691 | * 8: add "Connection: close" if needed and not yet set. |
| 2692 | */ |
| 2693 | if (((t->fe->options | t->be->beprm->options) & PR_O_HTTP_CLOSE) && |
| 2694 | !(t->flags & SN_CONN_CLOSED)) { |
| 2695 | int len; |
| 2696 | len = buffer_replace2(rep, rep->data + txn->rsp.eoh, |
| 2697 | rep->data + txn->rsp.eoh, "Connection: close\r\n", 19); |
| 2698 | txn->rsp.eoh += len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2699 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2700 | if (hdr_idx_add(17, 1, &txn->hdr_idx, txn->hdr_idx.tail) < 0) |
| 2701 | goto return_bad_resp; |
| 2702 | t->flags |= SN_CONN_CLOSED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2703 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2704 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2705 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2706 | /************************************************************* |
| 2707 | * OK, that's finished for the headers. We have done what we * |
| 2708 | * could. Let's switch to the DATA state. * |
| 2709 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2710 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2711 | t->srv_state = SV_STDATA; |
| 2712 | rep->rlim = rep->data + BUFSIZE; /* no more rewrite needed */ |
| 2713 | t->logs.t_data = tv_diff(&t->logs.tv_accept, &now); |
| 2714 | |
| 2715 | /* client connection already closed or option 'forceclose' required : |
| 2716 | * we close the server's outgoing connection right now. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2717 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2718 | if ((req->l == 0) && |
| 2719 | (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] | 2720 | MY_FD_CLR(t->srv_fd, StaticWriteEvent); |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2721 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2722 | |
| 2723 | /* We must ensure that the read part is still alive when switching |
| 2724 | * to shutw */ |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 2725 | MY_FD_SET(t->srv_fd, StaticReadEvent); |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2726 | if (t->be->beprm->srvtimeout) |
| 2727 | tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2728 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2729 | shutdown(t->srv_fd, SHUT_WR); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2730 | t->srv_state = SV_STSHUTW; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2731 | } |
| 2732 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2733 | #ifdef CONFIG_HAP_TCPSPLICE |
| 2734 | if ((t->fe->options & t->be->beprm->options) & PR_O_TCPSPLICE) { |
| 2735 | /* TCP splicing supported by both FE and BE */ |
| 2736 | tcp_splice_splicefd(t->cli_fd, t->srv_fd, 0); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2737 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2738 | #endif |
| 2739 | /* if the user wants to log as soon as possible, without counting |
| 2740 | bytes from the server, then this is the right moment. */ |
| 2741 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 2742 | 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] | 2743 | t->logs.bytes_in = txn->rsp.eoh; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2744 | sess_log(t); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2745 | } |
| 2746 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2747 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 2748 | * otherwise we would not let the client side wake up. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2749 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 2750 | |
| 2751 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2752 | } |
| 2753 | else if (s == SV_STDATA) { |
| 2754 | /* read or write error */ |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2755 | if (req->flags & BF_WRITE_ERROR || rep->flags & BF_READ_ERROR) { |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2756 | tv_eternity(&rep->rex); |
| 2757 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2758 | fd_delete(t->srv_fd); |
| 2759 | if (t->srv) { |
| 2760 | t->srv->cur_sess--; |
| 2761 | t->srv->failed_resp++; |
| 2762 | } |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 2763 | t->be->failed_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2764 | t->srv_state = SV_STCLOSE; |
| 2765 | if (!(t->flags & SN_ERR_MASK)) |
| 2766 | t->flags |= SN_ERR_SRVCL; |
| 2767 | if (!(t->flags & SN_FINST_MASK)) |
| 2768 | t->flags |= SN_FINST_D; |
| 2769 | /* We used to have a free connection slot. Since we'll never use it, |
| 2770 | * we have to inform the server that it may be used by another session. |
| 2771 | */ |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2772 | if (may_dequeue_tasks(t->srv, t->be->beprm)) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2773 | task_wakeup(&rq, t->srv->queue_mgt); |
| 2774 | |
| 2775 | return 1; |
| 2776 | } |
| 2777 | /* last read, or end of client write */ |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2778 | 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] | 2779 | MY_FD_CLR(t->srv_fd, StaticReadEvent); |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2780 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2781 | shutdown(t->srv_fd, SHUT_RD); |
| 2782 | t->srv_state = SV_STSHUTR; |
| 2783 | //fprintf(stderr,"%p:%s(%d), c=%d, s=%d\n", t, __FUNCTION__, __LINE__, t->cli_state, t->cli_state); |
| 2784 | return 1; |
| 2785 | } |
| 2786 | /* end of client read and no more data to send */ |
| 2787 | else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) { |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 2788 | MY_FD_CLR(t->srv_fd, StaticWriteEvent); |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2789 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2790 | shutdown(t->srv_fd, SHUT_WR); |
| 2791 | /* We must ensure that the read part is still alive when switching |
| 2792 | * to shutw */ |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 2793 | MY_FD_SET(t->srv_fd, StaticReadEvent); |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2794 | if (t->be->beprm->srvtimeout) |
| 2795 | tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2796 | |
| 2797 | t->srv_state = SV_STSHUTW; |
| 2798 | return 1; |
| 2799 | } |
| 2800 | /* read timeout */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2801 | else if (tv_cmp2_ms(&rep->rex, &now) <= 0) { |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 2802 | MY_FD_CLR(t->srv_fd, StaticReadEvent); |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2803 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2804 | shutdown(t->srv_fd, SHUT_RD); |
| 2805 | t->srv_state = SV_STSHUTR; |
| 2806 | if (!(t->flags & SN_ERR_MASK)) |
| 2807 | t->flags |= SN_ERR_SRVTO; |
| 2808 | if (!(t->flags & SN_FINST_MASK)) |
| 2809 | t->flags |= SN_FINST_D; |
| 2810 | return 1; |
| 2811 | } |
| 2812 | /* write timeout */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2813 | else if (tv_cmp2_ms(&req->wex, &now) <= 0) { |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 2814 | MY_FD_CLR(t->srv_fd, StaticWriteEvent); |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2815 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2816 | shutdown(t->srv_fd, SHUT_WR); |
| 2817 | /* We must ensure that the read part is still alive when switching |
| 2818 | * to shutw */ |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 2819 | MY_FD_SET(t->srv_fd, StaticReadEvent); |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2820 | if (t->be->beprm->srvtimeout) |
| 2821 | tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2822 | t->srv_state = SV_STSHUTW; |
| 2823 | if (!(t->flags & SN_ERR_MASK)) |
| 2824 | t->flags |= SN_ERR_SRVTO; |
| 2825 | if (!(t->flags & SN_FINST_MASK)) |
| 2826 | t->flags |= SN_FINST_D; |
| 2827 | return 1; |
| 2828 | } |
| 2829 | |
| 2830 | /* recompute request time-outs */ |
| 2831 | if (req->l == 0) { |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 2832 | if (MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 2833 | MY_FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2834 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2835 | } |
| 2836 | } |
| 2837 | else { /* buffer not empty, there are still data to be transferred */ |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 2838 | if (! MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 2839 | MY_FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */ |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2840 | if (t->be->beprm->srvtimeout) { |
| 2841 | tv_delayfrom(&req->wex, &now, t->be->beprm->srvtimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2842 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 2843 | * we refresh it. */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2844 | rep->rex = req->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2845 | } |
| 2846 | else |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2847 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2848 | } |
| 2849 | } |
| 2850 | |
| 2851 | /* recompute response time-outs */ |
| 2852 | if (rep->l == BUFSIZE) { /* no room to read more data */ |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 2853 | if (MY_FD_ISSET(t->srv_fd, StaticReadEvent)) { |
| 2854 | MY_FD_CLR(t->srv_fd, StaticReadEvent); |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2855 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2856 | } |
| 2857 | } |
| 2858 | else { |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 2859 | if (! MY_FD_ISSET(t->srv_fd, StaticReadEvent)) { |
| 2860 | MY_FD_SET(t->srv_fd, StaticReadEvent); |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2861 | if (t->be->beprm->srvtimeout) |
| 2862 | tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2863 | else |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2864 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2865 | } |
| 2866 | } |
| 2867 | |
| 2868 | return 0; /* other cases change nothing */ |
| 2869 | } |
| 2870 | else if (s == SV_STSHUTR) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2871 | if (req->flags & BF_WRITE_ERROR) { |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 2872 | //MY_FD_CLR(t->srv_fd, StaticWriteEvent); |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2873 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2874 | fd_delete(t->srv_fd); |
| 2875 | if (t->srv) { |
| 2876 | t->srv->cur_sess--; |
| 2877 | t->srv->failed_resp++; |
| 2878 | } |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 2879 | t->be->failed_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2880 | //close(t->srv_fd); |
| 2881 | t->srv_state = SV_STCLOSE; |
| 2882 | if (!(t->flags & SN_ERR_MASK)) |
| 2883 | t->flags |= SN_ERR_SRVCL; |
| 2884 | if (!(t->flags & SN_FINST_MASK)) |
| 2885 | t->flags |= SN_FINST_D; |
| 2886 | /* We used to have a free connection slot. Since we'll never use it, |
| 2887 | * we have to inform the server that it may be used by another session. |
| 2888 | */ |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2889 | if (may_dequeue_tasks(t->srv, t->be->beprm)) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2890 | task_wakeup(&rq, t->srv->queue_mgt); |
| 2891 | |
| 2892 | return 1; |
| 2893 | } |
| 2894 | else if ((c == CL_STSHUTR || c == CL_STCLOSE) && (req->l == 0)) { |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 2895 | //MY_FD_CLR(t->srv_fd, StaticWriteEvent); |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2896 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2897 | fd_delete(t->srv_fd); |
| 2898 | if (t->srv) |
| 2899 | t->srv->cur_sess--; |
| 2900 | //close(t->srv_fd); |
| 2901 | t->srv_state = SV_STCLOSE; |
| 2902 | /* We used to have a free connection slot. Since we'll never use it, |
| 2903 | * we have to inform the server that it may be used by another session. |
| 2904 | */ |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2905 | if (may_dequeue_tasks(t->srv, t->be->beprm)) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2906 | task_wakeup(&rq, t->srv->queue_mgt); |
| 2907 | |
| 2908 | return 1; |
| 2909 | } |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2910 | else if (tv_cmp2_ms(&req->wex, &now) <= 0) { |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 2911 | //MY_FD_CLR(t->srv_fd, StaticWriteEvent); |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2912 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2913 | fd_delete(t->srv_fd); |
| 2914 | if (t->srv) |
| 2915 | t->srv->cur_sess--; |
| 2916 | //close(t->srv_fd); |
| 2917 | t->srv_state = SV_STCLOSE; |
| 2918 | if (!(t->flags & SN_ERR_MASK)) |
| 2919 | t->flags |= SN_ERR_SRVTO; |
| 2920 | if (!(t->flags & SN_FINST_MASK)) |
| 2921 | t->flags |= SN_FINST_D; |
| 2922 | /* We used to have a free connection slot. Since we'll never use it, |
| 2923 | * we have to inform the server that it may be used by another session. |
| 2924 | */ |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2925 | if (may_dequeue_tasks(t->srv, t->be->beprm)) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2926 | task_wakeup(&rq, t->srv->queue_mgt); |
| 2927 | |
| 2928 | return 1; |
| 2929 | } |
| 2930 | else if (req->l == 0) { |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 2931 | if (MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 2932 | MY_FD_CLR(t->srv_fd, StaticWriteEvent); /* stop writing */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2933 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2934 | } |
| 2935 | } |
| 2936 | else { /* buffer not empty */ |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 2937 | if (! MY_FD_ISSET(t->srv_fd, StaticWriteEvent)) { |
| 2938 | MY_FD_SET(t->srv_fd, StaticWriteEvent); /* restart writing */ |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2939 | if (t->be->beprm->srvtimeout) { |
| 2940 | tv_delayfrom(&req->wex, &now, t->be->beprm->srvtimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2941 | /* FIXME: to prevent the server from expiring read timeouts during writes, |
| 2942 | * we refresh it. */ |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2943 | rep->rex = req->wex; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2944 | } |
| 2945 | else |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2946 | tv_eternity(&req->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2947 | } |
| 2948 | } |
| 2949 | return 0; |
| 2950 | } |
| 2951 | else if (s == SV_STSHUTW) { |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2952 | if (rep->flags & BF_READ_ERROR) { |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 2953 | //MY_FD_CLR(t->srv_fd, StaticReadEvent); |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2954 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2955 | fd_delete(t->srv_fd); |
| 2956 | if (t->srv) { |
| 2957 | t->srv->cur_sess--; |
| 2958 | t->srv->failed_resp++; |
| 2959 | } |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 2960 | t->be->failed_resp++; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2961 | //close(t->srv_fd); |
| 2962 | t->srv_state = SV_STCLOSE; |
| 2963 | if (!(t->flags & SN_ERR_MASK)) |
| 2964 | t->flags |= SN_ERR_SRVCL; |
| 2965 | if (!(t->flags & SN_FINST_MASK)) |
| 2966 | t->flags |= SN_FINST_D; |
| 2967 | /* We used to have a free connection slot. Since we'll never use it, |
| 2968 | * we have to inform the server that it may be used by another session. |
| 2969 | */ |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2970 | if (may_dequeue_tasks(t->srv, t->be->beprm)) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2971 | task_wakeup(&rq, t->srv->queue_mgt); |
| 2972 | |
| 2973 | return 1; |
| 2974 | } |
Willy Tarreau | 0f9f505 | 2006-07-29 17:39:25 +0200 | [diff] [blame] | 2975 | 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] | 2976 | //MY_FD_CLR(t->srv_fd, StaticReadEvent); |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2977 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2978 | fd_delete(t->srv_fd); |
| 2979 | if (t->srv) |
| 2980 | t->srv->cur_sess--; |
| 2981 | //close(t->srv_fd); |
| 2982 | t->srv_state = SV_STCLOSE; |
| 2983 | /* We used to have a free connection slot. Since we'll never use it, |
| 2984 | * we have to inform the server that it may be used by another session. |
| 2985 | */ |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 2986 | if (may_dequeue_tasks(t->srv, t->be->beprm)) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2987 | task_wakeup(&rq, t->srv->queue_mgt); |
| 2988 | |
| 2989 | return 1; |
| 2990 | } |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2991 | else if (tv_cmp2_ms(&rep->rex, &now) <= 0) { |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 2992 | //MY_FD_CLR(t->srv_fd, StaticReadEvent); |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 2993 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2994 | fd_delete(t->srv_fd); |
| 2995 | if (t->srv) |
| 2996 | t->srv->cur_sess--; |
| 2997 | //close(t->srv_fd); |
| 2998 | t->srv_state = SV_STCLOSE; |
| 2999 | if (!(t->flags & SN_ERR_MASK)) |
| 3000 | t->flags |= SN_ERR_SRVTO; |
| 3001 | if (!(t->flags & SN_FINST_MASK)) |
| 3002 | t->flags |= SN_FINST_D; |
| 3003 | /* We used to have a free connection slot. Since we'll never use it, |
| 3004 | * we have to inform the server that it may be used by another session. |
| 3005 | */ |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 3006 | if (may_dequeue_tasks(t->srv, t->be->beprm)) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3007 | task_wakeup(&rq, t->srv->queue_mgt); |
| 3008 | |
| 3009 | return 1; |
| 3010 | } |
| 3011 | else if (rep->l == BUFSIZE) { /* no room to read more data */ |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 3012 | if (MY_FD_ISSET(t->srv_fd, StaticReadEvent)) { |
| 3013 | MY_FD_CLR(t->srv_fd, StaticReadEvent); |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3014 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3015 | } |
| 3016 | } |
| 3017 | else { |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 3018 | if (! MY_FD_ISSET(t->srv_fd, StaticReadEvent)) { |
| 3019 | MY_FD_SET(t->srv_fd, StaticReadEvent); |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 3020 | if (t->be->beprm->srvtimeout) |
| 3021 | tv_delayfrom(&rep->rex, &now, t->be->beprm->srvtimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3022 | else |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 3023 | tv_eternity(&rep->rex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3024 | } |
| 3025 | } |
| 3026 | return 0; |
| 3027 | } |
| 3028 | else { /* SV_STCLOSE : nothing to do */ |
| 3029 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 3030 | int len; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3031 | len = sprintf(trash, "%08x:%s.srvcls[%04x:%04x]\n", |
| 3032 | 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] | 3033 | write(1, trash, len); |
| 3034 | } |
| 3035 | return 0; |
| 3036 | } |
| 3037 | return 0; |
| 3038 | } |
| 3039 | |
| 3040 | |
| 3041 | /* |
| 3042 | * Produces data for the session <s> depending on its source. Expects to be |
| 3043 | * called with s->cli_state == CL_STSHUTR. Right now, only statistics can be |
| 3044 | * produced. It stops by itself by unsetting the SN_SELF_GEN flag from the |
| 3045 | * session, which it uses to keep on being called when there is free space in |
| 3046 | * the buffer, of simply by letting an empty buffer upon return. It returns 1 |
| 3047 | * if it changes the session state from CL_STSHUTR, otherwise 0. |
| 3048 | */ |
| 3049 | int produce_content(struct session *s) |
| 3050 | { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3051 | if (s->data_source == DATA_SRC_NONE) { |
| 3052 | s->flags &= ~SN_SELF_GEN; |
| 3053 | return 1; |
| 3054 | } |
| 3055 | else if (s->data_source == DATA_SRC_STATS) { |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3056 | /* dump server statistics */ |
| 3057 | return produce_content_stats(s); |
| 3058 | } |
| 3059 | else { |
| 3060 | /* unknown data source */ |
| 3061 | s->logs.status = 500; |
| 3062 | client_retnclose(s, error_message(s, HTTP_ERR_500)); |
| 3063 | if (!(s->flags & SN_ERR_MASK)) |
| 3064 | s->flags |= SN_ERR_PRXCOND; |
| 3065 | if (!(s->flags & SN_FINST_MASK)) |
| 3066 | s->flags |= SN_FINST_R; |
| 3067 | s->flags &= ~SN_SELF_GEN; |
| 3068 | return 1; |
| 3069 | } |
| 3070 | } |
| 3071 | |
| 3072 | |
| 3073 | /* |
| 3074 | * Produces statistics data for the session <s>. Expects to be called with |
| 3075 | * s->cli_state == CL_STSHUTR. It stops by itself by unsetting the SN_SELF_GEN |
| 3076 | * flag from the session, which it uses to keep on being called when there is |
| 3077 | * free space in the buffer, of simply by letting an empty buffer upon return. |
| 3078 | * It returns 1 if it changes the session state from CL_STSHUTR, otherwise 0. |
| 3079 | */ |
| 3080 | int produce_content_stats(struct session *s) |
| 3081 | { |
| 3082 | struct buffer *rep = s->rep; |
| 3083 | struct proxy *px; |
| 3084 | struct chunk msg; |
| 3085 | unsigned int up; |
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 | msg.len = 0; |
| 3088 | msg.str = trash; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3089 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3090 | switch (s->data_state) { |
| 3091 | case DATA_ST_INIT: |
| 3092 | /* the function had not been called yet */ |
| 3093 | s->flags |= SN_SELF_GEN; // more data will follow |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3094 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3095 | chunk_printf(&msg, sizeof(trash), |
| 3096 | "HTTP/1.0 200 OK\r\n" |
| 3097 | "Cache-Control: no-cache\r\n" |
| 3098 | "Connection: close\r\n" |
| 3099 | "Content-Type: text/html\r\n" |
| 3100 | "\r\n"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3101 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3102 | s->logs.status = 200; |
| 3103 | client_retnclose(s, &msg); // send the start of the response. |
| 3104 | msg.len = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3105 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3106 | if (!(s->flags & SN_ERR_MASK)) // this is not really an error but it is |
| 3107 | s->flags |= SN_ERR_PRXCOND; // to mark that it comes from the proxy |
| 3108 | if (!(s->flags & SN_FINST_MASK)) |
| 3109 | s->flags |= SN_FINST_R; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3110 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 3111 | if (s->txn.meth == HTTP_METH_HEAD) { |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 3112 | /* that's all we return in case of HEAD request */ |
| 3113 | s->data_state = DATA_ST_FIN; |
| 3114 | s->flags &= ~SN_SELF_GEN; |
| 3115 | return 1; |
| 3116 | } |
| 3117 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3118 | s->data_state = DATA_ST_HEAD; /* let's start producing data */ |
| 3119 | /* fall through */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3120 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3121 | case DATA_ST_HEAD: |
| 3122 | /* WARNING! This must fit in the first buffer !!! */ |
| 3123 | chunk_printf(&msg, sizeof(trash), |
| 3124 | "<html><head><title>Statistics Report for " PRODUCT_NAME "</title>\n" |
| 3125 | "<meta http-equiv=\"content-type\" content=\"text/html; charset=iso-8859-1\">\n" |
| 3126 | "<style type=\"text/css\"><!--\n" |
| 3127 | "body {" |
| 3128 | " font-family: helvetica, arial;" |
| 3129 | " font-size: 12px;" |
| 3130 | " font-weight: normal;" |
| 3131 | " color: black;" |
| 3132 | " background: white;" |
| 3133 | "}\n" |
| 3134 | "th,td {" |
| 3135 | " font-size: 0.8em;" |
| 3136 | " align: center;" |
| 3137 | "}" |
| 3138 | "h1 {" |
| 3139 | " font-size: xx-large;" |
| 3140 | " margin-bottom: 0.5em;" |
| 3141 | "}\n" |
| 3142 | "h2 {" |
| 3143 | " font-family: helvetica, arial;" |
| 3144 | " font-size: x-large;" |
| 3145 | " font-weight: bold;" |
| 3146 | " font-style: italic;" |
| 3147 | " color: #6020a0;" |
| 3148 | " margin-top: 0em;" |
| 3149 | " margin-bottom: 0em;" |
| 3150 | "}\n" |
| 3151 | "h3 {" |
| 3152 | " font-family: helvetica, arial;" |
| 3153 | " font-size: 16px;" |
| 3154 | " font-weight: bold;" |
| 3155 | " color: #b00040;" |
| 3156 | " background: #e8e8d0;" |
| 3157 | " margin-top: 0em;" |
| 3158 | " margin-bottom: 0em;" |
| 3159 | "}\n" |
| 3160 | "li {" |
| 3161 | " margin-top: 0.25em;" |
| 3162 | " margin-right: 2em;" |
| 3163 | "}\n" |
| 3164 | ".hr {margin-top: 0.25em;" |
| 3165 | " border-color: black;" |
| 3166 | " border-bottom-style: solid;" |
| 3167 | "}\n" |
| 3168 | ".pxname {background: #b00040;color: #ffff40;font-weight: bold;}\n" |
| 3169 | ".titre {background: #20D0D0;color: #000000;font-weight: bold;}\n" |
| 3170 | ".total {background: #20D0D0;color: #ffff80;}\n" |
| 3171 | ".frontend {background: #e8e8d0;}\n" |
| 3172 | ".backend {background: #e8e8d0;}\n" |
| 3173 | ".active0 {background: #ff9090;}\n" |
| 3174 | ".active1 {background: #ffd020;}\n" |
| 3175 | ".active2 {background: #ffffa0;}\n" |
| 3176 | ".active3 {background: #c0ffc0;}\n" |
| 3177 | ".active4 {background: #e0e0e0;}\n" |
| 3178 | ".backup0 {background: #ff9090;}\n" |
| 3179 | ".backup1 {background: #ff80ff;}\n" |
| 3180 | ".backup2 {background: #c060ff;}\n" |
| 3181 | ".backup3 {background: #b0d0ff;}\n" |
| 3182 | ".backup4 {background: #e0e0e0;}\n" |
| 3183 | "table.tbl { border-collapse: collapse; border-style: none;}\n" |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 3184 | "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] | 3185 | "table.tbl th { border-width: 1px; border-style: solid solid solid solid; border-color: gray;}\n" |
| 3186 | "table.tbl th.empty { border-style: none; empty-cells: hide;}\n" |
| 3187 | "table.lgd { border-collapse: collapse; border-width: 1px; border-style: none none none solid; border-color: black;}\n" |
| 3188 | "table.lgd td { border-width: 1px; border-style: solid solid solid solid; border-color: gray; padding: 2px;}\n" |
| 3189 | "table.lgd td.noborder { border-style: none; padding: 2px; white-space: nowrap;}\n" |
| 3190 | "-->" |
| 3191 | "</style></head>"); |
| 3192 | |
| 3193 | if (buffer_write_chunk(rep, &msg) != 0) |
| 3194 | return 0; |
| 3195 | |
| 3196 | s->data_state = DATA_ST_INFO; |
| 3197 | /* fall through */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3198 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3199 | case DATA_ST_INFO: |
| 3200 | up = (now.tv_sec - start_date.tv_sec); |
| 3201 | |
| 3202 | /* WARNING! this has to fit the first packet too. |
| 3203 | * We are around 3.5 kB, add adding entries will |
| 3204 | * become tricky if we want to support 4kB buffers ! |
| 3205 | */ |
| 3206 | chunk_printf(&msg, sizeof(trash), |
| 3207 | "<body><h1><a href=\"" PRODUCT_URL "\" style=\"text-decoration: none;\">" |
| 3208 | PRODUCT_NAME "</a></h1>\n" |
| 3209 | "<h2>Statistics Report for pid %d</h2>\n" |
| 3210 | "<hr width=\"100%%\" class=\"hr\">\n" |
| 3211 | "<h3>> General process information</h3>\n" |
| 3212 | "<table border=0 cols=3><tr><td align=\"left\" nowrap width=\"1%%\">\n" |
| 3213 | "<p><b>pid = </b> %d (nbproc = %d)<br>\n" |
| 3214 | "<b>uptime = </b> %dd %dh%02dm%02ds<br>\n" |
| 3215 | "<b>system limits :</b> memmax = %s%s ; ulimit-n = %d<br>\n" |
| 3216 | "<b>maxsock = </b> %d<br>\n" |
| 3217 | "<b>maxconn = </b> %d (current conns = %d)<br>\n" |
| 3218 | "</td><td align=\"center\" nowrap>\n" |
| 3219 | "<table class=\"lgd\"><tr>" |
| 3220 | "<td class=\"active3\"> </td><td class=\"noborder\">active UP </td>" |
| 3221 | "<td class=\"backup3\"> </td><td class=\"noborder\">backup UP </td>" |
| 3222 | "</tr><tr>" |
| 3223 | "<td class=\"active2\"></td><td class=\"noborder\">active UP, going down </td>" |
| 3224 | "<td class=\"backup2\"></td><td class=\"noborder\">backup UP, going down </td>" |
| 3225 | "</tr><tr>" |
| 3226 | "<td class=\"active1\"></td><td class=\"noborder\">active DOWN, going up </td>" |
| 3227 | "<td class=\"backup1\"></td><td class=\"noborder\">backup DOWN, going up </td>" |
| 3228 | "</tr><tr>" |
| 3229 | "<td class=\"active0\"></td><td class=\"noborder\">active or backup DOWN </td>" |
| 3230 | "<td class=\"active4\"></td><td class=\"noborder\">not checked </td>" |
| 3231 | "</tr></table>\n" |
| 3232 | "</td>" |
| 3233 | "<td align=\"left\" nowrap width=\"1%%\">" |
| 3234 | "<b>External ressources:</b><ul style=\"margin-top: 0.25em;\">" |
| 3235 | "<li><a href=\"" PRODUCT_URL "\">Primary site</a><br>" |
| 3236 | "<li><a href=\"" PRODUCT_URL_UPD "\">Updates (v" PRODUCT_BRANCH ")</a><br>" |
| 3237 | "<li><a href=\"" PRODUCT_URL_DOC "\">Online manual</a><br>" |
| 3238 | "</ul>" |
| 3239 | "</td>" |
| 3240 | "</tr></table>\n" |
| 3241 | "", |
| 3242 | pid, pid, global.nbproc, |
| 3243 | up / 86400, (up % 86400) / 3600, |
| 3244 | (up % 3600) / 60, (up % 60), |
| 3245 | global.rlimit_memmax ? ultoa(global.rlimit_memmax) : "unlimited", |
| 3246 | global.rlimit_memmax ? " MB" : "", |
| 3247 | global.rlimit_nofile, |
| 3248 | global.maxsock, |
| 3249 | global.maxconn, |
| 3250 | actconn |
| 3251 | ); |
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 | if (buffer_write_chunk(rep, &msg) != 0) |
| 3254 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3255 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3256 | memset(&s->data_ctx, 0, sizeof(s->data_ctx)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3257 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3258 | s->data_ctx.stats.px = proxy; |
| 3259 | s->data_ctx.stats.px_st = DATA_ST_PX_INIT; |
| 3260 | s->data_state = DATA_ST_LIST; |
| 3261 | /* fall through */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3262 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3263 | case DATA_ST_LIST: |
| 3264 | /* dump proxies */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3265 | while (s->data_ctx.stats.px) { |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3266 | px = s->data_ctx.stats.px; |
| 3267 | /* skip the disabled proxies and non-networked ones */ |
| 3268 | if (px->state != PR_STSTOPPED && (px->cap & (PR_CAP_FE | PR_CAP_BE))) |
| 3269 | if (produce_content_stats_proxy(s, px) == 0) |
| 3270 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3271 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3272 | s->data_ctx.stats.px = px->next; |
| 3273 | s->data_ctx.stats.px_st = DATA_ST_PX_INIT; |
| 3274 | } |
| 3275 | /* here, we just have reached the last proxy */ |
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 | s->data_state = DATA_ST_END; |
| 3278 | /* fall through */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3279 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3280 | case DATA_ST_END: |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 3281 | chunk_printf(&msg, sizeof(trash), "</body></html>\n"); |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3282 | if (buffer_write_chunk(rep, &msg) != 0) |
| 3283 | return 0; |
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 | s->data_state = DATA_ST_FIN; |
| 3286 | /* fall through */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3287 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3288 | case DATA_ST_FIN: |
| 3289 | s->flags &= ~SN_SELF_GEN; |
| 3290 | return 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3291 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3292 | default: |
| 3293 | /* unknown state ! */ |
| 3294 | s->logs.status = 500; |
| 3295 | client_retnclose(s, error_message(s, HTTP_ERR_500)); |
| 3296 | if (!(s->flags & SN_ERR_MASK)) |
| 3297 | s->flags |= SN_ERR_PRXCOND; |
| 3298 | if (!(s->flags & SN_FINST_MASK)) |
| 3299 | s->flags |= SN_FINST_R; |
| 3300 | s->flags &= ~SN_SELF_GEN; |
| 3301 | return 1; |
| 3302 | } |
| 3303 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3304 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3305 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3306 | /* |
| 3307 | * Dumps statistics for a proxy. |
| 3308 | * Returns 0 if it had to stop dumping data because of lack of buffer space, |
| 3309 | * ot non-zero if everything completed. |
| 3310 | */ |
| 3311 | int produce_content_stats_proxy(struct session *s, struct proxy *px) |
| 3312 | { |
| 3313 | struct buffer *rep = s->rep; |
| 3314 | struct server *sv; |
| 3315 | struct chunk msg; |
| 3316 | |
| 3317 | msg.len = 0; |
| 3318 | msg.str = trash; |
| 3319 | |
| 3320 | switch (s->data_ctx.stats.px_st) { |
| 3321 | case DATA_ST_PX_INIT: |
| 3322 | /* we are on a new proxy */ |
| 3323 | |
| 3324 | if (s->be->fiprm->uri_auth && s->be->fiprm->uri_auth->scope) { |
| 3325 | /* we have a limited scope, we have to check the proxy name */ |
| 3326 | struct stat_scope *scope; |
| 3327 | int len; |
| 3328 | |
| 3329 | len = strlen(px->id); |
| 3330 | scope = s->be->fiprm->uri_auth->scope; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3331 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3332 | while (scope) { |
| 3333 | /* match exact proxy name */ |
| 3334 | if (scope->px_len == len && !memcmp(px->id, scope->px_id, len)) |
| 3335 | break; |
| 3336 | |
| 3337 | /* match '.' which means 'self' proxy */ |
| 3338 | if (!strcmp(scope->px_id, ".") && px == s->fe) |
| 3339 | break; |
| 3340 | scope = scope->next; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3341 | } |
| 3342 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3343 | /* proxy name not found : don't dump anything */ |
| 3344 | if (scope == NULL) |
| 3345 | return 1; |
| 3346 | } |
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 | s->data_ctx.stats.px_st = DATA_ST_PX_TH; |
| 3349 | /* fall through */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3350 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3351 | case DATA_ST_PX_TH: |
| 3352 | /* print a new table */ |
| 3353 | chunk_printf(&msg, sizeof(trash), |
| 3354 | "<table cols=\"20\" class=\"tbl\" width=\"100%%\">\n" |
| 3355 | "<tr align=\"center\" class=\"titre\">" |
| 3356 | "<th colspan=2 class=\"pxname\">%s</th>" |
| 3357 | "<th colspan=18 class=\"empty\"></th>" |
| 3358 | "</tr>\n" |
| 3359 | "<tr align=\"center\" class=\"titre\">" |
| 3360 | "<th rowspan=2></th>" |
| 3361 | "<th colspan=2>Queue</th><th colspan=4>Sessions</th>" |
| 3362 | "<th colspan=2>Bytes</th><th colspan=2>Denied</th>" |
| 3363 | "<th colspan=3>Errors</th><th colspan=6>Server</th>" |
| 3364 | "</tr>\n" |
| 3365 | "<tr align=\"center\" class=\"titre\">" |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 3366 | "<th>Cur</th><th>Max</th><th>Cur</th><th>Max</th>" |
| 3367 | "<th>Limit</th><th>Cumul</th><th>In</th><th>Out</th>" |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3368 | "<th>Req</th><th>Resp</th><th>Req</th><th>Conn</th>" |
| 3369 | "<th>Resp</th><th>Status</th><th>Weight</th><th>Act</th>" |
| 3370 | "<th>Bck</th><th>Check</th><th>Down</th></tr>\n" |
| 3371 | "", |
| 3372 | px->id); |
| 3373 | |
| 3374 | if (buffer_write_chunk(rep, &msg) != 0) |
| 3375 | return 0; |
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 | s->data_ctx.stats.px_st = DATA_ST_PX_FE; |
| 3378 | /* fall through */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3379 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3380 | case DATA_ST_PX_FE: |
| 3381 | /* print the frontend */ |
| 3382 | if (px->cap & PR_CAP_FE) { |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3383 | chunk_printf(&msg, sizeof(trash), |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3384 | /* name, queue */ |
| 3385 | "<tr align=center class=\"frontend\"><td>Frontend</td><td colspan=2></td>" |
| 3386 | /* sessions : current, max, limit, cumul. */ |
| 3387 | "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>" |
| 3388 | /* bytes : in, out */ |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 3389 | "<td align=right>%lld</td><td align=right>%lld</td>" |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3390 | /* denied: req, resp */ |
| 3391 | "<td align=right>%d</td><td align=right>%d</td>" |
| 3392 | /* errors : request, connect, response */ |
| 3393 | "<td align=right>%d</td><td align=right></td><td align=right></td>" |
| 3394 | /* server status : reflect backend status */ |
| 3395 | "<td align=center>%s</td>" |
| 3396 | /* rest of server: nothing */ |
| 3397 | "<td align=center colspan=5></td></tr>" |
| 3398 | "", |
| 3399 | px->feconn, px->feconn_max, px->maxconn, px->cum_feconn, |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 3400 | px->bytes_in, px->bytes_out, |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3401 | px->denied_req, px->denied_resp, |
| 3402 | px->failed_req, |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3403 | px->state == PR_STRUN ? "OPEN" : |
| 3404 | px->state == PR_STIDLE ? "FULL" : "STOP"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3405 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3406 | if (buffer_write_chunk(rep, &msg) != 0) |
| 3407 | return 0; |
| 3408 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3409 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3410 | s->data_ctx.stats.sv = px->srv; /* may be NULL */ |
| 3411 | s->data_ctx.stats.px_st = DATA_ST_PX_SV; |
| 3412 | /* fall through */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3413 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3414 | case DATA_ST_PX_SV: |
| 3415 | /* stats.sv has been initialized above */ |
| 3416 | while (s->data_ctx.stats.sv != NULL) { |
| 3417 | static char *srv_hlt_st[5] = { "DOWN", "DN %d/%d ↑", "UP %d/%d ↓", "UP", "<i>no check</i>" }; |
| 3418 | 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] | 3419 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3420 | sv = s->data_ctx.stats.sv; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3421 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3422 | /* FIXME: produce some small strings for "UP/DOWN x/y &#xxxx;" */ |
| 3423 | if (!(sv->state & SRV_CHECKED)) |
| 3424 | sv_state = 4; |
| 3425 | else if (sv->state & SRV_RUNNING) |
| 3426 | if (sv->health == sv->rise + sv->fall - 1) |
| 3427 | sv_state = 3; /* UP */ |
| 3428 | else |
| 3429 | sv_state = 2; /* going down */ |
| 3430 | else |
| 3431 | if (sv->health) |
| 3432 | sv_state = 1; /* going up */ |
| 3433 | else |
| 3434 | sv_state = 0; /* DOWN */ |
| 3435 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3436 | chunk_printf(&msg, sizeof(trash), |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3437 | /* name */ |
| 3438 | "<tr align=\"center\" class=\"%s%d\"><td>%s</td>" |
| 3439 | /* queue : current, max */ |
| 3440 | "<td align=right>%d</td><td align=right>%d</td>" |
| 3441 | /* sessions : current, max, limit, cumul */ |
| 3442 | "<td align=right>%d</td><td align=right>%d</td><td align=right>%s</td><td align=right>%d</td>" |
| 3443 | /* bytes : in, out */ |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 3444 | "<td align=right>%lld</td><td align=right>%lld</td>" |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3445 | /* denied: req, resp */ |
| 3446 | "<td align=right></td><td align=right>%d</td>" |
| 3447 | /* errors : request, connect, response */ |
| 3448 | "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n" |
| 3449 | "", |
Willy Tarreau | 368e96a | 2007-01-07 00:16:15 +0100 | [diff] [blame] | 3450 | (sv->state & SRV_BACKUP) ? "backup" : "active", |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3451 | sv_state, sv->id, |
| 3452 | sv->nbpend, sv->nbpend_max, |
| 3453 | 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] | 3454 | sv->bytes_in, sv->bytes_out, |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3455 | sv->failed_secu, |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3456 | sv->failed_conns, sv->failed_resp); |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3457 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3458 | /* status */ |
| 3459 | chunk_printf(&msg, sizeof(trash), "<td nowrap>"); |
| 3460 | chunk_printf(&msg, sizeof(trash), |
| 3461 | srv_hlt_st[sv_state], |
| 3462 | (sv->state & SRV_RUNNING) ? (sv->health - sv->rise + 1) : (sv->health), |
| 3463 | (sv->state & SRV_RUNNING) ? (sv->fall) : (sv->rise)); |
| 3464 | |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3465 | chunk_printf(&msg, sizeof(trash), |
| 3466 | /* weight */ |
| 3467 | "</td><td>%d</td>" |
| 3468 | /* act, bck */ |
| 3469 | "<td>%s</td><td>%s</td>" |
| 3470 | "", |
| 3471 | sv->uweight+1, |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3472 | (sv->state & SRV_BACKUP) ? "-" : "Y", |
| 3473 | (sv->state & SRV_BACKUP) ? "Y" : "-"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3474 | |
| 3475 | /* check failures : unique, fatal */ |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3476 | if (sv->state & SRV_CHECKED) |
| 3477 | chunk_printf(&msg, sizeof(trash), |
| 3478 | "<td align=right>%d</td><td align=right>%d</td></tr>\n", |
| 3479 | sv->failed_checks, sv->down_trans); |
| 3480 | else |
| 3481 | chunk_printf(&msg, sizeof(trash), |
| 3482 | "<td colspan=2></td></tr>\n"); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3483 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3484 | if (buffer_write_chunk(rep, &msg) != 0) |
| 3485 | return 0; |
| 3486 | |
| 3487 | s->data_ctx.stats.sv = sv->next; |
| 3488 | } /* while sv */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3489 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3490 | s->data_ctx.stats.px_st = DATA_ST_PX_BE; |
| 3491 | /* fall through */ |
| 3492 | |
| 3493 | case DATA_ST_PX_BE: |
| 3494 | /* print the backend */ |
| 3495 | if (px->cap & PR_CAP_BE) { |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3496 | chunk_printf(&msg, sizeof(trash), |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3497 | /* name */ |
| 3498 | "<tr align=center class=\"backend\"><td>Backend</td>" |
| 3499 | /* queue : current, max */ |
| 3500 | "<td align=right>%d</td><td align=right>%d</td>" |
| 3501 | /* sessions : current, max, limit, cumul. */ |
| 3502 | "<td align=right>%d</td><td align=right>%d</td><td align=right>%d</td><td align=right>%d</td>" |
| 3503 | /* bytes : in, out */ |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 3504 | "<td align=right>%lld</td><td align=right>%lld</td>" |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3505 | /* denied: req, resp */ |
| 3506 | "<td align=right>%d</td><td align=right>%d</td>" |
| 3507 | /* errors : request, connect, response */ |
| 3508 | "<td align=right></td><td align=right>%d</td><td align=right>%d</td>\n" |
| 3509 | /* server status : reflect backend status (up/down) : we display UP |
| 3510 | * if the backend has known working servers or if it has no server at |
| 3511 | * all (eg: for stats). Tthen we display the total weight, number of |
| 3512 | * active and backups. */ |
| 3513 | "<td align=center>%s</td><td align=center>%d</td>" |
| 3514 | "<td align=center>%d</td><td align=center>%d</td>" |
| 3515 | /* rest of server: nothing */ |
| 3516 | "<td align=center colspan=2></td></tr>" |
| 3517 | "", |
| 3518 | px->nbpend /* or px->totpend ? */, px->nbpend_max, |
| 3519 | px->beconn, px->beconn_max, px->fullconn, px->cum_beconn, |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 3520 | px->bytes_in, px->bytes_out, |
Willy Tarreau | 128e954 | 2007-01-01 22:01:43 +0100 | [diff] [blame] | 3521 | px->denied_req, px->denied_resp, |
| 3522 | px->failed_conns, px->failed_resp, |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3523 | (px->srv_map_sz > 0 || !px->srv) ? "UP" : "DOWN", |
| 3524 | px->srv_map_sz, px->srv_act, px->srv_bck); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3525 | |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3526 | if (buffer_write_chunk(rep, &msg) != 0) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3527 | return 0; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3528 | } |
| 3529 | |
| 3530 | s->data_ctx.stats.px_st = DATA_ST_PX_END; |
| 3531 | /* fall through */ |
| 3532 | |
| 3533 | case DATA_ST_PX_END: |
| 3534 | chunk_printf(&msg, sizeof(trash), "</table><p>\n"); |
| 3535 | |
| 3536 | if (buffer_write_chunk(rep, &msg) != 0) |
| 3537 | return 0; |
| 3538 | |
| 3539 | s->data_ctx.stats.px_st = DATA_ST_PX_FIN; |
| 3540 | /* fall through */ |
| 3541 | |
| 3542 | case DATA_ST_PX_FIN: |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3543 | return 1; |
Willy Tarreau | c0dde7a | 2007-01-01 21:38:07 +0100 | [diff] [blame] | 3544 | |
| 3545 | default: |
| 3546 | /* unknown state, we should put an abort() here ! */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3547 | return 1; |
| 3548 | } |
| 3549 | } |
| 3550 | |
| 3551 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3552 | /* Iterate the same filter through all request headers. |
| 3553 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3554 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 3555 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3556 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3557 | 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] | 3558 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3559 | char term; |
| 3560 | char *cur_ptr, *cur_end, *cur_next; |
| 3561 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3562 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3563 | struct hdr_idx_elem *cur_hdr; |
| 3564 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 3565 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3566 | last_hdr = 0; |
| 3567 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3568 | 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] | 3569 | old_idx = 0; |
| 3570 | |
| 3571 | while (!last_hdr) { |
| 3572 | if (unlikely(t->flags & (SN_CLDENY | SN_CLTARPIT))) |
| 3573 | return 1; |
| 3574 | else if (unlikely(t->flags & SN_CLALLOW) && |
| 3575 | (exp->action == ACT_ALLOW || |
| 3576 | exp->action == ACT_DENY || |
| 3577 | exp->action == ACT_TARPIT)) |
| 3578 | return 0; |
| 3579 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3580 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3581 | if (!cur_idx) |
| 3582 | break; |
| 3583 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3584 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3585 | cur_ptr = cur_next; |
| 3586 | cur_end = cur_ptr + cur_hdr->len; |
| 3587 | cur_next = cur_end + cur_hdr->cr + 1; |
| 3588 | |
| 3589 | /* Now we have one header between cur_ptr and cur_end, |
| 3590 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3591 | */ |
| 3592 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3593 | /* The annoying part is that pattern matching needs |
| 3594 | * that we modify the contents to null-terminate all |
| 3595 | * strings before testing them. |
| 3596 | */ |
| 3597 | |
| 3598 | term = *cur_end; |
| 3599 | *cur_end = '\0'; |
| 3600 | |
| 3601 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 3602 | switch (exp->action) { |
| 3603 | case ACT_SETBE: |
| 3604 | /* It is not possible to jump a second time. |
| 3605 | * FIXME: should we return an HTTP/500 here so that |
| 3606 | * the admin knows there's a problem ? |
| 3607 | */ |
| 3608 | if (t->be != t->fe) |
| 3609 | break; |
| 3610 | |
| 3611 | /* Swithing Proxy */ |
| 3612 | t->be = (struct proxy *) exp->replace; |
| 3613 | |
| 3614 | /* right now, the backend switch is not too much complicated |
| 3615 | * because we have associated req_cap and rsp_cap to the |
| 3616 | * frontend, and the beconn will be updated later. |
| 3617 | */ |
| 3618 | |
| 3619 | t->rep->rto = t->req->wto = t->be->beprm->srvtimeout; |
| 3620 | t->req->cto = t->be->beprm->contimeout; |
| 3621 | last_hdr = 1; |
| 3622 | break; |
| 3623 | |
| 3624 | case ACT_ALLOW: |
| 3625 | t->flags |= SN_CLALLOW; |
| 3626 | last_hdr = 1; |
| 3627 | break; |
| 3628 | |
| 3629 | case ACT_DENY: |
| 3630 | t->flags |= SN_CLDENY; |
| 3631 | last_hdr = 1; |
| 3632 | t->be->beprm->denied_req++; |
| 3633 | break; |
| 3634 | |
| 3635 | case ACT_TARPIT: |
| 3636 | t->flags |= SN_CLTARPIT; |
| 3637 | last_hdr = 1; |
| 3638 | t->be->beprm->denied_req++; |
| 3639 | break; |
| 3640 | |
| 3641 | case ACT_REPLACE: |
| 3642 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 3643 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 3644 | /* FIXME: if the user adds a newline in the replacement, the |
| 3645 | * index will not be recalculated for now, and the new line |
| 3646 | * will not be counted as a new header. |
| 3647 | */ |
| 3648 | |
| 3649 | cur_end += delta; |
| 3650 | cur_next += delta; |
| 3651 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3652 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3653 | break; |
| 3654 | |
| 3655 | case ACT_REMOVE: |
| 3656 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 3657 | cur_next += delta; |
| 3658 | |
| 3659 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3660 | txn->req.eoh += delta; |
| 3661 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 3662 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3663 | cur_hdr->len = 0; |
| 3664 | cur_end = NULL; /* null-term has been rewritten */ |
| 3665 | break; |
| 3666 | |
| 3667 | } |
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 | if (cur_end) |
| 3670 | *cur_end = term; /* restore the string terminator */ |
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 | /* keep the link from this header to next one in case of later |
| 3673 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3674 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3675 | old_idx = cur_idx; |
| 3676 | } |
| 3677 | return 0; |
| 3678 | } |
| 3679 | |
| 3680 | |
| 3681 | /* Apply the filter to the request line. |
| 3682 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 3683 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3684 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 3685 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3686 | */ |
| 3687 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 3688 | { |
| 3689 | char term; |
| 3690 | char *cur_ptr, *cur_end; |
| 3691 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3692 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3693 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3694 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3695 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3696 | if (unlikely(t->flags & (SN_CLDENY | SN_CLTARPIT))) |
| 3697 | return 1; |
| 3698 | else if (unlikely(t->flags & SN_CLALLOW) && |
| 3699 | (exp->action == ACT_ALLOW || |
| 3700 | exp->action == ACT_DENY || |
| 3701 | exp->action == ACT_TARPIT)) |
| 3702 | return 0; |
| 3703 | else if (exp->action == ACT_REMOVE) |
| 3704 | return 0; |
| 3705 | |
| 3706 | done = 0; |
| 3707 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3708 | cur_ptr = req->data + txn->req.som; |
| 3709 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3710 | |
| 3711 | /* Now we have the request line between cur_ptr and cur_end */ |
| 3712 | |
| 3713 | /* The annoying part is that pattern matching needs |
| 3714 | * that we modify the contents to null-terminate all |
| 3715 | * strings before testing them. |
| 3716 | */ |
| 3717 | |
| 3718 | term = *cur_end; |
| 3719 | *cur_end = '\0'; |
| 3720 | |
| 3721 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 3722 | switch (exp->action) { |
| 3723 | case ACT_SETBE: |
| 3724 | /* It is not possible to jump a second time. |
| 3725 | * FIXME: should we return an HTTP/500 here so that |
| 3726 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3727 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3728 | if (t->be != t->fe) |
| 3729 | break; |
| 3730 | |
| 3731 | /* Swithing Proxy */ |
| 3732 | t->be = (struct proxy *) exp->replace; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3733 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3734 | /* right now, the backend switch is not too much complicated |
| 3735 | * because we have associated req_cap and rsp_cap to the |
| 3736 | * frontend, and the beconn will be updated later. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3737 | */ |
| 3738 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3739 | t->rep->rto = t->req->wto = t->be->beprm->srvtimeout; |
| 3740 | t->req->cto = t->be->beprm->contimeout; |
| 3741 | done = 1; |
| 3742 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3743 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3744 | case ACT_ALLOW: |
| 3745 | t->flags |= SN_CLALLOW; |
| 3746 | done = 1; |
| 3747 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3748 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3749 | case ACT_DENY: |
| 3750 | t->flags |= SN_CLDENY; |
| 3751 | t->be->beprm->denied_req++; |
| 3752 | done = 1; |
| 3753 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3754 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3755 | case ACT_TARPIT: |
| 3756 | t->flags |= SN_CLTARPIT; |
| 3757 | t->be->beprm->denied_req++; |
| 3758 | done = 1; |
| 3759 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3760 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3761 | case ACT_REPLACE: |
| 3762 | *cur_end = term; /* restore the string terminator */ |
| 3763 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 3764 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 3765 | /* FIXME: if the user adds a newline in the replacement, the |
| 3766 | * index will not be recalculated for now, and the new line |
| 3767 | * will not be counted as a new header. |
| 3768 | */ |
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 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3771 | cur_end += delta; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3772 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3773 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3774 | HTTP_MSG_RQMETH, |
| 3775 | cur_ptr, cur_end + 1, |
| 3776 | NULL, NULL); |
| 3777 | if (unlikely(!cur_end)) |
| 3778 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3779 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3780 | /* we have a full request and we know that we have either a CR |
| 3781 | * or an LF at <ptr>. |
| 3782 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3783 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 3784 | 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] | 3785 | /* there is no point trying this regex on headers */ |
| 3786 | return 1; |
| 3787 | } |
| 3788 | } |
| 3789 | *cur_end = term; /* restore the string terminator */ |
| 3790 | return done; |
| 3791 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 3792 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3793 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3794 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3795 | /* |
| 3796 | * Apply all the req filters <exp> to all headers in buffer <req> of session <t>. |
| 3797 | * 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] | 3798 | * unparsable request. Since it can manage the switch to another backend, it |
| 3799 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3800 | */ |
| 3801 | int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 3802 | { |
| 3803 | /* iterate through the filters in the outer loop */ |
| 3804 | while (exp && !(t->flags & (SN_CLDENY|SN_CLTARPIT))) { |
| 3805 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3806 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3807 | /* |
| 3808 | * The interleaving of transformations and verdicts |
| 3809 | * makes it difficult to decide to continue or stop |
| 3810 | * the evaluation. |
| 3811 | */ |
| 3812 | |
| 3813 | if ((t->flags & SN_CLALLOW) && |
| 3814 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 3815 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) { |
| 3816 | exp = exp->next; |
| 3817 | continue; |
| 3818 | } |
| 3819 | |
| 3820 | /* Apply the filter to the request line. */ |
| 3821 | ret = apply_filter_to_req_line(t, req, exp); |
| 3822 | if (unlikely(ret < 0)) |
| 3823 | return -1; |
| 3824 | |
| 3825 | if (likely(ret == 0)) { |
| 3826 | /* The filter did not match the request, it can be |
| 3827 | * iterated through all headers. |
| 3828 | */ |
| 3829 | apply_filter_to_req_headers(t, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3830 | } |
| 3831 | exp = exp->next; |
| 3832 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3833 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3834 | } |
| 3835 | |
| 3836 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3837 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3838 | /* |
| 3839 | * Manager client-side cookie |
| 3840 | */ |
| 3841 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 3842 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3843 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3844 | char *p1, *p2, *p3, *p4; |
| 3845 | char *del_colon, *del_cookie, *colon; |
| 3846 | int app_cookies; |
| 3847 | |
| 3848 | appsess *asession_temp = NULL; |
| 3849 | appsess local_asession; |
| 3850 | |
| 3851 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3852 | int cur_idx, old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3853 | |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 3854 | if (t->be->beprm->cookie_name == NULL && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3855 | t->be->beprm->appsession_name == NULL && |
| 3856 | t->be->fiprm->capture_name == NULL) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3857 | return; |
| 3858 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3859 | /* Iterate through the headers. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3860 | * we start with the start line. |
| 3861 | */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 3862 | old_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3863 | 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] | 3864 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3865 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3866 | struct hdr_idx_elem *cur_hdr; |
| 3867 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3868 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3869 | cur_ptr = cur_next; |
| 3870 | cur_end = cur_ptr + cur_hdr->len; |
| 3871 | cur_next = cur_end + cur_hdr->cr + 1; |
| 3872 | |
| 3873 | /* We have one full header between cur_ptr and cur_end, and the |
| 3874 | * next header starts at cur_next. We're only interested in |
| 3875 | * "Cookie:" headers. |
| 3876 | */ |
| 3877 | |
| 3878 | if ((cur_end - cur_ptr <= 7) || |
| 3879 | (strncasecmp(cur_ptr, "Cookie:", 7) != 0)) { |
| 3880 | old_idx = cur_idx; |
| 3881 | continue; |
| 3882 | } |
| 3883 | |
| 3884 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 3885 | * attributes whose name begin with a '$', and associate them with |
| 3886 | * the right cookie, if we want to delete this cookie. |
| 3887 | * So there are 3 cases for each cookie read : |
| 3888 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 3889 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 3890 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 3891 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 3892 | * "special" cookie. |
| 3893 | * At the end of loop, if a "special" cookie remains, we may have to |
| 3894 | * remove it. If no application cookie persists in the header, we |
| 3895 | * *MUST* delete it |
| 3896 | */ |
| 3897 | |
| 3898 | |
| 3899 | p1 = cur_ptr + 7; /* first char after 'Cookie:' */ |
| 3900 | if (isspace((int)*p1)) /* try to get the first space with it */ |
| 3901 | p1++; |
| 3902 | |
| 3903 | colon = p1; |
| 3904 | /* del_cookie == NULL => nothing to be deleted */ |
| 3905 | del_colon = del_cookie = NULL; |
| 3906 | app_cookies = 0; |
| 3907 | |
| 3908 | while (p1 < cur_end) { |
| 3909 | /* skip spaces and colons, but keep an eye on these ones */ |
| 3910 | while (p1 < cur_end) { |
| 3911 | if (*p1 == ';' || *p1 == ',') |
| 3912 | colon = p1; |
| 3913 | else if (!isspace((int)*p1)) |
| 3914 | break; |
| 3915 | p1++; |
| 3916 | } |
| 3917 | |
| 3918 | if (p1 == cur_end) |
| 3919 | break; |
| 3920 | |
| 3921 | /* p1 is at the beginning of the cookie name */ |
| 3922 | p2 = p1; |
| 3923 | while (p2 < cur_end && *p2 != '=') |
| 3924 | p2++; |
| 3925 | |
| 3926 | if (p2 == cur_end) |
| 3927 | break; |
| 3928 | |
| 3929 | p3 = p2 + 1; /* skips the '=' sign */ |
| 3930 | if (p3 == cur_end) |
| 3931 | break; |
| 3932 | |
| 3933 | p4 = p3; |
| 3934 | while (p4 < cur_end && !isspace((int)*p4) && *p4 != ';' && *p4 != ',') |
| 3935 | p4++; |
| 3936 | |
| 3937 | /* here, we have the cookie name between p1 and p2, |
| 3938 | * and its value between p3 and p4. |
| 3939 | * we can process it : |
| 3940 | * |
| 3941 | * Cookie: NAME=VALUE; |
| 3942 | * | || || | |
| 3943 | * | || || +--> p4 |
| 3944 | * | || |+-------> p3 |
| 3945 | * | || +--------> p2 |
| 3946 | * | |+------------> p1 |
| 3947 | * | +-------------> colon |
| 3948 | * +--------------------> cur_ptr |
| 3949 | */ |
| 3950 | |
| 3951 | if (*p1 == '$') { |
| 3952 | /* skip this one */ |
| 3953 | } |
| 3954 | else { |
| 3955 | /* first, let's see if we want to capture it */ |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 3956 | if (t->fe->fiprm->capture_name != NULL && |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3957 | t->logs.cli_cookie == NULL && |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 3958 | (p4 - p1 >= t->fe->fiprm->capture_namelen) && |
| 3959 | 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] | 3960 | int log_len = p4 - p1; |
| 3961 | |
| 3962 | if ((t->logs.cli_cookie = pool_alloc(capture)) == NULL) { |
| 3963 | Alert("HTTP logging : out of memory.\n"); |
| 3964 | } else { |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 3965 | if (log_len > t->fe->fiprm->capture_len) |
| 3966 | log_len = t->fe->fiprm->capture_len; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3967 | memcpy(t->logs.cli_cookie, p1, log_len); |
| 3968 | t->logs.cli_cookie[log_len] = 0; |
| 3969 | } |
| 3970 | } |
| 3971 | |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 3972 | if ((p2 - p1 == t->be->beprm->cookie_len) && (t->be->beprm->cookie_name != NULL) && |
| 3973 | (memcmp(p1, t->be->beprm->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3974 | /* Cool... it's the right one */ |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 3975 | struct server *srv = t->be->beprm->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3976 | char *delim; |
| 3977 | |
| 3978 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 3979 | * have the server ID betweek p3 and delim, and the original cookie between |
| 3980 | * delim+1 and p4. Otherwise, delim==p4 : |
| 3981 | * |
| 3982 | * Cookie: NAME=SRV~VALUE; |
| 3983 | * | || || | | |
| 3984 | * | || || | +--> p4 |
| 3985 | * | || || +--------> delim |
| 3986 | * | || |+-----------> p3 |
| 3987 | * | || +------------> p2 |
| 3988 | * | |+----------------> p1 |
| 3989 | * | +-----------------> colon |
| 3990 | * +------------------------> cur_ptr |
| 3991 | */ |
| 3992 | |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 3993 | if (t->be->beprm->options & PR_O_COOK_PFX) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3994 | for (delim = p3; delim < p4; delim++) |
| 3995 | if (*delim == COOKIE_DELIM) |
| 3996 | break; |
| 3997 | } |
| 3998 | else |
| 3999 | delim = p4; |
| 4000 | |
| 4001 | |
| 4002 | /* Here, we'll look for the first running server which supports the cookie. |
| 4003 | * This allows to share a same cookie between several servers, for example |
| 4004 | * to dedicate backup servers to specific servers only. |
| 4005 | * However, to prevent clients from sticking to cookie-less backup server |
| 4006 | * when they have incidentely learned an empty cookie, we simply ignore |
| 4007 | * empty cookies and mark them as invalid. |
| 4008 | */ |
| 4009 | if (delim == p3) |
| 4010 | srv = NULL; |
| 4011 | |
| 4012 | while (srv) { |
Willy Tarreau | 92f2ab1 | 2007-02-02 22:14:47 +0100 | [diff] [blame] | 4013 | if (srv->cookie && (srv->cklen == delim - p3) && |
| 4014 | !memcmp(p3, srv->cookie, delim - p3)) { |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 4015 | if (srv->state & SRV_RUNNING || t->be->beprm->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4016 | /* we found the server and it's usable */ |
| 4017 | t->flags &= ~SN_CK_MASK; |
| 4018 | t->flags |= SN_CK_VALID | SN_DIRECT | SN_ASSIGNED; |
| 4019 | t->srv = srv; |
| 4020 | break; |
| 4021 | } else { |
| 4022 | /* we found a server, but it's down */ |
| 4023 | t->flags &= ~SN_CK_MASK; |
| 4024 | t->flags |= SN_CK_DOWN; |
| 4025 | } |
| 4026 | } |
| 4027 | srv = srv->next; |
| 4028 | } |
| 4029 | |
| 4030 | if (!srv && !(t->flags & SN_CK_DOWN)) { |
| 4031 | /* no server matched this cookie */ |
| 4032 | t->flags &= ~SN_CK_MASK; |
| 4033 | t->flags |= SN_CK_INVALID; |
| 4034 | } |
| 4035 | |
| 4036 | /* depending on the cookie mode, we may have to either : |
| 4037 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 4038 | * the server never sees it ; |
| 4039 | * - remove the server id from the cookie value, and tag the cookie as an |
| 4040 | * application cookie so that it does not get accidentely removed later, |
| 4041 | * if we're in cookie prefix mode |
| 4042 | */ |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 4043 | if ((t->be->beprm->options & PR_O_COOK_PFX) && (delim != p4)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4044 | int delta; /* negative */ |
| 4045 | |
| 4046 | delta = buffer_replace2(req, p3, delim + 1, NULL, 0); |
| 4047 | p4 += delta; |
| 4048 | cur_end += delta; |
| 4049 | cur_next += delta; |
| 4050 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4051 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4052 | |
| 4053 | del_cookie = del_colon = NULL; |
| 4054 | app_cookies++; /* protect the header from deletion */ |
| 4055 | } |
| 4056 | else if (del_cookie == NULL && |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 4057 | (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] | 4058 | del_cookie = p1; |
| 4059 | del_colon = colon; |
| 4060 | } |
| 4061 | } else { |
| 4062 | /* now we know that we must keep this cookie since it's |
| 4063 | * not ours. But if we wanted to delete our cookie |
| 4064 | * earlier, we cannot remove the complete header, but we |
| 4065 | * can remove the previous block itself. |
| 4066 | */ |
| 4067 | app_cookies++; |
| 4068 | |
| 4069 | if (del_cookie != NULL) { |
| 4070 | int delta; /* negative */ |
| 4071 | |
| 4072 | delta = buffer_replace2(req, del_cookie, p1, NULL, 0); |
| 4073 | p4 += delta; |
| 4074 | cur_end += delta; |
| 4075 | cur_next += delta; |
| 4076 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4077 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4078 | del_cookie = del_colon = NULL; |
| 4079 | } |
| 4080 | } |
| 4081 | |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 4082 | if ((t->be->beprm->appsession_name != NULL) && |
| 4083 | (memcmp(p1, t->be->beprm->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4084 | /* first, let's see if the cookie is our appcookie*/ |
| 4085 | |
| 4086 | /* Cool... it's the right one */ |
| 4087 | |
| 4088 | asession_temp = &local_asession; |
| 4089 | |
| 4090 | if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) { |
| 4091 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 4092 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 4093 | return; |
| 4094 | } |
| 4095 | |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 4096 | memcpy(asession_temp->sessid, p3, t->be->beprm->appsession_len); |
| 4097 | asession_temp->sessid[t->be->beprm->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4098 | asession_temp->serverid = NULL; |
| 4099 | |
| 4100 | /* only do insert, if lookup fails */ |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 4101 | if (chtbl_lookup(&(t->be->beprm->htbl_proxy), (void *) &asession_temp) != 0) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4102 | if ((asession_temp = pool_alloc(appsess)) == NULL) { |
| 4103 | /* free previously allocated memory */ |
| 4104 | pool_free_to(apools.sessid, local_asession.sessid); |
| 4105 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 4106 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 4107 | return; |
| 4108 | } |
| 4109 | |
| 4110 | asession_temp->sessid = local_asession.sessid; |
| 4111 | asession_temp->serverid = local_asession.serverid; |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 4112 | chtbl_insert(&(t->be->beprm->htbl_proxy), (void *) asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4113 | } else { |
| 4114 | /* free previously allocated memory */ |
| 4115 | pool_free_to(apools.sessid, local_asession.sessid); |
| 4116 | } |
| 4117 | |
| 4118 | if (asession_temp->serverid == NULL) { |
| 4119 | Alert("Found Application Session without matching server.\n"); |
| 4120 | } else { |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 4121 | struct server *srv = t->be->beprm->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4122 | while (srv) { |
| 4123 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 4124 | if (srv->state & SRV_RUNNING || t->be->beprm->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4125 | /* we found the server and it's usable */ |
| 4126 | t->flags &= ~SN_CK_MASK; |
| 4127 | t->flags |= SN_CK_VALID | SN_DIRECT | SN_ASSIGNED; |
| 4128 | t->srv = srv; |
| 4129 | break; |
| 4130 | } else { |
| 4131 | t->flags &= ~SN_CK_MASK; |
| 4132 | t->flags |= SN_CK_DOWN; |
| 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) { |
| 4197 | if (unlikely(t->flags & SN_SVDENY)) |
| 4198 | return 1; |
| 4199 | else if (unlikely(t->flags & SN_SVALLOW) && |
| 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: |
| 4228 | t->flags |= SN_SVALLOW; |
| 4229 | last_hdr = 1; |
| 4230 | break; |
| 4231 | |
| 4232 | case ACT_DENY: |
| 4233 | t->flags |= SN_SVDENY; |
| 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 | |
| 4290 | if (unlikely(t->flags & SN_SVDENY)) |
| 4291 | return 1; |
| 4292 | else if (unlikely(t->flags & SN_SVALLOW) && |
| 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: |
| 4317 | t->flags |= SN_SVALLOW; |
| 4318 | done = 1; |
| 4319 | break; |
| 4320 | |
| 4321 | case ACT_DENY: |
| 4322 | t->flags |= SN_SVDENY; |
| 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 | */ |
| 4348 | t->logs.status = strl2ui(rtr->data + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l); |
| 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 | { |
| 4367 | /* iterate through the filters in the outer loop */ |
| 4368 | while (exp && !(t->flags & SN_SVDENY)) { |
| 4369 | int ret; |
| 4370 | |
| 4371 | /* |
| 4372 | * The interleaving of transformations and verdicts |
| 4373 | * makes it difficult to decide to continue or stop |
| 4374 | * the evaluation. |
| 4375 | */ |
| 4376 | |
| 4377 | if ((t->flags & SN_SVALLOW) && |
| 4378 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 4379 | exp->action == ACT_PASS)) { |
| 4380 | exp = exp->next; |
| 4381 | continue; |
| 4382 | } |
| 4383 | |
| 4384 | /* Apply the filter to the status line. */ |
| 4385 | ret = apply_filter_to_sts_line(t, rtr, exp); |
| 4386 | if (unlikely(ret < 0)) |
| 4387 | return -1; |
| 4388 | |
| 4389 | if (likely(ret == 0)) { |
| 4390 | /* The filter did not match the response, it can be |
| 4391 | * iterated through all headers. |
| 4392 | */ |
| 4393 | apply_filter_to_resp_headers(t, rtr, exp); |
| 4394 | } |
| 4395 | exp = exp->next; |
| 4396 | } |
| 4397 | return 0; |
| 4398 | } |
| 4399 | |
| 4400 | |
| 4401 | |
| 4402 | /* |
| 4403 | * Manager server-side cookies |
| 4404 | */ |
| 4405 | void manage_server_side_cookies(struct session *t, struct buffer *rtr) |
| 4406 | { |
| 4407 | struct http_txn *txn = &t->txn; |
| 4408 | char *p1, *p2, *p3, *p4; |
| 4409 | |
| 4410 | appsess *asession_temp = NULL; |
| 4411 | appsess local_asession; |
| 4412 | |
| 4413 | char *cur_ptr, *cur_end, *cur_next; |
| 4414 | int cur_idx, old_idx, delta; |
| 4415 | |
| 4416 | if (t->be->beprm->cookie_name == NULL && |
| 4417 | t->be->beprm->appsession_name == NULL && |
| 4418 | t->be->fiprm->capture_name == NULL && |
| 4419 | !(t->be->beprm->options & PR_O_CHK_CACHE)) |
| 4420 | return; |
| 4421 | |
| 4422 | /* Iterate through the headers. |
| 4423 | * we start with the start line. |
| 4424 | */ |
| 4425 | old_idx = 0; |
| 4426 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4427 | |
| 4428 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 4429 | struct hdr_idx_elem *cur_hdr; |
| 4430 | |
| 4431 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4432 | cur_ptr = cur_next; |
| 4433 | cur_end = cur_ptr + cur_hdr->len; |
| 4434 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4435 | |
| 4436 | /* We have one full header between cur_ptr and cur_end, and the |
| 4437 | * next header starts at cur_next. We're only interested in |
| 4438 | * "Cookie:" headers. |
| 4439 | */ |
| 4440 | |
| 4441 | if ((cur_end - cur_ptr <= 11) || |
| 4442 | (strncasecmp(cur_ptr, "Set-Cookie:", 11) != 0)) { |
| 4443 | old_idx = cur_idx; |
| 4444 | continue; |
| 4445 | } |
| 4446 | |
| 4447 | /* OK, right now we know we have a set-cookie at cur_ptr */ |
| 4448 | t->flags |= SN_SCK_ANY; |
| 4449 | |
| 4450 | |
| 4451 | /* maybe we only wanted to see if there was a set-cookie */ |
| 4452 | if (t->be->beprm->cookie_name == NULL && |
| 4453 | t->be->beprm->appsession_name == NULL && |
| 4454 | t->be->fiprm->capture_name == NULL) |
| 4455 | return; |
| 4456 | |
| 4457 | p1 = cur_ptr + 11; /* first char after 'Set-Cookie:' */ |
| 4458 | |
| 4459 | while (p1 < cur_end) { /* in fact, we'll break after the first cookie */ |
| 4460 | while (p1 < cur_end && (isspace((int)*p1))) |
| 4461 | p1++; |
| 4462 | |
| 4463 | if (p1 == cur_end || *p1 == ';') /* end of cookie */ |
| 4464 | break; |
| 4465 | |
| 4466 | /* p1 is at the beginning of the cookie name */ |
| 4467 | p2 = p1; |
| 4468 | |
| 4469 | while (p2 < cur_end && *p2 != '=' && *p2 != ';') |
| 4470 | p2++; |
| 4471 | |
| 4472 | if (p2 == cur_end || *p2 == ';') /* next cookie */ |
| 4473 | break; |
| 4474 | |
| 4475 | p3 = p2 + 1; /* skip the '=' sign */ |
| 4476 | if (p3 == cur_end) |
| 4477 | break; |
| 4478 | |
| 4479 | p4 = p3; |
| 4480 | while (p4 < cur_end && !isspace((int)*p4) && *p4 != ';') |
| 4481 | p4++; |
| 4482 | |
| 4483 | /* here, we have the cookie name between p1 and p2, |
| 4484 | * and its value between p3 and p4. |
| 4485 | * we can process it. |
| 4486 | */ |
| 4487 | |
| 4488 | /* first, let's see if we want to capture it */ |
| 4489 | if (t->be->fiprm->capture_name != NULL && |
| 4490 | t->logs.srv_cookie == NULL && |
| 4491 | (p4 - p1 >= t->be->fiprm->capture_namelen) && |
| 4492 | memcmp(p1, t->be->fiprm->capture_name, t->be->fiprm->capture_namelen) == 0) { |
| 4493 | int log_len = p4 - p1; |
| 4494 | |
| 4495 | if ((t->logs.srv_cookie = pool_alloc(capture)) == NULL) { |
| 4496 | Alert("HTTP logging : out of memory.\n"); |
| 4497 | } |
| 4498 | |
| 4499 | if (log_len > t->be->fiprm->capture_len) |
| 4500 | log_len = t->be->fiprm->capture_len; |
| 4501 | memcpy(t->logs.srv_cookie, p1, log_len); |
| 4502 | t->logs.srv_cookie[log_len] = 0; |
| 4503 | } |
| 4504 | |
| 4505 | /* now check if we need to process it for persistence */ |
| 4506 | if ((p2 - p1 == t->be->beprm->cookie_len) && (t->be->beprm->cookie_name != NULL) && |
| 4507 | (memcmp(p1, t->be->beprm->cookie_name, p2 - p1) == 0)) { |
| 4508 | /* Cool... it's the right one */ |
| 4509 | t->flags |= SN_SCK_SEEN; |
| 4510 | |
| 4511 | /* If the cookie is in insert mode on a known server, we'll delete |
| 4512 | * this occurrence because we'll insert another one later. |
| 4513 | * We'll delete it too if the "indirect" option is set and we're in |
| 4514 | * a direct access. */ |
| 4515 | if (((t->srv) && (t->be->beprm->options & PR_O_COOK_INS)) || |
| 4516 | ((t->flags & SN_DIRECT) && (t->be->beprm->options & PR_O_COOK_IND))) { |
| 4517 | /* this header must be deleted */ |
| 4518 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 4519 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4520 | txn->hdr_idx.used--; |
| 4521 | cur_hdr->len = 0; |
| 4522 | cur_next += delta; |
| 4523 | txn->rsp.eoh += delta; |
| 4524 | |
| 4525 | t->flags |= SN_SCK_DELETED; |
| 4526 | } |
| 4527 | else if ((t->srv) && (t->srv->cookie) && |
| 4528 | (t->be->beprm->options & PR_O_COOK_RW)) { |
| 4529 | /* replace bytes p3->p4 with the cookie name associated |
| 4530 | * with this server since we know it. |
| 4531 | */ |
| 4532 | delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen); |
| 4533 | cur_hdr->len += delta; |
| 4534 | cur_next += delta; |
| 4535 | txn->rsp.eoh += delta; |
| 4536 | |
| 4537 | t->flags |= SN_SCK_INSERTED | SN_SCK_DELETED; |
| 4538 | } |
| 4539 | else if ((t->srv) && (t->srv->cookie) && |
| 4540 | (t->be->beprm->options & PR_O_COOK_PFX)) { |
| 4541 | /* insert the cookie name associated with this server |
| 4542 | * before existing cookie, and insert a delimitor between them.. |
| 4543 | */ |
| 4544 | delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1); |
| 4545 | cur_hdr->len += delta; |
| 4546 | cur_next += delta; |
| 4547 | txn->rsp.eoh += delta; |
| 4548 | |
| 4549 | p3[t->srv->cklen] = COOKIE_DELIM; |
| 4550 | t->flags |= SN_SCK_INSERTED | SN_SCK_DELETED; |
| 4551 | } |
| 4552 | } |
| 4553 | /* next, let's see if the cookie is our appcookie */ |
| 4554 | else if ((t->be->beprm->appsession_name != NULL) && |
| 4555 | (memcmp(p1, t->be->beprm->appsession_name, p2 - p1) == 0)) { |
| 4556 | |
| 4557 | /* Cool... it's the right one */ |
| 4558 | |
| 4559 | size_t server_id_len = strlen(t->srv->id) + 1; |
| 4560 | asession_temp = &local_asession; |
| 4561 | |
| 4562 | if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) { |
| 4563 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4564 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4565 | return; |
| 4566 | } |
| 4567 | memcpy(asession_temp->sessid, p3, t->be->beprm->appsession_len); |
| 4568 | asession_temp->sessid[t->be->beprm->appsession_len] = 0; |
| 4569 | asession_temp->serverid = NULL; |
| 4570 | |
| 4571 | /* only do insert, if lookup fails */ |
| 4572 | if (chtbl_lookup(&(t->be->htbl_proxy), (void *) &asession_temp) != 0) { |
| 4573 | if ((asession_temp = pool_alloc(appsess)) == NULL) { |
| 4574 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 4575 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 4576 | return; |
| 4577 | } |
| 4578 | asession_temp->sessid = local_asession.sessid; |
| 4579 | asession_temp->serverid = local_asession.serverid; |
| 4580 | chtbl_insert(&(t->be->beprm->htbl_proxy), (void *) asession_temp); |
| 4581 | }/* end if (chtbl_lookup()) */ |
| 4582 | else { |
| 4583 | /* free wasted memory */ |
| 4584 | pool_free_to(apools.sessid, local_asession.sessid); |
| 4585 | } /* end else from if (chtbl_lookup()) */ |
| 4586 | |
| 4587 | if (asession_temp->serverid == NULL) { |
| 4588 | if ((asession_temp->serverid = pool_alloc_from(apools.serverid, apools.ser_msize)) == NULL) { |
| 4589 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4590 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4591 | return; |
| 4592 | } |
| 4593 | asession_temp->serverid[0] = '\0'; |
| 4594 | } |
| 4595 | |
| 4596 | if (asession_temp->serverid[0] == '\0') |
| 4597 | memcpy(asession_temp->serverid, t->srv->id, server_id_len); |
| 4598 | |
| 4599 | tv_delayfrom(&asession_temp->expire, &now, t->be->beprm->appsession_timeout); |
| 4600 | |
| 4601 | #if defined(DEBUG_HASH) |
| 4602 | print_table(&(t->be->beprm->htbl_proxy)); |
| 4603 | #endif |
| 4604 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 4605 | break; /* we don't want to loop again since there cannot be another cookie on the same line */ |
| 4606 | } /* we're now at the end of the cookie value */ |
| 4607 | |
| 4608 | /* keep the link from this header to next one */ |
| 4609 | old_idx = cur_idx; |
| 4610 | } /* end of cookie processing on this header */ |
| 4611 | } |
| 4612 | |
| 4613 | |
| 4614 | |
| 4615 | /* |
| 4616 | * Check if response is cacheable or not. Updates t->flags. |
| 4617 | */ |
| 4618 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 4619 | { |
| 4620 | struct http_txn *txn = &t->txn; |
| 4621 | char *p1, *p2; |
| 4622 | |
| 4623 | char *cur_ptr, *cur_end, *cur_next; |
| 4624 | int cur_idx; |
| 4625 | |
| 4626 | if (!t->flags & SN_CACHEABLE) |
| 4627 | return; |
| 4628 | |
| 4629 | /* Iterate through the headers. |
| 4630 | * we start with the start line. |
| 4631 | */ |
| 4632 | cur_idx = 0; |
| 4633 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4634 | |
| 4635 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 4636 | struct hdr_idx_elem *cur_hdr; |
| 4637 | |
| 4638 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4639 | cur_ptr = cur_next; |
| 4640 | cur_end = cur_ptr + cur_hdr->len; |
| 4641 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4642 | |
| 4643 | /* We have one full header between cur_ptr and cur_end, and the |
| 4644 | * next header starts at cur_next. We're only interested in |
| 4645 | * "Cookie:" headers. |
| 4646 | */ |
| 4647 | |
| 4648 | if ((cur_end - cur_ptr >= 16) && |
| 4649 | strncasecmp(cur_ptr, "Pragma: no-cache", 16) == 0) { |
| 4650 | t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK; |
| 4651 | return; |
| 4652 | } |
| 4653 | |
| 4654 | if ((cur_end - cur_ptr <= 14) || |
| 4655 | (strncasecmp(cur_ptr, "Cache-control:", 14) != 0)) |
| 4656 | continue; |
| 4657 | |
| 4658 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 4659 | |
| 4660 | p1 = cur_ptr + 14; /* first char after 'cache-control:' */ |
| 4661 | |
| 4662 | while (p1 < cur_end && (isspace((int)*p1))) |
| 4663 | p1++; |
| 4664 | |
| 4665 | if (p1 >= cur_end) /* no more info */ |
| 4666 | continue; |
| 4667 | |
| 4668 | /* p1 is at the beginning of the value */ |
| 4669 | p2 = p1; |
| 4670 | |
| 4671 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((int)*p2)) |
| 4672 | p2++; |
| 4673 | |
| 4674 | /* we have a complete value between p1 and p2 */ |
| 4675 | if (p2 < cur_end && *p2 == '=') { |
| 4676 | /* we have something of the form no-cache="set-cookie" */ |
| 4677 | if ((cur_end - p1 >= 21) && |
| 4678 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 4679 | && (p1[20] == '"' || p1[20] == ',')) |
| 4680 | t->flags &= ~SN_CACHE_COOK; |
| 4681 | continue; |
| 4682 | } |
| 4683 | |
| 4684 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 4685 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 4686 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 4687 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 4688 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
| 4689 | t->flags &= ~SN_CACHEABLE & ~SN_CACHE_COOK; |
| 4690 | return; |
| 4691 | } |
| 4692 | |
| 4693 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
| 4694 | t->flags |= SN_CACHEABLE | SN_CACHE_COOK; |
| 4695 | continue; |
| 4696 | } |
| 4697 | } |
| 4698 | } |
| 4699 | |
| 4700 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4701 | /* |
| 4702 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 4703 | * If the server is found, it's assigned to the session. |
| 4704 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4705 | 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] | 4706 | { |
| 4707 | appsess *asession_temp = NULL; |
| 4708 | appsess local_asession; |
| 4709 | char *request_line; |
| 4710 | |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 4711 | if (t->be->beprm->appsession_name == NULL || |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 4712 | (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) || |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4713 | (request_line = memchr(begin, ';', len)) == NULL || |
| 4714 | ((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] | 4715 | return; |
| 4716 | |
| 4717 | /* skip ';' */ |
| 4718 | request_line++; |
| 4719 | |
| 4720 | /* look if we have a jsessionid */ |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 4721 | 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] | 4722 | return; |
| 4723 | |
| 4724 | /* skip jsessionid= */ |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 4725 | request_line += t->be->beprm->appsession_name_len + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4726 | |
| 4727 | /* First try if we already have an appsession */ |
| 4728 | asession_temp = &local_asession; |
| 4729 | |
| 4730 | if ((asession_temp->sessid = pool_alloc_from(apools.sessid, apools.ses_msize)) == NULL) { |
| 4731 | Alert("Not enough memory process_cli():asession_temp->sessid:calloc().\n"); |
| 4732 | send_log(t->be, LOG_ALERT, "Not enough Memory process_cli():asession_temp->sessid:calloc().\n"); |
| 4733 | return; |
| 4734 | } |
| 4735 | |
| 4736 | /* Copy the sessionid */ |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 4737 | memcpy(asession_temp->sessid, request_line, t->be->beprm->appsession_len); |
| 4738 | asession_temp->sessid[t->be->beprm->appsession_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4739 | asession_temp->serverid = NULL; |
| 4740 | |
| 4741 | /* only do insert, if lookup fails */ |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 4742 | if (chtbl_lookup(&(t->be->beprm->htbl_proxy), (void *)&asession_temp)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4743 | if ((asession_temp = pool_alloc(appsess)) == NULL) { |
| 4744 | /* free previously allocated memory */ |
| 4745 | pool_free_to(apools.sessid, local_asession.sessid); |
| 4746 | Alert("Not enough memory process_cli():asession:calloc().\n"); |
| 4747 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession:calloc().\n"); |
| 4748 | return; |
| 4749 | } |
| 4750 | asession_temp->sessid = local_asession.sessid; |
| 4751 | asession_temp->serverid = local_asession.serverid; |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 4752 | chtbl_insert(&(t->be->beprm->htbl_proxy), (void *) asession_temp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4753 | } |
| 4754 | else { |
| 4755 | /* free previously allocated memory */ |
| 4756 | pool_free_to(apools.sessid, local_asession.sessid); |
| 4757 | } |
| 4758 | |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 4759 | tv_delayfrom(&asession_temp->expire, &now, t->be->beprm->appsession_timeout); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4760 | asession_temp->request_count++; |
| 4761 | |
| 4762 | #if defined(DEBUG_HASH) |
| 4763 | print_table(&(t->proxy->htbl_proxy)); |
| 4764 | #endif |
| 4765 | if (asession_temp->serverid == NULL) { |
| 4766 | Alert("Found Application Session without matching server.\n"); |
| 4767 | } else { |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 4768 | struct server *srv = t->be->beprm->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4769 | while (srv) { |
| 4770 | if (strcmp(srv->id, asession_temp->serverid) == 0) { |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 4771 | if (srv->state & SRV_RUNNING || t->be->beprm->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4772 | /* we found the server and it's usable */ |
| 4773 | t->flags &= ~SN_CK_MASK; |
| 4774 | t->flags |= SN_CK_VALID | SN_DIRECT | SN_ASSIGNED; |
| 4775 | t->srv = srv; |
| 4776 | break; |
| 4777 | } else { |
| 4778 | t->flags &= ~SN_CK_MASK; |
| 4779 | t->flags |= SN_CK_DOWN; |
| 4780 | } |
| 4781 | } |
| 4782 | srv = srv->next; |
| 4783 | } |
| 4784 | } |
| 4785 | } |
| 4786 | |
| 4787 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4788 | /* |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 4789 | * In a GET or HEAD request, check if the requested URI matches the stats uri |
| 4790 | * 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] | 4791 | * |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 4792 | * It is assumed that the request is either a HEAD or GET and that the |
| 4793 | * t->be->fiprm->uri_auth field is valid. An HTTP/401 response may be sent, or |
| 4794 | * produce_content() can be called to start sending data. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4795 | * |
| 4796 | * Returns 1 if the session's state changes, otherwise 0. |
| 4797 | */ |
| 4798 | int stats_check_uri_auth(struct session *t, struct proxy *backend) |
| 4799 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4800 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4801 | struct uri_auth *uri_auth = backend->uri_auth; |
| 4802 | struct user_auth *user; |
| 4803 | int authenticated, cur_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4804 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4805 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4806 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4807 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4808 | return 0; |
| 4809 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4810 | h = t->req->data + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4811 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 4812 | /* the URI is in h */ |
| 4813 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4814 | return 0; |
| 4815 | |
| 4816 | /* we are in front of a interceptable URI. Let's check |
| 4817 | * if there's an authentication and if it's valid. |
| 4818 | */ |
| 4819 | user = uri_auth->users; |
| 4820 | if (!user) { |
| 4821 | /* no user auth required, it's OK */ |
| 4822 | authenticated = 1; |
| 4823 | } else { |
| 4824 | authenticated = 0; |
| 4825 | |
| 4826 | /* a user list is defined, we have to check. |
| 4827 | * skip 21 chars for "Authorization: Basic ". |
| 4828 | */ |
| 4829 | |
| 4830 | /* FIXME: this should move to an earlier place */ |
| 4831 | cur_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4832 | h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4833 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 4834 | int len = txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4835 | if (len > 14 && |
| 4836 | !strncasecmp("Authorization:", h, 14)) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4837 | txn->auth_hdr.str = h; |
| 4838 | txn->auth_hdr.len = len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4839 | break; |
| 4840 | } |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4841 | h += len + txn->hdr_idx.v[cur_idx].cr + 1; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4842 | } |
| 4843 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4844 | if (txn->auth_hdr.len < 21 || |
| 4845 | memcmp(txn->auth_hdr.str + 14, " Basic ", 7)) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4846 | user = NULL; |
| 4847 | |
| 4848 | while (user) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4849 | if ((txn->auth_hdr.len == user->user_len + 14 + 7) |
| 4850 | && !memcmp(txn->auth_hdr.str + 14 + 7, |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4851 | user->user_pwd, user->user_len)) { |
| 4852 | authenticated = 1; |
| 4853 | break; |
| 4854 | } |
| 4855 | user = user->next; |
| 4856 | } |
| 4857 | } |
| 4858 | |
| 4859 | if (!authenticated) { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 4860 | struct chunk msg; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4861 | |
| 4862 | /* no need to go further */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 4863 | msg.str = trash; |
| 4864 | msg.len = sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm); |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4865 | t->logs.status = 401; |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 4866 | client_retnclose(t, &msg); |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4867 | if (!(t->flags & SN_ERR_MASK)) |
| 4868 | t->flags |= SN_ERR_PRXCOND; |
| 4869 | if (!(t->flags & SN_FINST_MASK)) |
| 4870 | t->flags |= SN_FINST_R; |
| 4871 | return 1; |
| 4872 | } |
| 4873 | |
| 4874 | /* The request is valid, the user is authenticate. Let's start sending |
| 4875 | * data. |
| 4876 | */ |
| 4877 | t->cli_state = CL_STSHUTR; |
| 4878 | t->req->rlim = t->req->data + BUFSIZE; /* no more rewrite needed */ |
| 4879 | t->logs.t_request = tv_diff(&t->logs.tv_accept, &now); |
| 4880 | t->data_source = DATA_SRC_STATS; |
| 4881 | t->data_state = DATA_ST_INIT; |
| 4882 | produce_content(t); |
| 4883 | return 1; |
| 4884 | } |
| 4885 | |
| 4886 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4887 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4888 | * Print a debug line with a header |
| 4889 | */ |
| 4890 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 4891 | { |
| 4892 | int len, max; |
| 4893 | len = sprintf(trash, "%08x:%s.%s[%04x:%04x]: ", t->uniq_id, t->be->id, |
| 4894 | dir, (unsigned short)t->cli_fd, (unsigned short)t->srv_fd); |
| 4895 | max = end - start; |
| 4896 | UBOUND(max, sizeof(trash) - len - 1); |
| 4897 | len += strlcpy2(trash + len, start, max + 1); |
| 4898 | trash[len++] = '\n'; |
| 4899 | write(1, trash, len); |
| 4900 | } |
| 4901 | |
| 4902 | |
| 4903 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4904 | * Local variables: |
| 4905 | * c-indent-level: 8 |
| 4906 | * c-basic-offset: 8 |
| 4907 | * End: |
| 4908 | */ |