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