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 | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 529 | buffer_auto_close(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 */ |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 643 | if (rdr.len + s->srv->rdr_len > rdr.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 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; |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 656 | if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 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))) { |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 1398 | #if !defined(PARSE_PRESERVE_EMPTY_LINES) |
| 1399 | if (likely(ptr != buf->data)) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1400 | /* Remove empty leading lines, as recommended by |
| 1401 | * RFC2616. This takes a lot of time because we |
| 1402 | * must move all the buffer backwards, but this |
| 1403 | * is rarely needed. The method above will be |
| 1404 | * cleaner when we'll be able to start sending |
| 1405 | * the request from any place in the buffer. |
| 1406 | */ |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 1407 | ptr += buffer_replace2(buf, buf->lr, ptr, NULL, 0); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1408 | end = buf->r; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1409 | } |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 1410 | #endif |
| 1411 | msg->sol = ptr; |
| 1412 | msg->som = ptr - buf->data; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1413 | hdr_idx_init(idx); |
| 1414 | state = HTTP_MSG_RPVER; |
| 1415 | goto http_msg_rpver; |
| 1416 | } |
| 1417 | |
| 1418 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1419 | goto http_msg_invalid; |
| 1420 | |
| 1421 | if (unlikely(*ptr == '\n')) |
| 1422 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1423 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1424 | /* stop here */ |
| 1425 | |
| 1426 | http_msg_rpbefore_cr: |
| 1427 | case HTTP_MSG_RPBEFORE_CR: |
| 1428 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1429 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1430 | /* stop here */ |
| 1431 | |
| 1432 | http_msg_rpver: |
| 1433 | case HTTP_MSG_RPVER: |
| 1434 | case HTTP_MSG_RPVER_SP: |
| 1435 | case HTTP_MSG_RPCODE: |
| 1436 | case HTTP_MSG_RPCODE_SP: |
| 1437 | case HTTP_MSG_RPREASON: |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1438 | ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1439 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1440 | if (unlikely(!ptr)) |
| 1441 | return; |
| 1442 | |
| 1443 | /* we have a full response and we know that we have either a CR |
| 1444 | * or an LF at <ptr>. |
| 1445 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1446 | //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] | 1447 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1448 | |
| 1449 | msg->sol = ptr; |
| 1450 | if (likely(*ptr == '\r')) |
| 1451 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1452 | goto http_msg_rpline_end; |
| 1453 | |
| 1454 | http_msg_rpline_end: |
| 1455 | case HTTP_MSG_RPLINE_END: |
| 1456 | /* msg->sol must point to the first of CR or LF. */ |
| 1457 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1458 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1459 | /* stop here */ |
| 1460 | |
| 1461 | /* |
| 1462 | * Second, states that are specific to the request only |
| 1463 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1464 | http_msg_rqbefore: |
| 1465 | case HTTP_MSG_RQBEFORE: |
| 1466 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
| 1467 | if (likely(ptr == buf->data)) { |
| 1468 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1469 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1470 | } else { |
| 1471 | #if PARSE_PRESERVE_EMPTY_LINES |
| 1472 | /* only skip empty leading lines, don't remove them */ |
| 1473 | msg->sol = ptr; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1474 | msg->som = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1475 | #else |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1476 | /* Remove empty leading lines, as recommended by |
| 1477 | * RFC2616. This takes a lot of time because we |
| 1478 | * must move all the buffer backwards, but this |
| 1479 | * is rarely needed. The method above will be |
| 1480 | * cleaner when we'll be able to start sending |
| 1481 | * the request from any place in the buffer. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1482 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1483 | buf->lr = ptr; |
| 1484 | buffer_replace2(buf, buf->data, buf->lr, NULL, 0); |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1485 | msg->som = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1486 | msg->sol = buf->data; |
| 1487 | ptr = buf->data; |
| 1488 | end = buf->r; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1489 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1490 | } |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1491 | /* we will need this when keep-alive will be supported |
| 1492 | hdr_idx_init(idx); |
| 1493 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1494 | state = HTTP_MSG_RQMETH; |
| 1495 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1496 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1497 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1498 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1499 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1500 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1501 | if (unlikely(*ptr == '\n')) |
| 1502 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1503 | 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] | 1504 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1505 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1506 | http_msg_rqbefore_cr: |
| 1507 | case HTTP_MSG_RQBEFORE_CR: |
| 1508 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1509 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1510 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1511 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1512 | http_msg_rqmeth: |
| 1513 | case HTTP_MSG_RQMETH: |
| 1514 | case HTTP_MSG_RQMETH_SP: |
| 1515 | case HTTP_MSG_RQURI: |
| 1516 | case HTTP_MSG_RQURI_SP: |
| 1517 | case HTTP_MSG_RQVER: |
| 1518 | ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1519 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1520 | if (unlikely(!ptr)) |
| 1521 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1522 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1523 | /* we have a full request and we know that we have either a CR |
| 1524 | * or an LF at <ptr>. |
| 1525 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1526 | //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] | 1527 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1528 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1529 | msg->sol = ptr; |
| 1530 | if (likely(*ptr == '\r')) |
| 1531 | 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] | 1532 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1533 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1534 | http_msg_rqline_end: |
| 1535 | case HTTP_MSG_RQLINE_END: |
| 1536 | /* check for HTTP/0.9 request : no version information available. |
| 1537 | * msg->sol must point to the first of CR or LF. |
| 1538 | */ |
| 1539 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1540 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1541 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1542 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1543 | 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] | 1544 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1545 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1546 | /* |
| 1547 | * Common states below |
| 1548 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1549 | http_msg_hdr_first: |
| 1550 | case HTTP_MSG_HDR_FIRST: |
| 1551 | msg->sol = ptr; |
| 1552 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1553 | goto http_msg_hdr_name; |
| 1554 | } |
| 1555 | |
| 1556 | if (likely(*ptr == '\r')) |
| 1557 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1558 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1559 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1560 | http_msg_hdr_name: |
| 1561 | case HTTP_MSG_HDR_NAME: |
| 1562 | /* assumes msg->sol points to the first char */ |
| 1563 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1564 | 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] | 1565 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1566 | if (likely(*ptr == ':')) { |
| 1567 | msg->col = ptr - buf->data; |
| 1568 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| 1569 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1570 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1571 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1572 | goto http_msg_invalid; |
| 1573 | |
| 1574 | if (msg->err_pos == -1) /* capture error pointer */ |
| 1575 | msg->err_pos = ptr - buf->data; /* >= 0 now */ |
| 1576 | |
| 1577 | /* and we still accept this non-token character */ |
| 1578 | 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] | 1579 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1580 | http_msg_hdr_l1_sp: |
| 1581 | case HTTP_MSG_HDR_L1_SP: |
| 1582 | /* assumes msg->sol points to the first char and msg->col to the colon */ |
| 1583 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1584 | 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] | 1585 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1586 | /* header value can be basically anything except CR/LF */ |
| 1587 | msg->sov = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1588 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1589 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1590 | goto http_msg_hdr_val; |
| 1591 | } |
| 1592 | |
| 1593 | if (likely(*ptr == '\r')) |
| 1594 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1595 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1596 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1597 | http_msg_hdr_l1_lf: |
| 1598 | case HTTP_MSG_HDR_L1_LF: |
| 1599 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1600 | 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] | 1601 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1602 | http_msg_hdr_l1_lws: |
| 1603 | case HTTP_MSG_HDR_L1_LWS: |
| 1604 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1605 | /* replace HT,CR,LF with spaces */ |
| 1606 | for (; buf->data+msg->sov < ptr; msg->sov++) |
| 1607 | buf->data[msg->sov] = ' '; |
| 1608 | goto http_msg_hdr_l1_sp; |
| 1609 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1610 | /* we had a header consisting only in spaces ! */ |
| 1611 | msg->eol = buf->data + msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1612 | goto http_msg_complete_header; |
| 1613 | |
| 1614 | http_msg_hdr_val: |
| 1615 | case HTTP_MSG_HDR_VAL: |
| 1616 | /* assumes msg->sol points to the first char, msg->col to the |
| 1617 | * colon, and msg->sov points to the first character of the |
| 1618 | * value. |
| 1619 | */ |
| 1620 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1621 | 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] | 1622 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1623 | msg->eol = ptr; |
| 1624 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1625 | * real header end in case it ends with lots of LWS, but is this |
| 1626 | * really needed ? |
| 1627 | */ |
| 1628 | if (likely(*ptr == '\r')) |
| 1629 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1630 | goto http_msg_hdr_l2_lf; |
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 | http_msg_hdr_l2_lf: |
| 1633 | case HTTP_MSG_HDR_L2_LF: |
| 1634 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1635 | 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] | 1636 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1637 | http_msg_hdr_l2_lws: |
| 1638 | case HTTP_MSG_HDR_L2_LWS: |
| 1639 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1640 | /* LWS: replace HT,CR,LF with spaces */ |
| 1641 | for (; msg->eol < ptr; msg->eol++) |
| 1642 | *msg->eol = ' '; |
| 1643 | goto http_msg_hdr_val; |
| 1644 | } |
| 1645 | http_msg_complete_header: |
| 1646 | /* |
| 1647 | * It was a new header, so the last one is finished. |
| 1648 | * Assumes msg->sol points to the first char, msg->col to the |
| 1649 | * colon, msg->sov points to the first character of the value |
| 1650 | * and msg->eol to the first CR or LF so we know how the line |
| 1651 | * ends. We insert last header into the index. |
| 1652 | */ |
| 1653 | /* |
| 1654 | fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol); |
| 1655 | write(2, msg->sol, msg->eol-msg->sol); |
| 1656 | fprintf(stderr,"\n"); |
| 1657 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1658 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1659 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r', |
| 1660 | idx, idx->tail) < 0)) |
| 1661 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1662 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1663 | msg->sol = ptr; |
| 1664 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1665 | goto http_msg_hdr_name; |
| 1666 | } |
| 1667 | |
| 1668 | if (likely(*ptr == '\r')) |
| 1669 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1670 | goto http_msg_last_lf; |
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 | http_msg_last_lf: |
| 1673 | case HTTP_MSG_LAST_LF: |
| 1674 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1675 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1676 | ptr++; |
| 1677 | buf->lr = ptr; |
| 1678 | msg->eoh = msg->sol - buf->data; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1679 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1680 | return; |
| 1681 | #ifdef DEBUG_FULL |
| 1682 | default: |
| 1683 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1684 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1685 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1686 | } |
| 1687 | http_msg_ood: |
| 1688 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1689 | msg->msg_state = state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1690 | buf->lr = ptr; |
| 1691 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1692 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1693 | http_msg_invalid: |
| 1694 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1695 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1696 | buf->lr = ptr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1697 | return; |
| 1698 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1699 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1700 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1701 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1702 | * nothing is done and 1 is returned. |
| 1703 | */ |
| 1704 | static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn) |
| 1705 | { |
| 1706 | int delta; |
| 1707 | char *cur_end; |
| 1708 | |
| 1709 | if (msg->sl.rq.v_l != 0) |
| 1710 | return 1; |
| 1711 | |
| 1712 | msg->sol = req->data + msg->som; |
| 1713 | cur_end = msg->sol + msg->sl.rq.l; |
| 1714 | delta = 0; |
| 1715 | |
| 1716 | if (msg->sl.rq.u_l == 0) { |
| 1717 | /* if no URI was set, add "/" */ |
| 1718 | delta = buffer_replace2(req, cur_end, cur_end, " /", 2); |
| 1719 | cur_end += delta; |
| 1720 | msg->eoh += delta; |
| 1721 | } |
| 1722 | /* add HTTP version */ |
| 1723 | delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11); |
| 1724 | msg->eoh += delta; |
| 1725 | cur_end += delta; |
| 1726 | cur_end = (char *)http_parse_reqline(msg, req->data, |
| 1727 | HTTP_MSG_RQMETH, |
| 1728 | msg->sol, cur_end + 1, |
| 1729 | NULL, NULL); |
| 1730 | if (unlikely(!cur_end)) |
| 1731 | return 0; |
| 1732 | |
| 1733 | /* we have a full HTTP/1.0 request now and we know that |
| 1734 | * we have either a CR or an LF at <ptr>. |
| 1735 | */ |
| 1736 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1737 | return 1; |
| 1738 | } |
| 1739 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1740 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 1741 | * processing can continue on next analysers, or zero if it either needs more |
| 1742 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 1743 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers |
| 1744 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 1745 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1746 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 1747 | 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] | 1748 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1749 | /* |
| 1750 | * We will parse the partial (or complete) lines. |
| 1751 | * We will check the request syntax, and also join multi-line |
| 1752 | * headers. An index of all the lines will be elaborated while |
| 1753 | * parsing. |
| 1754 | * |
| 1755 | * For the parsing, we use a 28 states FSM. |
| 1756 | * |
| 1757 | * Here is the information we currently have : |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 1758 | * req->data + msg->som = beginning of request |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1759 | * req->data + req->eoh = end of processed headers / start of current one |
| 1760 | * req->data + req->eol = end of current header or line (LF or CRLF) |
| 1761 | * req->lr = first non-visited byte |
| 1762 | * req->r = end of data |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1763 | * |
| 1764 | * At end of parsing, we may perform a capture of the error (if any), and |
| 1765 | * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE). |
| 1766 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 1767 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1768 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1769 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1770 | int cur_idx; |
| 1771 | struct http_txn *txn = &s->txn; |
| 1772 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 1773 | struct hdr_ctx ctx; |
| 1774 | int conn_ka, conn_cl; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1775 | |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 1776 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 1777 | now_ms, __FUNCTION__, |
| 1778 | s, |
| 1779 | req, |
| 1780 | req->rex, req->wex, |
| 1781 | req->flags, |
| 1782 | req->l, |
| 1783 | req->analysers); |
| 1784 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 1785 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 1786 | s->srv_error = http_return_srv_error; |
| 1787 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1788 | if (likely(req->lr < req->r)) |
| 1789 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1790 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1791 | /* 1: we might have to print this header in debug mode */ |
| 1792 | if (unlikely((global.mode & MODE_DEBUG) && |
| 1793 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
| 1794 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
| 1795 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1796 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1797 | sol = req->data + msg->som; |
| 1798 | eol = sol + msg->sl.rq.l; |
| 1799 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 1800 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1801 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 1802 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1803 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1804 | while (cur_idx) { |
| 1805 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 1806 | debug_hdr("clihdr", s, sol, eol); |
| 1807 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 1808 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1809 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1810 | } |
| 1811 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1812 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1813 | /* |
| 1814 | * Now we quickly check if we have found a full valid request. |
| 1815 | * If not so, we check the FD and buffer states before leaving. |
| 1816 | * A full request is indicated by the fact that we have seen |
| 1817 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 1818 | * requests are checked first. |
| 1819 | * |
| 1820 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1821 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1822 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1823 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1824 | * First, let's catch bad requests. |
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_ERROR)) |
| 1827 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1828 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1829 | /* 1: Since we are in header mode, if there's no space |
| 1830 | * left for headers, we won't be able to free more |
| 1831 | * later, so the session will never terminate. We |
| 1832 | * must terminate it now. |
| 1833 | */ |
| 1834 | if (unlikely(req->flags & BF_FULL)) { |
| 1835 | /* FIXME: check if URI is set and return Status |
| 1836 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1837 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1838 | goto return_bad_req; |
| 1839 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1840 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1841 | /* 2: have we encountered a read error ? */ |
| 1842 | else if (req->flags & BF_READ_ERROR) { |
| 1843 | /* we cannot return any message on error */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 1844 | if (msg->err_pos >= 0) |
| 1845 | 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] | 1846 | msg->msg_state = HTTP_MSG_ERROR; |
| 1847 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1848 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 1849 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1850 | if (s->listener->counters) |
| 1851 | s->listener->counters->failed_req++; |
| 1852 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 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; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1869 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 1870 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1871 | if (s->listener->counters) |
| 1872 | s->listener->counters->failed_req++; |
| 1873 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1874 | if (!(s->flags & SN_ERR_MASK)) |
| 1875 | s->flags |= SN_ERR_CLITO; |
| 1876 | if (!(s->flags & SN_FINST_MASK)) |
| 1877 | s->flags |= SN_FINST_R; |
| 1878 | return 0; |
| 1879 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 1880 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1881 | /* 4: have we encountered a close ? */ |
| 1882 | else if (req->flags & BF_SHUTR) { |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 1883 | if (msg->err_pos >= 0) |
| 1884 | 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] | 1885 | txn->status = 400; |
| 1886 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 1887 | msg->msg_state = HTTP_MSG_ERROR; |
| 1888 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1889 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 1890 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 1891 | if (s->listener->counters) |
| 1892 | s->listener->counters->failed_req++; |
| 1893 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1894 | if (!(s->flags & SN_ERR_MASK)) |
| 1895 | s->flags |= SN_ERR_CLICL; |
| 1896 | if (!(s->flags & SN_FINST_MASK)) |
| 1897 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 1898 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1899 | } |
| 1900 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 1901 | buffer_dont_connect(req); |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 1902 | req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 1903 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1904 | /* just set the request timeout once at the beginning of the request */ |
| 1905 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 1906 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1907 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1908 | /* we're not ready yet */ |
| 1909 | return 0; |
| 1910 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1911 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1912 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 1913 | * complete the request parsing by setting a few fields we will need |
| 1914 | * later. |
| 1915 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1916 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1917 | /* Maybe we found in invalid header name while we were configured not |
| 1918 | * to block on that, so we have to capture it now. |
| 1919 | */ |
| 1920 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 1921 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
| 1922 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1923 | /* ensure we keep this pointer to the beginning of the message */ |
| 1924 | msg->sol = req->data + msg->som; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1925 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1926 | /* |
| 1927 | * 1: identify the method |
| 1928 | */ |
| 1929 | txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l); |
| 1930 | |
| 1931 | /* we can make use of server redirect on GET and HEAD */ |
| 1932 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 1933 | s->flags |= SN_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 1934 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1935 | /* |
| 1936 | * 2: check if the URI matches the monitor_uri. |
| 1937 | * We have to do this for every request which gets in, because |
| 1938 | * the monitor-uri is defined by the frontend. |
| 1939 | */ |
| 1940 | if (unlikely((s->fe->monitor_uri_len != 0) && |
| 1941 | (s->fe->monitor_uri_len == msg->sl.rq.u_l) && |
| 1942 | !memcmp(&req->data[msg->sl.rq.u], |
| 1943 | s->fe->monitor_uri, |
| 1944 | s->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1945 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1946 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1947 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1948 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1949 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1950 | s->flags |= SN_MONITOR; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1951 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1952 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 1953 | list_for_each_entry(cond, &s->fe->mon_fail_cond, list) { |
| 1954 | 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] | 1955 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1956 | ret = acl_pass(ret); |
| 1957 | if (cond->pol == ACL_COND_UNLESS) |
| 1958 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1959 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1960 | if (ret) { |
| 1961 | /* we fail this request, let's return 503 service unavail */ |
| 1962 | txn->status = 503; |
| 1963 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503)); |
| 1964 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 1965 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1966 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 1967 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1968 | /* nothing to fail, let's reply normaly */ |
| 1969 | txn->status = 200; |
| 1970 | stream_int_retnclose(req->prod, &http_200_chunk); |
| 1971 | goto return_prx_cond; |
| 1972 | } |
| 1973 | |
| 1974 | /* |
| 1975 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 1976 | * Note: we cannot log anymore if the request has been |
| 1977 | * classified as invalid. |
| 1978 | */ |
| 1979 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 1980 | /* we have a complete HTTP request that we must log */ |
| 1981 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 1982 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1983 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1984 | if (urilen >= REQURI_LEN) |
| 1985 | urilen = REQURI_LEN - 1; |
| 1986 | memcpy(txn->uri, &req->data[msg->som], urilen); |
| 1987 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1988 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1989 | if (!(s->logs.logwait &= ~LW_REQ)) |
| 1990 | s->do_log(s); |
| 1991 | } else { |
| 1992 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1993 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1994 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 1995 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 1996 | /* 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] | 1997 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) |
| 1998 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1999 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2000 | /* 5: we may need to capture headers */ |
| 2001 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap)) |
| 2002 | capture_headers(req->data + msg->som, &txn->hdr_idx, |
| 2003 | txn->req.cap, s->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2004 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2005 | /* 6: determine if we have a transfer-encoding or content-length. |
| 2006 | * RFC2616 #4.4 states that : |
| 2007 | * - If a Transfer-Encoding header field is present and has any value |
| 2008 | * other than "identity", then the transfer-length is defined by use |
| 2009 | * of the "chunked" transfer-coding ; |
| 2010 | * |
| 2011 | * - If a Content-Length header field is present, its decimal value in |
| 2012 | * OCTETs represents both the entity-length and the transfer-length. |
| 2013 | * If a message is received with both a Transfer-Encoding header |
| 2014 | * field and a Content-Length header field, the latter MUST be ignored. |
| 2015 | * |
| 2016 | * - If a request contains a message-body and a Content-Length is not |
| 2017 | * given, the server SHOULD respond with 400 (bad request) if it |
| 2018 | * cannot determine the length of the message, or with 411 (length |
| 2019 | * required) if it wishes to insist on receiving a valid Content-Length. |
| 2020 | */ |
| 2021 | |
| 2022 | /* FIXME: chunked encoding is HTTP/1.1 only */ |
| 2023 | ctx.idx = 0; |
| 2024 | while (http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) { |
| 2025 | if (ctx.vlen == 8 && strncasecmp(ctx.line + ctx.val, "identity", 8) == 0) |
| 2026 | continue; |
| 2027 | txn->flags |= TX_REQ_TE_CHNK; |
| 2028 | break; |
| 2029 | } |
| 2030 | |
| 2031 | /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */ |
| 2032 | ctx.idx = 0; |
| 2033 | while (!(txn->flags & TX_REQ_TE_CHNK) && |
| 2034 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) { |
| 2035 | signed long long cl; |
| 2036 | |
| 2037 | if (!ctx.vlen) |
| 2038 | goto return_bad_req; |
| 2039 | |
| 2040 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) |
| 2041 | goto return_bad_req; /* parse failure */ |
| 2042 | |
| 2043 | if (cl < 0) |
| 2044 | goto return_bad_req; |
| 2045 | |
| 2046 | if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl)) |
| 2047 | goto return_bad_req; /* already specified, was different */ |
| 2048 | |
| 2049 | txn->flags |= TX_REQ_CNT_LEN; |
| 2050 | msg->hdr_content_len = cl; |
| 2051 | } |
| 2052 | |
| 2053 | /* Determine if the client wishes keep-alive or close. |
| 2054 | * RFC2616 #8.1.2 and #14.10 state that HTTP/1.1 and above connections |
| 2055 | * are persistent unless "Connection: close" is explicitly specified. |
| 2056 | * RFC2616 #19.6.2 refers to RFC2068 for HTTP/1.0 persistent connections. |
| 2057 | * RFC2068 #19.7.1 states that HTTP/1.0 clients are not persistent unless |
| 2058 | * they explicitly specify "Connection: keep-alive", regardless of any |
| 2059 | * optional "keep-alive" header. |
| 2060 | */ |
| 2061 | |
| 2062 | /* FIXME: should we also remove any header specified in "connection" ? */ |
| 2063 | conn_ka = conn_cl = 0; |
| 2064 | ctx.idx = 0; |
| 2065 | while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) { |
| 2066 | if (ctx.vlen == 5 && strncasecmp(ctx.line + ctx.val, "close", 5) == 0) |
| 2067 | conn_cl = 1; |
| 2068 | else if (ctx.vlen == 10 && strncasecmp(ctx.line + ctx.val, "keep-alive", 10) == 0) |
| 2069 | conn_ka = 1; |
| 2070 | } |
| 2071 | |
| 2072 | if ((msg->sl.rq.v_l == 8) && |
| 2073 | (req->data[msg->som + msg->sl.rq.v + 5] == '1') && |
| 2074 | (req->data[msg->som + msg->sl.rq.v + 7] == '0')) { |
| 2075 | /* HTTP/1.0 */ |
| 2076 | if (conn_ka) |
| 2077 | txn->flags |= (TX_CLI_CONN_KA | TX_SRV_CONN_KA); |
| 2078 | } else { |
| 2079 | /* HTTP/1.0 */ |
| 2080 | if (!conn_cl) |
| 2081 | txn->flags |= (TX_CLI_CONN_KA | TX_SRV_CONN_KA); |
| 2082 | } |
| 2083 | |
| 2084 | /* we can mark the connection as non-persistent if needed */ |
| 2085 | if (!(txn->flags & TX_SRV_CONN_KA)) |
| 2086 | s->flags |= SN_CONN_CLOSED; |
| 2087 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2088 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2089 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2090 | req->analyse_exp = TICK_ETERNITY; |
| 2091 | return 1; |
| 2092 | |
| 2093 | return_bad_req: |
| 2094 | /* We centralize bad requests processing here */ |
| 2095 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2096 | /* we detected a parsing error. We want to archive this request |
| 2097 | * in the dedicated proxy area for later troubleshooting. |
| 2098 | */ |
| 2099 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
| 2100 | } |
| 2101 | |
| 2102 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2103 | txn->status = 400; |
| 2104 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2105 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2106 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2107 | if (s->listener->counters) |
| 2108 | s->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2109 | |
| 2110 | return_prx_cond: |
| 2111 | if (!(s->flags & SN_ERR_MASK)) |
| 2112 | s->flags |= SN_ERR_PRXCOND; |
| 2113 | if (!(s->flags & SN_FINST_MASK)) |
| 2114 | s->flags |= SN_FINST_R; |
| 2115 | |
| 2116 | req->analysers = 0; |
| 2117 | req->analyse_exp = TICK_ETERNITY; |
| 2118 | return 0; |
| 2119 | } |
| 2120 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2121 | /* This stream analyser runs all HTTP request processing which is common to |
| 2122 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 2123 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2124 | * 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] | 2125 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 2126 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2127 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2128 | 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] | 2129 | { |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2130 | struct http_txn *txn = &s->txn; |
| 2131 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2132 | struct acl_cond *cond; |
| 2133 | struct redirect_rule *rule; |
| 2134 | int cur_idx; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2135 | |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2136 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
| 2137 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2138 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2139 | return 0; |
| 2140 | } |
| 2141 | |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2142 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2143 | req->analyse_exp = TICK_ETERNITY; |
| 2144 | |
| 2145 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 2146 | now_ms, __FUNCTION__, |
| 2147 | s, |
| 2148 | req, |
| 2149 | req->rex, req->wex, |
| 2150 | req->flags, |
| 2151 | req->l, |
| 2152 | req->analysers); |
| 2153 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2154 | /* first check whether we have some ACLs set to block this request */ |
| 2155 | list_for_each_entry(cond, &px->block_cond, list) { |
| 2156 | int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ); |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2157 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2158 | ret = acl_pass(ret); |
| 2159 | if (cond->pol == ACL_COND_UNLESS) |
| 2160 | ret = !ret; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2161 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2162 | if (ret) { |
| 2163 | txn->status = 403; |
| 2164 | /* let's log the request time */ |
| 2165 | s->logs.tv_request = now; |
| 2166 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
| 2167 | goto return_prx_cond; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2168 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2169 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2170 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2171 | /* try headers filters */ |
| 2172 | if (px->req_exp != NULL) { |
| 2173 | if (apply_filters_to_request(s, req, px->req_exp) < 0) |
| 2174 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2175 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2176 | /* has the request been denied ? */ |
| 2177 | if (txn->flags & TX_CLDENY) { |
| 2178 | /* no need to go further */ |
| 2179 | txn->status = 403; |
| 2180 | /* let's log the request time */ |
| 2181 | s->logs.tv_request = now; |
| 2182 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
| 2183 | goto return_prx_cond; |
| 2184 | } |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2185 | |
| 2186 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 2187 | * which may be the same as the connect timeout if unspecified. |
| 2188 | * If unset, then set it to zero because we really want it to |
| 2189 | * eventually expire. We build the tarpit as an analyser. |
| 2190 | */ |
| 2191 | if (txn->flags & TX_CLTARPIT) { |
| 2192 | buffer_erase(s->req); |
| 2193 | /* wipe the request out so that we can drop the connection early |
| 2194 | * if the client closes first. |
| 2195 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2196 | buffer_dont_connect(req); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2197 | req->analysers = 0; /* remove switching rules etc... */ |
| 2198 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 2199 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 2200 | if (!req->analyse_exp) |
| 2201 | req->analyse_exp = tick_add(now_ms, 0); |
| 2202 | return 1; |
| 2203 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2204 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2205 | |
Willy Tarreau | 4273664 | 2009-10-18 21:04:35 +0200 | [diff] [blame^] | 2206 | /* We might have to check for "Connection:". The test for persistent |
| 2207 | * connections has already been performed, so we only enter here if |
| 2208 | * we are certain the connection is persistent. |
| 2209 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2210 | if (((s->fe->options | s->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) && |
| 2211 | !(s->flags & SN_CONN_CLOSED)) { |
| 2212 | char *cur_ptr, *cur_end, *cur_next; |
| 2213 | int old_idx, delta, val; |
| 2214 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2215 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2216 | cur_next = req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 2217 | old_idx = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2218 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2219 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 2220 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 2221 | cur_ptr = cur_next; |
| 2222 | cur_end = cur_ptr + cur_hdr->len; |
| 2223 | cur_next = cur_end + cur_hdr->cr + 1; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2224 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2225 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 2226 | if (val) { |
| 2227 | /* 3 possibilities : |
| 2228 | * - we have already set Connection: close, |
| 2229 | * so we remove this line. |
| 2230 | * - we have not yet set Connection: close, |
| 2231 | * but this line indicates close. We leave |
| 2232 | * it untouched and set the flag. |
| 2233 | * - we have not yet set Connection: close, |
| 2234 | * and this line indicates non-close. We |
| 2235 | * replace it. |
| 2236 | */ |
| 2237 | if (s->flags & SN_CONN_CLOSED) { |
| 2238 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 2239 | txn->req.eoh += delta; |
| 2240 | cur_next += delta; |
| 2241 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 2242 | txn->hdr_idx.used--; |
| 2243 | cur_hdr->len = 0; |
| 2244 | } else { |
| 2245 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 2246 | delta = buffer_replace2(req, cur_ptr + val, cur_end, |
| 2247 | "close", 5); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2248 | cur_next += delta; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2249 | cur_hdr->len += delta; |
| 2250 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2251 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2252 | s->flags |= SN_CONN_CLOSED; |
Willy Tarreau | 4273664 | 2009-10-18 21:04:35 +0200 | [diff] [blame^] | 2253 | txn->flags &= ~TX_SRV_CONN_KA; /* keep-alive closed on server side */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2254 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2255 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2256 | old_idx = cur_idx; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2257 | } |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 2258 | |
Willy Tarreau | 4273664 | 2009-10-18 21:04:35 +0200 | [diff] [blame^] | 2259 | /* if there is no "Connection: keep-alive" header left and we're |
| 2260 | * in HTTP/1.0, then non-persistent connection is implied */ |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 2261 | if (!(s->flags & SN_CONN_CLOSED) && (msg->sl.rq.v_l == 8) && |
| 2262 | (req->data[msg->som + msg->sl.rq.v + 5] == '1') && |
Willy Tarreau | 4273664 | 2009-10-18 21:04:35 +0200 | [diff] [blame^] | 2263 | (req->data[msg->som + msg->sl.rq.v + 7] == '0')) { |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 2264 | s->flags |= SN_CONN_CLOSED; |
Willy Tarreau | 4273664 | 2009-10-18 21:04:35 +0200 | [diff] [blame^] | 2265 | txn->flags &= ~TX_SRV_CONN_KA; /* keep-alive closed on server side */ |
| 2266 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2267 | } |
| 2268 | /* add request headers from the rule sets in the same order */ |
| 2269 | for (cur_idx = 0; cur_idx < px->nb_reqadd; cur_idx++) { |
| 2270 | if (unlikely(http_header_add_tail(req, |
| 2271 | &txn->req, |
| 2272 | &txn->hdr_idx, |
| 2273 | px->req_add[cur_idx])) < 0) |
| 2274 | goto return_bad_req; |
| 2275 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2276 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2277 | /* check if stats URI was requested, and if an auth is needed */ |
| 2278 | if (px->uri_auth != NULL && |
| 2279 | (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) { |
| 2280 | /* we have to check the URI and auth for this request. |
| 2281 | * FIXME!!! that one is rather dangerous, we want to |
| 2282 | * make it follow standard rules (eg: clear req->analysers). |
| 2283 | */ |
| 2284 | if (stats_check_uri_auth(s, px)) { |
| 2285 | req->analysers = 0; |
| 2286 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2287 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2288 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2289 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2290 | /* check whether we have some ACLs set to redirect this request */ |
| 2291 | list_for_each_entry(rule, &px->redirect_rules, list) { |
| 2292 | 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] | 2293 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2294 | ret = acl_pass(ret); |
| 2295 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2296 | ret = !ret; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2297 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2298 | if (ret) { |
| 2299 | struct chunk rdr = { trash, 0 }; |
| 2300 | const char *msg_fmt; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2301 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2302 | /* build redirect message */ |
| 2303 | switch(rule->code) { |
| 2304 | case 303: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2305 | msg_fmt = HTTP_303; |
| 2306 | break; |
| 2307 | case 301: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2308 | msg_fmt = HTTP_301; |
| 2309 | break; |
| 2310 | case 302: |
| 2311 | default: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2312 | msg_fmt = HTTP_302; |
| 2313 | break; |
| 2314 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2315 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 2316 | if (unlikely(chunk_strcpy(&rdr, msg_fmt))) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2317 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2318 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2319 | switch(rule->type) { |
| 2320 | case REDIRECT_TYPE_PREFIX: { |
| 2321 | const char *path; |
| 2322 | int pathlen; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2323 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2324 | path = http_get_path(txn); |
| 2325 | /* build message using path */ |
| 2326 | if (path) { |
| 2327 | pathlen = txn->req.sl.rq.u_l + (txn->req.sol+txn->req.sl.rq.u) - path; |
| 2328 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 2329 | int qs = 0; |
| 2330 | while (qs < pathlen) { |
| 2331 | if (path[qs] == '?') { |
| 2332 | pathlen = qs; |
| 2333 | break; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2334 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2335 | qs++; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2336 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2337 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2338 | } else { |
| 2339 | path = "/"; |
| 2340 | pathlen = 1; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2341 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2342 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 2343 | if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2344 | goto return_bad_req; |
| 2345 | |
| 2346 | /* add prefix. Note that if prefix == "/", we don't want to |
| 2347 | * add anything, otherwise it makes it hard for the user to |
| 2348 | * configure a self-redirection. |
| 2349 | */ |
| 2350 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2351 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 2352 | rdr.len += rule->rdr_len; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2353 | } |
| 2354 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2355 | /* add path */ |
| 2356 | memcpy(rdr.str + rdr.len, path, pathlen); |
| 2357 | rdr.len += pathlen; |
| 2358 | break; |
| 2359 | } |
| 2360 | case REDIRECT_TYPE_LOCATION: |
| 2361 | default: |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 2362 | if (rdr.len + rule->rdr_len > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2363 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2364 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2365 | /* add location */ |
| 2366 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 2367 | rdr.len += rule->rdr_len; |
| 2368 | break; |
| 2369 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2370 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2371 | if (rule->cookie_len) { |
| 2372 | memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14); |
| 2373 | rdr.len += 14; |
| 2374 | memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len); |
| 2375 | rdr.len += rule->cookie_len; |
| 2376 | memcpy(rdr.str + rdr.len, "\r\n", 2); |
| 2377 | rdr.len += 2; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2378 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2379 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2380 | /* add end of headers */ |
| 2381 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 2382 | rdr.len += 4; |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2383 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2384 | txn->status = rule->code; |
| 2385 | /* let's log the request time */ |
| 2386 | s->logs.tv_request = now; |
| 2387 | stream_int_retnclose(req->prod, &rdr); |
| 2388 | goto return_prx_cond; |
| 2389 | } |
| 2390 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2391 | |
Willy Tarreau | f1ba4b3 | 2009-10-17 14:37:52 +0200 | [diff] [blame] | 2392 | /* We can shut read side if "connection: close" && !abort_on_close && !content-length */ |
| 2393 | if ((txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) && |
| 2394 | (s->flags & SN_CONN_CLOSED) && !(s->be->options & PR_O_ABRT_CLOSE)) { |
| 2395 | struct hdr_ctx ctx; |
| 2396 | ctx.idx = 0; |
| 2397 | if (!http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) { |
| 2398 | ctx.idx = 0; |
| 2399 | if (!http_find_header2("Content-length", 14, msg->sol, &txn->hdr_idx, &ctx)) |
| 2400 | req->flags |= BF_DONT_READ; |
| 2401 | } |
| 2402 | } |
| 2403 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2404 | /* that's OK for us now, let's move on to next analysers */ |
| 2405 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2406 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2407 | return_bad_req: |
| 2408 | /* We centralize bad requests processing here */ |
| 2409 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2410 | /* we detected a parsing error. We want to archive this request |
| 2411 | * in the dedicated proxy area for later troubleshooting. |
| 2412 | */ |
| 2413 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
| 2414 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2415 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2416 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2417 | txn->status = 400; |
| 2418 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2419 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2420 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2421 | if (s->listener->counters) |
| 2422 | s->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2423 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2424 | return_prx_cond: |
| 2425 | if (!(s->flags & SN_ERR_MASK)) |
| 2426 | s->flags |= SN_ERR_PRXCOND; |
| 2427 | if (!(s->flags & SN_FINST_MASK)) |
| 2428 | s->flags |= SN_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 2429 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2430 | req->analysers = 0; |
| 2431 | req->analyse_exp = TICK_ETERNITY; |
| 2432 | return 0; |
| 2433 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2434 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2435 | /* This function performs all the processing enabled for the current request. |
| 2436 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 2437 | * needs more data, encounters an error, or wants to immediately abort the |
| 2438 | * request. It relies on buffers flags, and updates s->req->analysers. |
| 2439 | */ |
| 2440 | int http_process_request(struct session *s, struct buffer *req, int an_bit) |
| 2441 | { |
| 2442 | struct http_txn *txn = &s->txn; |
| 2443 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2444 | |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2445 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
| 2446 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2447 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2448 | return 0; |
| 2449 | } |
| 2450 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2451 | req->analysers &= ~an_bit; |
| 2452 | req->analyse_exp = TICK_ETERNITY; |
| 2453 | |
| 2454 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 2455 | now_ms, __FUNCTION__, |
| 2456 | s, |
| 2457 | req, |
| 2458 | req->rex, req->wex, |
| 2459 | req->flags, |
| 2460 | req->l, |
| 2461 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2462 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2463 | /* |
| 2464 | * Right now, we know that we have processed the entire headers |
| 2465 | * and that unwanted requests have been filtered out. We can do |
| 2466 | * whatever we want with the remaining request. Also, now we |
| 2467 | * may have separate values for ->fe, ->be. |
| 2468 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2469 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2470 | /* |
| 2471 | * If HTTP PROXY is set we simply get remote server address |
| 2472 | * parsing incoming request. |
| 2473 | */ |
| 2474 | if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) { |
| 2475 | url2sa(req->data + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr); |
| 2476 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2477 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2478 | /* |
| 2479 | * 7: the appsession cookie was looked up very early in 1.2, |
| 2480 | * so let's do the same now. |
| 2481 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2482 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2483 | /* It needs to look into the URI */ |
| 2484 | if (s->be->appsession_name) { |
| 2485 | get_srv_from_appsession(s, &req->data[msg->som], msg->sl.rq.l); |
| 2486 | } |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2487 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2488 | /* |
| 2489 | * 8: Now we can work with the cookies. |
| 2490 | * Note that doing so might move headers in the request, but |
| 2491 | * the fields will stay coherent and the URI will not move. |
| 2492 | * This should only be performed in the backend. |
| 2493 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 2494 | 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] | 2495 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 2496 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 2497 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2498 | /* |
| 2499 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 2500 | * asks for it. |
| 2501 | */ |
| 2502 | if ((s->fe->options | s->be->options) & PR_O_FWDFOR) { |
| 2503 | if (s->cli_addr.ss_family == AF_INET) { |
| 2504 | /* Add an X-Forwarded-For header unless the source IP is |
| 2505 | * in the 'except' network range. |
| 2506 | */ |
| 2507 | if ((!s->fe->except_mask.s_addr || |
| 2508 | (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr) |
| 2509 | != s->fe->except_net.s_addr) && |
| 2510 | (!s->be->except_mask.s_addr || |
| 2511 | (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr) |
| 2512 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2513 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2514 | unsigned char *pn; |
| 2515 | pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2516 | |
| 2517 | /* Note: we rely on the backend to get the header name to be used for |
| 2518 | * x-forwarded-for, because the header is really meant for the backends. |
| 2519 | * However, if the backend did not specify any option, we have to rely |
| 2520 | * on the frontend's header name. |
| 2521 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2522 | if (s->be->fwdfor_hdr_len) { |
| 2523 | len = s->be->fwdfor_hdr_len; |
| 2524 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 2525 | } else { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2526 | len = s->fe->fwdfor_hdr_len; |
| 2527 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
| 2528 | } |
| 2529 | 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] | 2530 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 2531 | if (unlikely(http_header_add_tail2(req, &txn->req, |
| 2532 | &txn->hdr_idx, trash, len)) < 0) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2533 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 2534 | } |
| 2535 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2536 | else if (s->cli_addr.ss_family == AF_INET6) { |
| 2537 | /* FIXME: for the sake of completeness, we should also support |
| 2538 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2539 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2540 | int len; |
| 2541 | char pn[INET6_ADDRSTRLEN]; |
| 2542 | inet_ntop(AF_INET6, |
| 2543 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 2544 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2545 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2546 | /* Note: we rely on the backend to get the header name to be used for |
| 2547 | * x-forwarded-for, because the header is really meant for the backends. |
| 2548 | * However, if the backend did not specify any option, we have to rely |
| 2549 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2550 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2551 | if (s->be->fwdfor_hdr_len) { |
| 2552 | len = s->be->fwdfor_hdr_len; |
| 2553 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
| 2554 | } else { |
| 2555 | len = s->fe->fwdfor_hdr_len; |
| 2556 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2557 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2558 | len += sprintf(trash + len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2559 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2560 | if (unlikely(http_header_add_tail2(req, &txn->req, |
| 2561 | &txn->hdr_idx, trash, len)) < 0) |
| 2562 | goto return_bad_req; |
| 2563 | } |
| 2564 | } |
| 2565 | |
| 2566 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 2567 | * 10: add X-Original-To if either the frontend or the backend |
| 2568 | * asks for it. |
| 2569 | */ |
| 2570 | if ((s->fe->options | s->be->options) & PR_O_ORGTO) { |
| 2571 | |
| 2572 | /* FIXME: don't know if IPv6 can handle that case too. */ |
| 2573 | if (s->cli_addr.ss_family == AF_INET) { |
| 2574 | /* Add an X-Original-To header unless the destination IP is |
| 2575 | * in the 'except' network range. |
| 2576 | */ |
| 2577 | if (!(s->flags & SN_FRT_ADDR_SET)) |
| 2578 | get_frt_addr(s); |
| 2579 | |
| 2580 | if ((!s->fe->except_mask_to.s_addr || |
| 2581 | (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr) |
| 2582 | != s->fe->except_to.s_addr) && |
| 2583 | (!s->be->except_mask_to.s_addr || |
| 2584 | (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr) |
| 2585 | != s->be->except_to.s_addr)) { |
| 2586 | int len; |
| 2587 | unsigned char *pn; |
| 2588 | pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr; |
| 2589 | |
| 2590 | /* Note: we rely on the backend to get the header name to be used for |
| 2591 | * x-original-to, because the header is really meant for the backends. |
| 2592 | * However, if the backend did not specify any option, we have to rely |
| 2593 | * on the frontend's header name. |
| 2594 | */ |
| 2595 | if (s->be->orgto_hdr_len) { |
| 2596 | len = s->be->orgto_hdr_len; |
| 2597 | memcpy(trash, s->be->orgto_hdr_name, len); |
| 2598 | } else { |
| 2599 | len = s->fe->orgto_hdr_len; |
| 2600 | memcpy(trash, s->fe->orgto_hdr_name, len); |
| 2601 | } |
| 2602 | len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
| 2603 | |
| 2604 | if (unlikely(http_header_add_tail2(req, &txn->req, |
| 2605 | &txn->hdr_idx, trash, len)) < 0) |
| 2606 | goto return_bad_req; |
| 2607 | } |
| 2608 | } |
| 2609 | } |
| 2610 | |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 2611 | /* 11: add "Connection: close" if needed and not yet set. */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2612 | if (!(s->flags & SN_CONN_CLOSED) && |
| 2613 | ((s->fe->options | s->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) { |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 2614 | if (unlikely(http_header_add_tail2(req, &txn->req, &txn->hdr_idx, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2615 | "Connection: close", 17)) < 0) |
| 2616 | goto return_bad_req; |
| 2617 | s->flags |= SN_CONN_CLOSED; |
| 2618 | } |
| 2619 | /* Before we switch to data, was assignment set in manage_client_side_cookie? |
| 2620 | * If not assigned, perhaps we are balancing on url_param, but this is a |
| 2621 | * POST; and the parameters are in the body, maybe scan there to find our server. |
| 2622 | * (unless headers overflowed the buffer?) |
| 2623 | */ |
| 2624 | if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 2625 | s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
| 2626 | s->be->url_param_post_limit != 0 && !(req->flags & BF_FULL) && |
| 2627 | memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) { |
| 2628 | /* are there enough bytes here? total == l || r || rlim ? |
| 2629 | * len is unsigned, but eoh is int, |
| 2630 | * how many bytes of body have we received? |
| 2631 | * eoh is the first empty line of the header |
| 2632 | */ |
| 2633 | /* already established CRLF or LF at eoh, move to start of message, find message length in buffer */ |
| 2634 | unsigned long len = req->l - (msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1); |
| 2635 | |
| 2636 | /* If we have HTTP/1.1 and Expect: 100-continue, then abort. |
| 2637 | * We can't assume responsibility for the server's decision, |
| 2638 | * on this URI and header set. See rfc2616: 14.20, 8.2.3, |
| 2639 | * We also can't change our mind later, about which server to choose, so round robin. |
| 2640 | */ |
| 2641 | if ((likely(msg->sl.rq.v_l == 8) && req->data[msg->som + msg->sl.rq.v + 7] == '1')) { |
| 2642 | struct hdr_ctx ctx; |
| 2643 | ctx.idx = 0; |
| 2644 | /* Expect is allowed in 1.1, look for it */ |
| 2645 | http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx); |
| 2646 | if (ctx.idx != 0 && |
| 2647 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) |
| 2648 | /* We can't reliablly stall and wait for data, because of |
| 2649 | * .NET clients that don't conform to rfc2616; so, no need for |
| 2650 | * the next block to check length expectations. |
| 2651 | * We could send 100 status back to the client, but then we need to |
| 2652 | * re-write headers, and send the message. And this isn't the right |
| 2653 | * place for that action. |
| 2654 | * TODO: support Expect elsewhere and delete this block. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2655 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2656 | goto end_check_maybe_wait_for_body; |
| 2657 | } |
| 2658 | |
| 2659 | if (likely(len > s->be->url_param_post_limit)) { |
| 2660 | /* nothing to do, we got enough */ |
| 2661 | } else { |
| 2662 | /* limit implies we are supposed to need this many bytes |
| 2663 | * to find the parameter. Let's see how many bytes we can wait for. |
| 2664 | */ |
| 2665 | long long hint = len; |
| 2666 | struct hdr_ctx ctx; |
| 2667 | ctx.idx = 0; |
| 2668 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx); |
| 2669 | if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) { |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2670 | buffer_dont_connect(req); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2671 | req->analysers |= AN_REQ_HTTP_BODY; |
| 2672 | } |
| 2673 | else { |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2674 | ctx.idx = 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2675 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx); |
| 2676 | /* now if we have a length, we'll take the hint */ |
| 2677 | if (ctx.idx) { |
| 2678 | /* We have Content-Length */ |
| 2679 | if (strl2llrc(ctx.line+ctx.val,ctx.vlen, &hint)) |
| 2680 | hint = 0; /* parse failure, untrusted client */ |
| 2681 | else { |
| 2682 | if (hint > 0) |
| 2683 | msg->hdr_content_len = hint; |
| 2684 | else |
| 2685 | hint = 0; /* bad client, sent negative length */ |
| 2686 | } |
| 2687 | } |
| 2688 | /* but limited to what we care about, maybe we don't expect any entity data (hint == 0) */ |
| 2689 | if (s->be->url_param_post_limit < hint) |
| 2690 | hint = s->be->url_param_post_limit; |
| 2691 | /* now do we really need to buffer more data? */ |
| 2692 | if (len < hint) { |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2693 | buffer_dont_connect(req); |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2694 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 41f40ed | 2008-08-21 10:05:00 +0200 | [diff] [blame] | 2695 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2696 | /* else... There are no body bytes to wait for */ |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 2697 | } |
| 2698 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2699 | } |
| 2700 | end_check_maybe_wait_for_body: |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2701 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2702 | /************************************************************* |
| 2703 | * OK, that's finished for the headers. We have done what we * |
| 2704 | * could. Let's switch to the DATA state. * |
| 2705 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2706 | |
Willy Tarreau | a07a34e | 2009-08-16 23:27:46 +0200 | [diff] [blame] | 2707 | buffer_set_rlim(req, req->size); /* no more rewrite needed */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2708 | s->logs.tv_request = now; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2709 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2710 | /* OK let's go on with the BODY now */ |
| 2711 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2712 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2713 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2714 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 2715 | /* we detected a parsing error. We want to archive this request |
| 2716 | * in the dedicated proxy area for later troubleshooting. |
| 2717 | */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2718 | 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] | 2719 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2720 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2721 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2722 | txn->status = 400; |
| 2723 | req->analysers = 0; |
| 2724 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2725 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2726 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2727 | if (s->listener->counters) |
| 2728 | s->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2729 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2730 | if (!(s->flags & SN_ERR_MASK)) |
| 2731 | s->flags |= SN_ERR_PRXCOND; |
| 2732 | if (!(s->flags & SN_FINST_MASK)) |
| 2733 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2734 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2735 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2736 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 2737 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 2738 | * returns zero, at the beginning because it prevents any other processing |
| 2739 | * from occurring, and at the end because it terminates the request. |
| 2740 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2741 | 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] | 2742 | { |
| 2743 | struct http_txn *txn = &s->txn; |
| 2744 | |
| 2745 | /* This connection is being tarpitted. The CLIENT side has |
| 2746 | * already set the connect expiration date to the right |
| 2747 | * timeout. We just have to check that the client is still |
| 2748 | * there and that the timeout has not expired. |
| 2749 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2750 | buffer_dont_connect(req); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 2751 | if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 && |
| 2752 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 2753 | return 0; |
| 2754 | |
| 2755 | /* We will set the queue timer to the time spent, just for |
| 2756 | * logging purposes. We fake a 500 server error, so that the |
| 2757 | * attacker will not suspect his connection has been tarpitted. |
| 2758 | * It will not cause trouble to the logs because we can exclude |
| 2759 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 2760 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 2761 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 2762 | |
| 2763 | txn->status = 500; |
| 2764 | if (req->flags != BF_READ_ERROR) |
| 2765 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500)); |
| 2766 | |
| 2767 | req->analysers = 0; |
| 2768 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2769 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2770 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2771 | if (s->listener->counters) |
| 2772 | s->listener->counters->failed_req++; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 2773 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 2774 | if (!(s->flags & SN_ERR_MASK)) |
| 2775 | s->flags |= SN_ERR_PRXCOND; |
| 2776 | if (!(s->flags & SN_FINST_MASK)) |
| 2777 | s->flags |= SN_FINST_T; |
| 2778 | return 0; |
| 2779 | } |
| 2780 | |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 2781 | /* This function is an analyser which processes the HTTP request body. It looks |
| 2782 | * for parameters to be used for the load balancing algorithm (url_param). It |
| 2783 | * must only be called after the standard HTTP request processing has occurred, |
| 2784 | * because it expects the request to be parsed. It returns zero if it needs to |
| 2785 | * read more data, or 1 once it has completed its analysis. |
| 2786 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2787 | 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] | 2788 | { |
| 2789 | struct http_msg *msg = &s->txn.req; |
| 2790 | unsigned long body = msg->sol[msg->eoh] == '\r' ? msg->eoh + 2 : msg->eoh + 1; |
| 2791 | long long limit = s->be->url_param_post_limit; |
| 2792 | struct hdr_ctx ctx; |
| 2793 | |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2794 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
| 2795 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2796 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2797 | return 0; |
| 2798 | } |
| 2799 | |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 2800 | /* We have to parse the HTTP request body to find any required data. |
| 2801 | * "balance url_param check_post" should have been the only way to get |
| 2802 | * into this. We were brought here after HTTP header analysis, so all |
| 2803 | * related structures are ready. |
| 2804 | */ |
| 2805 | |
| 2806 | ctx.idx = 0; |
| 2807 | |
| 2808 | /* now if we have a length, we'll take the hint */ |
| 2809 | http_find_header2("Transfer-Encoding", 17, msg->sol, &s->txn.hdr_idx, &ctx); |
| 2810 | if (ctx.idx && ctx.vlen >= 7 && strncasecmp(ctx.line+ctx.val, "chunked", 7) == 0) { |
| 2811 | unsigned int chunk = 0; |
| 2812 | while (body < req->l && !HTTP_IS_CRLF(msg->sol[body])) { |
| 2813 | char c = msg->sol[body]; |
| 2814 | if (ishex(c)) { |
| 2815 | unsigned int hex = toupper(c) - '0'; |
| 2816 | if (hex > 9) |
| 2817 | hex -= 'A' - '9' - 1; |
| 2818 | chunk = (chunk << 4) | hex; |
| 2819 | } else |
| 2820 | break; |
| 2821 | body++; |
| 2822 | } |
| 2823 | if (body + 2 >= req->l) /* we want CRLF too */ |
| 2824 | goto http_body_end; /* end of buffer? data missing! */ |
| 2825 | |
| 2826 | if (memcmp(msg->sol+body, "\r\n", 2) != 0) |
| 2827 | goto http_body_end; /* chunked encoding len ends with CRLF, and we don't have it yet */ |
| 2828 | |
| 2829 | body += 2; // skip CRLF |
| 2830 | |
| 2831 | /* if we support more then one chunk here, we have to do it again when assigning server |
| 2832 | * 1. how much entity data do we have? new var |
| 2833 | * 2. should save entity_start, entity_cursor, elen & rlen in req; so we don't repeat scanning here |
| 2834 | * 3. test if elen > limit, or set new limit to elen if 0 (end of entity found) |
| 2835 | */ |
| 2836 | |
| 2837 | if (chunk < limit) |
| 2838 | limit = chunk; /* only reading one chunk */ |
| 2839 | } else { |
| 2840 | if (msg->hdr_content_len < limit) |
| 2841 | limit = msg->hdr_content_len; |
| 2842 | } |
| 2843 | |
| 2844 | http_body_end: |
| 2845 | /* we leave once we know we have nothing left to do. This means that we have |
| 2846 | * enough bytes, or that we know we'll not get any more (buffer full, read |
| 2847 | * buffer closed). |
| 2848 | */ |
| 2849 | if (req->l - body >= limit || /* enough bytes! */ |
| 2850 | req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR | BF_READ_TIMEOUT) || |
| 2851 | tick_is_expired(req->analyse_exp, now_ms)) { |
| 2852 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 2853 | 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] | 2854 | req->analysers &= ~an_bit; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 2855 | req->analyse_exp = TICK_ETERNITY; |
| 2856 | return 1; |
| 2857 | } |
| 2858 | else { |
| 2859 | /* Not enough data. We'll re-use the http-request |
| 2860 | * timeout here. Ideally, we should set the timeout |
| 2861 | * relative to the accept() date. We just set the |
| 2862 | * request timeout once at the beginning of the |
| 2863 | * request. |
| 2864 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2865 | buffer_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 2866 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 2867 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 2868 | return 0; |
| 2869 | } |
| 2870 | } |
| 2871 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2872 | /* This function performs all the processing enabled for the current response. |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2873 | * It normally returns zero, but may return 1 if it absolutely needs to be |
| 2874 | * called again after other functions. It relies on buffers flags, and updates |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2875 | * 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] | 2876 | * functions. It works like process_request (see indications above). |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2877 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2878 | int process_response(struct session *t) |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2879 | { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2880 | struct http_txn *txn = &t->txn; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2881 | struct buffer *req = t->req; |
| 2882 | struct buffer *rep = t->rep; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 2883 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 2884 | |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 2885 | next_response: |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 2886 | 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] | 2887 | now_ms, __FUNCTION__, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 2888 | t, |
| 2889 | rep, |
| 2890 | rep->rex, rep->wex, |
| 2891 | rep->flags, |
| 2892 | rep->l, |
| 2893 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 2894 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2895 | if (rep->analysers & AN_RTR_HTTP_HDR) { /* receiving server headers */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2896 | /* |
| 2897 | * Now parse the partial (or complete) lines. |
| 2898 | * We will check the response syntax, and also join multi-line |
| 2899 | * headers. An index of all the lines will be elaborated while |
| 2900 | * parsing. |
| 2901 | * |
| 2902 | * For the parsing, we use a 28 states FSM. |
| 2903 | * |
| 2904 | * Here is the information we currently have : |
Willy Tarreau | e393fe2 | 2008-08-16 22:18:07 +0200 | [diff] [blame] | 2905 | * rep->data + rep->som = beginning of response |
| 2906 | * rep->data + rep->eoh = end of processed headers / start of current one |
| 2907 | * 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] | 2908 | * rep->lr = first non-visited byte |
| 2909 | * rep->r = end of data |
| 2910 | */ |
Willy Tarreau | 7f875f6 | 2008-08-11 17:35:01 +0200 | [diff] [blame] | 2911 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2912 | int cur_idx; |
| 2913 | struct http_msg *msg = &txn->rsp; |
| 2914 | struct proxy *cur_proxy; |
| 2915 | |
| 2916 | if (likely(rep->lr < rep->r)) |
| 2917 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
| 2918 | |
| 2919 | /* 1: we might have to print this header in debug mode */ |
| 2920 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2921 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
| 2922 | (msg->msg_state == HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
| 2923 | char *eol, *sol; |
| 2924 | |
| 2925 | sol = rep->data + msg->som; |
| 2926 | eol = sol + msg->sl.rq.l; |
| 2927 | debug_hdr("srvrep", t, sol, eol); |
| 2928 | |
| 2929 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2930 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
| 2931 | |
| 2932 | while (cur_idx) { |
| 2933 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2934 | debug_hdr("srvhdr", t, sol, eol); |
| 2935 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2936 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2937 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2938 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2939 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2940 | /* |
| 2941 | * Now we quickly check if we have found a full valid response. |
| 2942 | * If not so, we check the FD and buffer states before leaving. |
| 2943 | * A full response is indicated by the fact that we have seen |
| 2944 | * the double LF/CRLF, so the state is HTTP_MSG_BODY. Invalid |
| 2945 | * responses are checked first. |
| 2946 | * |
| 2947 | * Depending on whether the client is still there or not, we |
| 2948 | * may send an error response back or not. Note that normally |
| 2949 | * we should only check for HTTP status there, and check I/O |
| 2950 | * errors somewhere else. |
| 2951 | */ |
| 2952 | |
| 2953 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2954 | /* Invalid response */ |
| 2955 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 2956 | /* we detected a parsing error. We want to archive this response |
| 2957 | * in the dedicated proxy area for later troubleshooting. |
| 2958 | */ |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2959 | hdr_response_bad: |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2960 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
| 2961 | http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe); |
| 2962 | |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2963 | buffer_shutr_now(rep); |
| 2964 | buffer_shutw_now(req); |
Willy Tarreau | 8365f93 | 2009-03-15 23:11:49 +0100 | [diff] [blame] | 2965 | if (t->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2966 | t->srv->counters.failed_resp++; |
| 2967 | t->be->counters.failed_resp++; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2968 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2969 | txn->status = 502; |
Willy Tarreau | 81acfab | 2008-11-30 19:22:53 +0100 | [diff] [blame] | 2970 | stream_int_return(rep->cons, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2971 | if (!(t->flags & SN_ERR_MASK)) |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2972 | t->flags |= SN_ERR_PRXCOND; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 2973 | if (!(t->flags & SN_FINST_MASK)) |
| 2974 | t->flags |= SN_FINST_H; |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 2975 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2976 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2977 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2978 | /* too large response does not fit in buffer. */ |
| 2979 | else if (rep->flags & BF_FULL) { |
| 2980 | goto hdr_response_bad; |
| 2981 | } |
| 2982 | /* read error */ |
| 2983 | else if (rep->flags & BF_READ_ERROR) { |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2984 | if (msg->err_pos >= 0) |
| 2985 | 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] | 2986 | buffer_shutr_now(rep); |
| 2987 | buffer_shutw_now(req); |
Willy Tarreau | 8365f93 | 2009-03-15 23:11:49 +0100 | [diff] [blame] | 2988 | if (t->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2989 | t->srv->counters.failed_resp++; |
| 2990 | t->be->counters.failed_resp++; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 2991 | rep->analysers = 0; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2992 | txn->status = 502; |
Willy Tarreau | 81acfab | 2008-11-30 19:22:53 +0100 | [diff] [blame] | 2993 | stream_int_return(rep->cons, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 2994 | if (!(t->flags & SN_ERR_MASK)) |
| 2995 | t->flags |= SN_ERR_SRVCL; |
| 2996 | if (!(t->flags & SN_FINST_MASK)) |
| 2997 | t->flags |= SN_FINST_H; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2998 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2999 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3000 | /* read timeout : return a 504 to the client. */ |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 3001 | else if (rep->flags & BF_READ_TIMEOUT) { |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3002 | if (msg->err_pos >= 0) |
| 3003 | 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] | 3004 | buffer_shutr_now(rep); |
| 3005 | buffer_shutw_now(req); |
Willy Tarreau | 8365f93 | 2009-03-15 23:11:49 +0100 | [diff] [blame] | 3006 | if (t->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3007 | t->srv->counters.failed_resp++; |
| 3008 | t->be->counters.failed_resp++; |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3009 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3010 | txn->status = 504; |
Willy Tarreau | 81acfab | 2008-11-30 19:22:53 +0100 | [diff] [blame] | 3011 | stream_int_return(rep->cons, error_message(t, HTTP_ERR_504)); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3012 | if (!(t->flags & SN_ERR_MASK)) |
| 3013 | t->flags |= SN_ERR_SRVTO; |
| 3014 | if (!(t->flags & SN_FINST_MASK)) |
| 3015 | t->flags |= SN_FINST_H; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3016 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3017 | } |
Willy Tarreau | 8365f93 | 2009-03-15 23:11:49 +0100 | [diff] [blame] | 3018 | /* close from server */ |
| 3019 | else if (rep->flags & BF_SHUTR) { |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3020 | if (msg->err_pos >= 0) |
| 3021 | 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] | 3022 | buffer_shutw_now(req); |
Willy Tarreau | 8365f93 | 2009-03-15 23:11:49 +0100 | [diff] [blame] | 3023 | if (t->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3024 | t->srv->counters.failed_resp++; |
| 3025 | t->be->counters.failed_resp++; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 3026 | rep->analysers = 0; |
| 3027 | txn->status = 502; |
Willy Tarreau | 81acfab | 2008-11-30 19:22:53 +0100 | [diff] [blame] | 3028 | stream_int_return(rep->cons, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 3029 | if (!(t->flags & SN_ERR_MASK)) |
| 3030 | t->flags |= SN_ERR_SRVCL; |
| 3031 | if (!(t->flags & SN_FINST_MASK)) |
| 3032 | t->flags |= SN_FINST_H; |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 3033 | return 0; |
| 3034 | } |
Willy Tarreau | 8365f93 | 2009-03-15 23:11:49 +0100 | [diff] [blame] | 3035 | /* write error to client (we don't send any message then) */ |
| 3036 | else if (rep->flags & BF_WRITE_ERROR) { |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3037 | if (msg->err_pos >= 0) |
| 3038 | 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] | 3039 | buffer_shutr_now(rep); |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3040 | t->be->counters.failed_resp++; |
Willy Tarreau | 8365f93 | 2009-03-15 23:11:49 +0100 | [diff] [blame] | 3041 | rep->analysers = 0; |
| 3042 | if (!(t->flags & SN_ERR_MASK)) |
| 3043 | t->flags |= SN_ERR_CLICL; |
| 3044 | if (!(t->flags & SN_FINST_MASK)) |
| 3045 | t->flags |= SN_FINST_H; |
| 3046 | return 0; |
| 3047 | } |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 3048 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3049 | buffer_dont_close(rep); |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 3050 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3051 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3052 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3053 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3054 | /***************************************************************** |
| 3055 | * More interesting part now : we know that we have a complete * |
| 3056 | * response which at least looks like HTTP. We have an indicator * |
| 3057 | * of each header's length, so we can parse them quickly. * |
| 3058 | ****************************************************************/ |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3059 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3060 | if (msg->err_pos >= 0) |
| 3061 | http_capture_bad_message(&t->be->invalid_rep, t, rep, msg, t->fe); |
| 3062 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3063 | rep->analysers &= ~AN_RTR_HTTP_HDR; |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 3064 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3065 | /* ensure we keep this pointer to the beginning of the message */ |
| 3066 | msg->sol = rep->data + msg->som; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3067 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3068 | /* |
| 3069 | * 1: get the status code and check for cacheability. |
| 3070 | */ |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3071 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3072 | t->logs.logwait &= ~LW_RESP; |
| 3073 | 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] | 3074 | |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 3075 | n = rep->data[msg->sl.st.c] - '0'; |
| 3076 | if (n < 1 || n > 5) |
| 3077 | n = 0; |
| 3078 | |
| 3079 | t->srv->counters.p.http.rsp[n]++; |
| 3080 | t->be->counters.p.http.rsp[n]++; |
| 3081 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3082 | switch (txn->status) { |
| 3083 | case 200: |
| 3084 | case 203: |
| 3085 | case 206: |
| 3086 | case 300: |
| 3087 | case 301: |
| 3088 | case 410: |
| 3089 | /* RFC2616 @13.4: |
| 3090 | * "A response received with a status code of |
| 3091 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 3092 | * by a cache (...) unless a cache-control |
| 3093 | * directive prohibits caching." |
| 3094 | * |
| 3095 | * RFC2616 @9.5: POST method : |
| 3096 | * "Responses to this method are not cacheable, |
| 3097 | * unless the response includes appropriate |
| 3098 | * Cache-Control or Expires header fields." |
| 3099 | */ |
| 3100 | if (likely(txn->meth != HTTP_METH_POST) && |
| 3101 | (t->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
| 3102 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 3103 | break; |
| 3104 | default: |
| 3105 | break; |
| 3106 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3107 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3108 | /* |
| 3109 | * 2: we may need to capture headers |
| 3110 | */ |
| 3111 | if (unlikely((t->logs.logwait & LW_RSPHDR) && t->fe->rsp_cap)) |
| 3112 | capture_headers(rep->data + msg->som, &txn->hdr_idx, |
| 3113 | txn->rsp.cap, t->fe->rsp_cap); |
| 3114 | |
| 3115 | /* |
| 3116 | * 3: we will have to evaluate the filters. |
| 3117 | * As opposed to version 1.2, now they will be evaluated in the |
| 3118 | * filters order and not in the header order. This means that |
| 3119 | * each filter has to be validated among all headers. |
| 3120 | * |
| 3121 | * Filters are tried with ->be first, then with ->fe if it is |
| 3122 | * different from ->be. |
| 3123 | */ |
| 3124 | |
| 3125 | t->flags &= ~SN_CONN_CLOSED; /* prepare for inspection */ |
| 3126 | |
| 3127 | cur_proxy = t->be; |
| 3128 | while (1) { |
| 3129 | struct proxy *rule_set = cur_proxy; |
| 3130 | |
| 3131 | /* try headers filters */ |
| 3132 | if (rule_set->rsp_exp != NULL) { |
| 3133 | if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) { |
| 3134 | return_bad_resp: |
Willy Tarreau | 8365f93 | 2009-03-15 23:11:49 +0100 | [diff] [blame] | 3135 | if (t->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3136 | t->srv->counters.failed_resp++; |
| 3137 | cur_proxy->counters.failed_resp++; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3138 | return_srv_prx_502: |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 3139 | buffer_shutr_now(rep); |
| 3140 | buffer_shutw_now(req); |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3141 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3142 | txn->status = 502; |
Willy Tarreau | 81acfab | 2008-11-30 19:22:53 +0100 | [diff] [blame] | 3143 | stream_int_return(rep->cons, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3144 | if (!(t->flags & SN_ERR_MASK)) |
| 3145 | t->flags |= SN_ERR_PRXCOND; |
| 3146 | if (!(t->flags & SN_FINST_MASK)) |
| 3147 | t->flags |= SN_FINST_H; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3148 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3149 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3150 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 3151 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3152 | /* has the response been denied ? */ |
| 3153 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | 8365f93 | 2009-03-15 23:11:49 +0100 | [diff] [blame] | 3154 | if (t->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3155 | t->srv->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3156 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3157 | cur_proxy->counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3158 | if (t->listener->counters) |
| 3159 | t->listener->counters->denied_resp++; |
| 3160 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3161 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 3162 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3163 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3164 | /* We might have to check for "Connection:" */ |
| 3165 | if (((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) && |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 3166 | !(t->flags & SN_CONN_CLOSED) && |
| 3167 | txn->status >= 200) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3168 | char *cur_ptr, *cur_end, *cur_next; |
| 3169 | int cur_idx, old_idx, delta, val; |
| 3170 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3171 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3172 | cur_next = rep->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 3173 | old_idx = 0; |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 3174 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3175 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 3176 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 3177 | cur_ptr = cur_next; |
| 3178 | cur_end = cur_ptr + cur_hdr->len; |
| 3179 | cur_next = cur_end + cur_hdr->cr + 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3180 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3181 | val = http_header_match2(cur_ptr, cur_end, "Connection", 10); |
| 3182 | if (val) { |
| 3183 | /* 3 possibilities : |
| 3184 | * - we have already set Connection: close, |
| 3185 | * so we remove this line. |
| 3186 | * - we have not yet set Connection: close, |
| 3187 | * but this line indicates close. We leave |
| 3188 | * it untouched and set the flag. |
| 3189 | * - we have not yet set Connection: close, |
| 3190 | * and this line indicates non-close. We |
| 3191 | * replace it. |
| 3192 | */ |
| 3193 | if (t->flags & SN_CONN_CLOSED) { |
| 3194 | delta = buffer_replace2(rep, cur_ptr, cur_next, NULL, 0); |
| 3195 | txn->rsp.eoh += delta; |
| 3196 | cur_next += delta; |
| 3197 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 3198 | txn->hdr_idx.used--; |
| 3199 | cur_hdr->len = 0; |
| 3200 | } else { |
| 3201 | if (strncasecmp(cur_ptr + val, "close", 5) != 0) { |
| 3202 | delta = buffer_replace2(rep, cur_ptr + val, cur_end, |
| 3203 | "close", 5); |
| 3204 | cur_next += delta; |
| 3205 | cur_hdr->len += delta; |
| 3206 | txn->rsp.eoh += delta; |
| 3207 | } |
| 3208 | t->flags |= SN_CONN_CLOSED; |
| 3209 | } |
| 3210 | } |
| 3211 | old_idx = cur_idx; |
Willy Tarreau | 541b5c2 | 2008-01-06 23:34:21 +0100 | [diff] [blame] | 3212 | } |
| 3213 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3214 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3215 | /* add response headers from the rule sets in the same order */ |
| 3216 | for (cur_idx = 0; cur_idx < rule_set->nb_rspadd; cur_idx++) { |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 3217 | if (txn->status < 200) |
| 3218 | break; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3219 | if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, |
| 3220 | rule_set->rsp_add[cur_idx])) < 0) |
| 3221 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 3222 | } |
| 3223 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3224 | /* check whether we're already working on the frontend */ |
| 3225 | if (cur_proxy == t->fe) |
| 3226 | break; |
| 3227 | cur_proxy = t->fe; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3228 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3229 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3230 | /* |
| 3231 | * 4: check for server cookie. |
| 3232 | */ |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 3233 | if ((t->be->cookie_name || t->be->appsession_name || t->fe->capture_name |
| 3234 | || (t->be->options & PR_O_CHK_CACHE)) && txn->status >= 200) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3235 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3236 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3237 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3238 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3239 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3240 | */ |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 3241 | if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0 && txn->status >= 200) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3242 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3243 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3244 | /* |
| 3245 | * 6: add server cookie in the response if needed |
| 3246 | */ |
| 3247 | if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) && |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 3248 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST)) && |
| 3249 | txn->status >= 200) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3250 | int len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3251 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3252 | /* the server is known, it's not the one the client requested, we have to |
| 3253 | * insert a set-cookie here, except if we want to insert only on POST |
| 3254 | * requests and this one isn't. Note that servers which don't have cookies |
| 3255 | * (eg: some backup servers) will return a full cookie removal request. |
| 3256 | */ |
| 3257 | len = sprintf(trash, "Set-Cookie: %s=%s; path=/", |
| 3258 | t->be->cookie_name, |
| 3259 | 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] | 3260 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3261 | if (t->be->cookie_domain) |
| 3262 | len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3263 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3264 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3265 | trash, len)) < 0) |
| 3266 | goto return_bad_resp; |
| 3267 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3268 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3269 | /* Here, we will tell an eventual cache on the client side that we don't |
| 3270 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 3271 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 3272 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 3273 | */ |
| 3274 | if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3275 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3276 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 3277 | |
| 3278 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3279 | "Cache-control: private", 22)) < 0) |
| 3280 | goto return_bad_resp; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3281 | } |
| 3282 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3283 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3284 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3285 | /* |
| 3286 | * 7: check if result will be cacheable with a cookie. |
| 3287 | * We'll block the response if security checks have caught |
| 3288 | * nasty things such as a cacheable cookie. |
| 3289 | */ |
| 3290 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) == |
| 3291 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) && |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 3292 | (t->be->options & PR_O_CHK_CACHE) && |
| 3293 | txn->status >= 200) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3294 | |
| 3295 | /* we're in presence of a cacheable response containing |
| 3296 | * a set-cookie header. We'll block it as requested by |
| 3297 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3298 | */ |
Willy Tarreau | 8365f93 | 2009-03-15 23:11:49 +0100 | [diff] [blame] | 3299 | if (t->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3300 | t->srv->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3301 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3302 | cur_proxy->counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3303 | if (t->listener->counters) |
| 3304 | t->listener->counters->denied_resp++; |
| 3305 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3306 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 3307 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 3308 | send_log(t->be, LOG_ALERT, |
| 3309 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 3310 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 3311 | goto return_srv_prx_502; |
| 3312 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3313 | |
| 3314 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3315 | * 8: add "Connection: close" if needed and not yet set. |
| 3316 | * 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] | 3317 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3318 | if (!(t->flags & SN_CONN_CLOSED) && |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 3319 | ((t->fe->options | t->be->options) & (PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) && |
| 3320 | txn->status >= 200) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3321 | if ((unlikely(msg->sl.st.v_l != 8) || |
| 3322 | unlikely(req->data[msg->som + 7] != '0')) && |
| 3323 | unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
| 3324 | "Connection: close", 17)) < 0) |
| 3325 | goto return_bad_resp; |
| 3326 | t->flags |= SN_CONN_CLOSED; |
| 3327 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3328 | |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 3329 | /* |
| 3330 | * 9: we may be facing a 1xx response (100 continue, 101 switching protocols), |
| 3331 | * in which case this is not the right response, and we're waiting for the |
| 3332 | * next one. Let's allow this response to go to the client and wait for the |
| 3333 | * next one. |
| 3334 | */ |
| 3335 | if (txn->status < 200) { |
| 3336 | hdr_idx_init(&txn->hdr_idx); |
| 3337 | buffer_forward(rep, rep->lr - (rep->data + msg->som)); |
| 3338 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 3339 | txn->status = 0; |
| 3340 | rep->analysers |= AN_RTR_HTTP_HDR; |
| 3341 | goto next_response; |
| 3342 | } |
| 3343 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3344 | /************************************************************* |
| 3345 | * OK, that's finished for the headers. We have done what we * |
| 3346 | * could. Let's switch to the DATA state. * |
| 3347 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3348 | |
Willy Tarreau | a07a34e | 2009-08-16 23:27:46 +0200 | [diff] [blame] | 3349 | buffer_set_rlim(rep, rep->size); /* no more rewrite needed */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3350 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3351 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3352 | /* if the user wants to log as soon as possible, without counting |
| 3353 | * bytes from the server, then this is the right moment. We have |
| 3354 | * to temporarily assign bytes_out to log what we currently have. |
| 3355 | */ |
| 3356 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 3357 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 3358 | t->logs.bytes_out = txn->rsp.eoh; |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 3359 | t->do_log(t); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3360 | t->logs.bytes_out = 0; |
| 3361 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3362 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3363 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 3364 | * otherwise we would not let the client side wake up. |
| 3365 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3366 | |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3367 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3368 | } |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3369 | |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 3370 | /* Note: eventhough nobody should set an unknown flag, clearing them right now will |
| 3371 | * probably reduce one day's debugging session. |
| 3372 | */ |
| 3373 | #ifdef DEBUG_DEV |
| 3374 | if (rep->analysers & ~(AN_RTR_HTTP_HDR)) { |
| 3375 | fprintf(stderr, "FIXME !!!! unknown analysers flags %s:%d = 0x%08X\n", |
| 3376 | __FILE__, __LINE__, rep->analysers); |
| 3377 | ABORT_NOW(); |
| 3378 | } |
| 3379 | #endif |
| 3380 | rep->analysers &= AN_RTR_HTTP_HDR; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 3381 | return 0; |
| 3382 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3383 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3384 | /* Iterate the same filter through all request headers. |
| 3385 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3386 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 3387 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3388 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3389 | 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] | 3390 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3391 | char term; |
| 3392 | char *cur_ptr, *cur_end, *cur_next; |
| 3393 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3394 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3395 | struct hdr_idx_elem *cur_hdr; |
| 3396 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 3397 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3398 | last_hdr = 0; |
| 3399 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3400 | 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] | 3401 | old_idx = 0; |
| 3402 | |
| 3403 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3404 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3405 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3406 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3407 | (exp->action == ACT_ALLOW || |
| 3408 | exp->action == ACT_DENY || |
| 3409 | exp->action == ACT_TARPIT)) |
| 3410 | return 0; |
| 3411 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3412 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3413 | if (!cur_idx) |
| 3414 | break; |
| 3415 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3416 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3417 | cur_ptr = cur_next; |
| 3418 | cur_end = cur_ptr + cur_hdr->len; |
| 3419 | cur_next = cur_end + cur_hdr->cr + 1; |
| 3420 | |
| 3421 | /* Now we have one header between cur_ptr and cur_end, |
| 3422 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3423 | */ |
| 3424 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3425 | /* The annoying part is that pattern matching needs |
| 3426 | * that we modify the contents to null-terminate all |
| 3427 | * strings before testing them. |
| 3428 | */ |
| 3429 | |
| 3430 | term = *cur_end; |
| 3431 | *cur_end = '\0'; |
| 3432 | |
| 3433 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 3434 | switch (exp->action) { |
| 3435 | case ACT_SETBE: |
| 3436 | /* It is not possible to jump a second time. |
| 3437 | * FIXME: should we return an HTTP/500 here so that |
| 3438 | * the admin knows there's a problem ? |
| 3439 | */ |
| 3440 | if (t->be != t->fe) |
| 3441 | break; |
| 3442 | |
| 3443 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3444 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3445 | last_hdr = 1; |
| 3446 | break; |
| 3447 | |
| 3448 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3449 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3450 | last_hdr = 1; |
| 3451 | break; |
| 3452 | |
| 3453 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3454 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3455 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3456 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3457 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3458 | if (t->listener->counters) |
| 3459 | t->listener->counters->denied_resp++; |
| 3460 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3461 | break; |
| 3462 | |
| 3463 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3464 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3465 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3466 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3467 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3468 | if (t->listener->counters) |
| 3469 | t->listener->counters->denied_resp++; |
| 3470 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3471 | break; |
| 3472 | |
| 3473 | case ACT_REPLACE: |
| 3474 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 3475 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 3476 | /* FIXME: if the user adds a newline in the replacement, the |
| 3477 | * index will not be recalculated for now, and the new line |
| 3478 | * will not be counted as a new header. |
| 3479 | */ |
| 3480 | |
| 3481 | cur_end += delta; |
| 3482 | cur_next += delta; |
| 3483 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3484 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3485 | break; |
| 3486 | |
| 3487 | case ACT_REMOVE: |
| 3488 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 3489 | cur_next += delta; |
| 3490 | |
| 3491 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3492 | txn->req.eoh += delta; |
| 3493 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 3494 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3495 | cur_hdr->len = 0; |
| 3496 | cur_end = NULL; /* null-term has been rewritten */ |
| 3497 | break; |
| 3498 | |
| 3499 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3500 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3501 | if (cur_end) |
| 3502 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3503 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3504 | /* keep the link from this header to next one in case of later |
| 3505 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3506 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3507 | old_idx = cur_idx; |
| 3508 | } |
| 3509 | return 0; |
| 3510 | } |
| 3511 | |
| 3512 | |
| 3513 | /* Apply the filter to the request line. |
| 3514 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 3515 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3516 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 3517 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3518 | */ |
| 3519 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 3520 | { |
| 3521 | char term; |
| 3522 | char *cur_ptr, *cur_end; |
| 3523 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3524 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3525 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3526 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3527 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3528 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3529 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3530 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3531 | (exp->action == ACT_ALLOW || |
| 3532 | exp->action == ACT_DENY || |
| 3533 | exp->action == ACT_TARPIT)) |
| 3534 | return 0; |
| 3535 | else if (exp->action == ACT_REMOVE) |
| 3536 | return 0; |
| 3537 | |
| 3538 | done = 0; |
| 3539 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 3540 | 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] | 3541 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3542 | |
| 3543 | /* Now we have the request line between cur_ptr and cur_end */ |
| 3544 | |
| 3545 | /* The annoying part is that pattern matching needs |
| 3546 | * that we modify the contents to null-terminate all |
| 3547 | * strings before testing them. |
| 3548 | */ |
| 3549 | |
| 3550 | term = *cur_end; |
| 3551 | *cur_end = '\0'; |
| 3552 | |
| 3553 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 3554 | switch (exp->action) { |
| 3555 | case ACT_SETBE: |
| 3556 | /* It is not possible to jump a second time. |
| 3557 | * FIXME: should we return an HTTP/500 here so that |
| 3558 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3559 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3560 | if (t->be != t->fe) |
| 3561 | break; |
| 3562 | |
| 3563 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3564 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3565 | done = 1; |
| 3566 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3567 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3568 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3569 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3570 | done = 1; |
| 3571 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3572 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3573 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3574 | txn->flags |= TX_CLDENY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3575 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3576 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3577 | if (t->listener->counters) |
| 3578 | t->listener->counters->denied_resp++; |
| 3579 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3580 | done = 1; |
| 3581 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3582 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3583 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3584 | txn->flags |= TX_CLTARPIT; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3585 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3586 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3587 | if (t->listener->counters) |
| 3588 | t->listener->counters->denied_resp++; |
| 3589 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3590 | done = 1; |
| 3591 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3592 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3593 | case ACT_REPLACE: |
| 3594 | *cur_end = term; /* restore the string terminator */ |
| 3595 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 3596 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 3597 | /* FIXME: if the user adds a newline in the replacement, the |
| 3598 | * index will not be recalculated for now, and the new line |
| 3599 | * will not be counted as a new header. |
| 3600 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3601 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3602 | txn->req.eoh += delta; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3603 | cur_end += delta; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3604 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 3605 | 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] | 3606 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3607 | HTTP_MSG_RQMETH, |
| 3608 | cur_ptr, cur_end + 1, |
| 3609 | NULL, NULL); |
| 3610 | if (unlikely(!cur_end)) |
| 3611 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 3612 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3613 | /* we have a full request and we know that we have either a CR |
| 3614 | * or an LF at <ptr>. |
| 3615 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3616 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 3617 | 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] | 3618 | /* there is no point trying this regex on headers */ |
| 3619 | return 1; |
| 3620 | } |
| 3621 | } |
| 3622 | *cur_end = term; /* restore the string terminator */ |
| 3623 | return done; |
| 3624 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 3625 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3626 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3627 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3628 | /* |
| 3629 | * Apply all the req filters <exp> to all headers in buffer <req> of session <t>. |
| 3630 | * 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] | 3631 | * unparsable request. Since it can manage the switch to another backend, it |
| 3632 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3633 | */ |
| 3634 | int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 3635 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3636 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3637 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3638 | while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3639 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3640 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3641 | /* |
| 3642 | * The interleaving of transformations and verdicts |
| 3643 | * makes it difficult to decide to continue or stop |
| 3644 | * the evaluation. |
| 3645 | */ |
| 3646 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3647 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3648 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 3649 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) { |
| 3650 | exp = exp->next; |
| 3651 | continue; |
| 3652 | } |
| 3653 | |
| 3654 | /* Apply the filter to the request line. */ |
| 3655 | ret = apply_filter_to_req_line(t, req, exp); |
| 3656 | if (unlikely(ret < 0)) |
| 3657 | return -1; |
| 3658 | |
| 3659 | if (likely(ret == 0)) { |
| 3660 | /* The filter did not match the request, it can be |
| 3661 | * iterated through all headers. |
| 3662 | */ |
| 3663 | apply_filter_to_req_headers(t, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3664 | } |
| 3665 | exp = exp->next; |
| 3666 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3667 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3668 | } |
| 3669 | |
| 3670 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 3671 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3672 | /* |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 3673 | * Try to retrieve the server associated to the appsession. |
| 3674 | * If the server is found, it's assigned to the session. |
| 3675 | */ |
| 3676 | void manage_client_side_appsession(struct session *t, const char *buf) { |
| 3677 | struct http_txn *txn = &t->txn; |
| 3678 | appsess *asession = NULL; |
| 3679 | char *sessid_temp = NULL; |
| 3680 | |
| 3681 | if (t->be->options2 & PR_O2_AS_REQL) { |
| 3682 | /* request-learn option is enabled : store the sessid in the session for future use */ |
| 3683 | if (t->sessid != NULL) { |
| 3684 | /* free previously allocated memory as we don't need the session id found in the URL anymore */ |
| 3685 | pool_free2(apools.sessid, t->sessid); |
| 3686 | } |
| 3687 | |
| 3688 | if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 3689 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 3690 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 3691 | return; |
| 3692 | } |
| 3693 | |
| 3694 | memcpy(t->sessid, buf, t->be->appsession_len); |
| 3695 | t->sessid[t->be->appsession_len] = 0; |
| 3696 | } |
| 3697 | |
| 3698 | if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) { |
| 3699 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 3700 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 3701 | return; |
| 3702 | } |
| 3703 | |
| 3704 | memcpy(sessid_temp, buf, t->be->appsession_len); |
| 3705 | sessid_temp[t->be->appsession_len] = 0; |
| 3706 | |
| 3707 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp); |
| 3708 | /* free previously allocated memory */ |
| 3709 | pool_free2(apools.sessid, sessid_temp); |
| 3710 | |
| 3711 | if (asession != NULL) { |
| 3712 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 3713 | if (!(t->be->options2 & PR_O2_AS_REQL)) |
| 3714 | asession->request_count++; |
| 3715 | |
| 3716 | if (asession->serverid != NULL) { |
| 3717 | struct server *srv = t->be->srv; |
| 3718 | while (srv) { |
| 3719 | if (strcmp(srv->id, asession->serverid) == 0) { |
| 3720 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
| 3721 | /* we found the server and it's usable */ |
| 3722 | txn->flags &= ~TX_CK_MASK; |
| 3723 | txn->flags |= TX_CK_VALID; |
| 3724 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
| 3725 | t->srv = srv; |
| 3726 | break; |
| 3727 | } else { |
| 3728 | txn->flags &= ~TX_CK_MASK; |
| 3729 | txn->flags |= TX_CK_DOWN; |
| 3730 | } |
| 3731 | } |
| 3732 | srv = srv->next; |
| 3733 | } |
| 3734 | } |
| 3735 | } |
| 3736 | } |
| 3737 | |
| 3738 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 3739 | * Manage client-side cookie. It can impact performance by about 2% so it is |
| 3740 | * desirable to call it only when needed. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3741 | */ |
| 3742 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 3743 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3744 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3745 | char *p1, *p2, *p3, *p4; |
| 3746 | char *del_colon, *del_cookie, *colon; |
| 3747 | int app_cookies; |
| 3748 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3749 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 3750 | int cur_idx, old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3751 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3752 | /* Iterate through the headers. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3753 | * we start with the start line. |
| 3754 | */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 3755 | old_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3756 | 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] | 3757 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3758 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3759 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 3760 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3761 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3762 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3763 | cur_ptr = cur_next; |
| 3764 | cur_end = cur_ptr + cur_hdr->len; |
| 3765 | cur_next = cur_end + cur_hdr->cr + 1; |
| 3766 | |
| 3767 | /* We have one full header between cur_ptr and cur_end, and the |
| 3768 | * next header starts at cur_next. We're only interested in |
| 3769 | * "Cookie:" headers. |
| 3770 | */ |
| 3771 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 3772 | val = http_header_match2(cur_ptr, cur_end, "Cookie", 6); |
| 3773 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3774 | old_idx = cur_idx; |
| 3775 | continue; |
| 3776 | } |
| 3777 | |
| 3778 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 3779 | * attributes whose name begin with a '$', and associate them with |
| 3780 | * the right cookie, if we want to delete this cookie. |
| 3781 | * So there are 3 cases for each cookie read : |
| 3782 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 3783 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 3784 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 3785 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 3786 | * "special" cookie. |
| 3787 | * At the end of loop, if a "special" cookie remains, we may have to |
| 3788 | * remove it. If no application cookie persists in the header, we |
| 3789 | * *MUST* delete it |
| 3790 | */ |
| 3791 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 3792 | colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3793 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3794 | /* del_cookie == NULL => nothing to be deleted */ |
| 3795 | del_colon = del_cookie = NULL; |
| 3796 | app_cookies = 0; |
| 3797 | |
| 3798 | while (p1 < cur_end) { |
| 3799 | /* skip spaces and colons, but keep an eye on these ones */ |
| 3800 | while (p1 < cur_end) { |
| 3801 | if (*p1 == ';' || *p1 == ',') |
| 3802 | colon = p1; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 3803 | else if (!isspace((unsigned char)*p1)) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3804 | break; |
| 3805 | p1++; |
| 3806 | } |
| 3807 | |
| 3808 | if (p1 == cur_end) |
| 3809 | break; |
| 3810 | |
| 3811 | /* p1 is at the beginning of the cookie name */ |
| 3812 | p2 = p1; |
| 3813 | while (p2 < cur_end && *p2 != '=') |
| 3814 | p2++; |
| 3815 | |
| 3816 | if (p2 == cur_end) |
| 3817 | break; |
| 3818 | |
| 3819 | p3 = p2 + 1; /* skips the '=' sign */ |
| 3820 | if (p3 == cur_end) |
| 3821 | break; |
| 3822 | |
| 3823 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 3824 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';' && *p4 != ',') |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3825 | p4++; |
| 3826 | |
| 3827 | /* here, we have the cookie name between p1 and p2, |
| 3828 | * and its value between p3 and p4. |
| 3829 | * we can process it : |
| 3830 | * |
| 3831 | * Cookie: NAME=VALUE; |
| 3832 | * | || || | |
| 3833 | * | || || +--> p4 |
| 3834 | * | || |+-------> p3 |
| 3835 | * | || +--------> p2 |
| 3836 | * | |+------------> p1 |
| 3837 | * | +-------------> colon |
| 3838 | * +--------------------> cur_ptr |
| 3839 | */ |
| 3840 | |
| 3841 | if (*p1 == '$') { |
| 3842 | /* skip this one */ |
| 3843 | } |
| 3844 | else { |
| 3845 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3846 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 3847 | txn->cli_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3848 | (p4 - p1 >= t->fe->capture_namelen) && |
| 3849 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3850 | int log_len = p4 - p1; |
| 3851 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 3852 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3853 | Alert("HTTP logging : out of memory.\n"); |
| 3854 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3855 | if (log_len > t->fe->capture_len) |
| 3856 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 3857 | memcpy(txn->cli_cookie, p1, log_len); |
| 3858 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3859 | } |
| 3860 | } |
| 3861 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3862 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 3863 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3864 | /* Cool... it's the right one */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3865 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3866 | char *delim; |
| 3867 | |
| 3868 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 3869 | * have the server ID betweek p3 and delim, and the original cookie between |
| 3870 | * delim+1 and p4. Otherwise, delim==p4 : |
| 3871 | * |
| 3872 | * Cookie: NAME=SRV~VALUE; |
| 3873 | * | || || | | |
| 3874 | * | || || | +--> p4 |
| 3875 | * | || || +--------> delim |
| 3876 | * | || |+-----------> p3 |
| 3877 | * | || +------------> p2 |
| 3878 | * | |+----------------> p1 |
| 3879 | * | +-----------------> colon |
| 3880 | * +------------------------> cur_ptr |
| 3881 | */ |
| 3882 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3883 | if (t->be->options & PR_O_COOK_PFX) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3884 | for (delim = p3; delim < p4; delim++) |
| 3885 | if (*delim == COOKIE_DELIM) |
| 3886 | break; |
| 3887 | } |
| 3888 | else |
| 3889 | delim = p4; |
| 3890 | |
| 3891 | |
| 3892 | /* Here, we'll look for the first running server which supports the cookie. |
| 3893 | * This allows to share a same cookie between several servers, for example |
| 3894 | * to dedicate backup servers to specific servers only. |
| 3895 | * However, to prevent clients from sticking to cookie-less backup server |
| 3896 | * when they have incidentely learned an empty cookie, we simply ignore |
| 3897 | * empty cookies and mark them as invalid. |
| 3898 | */ |
| 3899 | if (delim == p3) |
| 3900 | srv = NULL; |
| 3901 | |
| 3902 | while (srv) { |
Willy Tarreau | 92f2ab1 | 2007-02-02 22:14:47 +0100 | [diff] [blame] | 3903 | if (srv->cookie && (srv->cklen == delim - p3) && |
| 3904 | !memcmp(p3, srv->cookie, delim - p3)) { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3905 | if (srv->state & SRV_RUNNING || t->be->options & PR_O_PERSIST) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3906 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3907 | txn->flags &= ~TX_CK_MASK; |
| 3908 | txn->flags |= TX_CK_VALID; |
| 3909 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3910 | t->srv = srv; |
| 3911 | break; |
| 3912 | } else { |
| 3913 | /* we found a server, but it's down */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3914 | txn->flags &= ~TX_CK_MASK; |
| 3915 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3916 | } |
| 3917 | } |
| 3918 | srv = srv->next; |
| 3919 | } |
| 3920 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3921 | if (!srv && !(txn->flags & TX_CK_DOWN)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3922 | /* no server matched this cookie */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 3923 | txn->flags &= ~TX_CK_MASK; |
| 3924 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3925 | } |
| 3926 | |
| 3927 | /* depending on the cookie mode, we may have to either : |
| 3928 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 3929 | * the server never sees it ; |
| 3930 | * - remove the server id from the cookie value, and tag the cookie as an |
| 3931 | * application cookie so that it does not get accidentely removed later, |
| 3932 | * if we're in cookie prefix mode |
| 3933 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3934 | if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3935 | int delta; /* negative */ |
| 3936 | |
| 3937 | delta = buffer_replace2(req, p3, delim + 1, NULL, 0); |
| 3938 | p4 += delta; |
| 3939 | cur_end += delta; |
| 3940 | cur_next += delta; |
| 3941 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3942 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3943 | |
| 3944 | del_cookie = del_colon = NULL; |
| 3945 | app_cookies++; /* protect the header from deletion */ |
| 3946 | } |
| 3947 | else if (del_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3948 | (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] | 3949 | del_cookie = p1; |
| 3950 | del_colon = colon; |
| 3951 | } |
| 3952 | } else { |
| 3953 | /* now we know that we must keep this cookie since it's |
| 3954 | * not ours. But if we wanted to delete our cookie |
| 3955 | * earlier, we cannot remove the complete header, but we |
| 3956 | * can remove the previous block itself. |
| 3957 | */ |
| 3958 | app_cookies++; |
| 3959 | |
| 3960 | if (del_cookie != NULL) { |
| 3961 | int delta; /* negative */ |
| 3962 | |
| 3963 | delta = buffer_replace2(req, del_cookie, p1, NULL, 0); |
| 3964 | p4 += delta; |
| 3965 | cur_end += delta; |
| 3966 | cur_next += delta; |
| 3967 | cur_hdr->len += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 3968 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3969 | del_cookie = del_colon = NULL; |
| 3970 | } |
| 3971 | } |
| 3972 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 3973 | if ((t->be->appsession_name != NULL) && |
| 3974 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3975 | /* first, let's see if the cookie is our appcookie*/ |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 3976 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3977 | /* Cool... it's the right one */ |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 3978 | manage_client_side_appsession(t, p3); |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 3979 | #if defined(DEBUG_HASH) |
| 3980 | Alert("manage_client_side_cookies\n"); |
| 3981 | appsession_hash_dump(&(t->be->htbl_proxy)); |
| 3982 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3983 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 3984 | } |
| 3985 | |
| 3986 | /* we'll have to look for another cookie ... */ |
| 3987 | p1 = p4; |
| 3988 | } /* while (p1 < cur_end) */ |
| 3989 | |
| 3990 | /* There's no more cookie on this line. |
| 3991 | * We may have marked the last one(s) for deletion. |
| 3992 | * We must do this now in two ways : |
| 3993 | * - if there is no app cookie, we simply delete the header ; |
| 3994 | * - if there are app cookies, we must delete the end of the |
| 3995 | * string properly, including the colon/semi-colon before |
| 3996 | * the cookie name. |
| 3997 | */ |
| 3998 | if (del_cookie != NULL) { |
| 3999 | int delta; |
| 4000 | if (app_cookies) { |
| 4001 | delta = buffer_replace2(req, del_colon, cur_end, NULL, 0); |
| 4002 | cur_end = del_colon; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4003 | cur_hdr->len += delta; |
| 4004 | } else { |
| 4005 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4006 | |
| 4007 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4008 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4009 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4010 | cur_hdr->len = 0; |
| 4011 | } |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 4012 | cur_next += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4013 | txn->req.eoh += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4014 | } |
| 4015 | |
| 4016 | /* keep the link from this header to next one */ |
| 4017 | old_idx = cur_idx; |
| 4018 | } /* end of cookie processing on this header */ |
| 4019 | } |
| 4020 | |
| 4021 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4022 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 4023 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 4024 | */ |
| 4025 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4026 | { |
| 4027 | char term; |
| 4028 | char *cur_ptr, *cur_end, *cur_next; |
| 4029 | int cur_idx, old_idx, last_hdr; |
| 4030 | struct http_txn *txn = &t->txn; |
| 4031 | struct hdr_idx_elem *cur_hdr; |
| 4032 | int len, delta; |
| 4033 | |
| 4034 | last_hdr = 0; |
| 4035 | |
| 4036 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4037 | old_idx = 0; |
| 4038 | |
| 4039 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4040 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4041 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4042 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4043 | (exp->action == ACT_ALLOW || |
| 4044 | exp->action == ACT_DENY)) |
| 4045 | return 0; |
| 4046 | |
| 4047 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 4048 | if (!cur_idx) |
| 4049 | break; |
| 4050 | |
| 4051 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4052 | cur_ptr = cur_next; |
| 4053 | cur_end = cur_ptr + cur_hdr->len; |
| 4054 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4055 | |
| 4056 | /* Now we have one header between cur_ptr and cur_end, |
| 4057 | * and the next header starts at cur_next. |
| 4058 | */ |
| 4059 | |
| 4060 | /* The annoying part is that pattern matching needs |
| 4061 | * that we modify the contents to null-terminate all |
| 4062 | * strings before testing them. |
| 4063 | */ |
| 4064 | |
| 4065 | term = *cur_end; |
| 4066 | *cur_end = '\0'; |
| 4067 | |
| 4068 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4069 | switch (exp->action) { |
| 4070 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4071 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4072 | last_hdr = 1; |
| 4073 | break; |
| 4074 | |
| 4075 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4076 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4077 | last_hdr = 1; |
| 4078 | break; |
| 4079 | |
| 4080 | case ACT_REPLACE: |
| 4081 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4082 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 4083 | /* FIXME: if the user adds a newline in the replacement, the |
| 4084 | * index will not be recalculated for now, and the new line |
| 4085 | * will not be counted as a new header. |
| 4086 | */ |
| 4087 | |
| 4088 | cur_end += delta; |
| 4089 | cur_next += delta; |
| 4090 | cur_hdr->len += delta; |
| 4091 | txn->rsp.eoh += delta; |
| 4092 | break; |
| 4093 | |
| 4094 | case ACT_REMOVE: |
| 4095 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 4096 | cur_next += delta; |
| 4097 | |
| 4098 | /* FIXME: this should be a separate function */ |
| 4099 | txn->rsp.eoh += delta; |
| 4100 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4101 | txn->hdr_idx.used--; |
| 4102 | cur_hdr->len = 0; |
| 4103 | cur_end = NULL; /* null-term has been rewritten */ |
| 4104 | break; |
| 4105 | |
| 4106 | } |
| 4107 | } |
| 4108 | if (cur_end) |
| 4109 | *cur_end = term; /* restore the string terminator */ |
| 4110 | |
| 4111 | /* keep the link from this header to next one in case of later |
| 4112 | * removal of next header. |
| 4113 | */ |
| 4114 | old_idx = cur_idx; |
| 4115 | } |
| 4116 | return 0; |
| 4117 | } |
| 4118 | |
| 4119 | |
| 4120 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 4121 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 4122 | * or -1 if a replacement resulted in an invalid status line. |
| 4123 | */ |
| 4124 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4125 | { |
| 4126 | char term; |
| 4127 | char *cur_ptr, *cur_end; |
| 4128 | int done; |
| 4129 | struct http_txn *txn = &t->txn; |
| 4130 | int len, delta; |
| 4131 | |
| 4132 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4133 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4134 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4135 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4136 | (exp->action == ACT_ALLOW || |
| 4137 | exp->action == ACT_DENY)) |
| 4138 | return 0; |
| 4139 | else if (exp->action == ACT_REMOVE) |
| 4140 | return 0; |
| 4141 | |
| 4142 | done = 0; |
| 4143 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4144 | 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] | 4145 | cur_end = cur_ptr + txn->rsp.sl.rq.l; |
| 4146 | |
| 4147 | /* Now we have the status line between cur_ptr and cur_end */ |
| 4148 | |
| 4149 | /* The annoying part is that pattern matching needs |
| 4150 | * that we modify the contents to null-terminate all |
| 4151 | * strings before testing them. |
| 4152 | */ |
| 4153 | |
| 4154 | term = *cur_end; |
| 4155 | *cur_end = '\0'; |
| 4156 | |
| 4157 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4158 | switch (exp->action) { |
| 4159 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4160 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4161 | done = 1; |
| 4162 | break; |
| 4163 | |
| 4164 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4165 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4166 | done = 1; |
| 4167 | break; |
| 4168 | |
| 4169 | case ACT_REPLACE: |
| 4170 | *cur_end = term; /* restore the string terminator */ |
| 4171 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4172 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 4173 | /* FIXME: if the user adds a newline in the replacement, the |
| 4174 | * index will not be recalculated for now, and the new line |
| 4175 | * will not be counted as a new header. |
| 4176 | */ |
| 4177 | |
| 4178 | txn->rsp.eoh += delta; |
| 4179 | cur_end += delta; |
| 4180 | |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 4181 | 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] | 4182 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 4183 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4184 | cur_ptr, cur_end + 1, |
| 4185 | NULL, NULL); |
| 4186 | if (unlikely(!cur_end)) |
| 4187 | return -1; |
| 4188 | |
| 4189 | /* we have a full respnse and we know that we have either a CR |
| 4190 | * or an LF at <ptr>. |
| 4191 | */ |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4192 | 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] | 4193 | hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r'); |
| 4194 | /* there is no point trying this regex on headers */ |
| 4195 | return 1; |
| 4196 | } |
| 4197 | } |
| 4198 | *cur_end = term; /* restore the string terminator */ |
| 4199 | return done; |
| 4200 | } |
| 4201 | |
| 4202 | |
| 4203 | |
| 4204 | /* |
| 4205 | * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>. |
| 4206 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 4207 | * unparsable response. |
| 4208 | */ |
| 4209 | int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 4210 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4211 | struct http_txn *txn = &t->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4212 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4213 | while (exp && !(txn->flags & TX_SVDENY)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4214 | int ret; |
| 4215 | |
| 4216 | /* |
| 4217 | * The interleaving of transformations and verdicts |
| 4218 | * makes it difficult to decide to continue or stop |
| 4219 | * the evaluation. |
| 4220 | */ |
| 4221 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4222 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4223 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 4224 | exp->action == ACT_PASS)) { |
| 4225 | exp = exp->next; |
| 4226 | continue; |
| 4227 | } |
| 4228 | |
| 4229 | /* Apply the filter to the status line. */ |
| 4230 | ret = apply_filter_to_sts_line(t, rtr, exp); |
| 4231 | if (unlikely(ret < 0)) |
| 4232 | return -1; |
| 4233 | |
| 4234 | if (likely(ret == 0)) { |
| 4235 | /* The filter did not match the response, it can be |
| 4236 | * iterated through all headers. |
| 4237 | */ |
| 4238 | apply_filter_to_resp_headers(t, rtr, exp); |
| 4239 | } |
| 4240 | exp = exp->next; |
| 4241 | } |
| 4242 | return 0; |
| 4243 | } |
| 4244 | |
| 4245 | |
| 4246 | |
| 4247 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 4248 | * Manage server-side cookies. It can impact performance by about 2% so it is |
| 4249 | * desirable to call it only when needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4250 | */ |
| 4251 | void manage_server_side_cookies(struct session *t, struct buffer *rtr) |
| 4252 | { |
| 4253 | struct http_txn *txn = &t->txn; |
| 4254 | char *p1, *p2, *p3, *p4; |
| 4255 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4256 | char *cur_ptr, *cur_end, *cur_next; |
| 4257 | int cur_idx, old_idx, delta; |
| 4258 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4259 | /* Iterate through the headers. |
| 4260 | * we start with the start line. |
| 4261 | */ |
| 4262 | old_idx = 0; |
| 4263 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4264 | |
| 4265 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 4266 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4267 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4268 | |
| 4269 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4270 | cur_ptr = cur_next; |
| 4271 | cur_end = cur_ptr + cur_hdr->len; |
| 4272 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4273 | |
| 4274 | /* We have one full header between cur_ptr and cur_end, and the |
| 4275 | * next header starts at cur_next. We're only interested in |
| 4276 | * "Cookie:" headers. |
| 4277 | */ |
| 4278 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4279 | val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10); |
| 4280 | if (!val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4281 | old_idx = cur_idx; |
| 4282 | continue; |
| 4283 | } |
| 4284 | |
| 4285 | /* 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] | 4286 | txn->flags |= TX_SCK_ANY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4287 | |
| 4288 | |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 4289 | /* maybe we only wanted to see if there was a set-cookie. Note that |
| 4290 | * the cookie capture is declared in the fronend. |
| 4291 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4292 | if (t->be->cookie_name == NULL && |
| 4293 | t->be->appsession_name == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 4294 | t->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4295 | return; |
| 4296 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4297 | p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4298 | |
| 4299 | 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] | 4300 | if (p1 == cur_end || *p1 == ';') /* end of cookie */ |
| 4301 | break; |
| 4302 | |
| 4303 | /* p1 is at the beginning of the cookie name */ |
| 4304 | p2 = p1; |
| 4305 | |
| 4306 | while (p2 < cur_end && *p2 != '=' && *p2 != ';') |
| 4307 | p2++; |
| 4308 | |
| 4309 | if (p2 == cur_end || *p2 == ';') /* next cookie */ |
| 4310 | break; |
| 4311 | |
| 4312 | p3 = p2 + 1; /* skip the '=' sign */ |
| 4313 | if (p3 == cur_end) |
| 4314 | break; |
| 4315 | |
| 4316 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4317 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';') |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4318 | p4++; |
| 4319 | |
| 4320 | /* here, we have the cookie name between p1 and p2, |
| 4321 | * and its value between p3 and p4. |
| 4322 | * we can process it. |
| 4323 | */ |
| 4324 | |
| 4325 | /* first, let's see if we want to capture it */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 4326 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4327 | txn->srv_cookie == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 4328 | (p4 - p1 >= t->fe->capture_namelen) && |
| 4329 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4330 | int log_len = p4 - p1; |
| 4331 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 4332 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4333 | Alert("HTTP logging : out of memory.\n"); |
| 4334 | } |
| 4335 | |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 4336 | if (log_len > t->fe->capture_len) |
| 4337 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4338 | memcpy(txn->srv_cookie, p1, log_len); |
| 4339 | txn->srv_cookie[log_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4340 | } |
| 4341 | |
| 4342 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4343 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 4344 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4345 | /* Cool... it's the right one */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4346 | txn->flags |= TX_SCK_SEEN; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4347 | |
| 4348 | /* If the cookie is in insert mode on a known server, we'll delete |
| 4349 | * this occurrence because we'll insert another one later. |
| 4350 | * We'll delete it too if the "indirect" option is set and we're in |
| 4351 | * a direct access. */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4352 | if (((t->srv) && (t->be->options & PR_O_COOK_INS)) || |
| 4353 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4354 | /* this header must be deleted */ |
| 4355 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 4356 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4357 | txn->hdr_idx.used--; |
| 4358 | cur_hdr->len = 0; |
| 4359 | cur_next += delta; |
| 4360 | txn->rsp.eoh += delta; |
| 4361 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4362 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4363 | } |
| 4364 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4365 | (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4366 | /* replace bytes p3->p4 with the cookie name associated |
| 4367 | * with this server since we know it. |
| 4368 | */ |
| 4369 | delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen); |
| 4370 | cur_hdr->len += delta; |
| 4371 | cur_next += delta; |
| 4372 | txn->rsp.eoh += delta; |
| 4373 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4374 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4375 | } |
| 4376 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4377 | (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4378 | /* insert the cookie name associated with this server |
| 4379 | * before existing cookie, and insert a delimitor between them.. |
| 4380 | */ |
| 4381 | delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1); |
| 4382 | cur_hdr->len += delta; |
| 4383 | cur_next += delta; |
| 4384 | txn->rsp.eoh += delta; |
| 4385 | |
| 4386 | p3[t->srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4387 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4388 | } |
| 4389 | } |
| 4390 | /* next, let's see if the cookie is our appcookie */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4391 | else if ((t->be->appsession_name != NULL) && |
| 4392 | (memcmp(p1, t->be->appsession_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4393 | |
| 4394 | /* Cool... it's the right one */ |
| 4395 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 4396 | if (t->sessid != NULL) { |
| 4397 | /* free previously allocated memory as we don't need it anymore */ |
| 4398 | pool_free2(apools.sessid, t->sessid); |
| 4399 | } |
| 4400 | /* Store the sessid in the session for future use */ |
| 4401 | if ((t->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4402 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4403 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4404 | return; |
| 4405 | } |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 4406 | memcpy(t->sessid, p3, t->be->appsession_len); |
| 4407 | t->sessid[t->be->appsession_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4408 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 4409 | break; /* we don't want to loop again since there cannot be another cookie on the same line */ |
| 4410 | } /* we're now at the end of the cookie value */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4411 | /* keep the link from this header to next one */ |
| 4412 | old_idx = cur_idx; |
| 4413 | } /* end of cookie processing on this header */ |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 4414 | |
| 4415 | if (t->sessid != NULL) { |
| 4416 | appsess *asession = NULL; |
| 4417 | /* only do insert, if lookup fails */ |
| 4418 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), t->sessid); |
| 4419 | if (asession == NULL) { |
| 4420 | if ((asession = pool_alloc2(pool2_appsess)) == NULL) { |
| 4421 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 4422 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 4423 | return; |
| 4424 | } |
| 4425 | if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 4426 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4427 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4428 | return; |
| 4429 | } |
| 4430 | memcpy(asession->sessid, t->sessid, t->be->appsession_len); |
| 4431 | asession->sessid[t->be->appsession_len] = 0; |
| 4432 | |
| 4433 | size_t server_id_len = strlen(t->srv->id) + 1; |
| 4434 | if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) { |
| 4435 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4436 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 4437 | return; |
| 4438 | } |
| 4439 | asession->serverid[0] = '\0'; |
| 4440 | memcpy(asession->serverid, t->srv->id, server_id_len); |
| 4441 | |
| 4442 | asession->request_count = 0; |
| 4443 | appsession_hash_insert(&(t->be->htbl_proxy), asession); |
| 4444 | } |
| 4445 | |
| 4446 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 4447 | asession->request_count++; |
| 4448 | } |
| 4449 | |
| 4450 | #if defined(DEBUG_HASH) |
| 4451 | Alert("manage_server_side_cookies\n"); |
| 4452 | appsession_hash_dump(&(t->be->htbl_proxy)); |
| 4453 | #endif |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4454 | } |
| 4455 | |
| 4456 | |
| 4457 | |
| 4458 | /* |
| 4459 | * Check if response is cacheable or not. Updates t->flags. |
| 4460 | */ |
| 4461 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 4462 | { |
| 4463 | struct http_txn *txn = &t->txn; |
| 4464 | char *p1, *p2; |
| 4465 | |
| 4466 | char *cur_ptr, *cur_end, *cur_next; |
| 4467 | int cur_idx; |
| 4468 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 4469 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4470 | return; |
| 4471 | |
| 4472 | /* Iterate through the headers. |
| 4473 | * we start with the start line. |
| 4474 | */ |
| 4475 | cur_idx = 0; |
| 4476 | cur_next = rtr->data + txn->rsp.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4477 | |
| 4478 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 4479 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4480 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4481 | |
| 4482 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 4483 | cur_ptr = cur_next; |
| 4484 | cur_end = cur_ptr + cur_hdr->len; |
| 4485 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4486 | |
| 4487 | /* We have one full header between cur_ptr and cur_end, and the |
| 4488 | * next header starts at cur_next. We're only interested in |
| 4489 | * "Cookie:" headers. |
| 4490 | */ |
| 4491 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4492 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 4493 | if (val) { |
| 4494 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 4495 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 4496 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 4497 | return; |
| 4498 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4499 | } |
| 4500 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4501 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 4502 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4503 | continue; |
| 4504 | |
| 4505 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 4506 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 4507 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4508 | |
| 4509 | if (p1 >= cur_end) /* no more info */ |
| 4510 | continue; |
| 4511 | |
| 4512 | /* p1 is at the beginning of the value */ |
| 4513 | p2 = p1; |
| 4514 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 4515 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4516 | p2++; |
| 4517 | |
| 4518 | /* we have a complete value between p1 and p2 */ |
| 4519 | if (p2 < cur_end && *p2 == '=') { |
| 4520 | /* we have something of the form no-cache="set-cookie" */ |
| 4521 | if ((cur_end - p1 >= 21) && |
| 4522 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 4523 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4524 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4525 | continue; |
| 4526 | } |
| 4527 | |
| 4528 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 4529 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 4530 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 4531 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 4532 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4533 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4534 | return; |
| 4535 | } |
| 4536 | |
| 4537 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4538 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4539 | continue; |
| 4540 | } |
| 4541 | } |
| 4542 | } |
| 4543 | |
| 4544 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4545 | /* |
| 4546 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 4547 | * If the server is found, it's assigned to the session. |
| 4548 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4549 | 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] | 4550 | { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4551 | char *request_line; |
| 4552 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4553 | if (t->be->appsession_name == NULL || |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 4554 | (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST) || |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4555 | (request_line = memchr(begin, ';', len)) == NULL || |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4556 | ((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] | 4557 | return; |
| 4558 | |
| 4559 | /* skip ';' */ |
| 4560 | request_line++; |
| 4561 | |
| 4562 | /* look if we have a jsessionid */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4563 | 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] | 4564 | return; |
| 4565 | |
| 4566 | /* skip jsessionid= */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 4567 | request_line += t->be->appsession_name_len + 1; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4568 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 4569 | manage_client_side_appsession(t, request_line); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4570 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 4571 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 4572 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4573 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4574 | } |
| 4575 | |
| 4576 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4577 | /* |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 4578 | * In a GET or HEAD request, check if the requested URI matches the stats uri |
| 4579 | * 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] | 4580 | * |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 4581 | * 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] | 4582 | * t->be->uri_auth field is valid. An HTTP/401 response may be sent, or |
Willy Tarreau | b0c9bc4 | 2009-10-04 15:56:38 +0200 | [diff] [blame] | 4583 | * the stats I/O handler will be registered to start sending data. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4584 | * |
| 4585 | * Returns 1 if the session's state changes, otherwise 0. |
| 4586 | */ |
| 4587 | int stats_check_uri_auth(struct session *t, struct proxy *backend) |
| 4588 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4589 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4590 | struct uri_auth *uri_auth = backend->uri_auth; |
| 4591 | struct user_auth *user; |
| 4592 | int authenticated, cur_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4593 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4594 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 4595 | memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats)); |
| 4596 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4597 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4598 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4599 | return 0; |
| 4600 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4601 | h = t->req->data + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4602 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 4603 | /* the URI is in h */ |
| 4604 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4605 | return 0; |
| 4606 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 4607 | h += uri_auth->uri_len; |
| 4608 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) { |
| 4609 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 4610 | t->data_ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 4611 | break; |
| 4612 | } |
| 4613 | h++; |
| 4614 | } |
| 4615 | |
| 4616 | if (uri_auth->refresh) { |
| 4617 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 4618 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) { |
| 4619 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 4620 | t->data_ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 4621 | break; |
| 4622 | } |
| 4623 | h++; |
| 4624 | } |
| 4625 | } |
| 4626 | |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 4627 | h = t->req->data + txn->req.sl.rq.u + uri_auth->uri_len; |
| 4628 | while (h <= t->req->data + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) { |
| 4629 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 4630 | t->data_ctx.stats.flags |= STAT_FMT_CSV; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 4631 | break; |
| 4632 | } |
| 4633 | h++; |
| 4634 | } |
| 4635 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 4636 | t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO; |
| 4637 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4638 | /* we are in front of a interceptable URI. Let's check |
| 4639 | * if there's an authentication and if it's valid. |
| 4640 | */ |
| 4641 | user = uri_auth->users; |
| 4642 | if (!user) { |
| 4643 | /* no user auth required, it's OK */ |
| 4644 | authenticated = 1; |
| 4645 | } else { |
| 4646 | authenticated = 0; |
| 4647 | |
| 4648 | /* a user list is defined, we have to check. |
| 4649 | * skip 21 chars for "Authorization: Basic ". |
| 4650 | */ |
| 4651 | |
| 4652 | /* FIXME: this should move to an earlier place */ |
| 4653 | cur_idx = 0; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4654 | h = t->req->data + txn->req.som + hdr_idx_first_pos(&txn->hdr_idx); |
| 4655 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 4656 | int len = txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4657 | if (len > 14 && |
| 4658 | !strncasecmp("Authorization:", h, 14)) { |
Krzysztof Piotr Oledzki | 6f61b21 | 2009-10-04 23:34:15 +0200 | [diff] [blame] | 4659 | chunk_initlen(&txn->auth_hdr, h, 0, len); |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4660 | break; |
| 4661 | } |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4662 | h += len + txn->hdr_idx.v[cur_idx].cr + 1; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4663 | } |
| 4664 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4665 | if (txn->auth_hdr.len < 21 || |
| 4666 | memcmp(txn->auth_hdr.str + 14, " Basic ", 7)) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4667 | user = NULL; |
| 4668 | |
| 4669 | while (user) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4670 | if ((txn->auth_hdr.len == user->user_len + 14 + 7) |
| 4671 | && !memcmp(txn->auth_hdr.str + 14 + 7, |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4672 | user->user_pwd, user->user_len)) { |
| 4673 | authenticated = 1; |
| 4674 | break; |
| 4675 | } |
| 4676 | user = user->next; |
| 4677 | } |
| 4678 | } |
| 4679 | |
| 4680 | if (!authenticated) { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 4681 | struct chunk msg; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4682 | |
| 4683 | /* no need to go further */ |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 4684 | sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm); |
| 4685 | chunk_initlen(&msg, trash, sizeof(trash), strlen(trash)); |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 4686 | txn->status = 401; |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 4687 | stream_int_retnclose(t->req->prod, &msg); |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 4688 | t->req->analysers = 0; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4689 | if (!(t->flags & SN_ERR_MASK)) |
| 4690 | t->flags |= SN_ERR_PRXCOND; |
| 4691 | if (!(t->flags & SN_FINST_MASK)) |
| 4692 | t->flags |= SN_FINST_R; |
| 4693 | return 1; |
| 4694 | } |
| 4695 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 4696 | /* The request is valid, the user is authenticated. Let's start sending |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4697 | * data. |
| 4698 | */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 4699 | t->logs.tv_request = now; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4700 | t->data_source = DATA_SRC_STATS; |
| 4701 | t->data_state = DATA_ST_INIT; |
Willy Tarreau | 91e9993 | 2008-06-30 07:51:00 +0200 | [diff] [blame] | 4702 | t->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | b0c9bc4 | 2009-10-04 15:56:38 +0200 | [diff] [blame] | 4703 | stream_int_register_handler(t->rep->prod, http_stats_io_handler); |
| 4704 | t->rep->prod->private = t; |
| 4705 | t->rep->prod->st0 = t->rep->prod->st1 = 0; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4706 | return 1; |
| 4707 | } |
| 4708 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4709 | /* |
| 4710 | * Capture a bad request or response and archive it in the proxy's structure. |
| 4711 | */ |
| 4712 | void http_capture_bad_message(struct error_snapshot *es, struct session *s, |
| 4713 | struct buffer *buf, struct http_msg *msg, |
| 4714 | struct proxy *other_end) |
| 4715 | { |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 4716 | es->len = buf->r - (buf->data + msg->som); |
| 4717 | 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] | 4718 | if (msg->err_pos >= 0) |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 4719 | es->pos = msg->err_pos - msg->som; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4720 | else |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 4721 | es->pos = buf->lr - (buf->data + msg->som); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4722 | es->when = date; // user-visible date |
| 4723 | es->sid = s->uniq_id; |
| 4724 | es->srv = s->srv; |
| 4725 | es->oe = other_end; |
| 4726 | es->src = s->cli_addr; |
| 4727 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 4728 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4729 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4730 | * Print a debug line with a header |
| 4731 | */ |
| 4732 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 4733 | { |
| 4734 | int len, max; |
| 4735 | 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] | 4736 | 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] | 4737 | max = end - start; |
| 4738 | UBOUND(max, sizeof(trash) - len - 1); |
| 4739 | len += strlcpy2(trash + len, start, max + 1); |
| 4740 | trash[len++] = '\n'; |
| 4741 | write(1, trash, len); |
| 4742 | } |
| 4743 | |
| 4744 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4745 | /************************************************************************/ |
| 4746 | /* The code below is dedicated to ACL parsing and matching */ |
| 4747 | /************************************************************************/ |
| 4748 | |
| 4749 | |
| 4750 | |
| 4751 | |
| 4752 | /* 1. Check on METHOD |
| 4753 | * We use the pre-parsed method if it is known, and store its number as an |
| 4754 | * integer. If it is unknown, we use the pointer and the length. |
| 4755 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4756 | 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] | 4757 | { |
| 4758 | int len, meth; |
| 4759 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4760 | len = strlen(*text); |
| 4761 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4762 | |
| 4763 | pattern->val.i = meth; |
| 4764 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4765 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4766 | if (!pattern->ptr.str) |
| 4767 | return 0; |
| 4768 | pattern->len = len; |
| 4769 | } |
| 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_meth(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 | int meth; |
| 4778 | struct http_txn *txn = l7; |
| 4779 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4780 | if (!txn) |
| 4781 | return 0; |
| 4782 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4783 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 4784 | return 0; |
| 4785 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4786 | meth = txn->meth; |
| 4787 | test->i = meth; |
| 4788 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4789 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 4790 | /* ensure the indexes are not affected */ |
| 4791 | return 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4792 | test->len = txn->req.sl.rq.m_l; |
| 4793 | test->ptr = txn->req.sol; |
| 4794 | } |
| 4795 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 4796 | return 1; |
| 4797 | } |
| 4798 | |
| 4799 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 4800 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 4801 | int icase; |
| 4802 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4803 | if (test->i != pattern->val.i) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 4804 | return ACL_PAT_FAIL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4805 | |
| 4806 | if (test->i != HTTP_METH_OTHER) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 4807 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4808 | |
| 4809 | /* Other method, we must compare the strings */ |
| 4810 | if (pattern->len != test->len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 4811 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 4812 | |
| 4813 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 4814 | if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) || |
| 4815 | (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 4816 | return ACL_PAT_FAIL; |
| 4817 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4818 | } |
| 4819 | |
| 4820 | /* 2. Check on Request/Status Version |
| 4821 | * We simply compare strings here. |
| 4822 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4823 | 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] | 4824 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4825 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4826 | if (!pattern->ptr.str) |
| 4827 | return 0; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 4828 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4829 | return 1; |
| 4830 | } |
| 4831 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 4832 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 4833 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4834 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4835 | { |
| 4836 | struct http_txn *txn = l7; |
| 4837 | char *ptr; |
| 4838 | int len; |
| 4839 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4840 | if (!txn) |
| 4841 | return 0; |
| 4842 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4843 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 4844 | return 0; |
| 4845 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4846 | len = txn->req.sl.rq.v_l; |
| 4847 | ptr = txn->req.sol + txn->req.sl.rq.v - txn->req.som; |
| 4848 | |
| 4849 | while ((len-- > 0) && (*ptr++ != '/')); |
| 4850 | if (len <= 0) |
| 4851 | return 0; |
| 4852 | |
| 4853 | test->ptr = ptr; |
| 4854 | test->len = len; |
| 4855 | |
| 4856 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 4857 | return 1; |
| 4858 | } |
| 4859 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 4860 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 4861 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4862 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4863 | { |
| 4864 | struct http_txn *txn = l7; |
| 4865 | char *ptr; |
| 4866 | int len; |
| 4867 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4868 | if (!txn) |
| 4869 | return 0; |
| 4870 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4871 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 4872 | return 0; |
| 4873 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4874 | len = txn->rsp.sl.st.v_l; |
| 4875 | ptr = txn->rsp.sol; |
| 4876 | |
| 4877 | while ((len-- > 0) && (*ptr++ != '/')); |
| 4878 | if (len <= 0) |
| 4879 | return 0; |
| 4880 | |
| 4881 | test->ptr = ptr; |
| 4882 | test->len = len; |
| 4883 | |
| 4884 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 4885 | return 1; |
| 4886 | } |
| 4887 | |
| 4888 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 4889 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 4890 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4891 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4892 | { |
| 4893 | struct http_txn *txn = l7; |
| 4894 | char *ptr; |
| 4895 | int len; |
| 4896 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4897 | if (!txn) |
| 4898 | return 0; |
| 4899 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4900 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 4901 | return 0; |
| 4902 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4903 | len = txn->rsp.sl.st.c_l; |
| 4904 | ptr = txn->rsp.sol + txn->rsp.sl.st.c - txn->rsp.som; |
| 4905 | |
| 4906 | test->i = __strl2ui(ptr, len); |
| 4907 | test->flags = ACL_TEST_F_VOL_1ST; |
| 4908 | return 1; |
| 4909 | } |
| 4910 | |
| 4911 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 4912 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 4913 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4914 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4915 | { |
| 4916 | struct http_txn *txn = l7; |
| 4917 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4918 | if (!txn) |
| 4919 | return 0; |
| 4920 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4921 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 4922 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4923 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4924 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 4925 | /* ensure the indexes are not affected */ |
| 4926 | return 0; |
| 4927 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4928 | test->len = txn->req.sl.rq.u_l; |
| 4929 | test->ptr = txn->req.sol + txn->req.sl.rq.u; |
| 4930 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 4931 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 4932 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 4933 | return 1; |
| 4934 | } |
| 4935 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 4936 | static int |
| 4937 | acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4938 | struct acl_expr *expr, struct acl_test *test) |
| 4939 | { |
| 4940 | struct http_txn *txn = l7; |
| 4941 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4942 | if (!txn) |
| 4943 | return 0; |
| 4944 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 4945 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 4946 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4947 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 4948 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 4949 | /* ensure the indexes are not affected */ |
| 4950 | return 0; |
| 4951 | |
| 4952 | /* Parse HTTP request */ |
| 4953 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
| 4954 | test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr; |
| 4955 | test->i = AF_INET; |
| 4956 | |
| 4957 | /* |
| 4958 | * If we are parsing url in frontend space, we prepare backend stage |
| 4959 | * to not parse again the same url ! optimization lazyness... |
| 4960 | */ |
| 4961 | if (px->options & PR_O_HTTP_PROXY) |
| 4962 | l4->flags |= SN_ADDR_SET; |
| 4963 | |
| 4964 | test->flags = ACL_TEST_F_READ_ONLY; |
| 4965 | return 1; |
| 4966 | } |
| 4967 | |
| 4968 | static int |
| 4969 | acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir, |
| 4970 | struct acl_expr *expr, struct acl_test *test) |
| 4971 | { |
| 4972 | struct http_txn *txn = l7; |
| 4973 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4974 | if (!txn) |
| 4975 | return 0; |
| 4976 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 4977 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 4978 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 4979 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 4980 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 4981 | /* ensure the indexes are not affected */ |
| 4982 | return 0; |
| 4983 | |
| 4984 | /* Same optimization as url_ip */ |
| 4985 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
| 4986 | test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port); |
| 4987 | |
| 4988 | if (px->options & PR_O_HTTP_PROXY) |
| 4989 | l4->flags |= SN_ADDR_SET; |
| 4990 | |
| 4991 | test->flags = ACL_TEST_F_READ_ONLY; |
| 4992 | return 1; |
| 4993 | } |
| 4994 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4995 | /* 5. Check on HTTP header. A pointer to the beginning of the value is returned. |
| 4996 | * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr(). |
| 4997 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 4998 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 4999 | 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] | 5000 | struct acl_expr *expr, struct acl_test *test) |
| 5001 | { |
| 5002 | struct http_txn *txn = l7; |
| 5003 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5004 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5005 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5006 | if (!txn) |
| 5007 | return 0; |
| 5008 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5009 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 5010 | /* search for header from the beginning */ |
| 5011 | ctx->idx = 0; |
| 5012 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5013 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 5014 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 5015 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5016 | test->len = ctx->vlen; |
| 5017 | test->ptr = (char *)ctx->line + ctx->val; |
| 5018 | return 1; |
| 5019 | } |
| 5020 | |
| 5021 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 5022 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5023 | return 0; |
| 5024 | } |
| 5025 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5026 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5027 | acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5028 | struct acl_expr *expr, struct acl_test *test) |
| 5029 | { |
| 5030 | struct http_txn *txn = l7; |
| 5031 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5032 | if (!txn) |
| 5033 | return 0; |
| 5034 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5035 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5036 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5037 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5038 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5039 | /* ensure the indexes are not affected */ |
| 5040 | return 0; |
| 5041 | |
| 5042 | return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test); |
| 5043 | } |
| 5044 | |
| 5045 | static int |
| 5046 | acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5047 | struct acl_expr *expr, struct acl_test *test) |
| 5048 | { |
| 5049 | struct http_txn *txn = l7; |
| 5050 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5051 | if (!txn) |
| 5052 | return 0; |
| 5053 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5054 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5055 | return 0; |
| 5056 | |
| 5057 | return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test); |
| 5058 | } |
| 5059 | |
| 5060 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 5061 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 5062 | */ |
| 5063 | static int |
| 5064 | 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] | 5065 | struct acl_expr *expr, struct acl_test *test) |
| 5066 | { |
| 5067 | struct http_txn *txn = l7; |
| 5068 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5069 | struct hdr_ctx ctx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5070 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5071 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5072 | if (!txn) |
| 5073 | return 0; |
| 5074 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5075 | ctx.idx = 0; |
| 5076 | cnt = 0; |
| 5077 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx)) |
| 5078 | cnt++; |
| 5079 | |
| 5080 | test->i = cnt; |
| 5081 | test->flags = ACL_TEST_F_VOL_HDR; |
| 5082 | return 1; |
| 5083 | } |
| 5084 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5085 | static int |
| 5086 | acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5087 | struct acl_expr *expr, struct acl_test *test) |
| 5088 | { |
| 5089 | struct http_txn *txn = l7; |
| 5090 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5091 | if (!txn) |
| 5092 | return 0; |
| 5093 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5094 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5095 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5096 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5097 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5098 | /* ensure the indexes are not affected */ |
| 5099 | return 0; |
| 5100 | |
| 5101 | return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test); |
| 5102 | } |
| 5103 | |
| 5104 | static int |
| 5105 | acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5106 | struct acl_expr *expr, struct acl_test *test) |
| 5107 | { |
| 5108 | struct http_txn *txn = l7; |
| 5109 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5110 | if (!txn) |
| 5111 | return 0; |
| 5112 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5113 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5114 | return 0; |
| 5115 | |
| 5116 | return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test); |
| 5117 | } |
| 5118 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5119 | /* 7. Check on HTTP header's integer value. The integer value is returned. |
| 5120 | * FIXME: the type is 'int', it may not be appropriate for everything. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5121 | * 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] | 5122 | */ |
| 5123 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5124 | 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] | 5125 | struct acl_expr *expr, struct acl_test *test) |
| 5126 | { |
| 5127 | struct http_txn *txn = l7; |
| 5128 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5129 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5130 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5131 | if (!txn) |
| 5132 | return 0; |
| 5133 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5134 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 5135 | /* search for header from the beginning */ |
| 5136 | ctx->idx = 0; |
| 5137 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5138 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 5139 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 5140 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5141 | test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen); |
| 5142 | return 1; |
| 5143 | } |
| 5144 | |
| 5145 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 5146 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5147 | return 0; |
| 5148 | } |
| 5149 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5150 | static int |
| 5151 | acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5152 | struct acl_expr *expr, struct acl_test *test) |
| 5153 | { |
| 5154 | struct http_txn *txn = l7; |
| 5155 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5156 | if (!txn) |
| 5157 | return 0; |
| 5158 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5159 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5160 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5161 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5162 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5163 | /* ensure the indexes are not affected */ |
| 5164 | return 0; |
| 5165 | |
| 5166 | return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test); |
| 5167 | } |
| 5168 | |
| 5169 | static int |
| 5170 | acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5171 | struct acl_expr *expr, struct acl_test *test) |
| 5172 | { |
| 5173 | struct http_txn *txn = l7; |
| 5174 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5175 | if (!txn) |
| 5176 | return 0; |
| 5177 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5178 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5179 | return 0; |
| 5180 | |
| 5181 | return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test); |
| 5182 | } |
| 5183 | |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 5184 | /* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned. |
| 5185 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 5186 | */ |
| 5187 | static int |
| 5188 | acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol, |
| 5189 | struct acl_expr *expr, struct acl_test *test) |
| 5190 | { |
| 5191 | struct http_txn *txn = l7; |
| 5192 | struct hdr_idx *idx = &txn->hdr_idx; |
| 5193 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
| 5194 | |
| 5195 | if (!txn) |
| 5196 | return 0; |
| 5197 | |
| 5198 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 5199 | /* search for header from the beginning */ |
| 5200 | ctx->idx = 0; |
| 5201 | |
| 5202 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 5203 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 5204 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5205 | /* Same optimization as url_ip */ |
| 5206 | memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr)); |
| 5207 | url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr); |
| 5208 | test->ptr = (void *)&l4->srv_addr.sin_addr; |
| 5209 | test->i = AF_INET; |
| 5210 | return 1; |
| 5211 | } |
| 5212 | |
| 5213 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 5214 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 5215 | return 0; |
| 5216 | } |
| 5217 | |
| 5218 | static int |
| 5219 | acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5220 | struct acl_expr *expr, struct acl_test *test) |
| 5221 | { |
| 5222 | struct http_txn *txn = l7; |
| 5223 | |
| 5224 | if (!txn) |
| 5225 | return 0; |
| 5226 | |
| 5227 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5228 | return 0; |
| 5229 | |
| 5230 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5231 | /* ensure the indexes are not affected */ |
| 5232 | return 0; |
| 5233 | |
| 5234 | return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test); |
| 5235 | } |
| 5236 | |
| 5237 | static int |
| 5238 | acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5239 | struct acl_expr *expr, struct acl_test *test) |
| 5240 | { |
| 5241 | struct http_txn *txn = l7; |
| 5242 | |
| 5243 | if (!txn) |
| 5244 | return 0; |
| 5245 | |
| 5246 | if (txn->rsp.msg_state != HTTP_MSG_BODY) |
| 5247 | return 0; |
| 5248 | |
| 5249 | return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test); |
| 5250 | } |
| 5251 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5252 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 5253 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 5254 | */ |
| 5255 | static int |
| 5256 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir, |
| 5257 | struct acl_expr *expr, struct acl_test *test) |
| 5258 | { |
| 5259 | struct http_txn *txn = l7; |
| 5260 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 5261 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5262 | if (!txn) |
| 5263 | return 0; |
| 5264 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5265 | if (txn->req.msg_state != HTTP_MSG_BODY) |
| 5266 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 5267 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5268 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 5269 | /* ensure the indexes are not affected */ |
| 5270 | return 0; |
| 5271 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 5272 | end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 5273 | ptr = http_get_path(txn); |
| 5274 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5275 | return 0; |
| 5276 | |
| 5277 | /* OK, we got the '/' ! */ |
| 5278 | test->ptr = ptr; |
| 5279 | |
| 5280 | while (ptr < end && *ptr != '?') |
| 5281 | ptr++; |
| 5282 | |
| 5283 | test->len = ptr - test->ptr; |
| 5284 | |
| 5285 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 5286 | test->flags = ACL_TEST_F_VOL_1ST; |
| 5287 | return 1; |
| 5288 | } |
| 5289 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 5290 | static int |
| 5291 | acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir, |
| 5292 | struct acl_expr *expr, struct acl_test *test) |
| 5293 | { |
| 5294 | struct buffer *req = s->req; |
| 5295 | struct http_txn *txn = &s->txn; |
| 5296 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5297 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 5298 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 5299 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 5300 | */ |
| 5301 | |
| 5302 | if (!s || !req) |
| 5303 | return 0; |
| 5304 | |
| 5305 | if (unlikely(msg->msg_state == HTTP_MSG_BODY)) { |
| 5306 | /* Already decoded as OK */ |
| 5307 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 5308 | return 1; |
| 5309 | } |
| 5310 | |
| 5311 | /* Try to decode HTTP request */ |
| 5312 | if (likely(req->lr < req->r)) |
| 5313 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
| 5314 | |
| 5315 | if (unlikely(msg->msg_state != HTTP_MSG_BODY)) { |
| 5316 | if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) { |
| 5317 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 5318 | return 1; |
| 5319 | } |
| 5320 | /* wait for final state */ |
| 5321 | test->flags |= ACL_TEST_F_MAY_CHANGE; |
| 5322 | return 0; |
| 5323 | } |
| 5324 | |
| 5325 | /* OK we got a valid HTTP request. We have some minor preparation to |
| 5326 | * perform so that further checks can rely on HTTP tests. |
| 5327 | */ |
| 5328 | msg->sol = req->data + msg->som; |
| 5329 | txn->meth = find_http_meth(&req->data[msg->som], msg->sl.rq.m_l); |
| 5330 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 5331 | s->flags |= SN_REDIRECTABLE; |
| 5332 | |
| 5333 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) { |
| 5334 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 5335 | return 1; |
| 5336 | } |
| 5337 | |
| 5338 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 5339 | return 1; |
| 5340 | } |
| 5341 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5342 | |
| 5343 | /************************************************************************/ |
| 5344 | /* All supported keywords must be declared here. */ |
| 5345 | /************************************************************************/ |
| 5346 | |
| 5347 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 5348 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 5349 | { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT }, |
| 5350 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5351 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT }, |
| 5352 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5353 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 5354 | { "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] | 5355 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5356 | { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5357 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 5358 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 5359 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 5360 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 5361 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 5362 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 5363 | { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE }, |
| 5364 | { "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] | 5365 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5366 | /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */ |
| 5367 | { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5368 | { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 5369 | { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 5370 | { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 5371 | { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 5372 | { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 5373 | { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 5374 | { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
| 5375 | { "hdr_val", acl_parse_int, acl_fetch_chdr_val,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 5376 | { "hdr_ip", acl_parse_ip, acl_fetch_chdr_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE }, |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 5377 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5378 | { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 5379 | { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE }, |
| 5380 | { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE }, |
| 5381 | { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE }, |
| 5382 | { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE }, |
| 5383 | { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE }, |
| 5384 | { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE }, |
| 5385 | { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
| 5386 | { "shdr_val", acl_parse_int, acl_fetch_shdr_val,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 5387 | { "shdr_ip", acl_parse_ip, acl_fetch_shdr_ip, acl_match_ip, ACL_USE_L7RTR_VOLATILE }, |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 5388 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 5389 | { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 5390 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 5391 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 5392 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 5393 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 5394 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 5395 | { "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] | 5396 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 5397 | { NULL, NULL, NULL, NULL }, |
| 5398 | |
| 5399 | #if 0 |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5400 | { "line", acl_parse_str, acl_fetch_line, acl_match_str }, |
| 5401 | { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg }, |
| 5402 | { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg }, |
| 5403 | { "line_end", acl_parse_str, acl_fetch_line, acl_match_end }, |
| 5404 | { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub }, |
| 5405 | { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir }, |
| 5406 | { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom }, |
| 5407 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 5408 | { "cook", acl_parse_str, acl_fetch_cook, acl_match_str }, |
| 5409 | { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg }, |
| 5410 | { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg }, |
| 5411 | { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end }, |
| 5412 | { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub }, |
| 5413 | { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir }, |
| 5414 | { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom }, |
| 5415 | { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst }, |
| 5416 | |
| 5417 | { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str }, |
| 5418 | { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg }, |
| 5419 | { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str }, |
| 5420 | { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 }, |
| 5421 | { NULL, NULL, NULL, NULL }, |
| 5422 | #endif |
| 5423 | }}; |
| 5424 | |
| 5425 | |
| 5426 | __attribute__((constructor)) |
| 5427 | static void __http_protocol_init(void) |
| 5428 | { |
| 5429 | acl_register_keywords(&acl_kws); |
| 5430 | } |
| 5431 | |
| 5432 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5433 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5434 | * Local variables: |
| 5435 | * c-indent-level: 8 |
| 5436 | * c-basic-offset: 8 |
| 5437 | * End: |
| 5438 | */ |