Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * HTTP protocol analyzer |
| 3 | * |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 4 | * Copyright 2000-2010 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> |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 44 | #include <proto/checks.h> |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 45 | #include <proto/client.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 46 | #include <proto/dumpstats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 47 | #include <proto/fd.h> |
| 48 | #include <proto/log.h> |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 49 | #include <proto/hdr_idx.h> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 50 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 51 | #include <proto/proto_http.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 52 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 53 | #include <proto/queue.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 54 | #include <proto/server.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 55 | #include <proto/session.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 56 | #include <proto/stream_interface.h> |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 57 | #include <proto/stream_sock.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 58 | #include <proto/task.h> |
| 59 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 60 | const char HTTP_100[] = |
| 61 | "HTTP/1.1 100 Continue\r\n\r\n"; |
| 62 | |
| 63 | const struct chunk http_100_chunk = { |
| 64 | .str = (char *)&HTTP_100, |
| 65 | .len = sizeof(HTTP_100)-1 |
| 66 | }; |
| 67 | |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 68 | /* This is used by remote monitoring */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 69 | const char HTTP_200[] = |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 70 | "HTTP/1.0 200 OK\r\n" |
| 71 | "Cache-Control: no-cache\r\n" |
| 72 | "Connection: close\r\n" |
| 73 | "Content-Type: text/html\r\n" |
| 74 | "\r\n" |
| 75 | "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n"; |
| 76 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 77 | const struct chunk http_200_chunk = { |
| 78 | .str = (char *)&HTTP_200, |
| 79 | .len = sizeof(HTTP_200)-1 |
| 80 | }; |
| 81 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 82 | /* Warning: no "connection" header is provided with the 3xx messages below */ |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 83 | const char *HTTP_301 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 84 | "HTTP/1.1 301 Moved Permanently\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 85 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 86 | "Content-length: 0\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 87 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 88 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 89 | const char *HTTP_302 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 90 | "HTTP/1.1 302 Found\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 91 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 92 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 93 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 94 | |
| 95 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 96 | const char *HTTP_303 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 97 | "HTTP/1.1 303 See Other\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 98 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 99 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 100 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 101 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 102 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 103 | const char *HTTP_401_fmt = |
| 104 | "HTTP/1.0 401 Unauthorized\r\n" |
| 105 | "Cache-Control: no-cache\r\n" |
| 106 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 107 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 108 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 109 | "\r\n" |
| 110 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 111 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 112 | |
| 113 | const int http_err_codes[HTTP_ERR_SIZE] = { |
| 114 | [HTTP_ERR_400] = 400, |
| 115 | [HTTP_ERR_403] = 403, |
| 116 | [HTTP_ERR_408] = 408, |
| 117 | [HTTP_ERR_500] = 500, |
| 118 | [HTTP_ERR_502] = 502, |
| 119 | [HTTP_ERR_503] = 503, |
| 120 | [HTTP_ERR_504] = 504, |
| 121 | }; |
| 122 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 123 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 124 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 125 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 126 | "Cache-Control: no-cache\r\n" |
| 127 | "Connection: close\r\n" |
| 128 | "Content-Type: text/html\r\n" |
| 129 | "\r\n" |
| 130 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 131 | |
| 132 | [HTTP_ERR_403] = |
| 133 | "HTTP/1.0 403 Forbidden\r\n" |
| 134 | "Cache-Control: no-cache\r\n" |
| 135 | "Connection: close\r\n" |
| 136 | "Content-Type: text/html\r\n" |
| 137 | "\r\n" |
| 138 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 139 | |
| 140 | [HTTP_ERR_408] = |
| 141 | "HTTP/1.0 408 Request Time-out\r\n" |
| 142 | "Cache-Control: no-cache\r\n" |
| 143 | "Connection: close\r\n" |
| 144 | "Content-Type: text/html\r\n" |
| 145 | "\r\n" |
| 146 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 147 | |
| 148 | [HTTP_ERR_500] = |
| 149 | "HTTP/1.0 500 Server Error\r\n" |
| 150 | "Cache-Control: no-cache\r\n" |
| 151 | "Connection: close\r\n" |
| 152 | "Content-Type: text/html\r\n" |
| 153 | "\r\n" |
| 154 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 155 | |
| 156 | [HTTP_ERR_502] = |
| 157 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 158 | "Cache-Control: no-cache\r\n" |
| 159 | "Connection: close\r\n" |
| 160 | "Content-Type: text/html\r\n" |
| 161 | "\r\n" |
| 162 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 163 | |
| 164 | [HTTP_ERR_503] = |
| 165 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 166 | "Cache-Control: no-cache\r\n" |
| 167 | "Connection: close\r\n" |
| 168 | "Content-Type: text/html\r\n" |
| 169 | "\r\n" |
| 170 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 171 | |
| 172 | [HTTP_ERR_504] = |
| 173 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 174 | "Cache-Control: no-cache\r\n" |
| 175 | "Connection: close\r\n" |
| 176 | "Content-Type: text/html\r\n" |
| 177 | "\r\n" |
| 178 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 179 | |
| 180 | }; |
| 181 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 182 | /* We must put the messages here since GCC cannot initialize consts depending |
| 183 | * on strlen(). |
| 184 | */ |
| 185 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 186 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 187 | #define FD_SETS_ARE_BITFIELDS |
| 188 | #ifdef FD_SETS_ARE_BITFIELDS |
| 189 | /* |
| 190 | * This map is used with all the FD_* macros to check whether a particular bit |
| 191 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 192 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 193 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 194 | * exclusively to the macros. |
| 195 | */ |
| 196 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 197 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 198 | |
| 199 | #else |
| 200 | #error "Check if your OS uses bitfields for fd_sets" |
| 201 | #endif |
| 202 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 203 | void init_proto_http() |
| 204 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 205 | int i; |
| 206 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 207 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 208 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 209 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 210 | if (!http_err_msgs[msg]) { |
| 211 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 212 | abort(); |
| 213 | } |
| 214 | |
| 215 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 216 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 217 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 218 | |
| 219 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 220 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 221 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 222 | * printable characters above. |
| 223 | */ |
| 224 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 225 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 226 | for (i = 0; i < 32; i++) { |
| 227 | FD_SET(i, hdr_encode_map); |
| 228 | FD_SET(i, url_encode_map); |
| 229 | } |
| 230 | for (i = 127; i < 256; i++) { |
| 231 | FD_SET(i, hdr_encode_map); |
| 232 | FD_SET(i, url_encode_map); |
| 233 | } |
| 234 | |
| 235 | tmp = "\"#{|}"; |
| 236 | while (*tmp) { |
| 237 | FD_SET(*tmp, hdr_encode_map); |
| 238 | tmp++; |
| 239 | } |
| 240 | |
| 241 | tmp = "\"#"; |
| 242 | while (*tmp) { |
| 243 | FD_SET(*tmp, url_encode_map); |
| 244 | tmp++; |
| 245 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 246 | |
| 247 | /* memory allocations */ |
| 248 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 249 | pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 250 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 251 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 252 | /* |
| 253 | * We have 26 list of methods (1 per first letter), each of which can have |
| 254 | * up to 3 entries (2 valid, 1 null). |
| 255 | */ |
| 256 | struct http_method_desc { |
| 257 | http_meth_t meth; |
| 258 | int len; |
| 259 | const char text[8]; |
| 260 | }; |
| 261 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 262 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 263 | ['C' - 'A'] = { |
| 264 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 265 | }, |
| 266 | ['D' - 'A'] = { |
| 267 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 268 | }, |
| 269 | ['G' - 'A'] = { |
| 270 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 271 | }, |
| 272 | ['H' - 'A'] = { |
| 273 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 274 | }, |
| 275 | ['P' - 'A'] = { |
| 276 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 277 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 278 | }, |
| 279 | ['T' - 'A'] = { |
| 280 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 281 | }, |
| 282 | /* rest is empty like this : |
| 283 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 284 | */ |
| 285 | }; |
| 286 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 287 | /* 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] | 288 | * 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] | 289 | * RFC2616 for those chars. |
| 290 | */ |
| 291 | |
| 292 | const char http_is_spht[256] = { |
| 293 | [' '] = 1, ['\t'] = 1, |
| 294 | }; |
| 295 | |
| 296 | const char http_is_crlf[256] = { |
| 297 | ['\r'] = 1, ['\n'] = 1, |
| 298 | }; |
| 299 | |
| 300 | const char http_is_lws[256] = { |
| 301 | [' '] = 1, ['\t'] = 1, |
| 302 | ['\r'] = 1, ['\n'] = 1, |
| 303 | }; |
| 304 | |
| 305 | const char http_is_sep[256] = { |
| 306 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 307 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 308 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 309 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 310 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 311 | }; |
| 312 | |
| 313 | const char http_is_ctl[256] = { |
| 314 | [0 ... 31] = 1, |
| 315 | [127] = 1, |
| 316 | }; |
| 317 | |
| 318 | /* |
| 319 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 320 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 321 | * them correctly. Instead, define every non-CTL char's status. |
| 322 | */ |
| 323 | const char http_is_token[256] = { |
| 324 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 325 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 326 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 327 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 328 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 329 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 330 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 331 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 332 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 333 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 334 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 335 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 336 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 337 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 338 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 339 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 340 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 341 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 342 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 343 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 344 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 345 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 346 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 347 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 348 | }; |
| 349 | |
| 350 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 351 | /* |
| 352 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 353 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 354 | */ |
| 355 | const char http_is_ver_token[256] = { |
| 356 | ['.'] = 1, ['/'] = 1, |
| 357 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 358 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| 359 | ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| 360 | }; |
| 361 | |
| 362 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 363 | /* |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 364 | * Silent debug that outputs only in strace, using fd #-1. Trash is modified. |
| 365 | */ |
| 366 | #if defined(DEBUG_FSM) |
| 367 | static void http_silent_debug(int line, struct session *s) |
| 368 | { |
| 369 | int size = 0; |
| 370 | size += snprintf(trash + size, sizeof(trash) - size, |
| 371 | "[%04d] req: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p lr=%p sm=%d fw=%ld tf=%08x\n", |
| 372 | line, |
| 373 | s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers, |
| 374 | s->req->data, s->req->size, s->req->l, s->req->w, s->req->r, s->req->lr, s->req->send_max, s->req->to_forward, s->txn.flags); |
| 375 | write(-1, trash, size); |
| 376 | size = 0; |
| 377 | size += snprintf(trash + size, sizeof(trash) - size, |
| 378 | " %04d rep: p=%d(%d) s=%d bf=%08x an=%08x data=%p size=%d l=%d w=%p r=%p lr=%p sm=%d fw=%ld\n", |
| 379 | line, |
| 380 | s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers, |
| 381 | s->rep->data, s->rep->size, s->rep->l, s->rep->w, s->rep->r, s->rep->lr, s->rep->send_max, s->rep->to_forward); |
| 382 | |
| 383 | write(-1, trash, size); |
| 384 | } |
| 385 | #else |
| 386 | #define http_silent_debug(l,s) do { } while (0) |
| 387 | #endif |
| 388 | |
| 389 | /* |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 390 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 391 | * CRLF. Text length is measured first, so it cannot be NULL. |
| 392 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 393 | * of headers is automatically adjusted. The number of bytes added is returned |
| 394 | * on success, otherwise <0 is returned indicating an error. |
| 395 | */ |
| 396 | int http_header_add_tail(struct buffer *b, struct http_msg *msg, |
| 397 | struct hdr_idx *hdr_idx, const char *text) |
| 398 | { |
| 399 | int bytes, len; |
| 400 | |
| 401 | len = strlen(text); |
| 402 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 403 | if (!bytes) |
| 404 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 405 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 406 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 407 | } |
| 408 | |
| 409 | /* |
| 410 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 411 | * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then |
| 412 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 413 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 414 | * of headers is automatically adjusted. The number of bytes added is returned |
| 415 | * on success, otherwise <0 is returned indicating an error. |
| 416 | */ |
| 417 | int http_header_add_tail2(struct buffer *b, struct http_msg *msg, |
| 418 | struct hdr_idx *hdr_idx, const char *text, int len) |
| 419 | { |
| 420 | int bytes; |
| 421 | |
| 422 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 423 | if (!bytes) |
| 424 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 425 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 426 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 427 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 428 | |
| 429 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 430 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 431 | * If so, returns the position of the first non-space character relative to |
| 432 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 433 | * to return a pointer to the place after the first space. Returns 0 if the |
| 434 | * header name does not match. Checks are case-insensitive. |
| 435 | */ |
| 436 | int http_header_match2(const char *hdr, const char *end, |
| 437 | const char *name, int len) |
| 438 | { |
| 439 | const char *val; |
| 440 | |
| 441 | if (hdr + len >= end) |
| 442 | return 0; |
| 443 | if (hdr[len] != ':') |
| 444 | return 0; |
| 445 | if (strncasecmp(hdr, name, len) != 0) |
| 446 | return 0; |
| 447 | val = hdr + len + 1; |
| 448 | while (val < end && HTTP_IS_SPHT(*val)) |
| 449 | val++; |
| 450 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 451 | return len + 2; /* we may replace starting from second space */ |
| 452 | return val - hdr; |
| 453 | } |
| 454 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 455 | /* Find the end of the header value contained between <s> and <e>. See RFC2616, |
| 456 | * par 2.2 for more information. Note that it requires a valid header to return |
| 457 | * a valid result. This works for headers defined as comma-separated lists. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 458 | */ |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 459 | char *find_hdr_value_end(char *s, const char *e) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 460 | { |
| 461 | int quoted, qdpair; |
| 462 | |
| 463 | quoted = qdpair = 0; |
| 464 | for (; s < e; s++) { |
| 465 | if (qdpair) qdpair = 0; |
| 466 | else if (quoted && *s == '\\') qdpair = 1; |
| 467 | else if (quoted && *s == '"') quoted = 0; |
| 468 | else if (*s == '"') quoted = 1; |
| 469 | else if (*s == ',') return s; |
| 470 | } |
| 471 | return s; |
| 472 | } |
| 473 | |
| 474 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 475 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 476 | * structure holds everything necessary to use the header and find next |
| 477 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 478 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 479 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 480 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 481 | * is NULL when searching for a new values of a header, the current header is |
| 482 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 483 | */ |
| 484 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 485 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 486 | struct hdr_ctx *ctx) |
| 487 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 488 | char *eol, *sov; |
| 489 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 490 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 491 | cur_idx = ctx->idx; |
| 492 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 493 | /* We have previously returned a value, let's search |
| 494 | * another one on the same line. |
| 495 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 496 | sol = ctx->line; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 497 | ctx->del = ctx->val + ctx->vlen; |
| 498 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 499 | eol = sol + idx->v[cur_idx].len; |
| 500 | |
| 501 | if (sov >= eol) |
| 502 | /* no more values in this header */ |
| 503 | goto next_hdr; |
| 504 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 505 | /* values remaining for this header, skip the comma but save it |
| 506 | * for later use (eg: for header deletion). |
| 507 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 508 | sov++; |
| 509 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 510 | sov++; |
| 511 | |
| 512 | goto return_hdr; |
| 513 | } |
| 514 | |
| 515 | /* first request for this header */ |
| 516 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 517 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 518 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 519 | while (cur_idx) { |
| 520 | eol = sol + idx->v[cur_idx].len; |
| 521 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 522 | if (len == 0) { |
| 523 | /* No argument was passed, we want any header. |
| 524 | * To achieve this, we simply build a fake request. */ |
| 525 | while (sol + len < eol && sol[len] != ':') |
| 526 | len++; |
| 527 | name = sol; |
| 528 | } |
| 529 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 530 | if ((len < eol - sol) && |
| 531 | (sol[len] == ':') && |
| 532 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 533 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 534 | sov = sol + len + 1; |
| 535 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 536 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 537 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 538 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 539 | ctx->prev = old_idx; |
| 540 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 541 | ctx->idx = cur_idx; |
| 542 | ctx->val = sov - sol; |
| 543 | |
| 544 | eol = find_hdr_value_end(sov, eol); |
| 545 | ctx->vlen = eol - sov; |
| 546 | return 1; |
| 547 | } |
| 548 | next_hdr: |
| 549 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 550 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 551 | cur_idx = idx->v[cur_idx].next; |
| 552 | } |
| 553 | return 0; |
| 554 | } |
| 555 | |
| 556 | int http_find_header(const char *name, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 557 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 558 | struct hdr_ctx *ctx) |
| 559 | { |
| 560 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 561 | } |
| 562 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 563 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 564 | * the http_find_header functions. The value is removed, as well as surrounding |
| 565 | * commas if any. If the removed value was alone, the whole header is removed. |
| 566 | * The ctx is always updated accordingly, as well as buffer <buf> and HTTP |
| 567 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 568 | * no more header, so any processing may stop. The ctx is always left in a form |
| 569 | * that can be handled by http_find_header2() to find next occurrence. |
| 570 | */ |
| 571 | int http_remove_header2(struct http_msg *msg, struct buffer *buf, |
| 572 | struct hdr_idx *idx, struct hdr_ctx *ctx) |
| 573 | { |
| 574 | int cur_idx = ctx->idx; |
| 575 | char *sol = ctx->line; |
| 576 | struct hdr_idx_elem *hdr; |
| 577 | int delta, skip_comma; |
| 578 | |
| 579 | if (!cur_idx) |
| 580 | return 0; |
| 581 | |
| 582 | hdr = &idx->v[cur_idx]; |
| 583 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen == hdr->len) { |
| 584 | /* This was the only value of the header, we must now remove it entirely. */ |
| 585 | delta = buffer_replace2(buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0); |
| 586 | http_msg_move_end(msg, delta); |
| 587 | idx->used--; |
| 588 | hdr->len = 0; /* unused entry */ |
| 589 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
| 590 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
| 591 | ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1; |
| 592 | ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */ |
| 593 | ctx->vlen = 0; |
| 594 | return ctx->idx; |
| 595 | } |
| 596 | |
| 597 | /* This was not the only value of this header. We have to remove between |
| 598 | * ctx->del+1 and ctx->val+ctx->vlen+1 included. If it is the last entry |
| 599 | * of the list, we remove the last separator. |
| 600 | */ |
| 601 | |
| 602 | skip_comma = (ctx->val + ctx->vlen == hdr->len) ? 0 : 1; |
| 603 | delta = buffer_replace2(buf, sol + ctx->del + skip_comma, |
| 604 | sol + ctx->val + ctx->vlen + skip_comma, |
| 605 | NULL, 0); |
| 606 | hdr->len += delta; |
| 607 | http_msg_move_end(msg, delta); |
| 608 | ctx->val = ctx->del; |
| 609 | ctx->vlen = 0; |
| 610 | return ctx->idx; |
| 611 | } |
| 612 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 613 | /* This function handles a server error at the stream interface level. The |
| 614 | * stream interface is assumed to be already in a closed state. An optional |
| 615 | * message is copied into the input buffer, and an HTTP status code stored. |
| 616 | * 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] | 617 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 618 | */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 619 | static void http_server_error(struct session *t, struct stream_interface *si, |
| 620 | int err, int finst, int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 621 | { |
Willy Tarreau | d5fd51c | 2010-01-22 14:17:47 +0100 | [diff] [blame] | 622 | buffer_auto_read(si->ob); |
| 623 | buffer_abort(si->ob); |
| 624 | buffer_auto_close(si->ob); |
| 625 | buffer_erase(si->ob); |
Willy Tarreau | 6f0aa47 | 2009-03-08 20:33:29 +0100 | [diff] [blame] | 626 | buffer_erase(si->ob); |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 627 | buffer_auto_close(si->ib); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 628 | buffer_auto_read(si->ib); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 629 | if (status > 0 && msg) { |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 630 | t->txn.status = status; |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 631 | buffer_write(si->ib, msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 632 | } |
| 633 | if (!(t->flags & SN_ERR_MASK)) |
| 634 | t->flags |= err; |
| 635 | if (!(t->flags & SN_FINST_MASK)) |
| 636 | t->flags |= finst; |
| 637 | } |
| 638 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 639 | /* This function returns the appropriate error location for the given session |
| 640 | * and message. |
| 641 | */ |
| 642 | |
| 643 | struct chunk *error_message(struct session *s, int msgnum) |
| 644 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 645 | if (s->be->errmsg[msgnum].str) |
| 646 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 647 | else if (s->fe->errmsg[msgnum].str) |
| 648 | return &s->fe->errmsg[msgnum]; |
| 649 | else |
| 650 | return &http_err_chunks[msgnum]; |
| 651 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 652 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 653 | /* |
| 654 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 655 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 656 | */ |
| 657 | static http_meth_t find_http_meth(const char *str, const int len) |
| 658 | { |
| 659 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 660 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 661 | |
| 662 | m = ((unsigned)*str - 'A'); |
| 663 | |
| 664 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 665 | for (h = http_methods[m]; h->len > 0; h++) { |
| 666 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 667 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 668 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 669 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 670 | }; |
| 671 | return HTTP_METH_OTHER; |
| 672 | } |
| 673 | return HTTP_METH_NONE; |
| 674 | |
| 675 | } |
| 676 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 677 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 678 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 679 | * It is returned otherwise. |
| 680 | */ |
| 681 | static char * |
| 682 | http_get_path(struct http_txn *txn) |
| 683 | { |
| 684 | char *ptr, *end; |
| 685 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 686 | ptr = txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 687 | end = ptr + txn->req.sl.rq.u_l; |
| 688 | |
| 689 | if (ptr >= end) |
| 690 | return NULL; |
| 691 | |
| 692 | /* RFC2616, par. 5.1.2 : |
| 693 | * Request-URI = "*" | absuri | abspath | authority |
| 694 | */ |
| 695 | |
| 696 | if (*ptr == '*') |
| 697 | return NULL; |
| 698 | |
| 699 | if (isalpha((unsigned char)*ptr)) { |
| 700 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 701 | ptr++; |
| 702 | while (ptr < end && |
| 703 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 704 | ptr++; |
| 705 | /* skip '://' */ |
| 706 | if (ptr == end || *ptr++ != ':') |
| 707 | return NULL; |
| 708 | if (ptr == end || *ptr++ != '/') |
| 709 | return NULL; |
| 710 | if (ptr == end || *ptr++ != '/') |
| 711 | return NULL; |
| 712 | } |
| 713 | /* skip [user[:passwd]@]host[:[port]] */ |
| 714 | |
| 715 | while (ptr < end && *ptr != '/') |
| 716 | ptr++; |
| 717 | |
| 718 | if (ptr == end) |
| 719 | return NULL; |
| 720 | |
| 721 | /* OK, we got the '/' ! */ |
| 722 | return ptr; |
| 723 | } |
| 724 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 725 | /* Returns a 302 for a redirectable request. This may only be called just after |
| 726 | * the stream interface has moved to SI_ST_ASS. Unprocessable requests are |
| 727 | * left unchanged and will follow normal proxy processing. |
| 728 | */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 729 | void perform_http_redirect(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 730 | { |
| 731 | struct http_txn *txn; |
| 732 | struct chunk rdr; |
| 733 | char *path; |
| 734 | int len; |
| 735 | |
| 736 | /* 1: create the response header */ |
| 737 | rdr.len = strlen(HTTP_302); |
| 738 | rdr.str = trash; |
Willy Tarreau | 59e0b0f | 2010-01-09 21:29:23 +0100 | [diff] [blame] | 739 | rdr.size = sizeof(trash); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 740 | memcpy(rdr.str, HTTP_302, rdr.len); |
| 741 | |
| 742 | /* 2: add the server's prefix */ |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 743 | if (rdr.len + s->srv->rdr_len > rdr.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 744 | return; |
| 745 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 746 | /* special prefix "/" means don't change URL */ |
| 747 | if (s->srv->rdr_len != 1 || *s->srv->rdr_pfx != '/') { |
| 748 | memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len); |
| 749 | rdr.len += s->srv->rdr_len; |
| 750 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 751 | |
| 752 | /* 3: add the request URI */ |
| 753 | txn = &s->txn; |
| 754 | path = http_get_path(txn); |
| 755 | if (!path) |
| 756 | return; |
| 757 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 758 | 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] | 759 | if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 760 | return; |
| 761 | |
| 762 | memcpy(rdr.str + rdr.len, path, len); |
| 763 | rdr.len += len; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 764 | memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23); |
| 765 | rdr.len += 23; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 766 | |
| 767 | /* prepare to return without error. */ |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame] | 768 | si->shutr(si); |
| 769 | si->shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 770 | si->err_type = SI_ET_NONE; |
| 771 | si->err_loc = NULL; |
| 772 | si->state = SI_ST_CLO; |
| 773 | |
| 774 | /* send the message */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 775 | 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] | 776 | |
| 777 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
| 778 | if (s->srv) |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 779 | srv_inc_sess_ctr(s->srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 780 | } |
| 781 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 782 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 783 | * that the server side is closed. Note that err_type is actually a |
| 784 | * bitmask, where almost only aborts may be cumulated with other |
| 785 | * values. We consider that aborted operations are more important |
| 786 | * than timeouts or errors due to the fact that nobody else in the |
| 787 | * logs might explain incomplete retries. All others should avoid |
| 788 | * being cumulated. It should normally not be possible to have multiple |
| 789 | * aborts at once, but just in case, the first one in sequence is reported. |
| 790 | */ |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 791 | void http_return_srv_error(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 792 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 793 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 794 | |
| 795 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 796 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q, |
| 797 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 798 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 799 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C, |
| 800 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 801 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 802 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q, |
| 803 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 804 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 805 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q, |
| 806 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 807 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 808 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C, |
| 809 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 810 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 811 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C, |
| 812 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 813 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 814 | http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C, |
| 815 | 500, error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 816 | } |
| 817 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 818 | extern const char sess_term_cond[8]; |
| 819 | extern const char sess_fin_state[8]; |
| 820 | extern const char *monthname[12]; |
| 821 | const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */ |
| 822 | const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown, |
| 823 | Set-cookie seen and left unchanged (passive), Set-cookie Deleted, |
| 824 | unknown, Set-cookie Rewritten */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 825 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 826 | struct pool_head *pool2_capture; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 827 | |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 828 | void http_sess_clflog(struct session *s) |
| 829 | { |
| 830 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 831 | struct proxy *fe = s->fe; |
| 832 | struct proxy *be = s->be; |
| 833 | struct proxy *prx_log; |
| 834 | struct http_txn *txn = &s->txn; |
| 835 | int tolog, level, err; |
| 836 | char *uri, *h; |
| 837 | char *svid; |
| 838 | struct tm tm; |
| 839 | static char tmpline[MAX_SYSLOG_LEN]; |
| 840 | int hdr; |
| 841 | size_t w; |
| 842 | int t_request; |
| 843 | |
| 844 | prx_log = fe; |
| 845 | err = (s->flags & (SN_ERR_MASK | SN_REDISP)) || |
| 846 | (s->conn_retries != be->conn_retries) || |
| 847 | txn->status >= 500; |
| 848 | |
| 849 | if (s->cli_addr.ss_family == AF_INET) |
| 850 | inet_ntop(AF_INET, |
| 851 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 852 | pn, sizeof(pn)); |
| 853 | else |
| 854 | inet_ntop(AF_INET6, |
| 855 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 856 | pn, sizeof(pn)); |
| 857 | |
| 858 | get_gmtime(s->logs.accept_date.tv_sec, &tm); |
| 859 | |
| 860 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 861 | tolog = fe->to_log; |
| 862 | |
| 863 | h = tmpline; |
| 864 | |
| 865 | w = snprintf(h, sizeof(tmpline), |
| 866 | "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]", |
| 867 | pn, |
| 868 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 869 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
| 870 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 871 | goto trunc; |
| 872 | h += w; |
| 873 | |
| 874 | if (h >= tmpline + sizeof(tmpline) - 4) |
| 875 | goto trunc; |
| 876 | |
| 877 | *(h++) = ' '; |
| 878 | *(h++) = '\"'; |
| 879 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 880 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 881 | '#', url_encode_map, uri); |
| 882 | *(h++) = '\"'; |
| 883 | |
| 884 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out); |
| 885 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 886 | goto trunc; |
| 887 | h += w; |
| 888 | |
| 889 | if (h >= tmpline + sizeof(tmpline) - 9) |
| 890 | goto trunc; |
| 891 | memcpy(h, " \"-\" \"-\"", 8); |
| 892 | h += 8; |
| 893 | |
| 894 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 895 | " %d %03d", |
| 896 | (s->cli_addr.ss_family == AF_INET) ? |
| 897 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 898 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
| 899 | (int)s->logs.accept_date.tv_usec/1000); |
| 900 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 901 | goto trunc; |
| 902 | h += w; |
| 903 | |
| 904 | w = strlen(fe->id); |
| 905 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 906 | goto trunc; |
| 907 | *(h++) = ' '; |
| 908 | *(h++) = '\"'; |
| 909 | memcpy(h, fe->id, w); |
| 910 | h += w; |
| 911 | *(h++) = '\"'; |
| 912 | |
| 913 | w = strlen(be->id); |
| 914 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 915 | goto trunc; |
| 916 | *(h++) = ' '; |
| 917 | *(h++) = '\"'; |
| 918 | memcpy(h, be->id, w); |
| 919 | h += w; |
| 920 | *(h++) = '\"'; |
| 921 | |
| 922 | svid = (tolog & LW_SVID) ? |
| 923 | (s->data_source != DATA_SRC_STATS) ? |
| 924 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 925 | |
| 926 | w = strlen(svid); |
| 927 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 928 | goto trunc; |
| 929 | *(h++) = ' '; |
| 930 | *(h++) = '\"'; |
| 931 | memcpy(h, svid, w); |
| 932 | h += w; |
| 933 | *(h++) = '\"'; |
| 934 | |
| 935 | t_request = -1; |
| 936 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 937 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 938 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 939 | " %d %ld %ld %ld %ld", |
| 940 | t_request, |
| 941 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
| 942 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 943 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 944 | s->logs.t_close); |
| 945 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 946 | goto trunc; |
| 947 | h += w; |
| 948 | |
| 949 | if (h >= tmpline + sizeof(tmpline) - 8) |
| 950 | goto trunc; |
| 951 | *(h++) = ' '; |
| 952 | *(h++) = '\"'; |
| 953 | *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT]; |
| 954 | *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT]; |
| 955 | *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 956 | *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-'; |
| 957 | *(h++) = '\"'; |
| 958 | |
| 959 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 960 | " %d %d %d %d %d %ld %ld", |
| 961 | actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0, |
| 962 | (s->conn_retries > 0) ? (be->conn_retries - s->conn_retries) : be->conn_retries, |
| 963 | s->logs.srv_queue_size, s->logs.prx_queue_size); |
| 964 | |
| 965 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 966 | goto trunc; |
| 967 | h += w; |
| 968 | |
| 969 | if (txn->cli_cookie) { |
| 970 | w = strlen(txn->cli_cookie); |
| 971 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 972 | goto trunc; |
| 973 | *(h++) = ' '; |
| 974 | *(h++) = '\"'; |
| 975 | memcpy(h, txn->cli_cookie, w); |
| 976 | h += w; |
| 977 | *(h++) = '\"'; |
| 978 | } else { |
| 979 | if (h >= tmpline + sizeof(tmpline) - 5) |
| 980 | goto trunc; |
| 981 | memcpy(h, " \"-\"", 4); |
| 982 | h += 4; |
| 983 | } |
| 984 | |
| 985 | if (txn->srv_cookie) { |
| 986 | w = strlen(txn->srv_cookie); |
| 987 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 988 | goto trunc; |
| 989 | *(h++) = ' '; |
| 990 | *(h++) = '\"'; |
| 991 | memcpy(h, txn->srv_cookie, w); |
| 992 | h += w; |
| 993 | *(h++) = '\"'; |
| 994 | } else { |
| 995 | if (h >= tmpline + sizeof(tmpline) - 5) |
| 996 | goto trunc; |
| 997 | memcpy(h, " \"-\"", 4); |
| 998 | h += 4; |
| 999 | } |
| 1000 | |
| 1001 | if ((fe->to_log & LW_REQHDR) && txn->req.cap) { |
| 1002 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 1003 | if (h >= sizeof (tmpline) + tmpline - 4) |
| 1004 | goto trunc; |
| 1005 | *(h++) = ' '; |
| 1006 | *(h++) = '\"'; |
| 1007 | h = encode_string(h, tmpline + sizeof(tmpline) - 2, |
| 1008 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 1009 | *(h++) = '\"'; |
| 1010 | } |
| 1011 | } |
| 1012 | |
| 1013 | if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) { |
| 1014 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 1015 | if (h >= sizeof (tmpline) + tmpline - 4) |
| 1016 | goto trunc; |
| 1017 | *(h++) = ' '; |
| 1018 | *(h++) = '\"'; |
| 1019 | h = encode_string(h, tmpline + sizeof(tmpline) - 2, |
| 1020 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 1021 | *(h++) = '\"'; |
| 1022 | } |
| 1023 | } |
| 1024 | |
| 1025 | trunc: |
| 1026 | *h = '\0'; |
| 1027 | |
| 1028 | level = LOG_INFO; |
| 1029 | if (err && (fe->options2 & PR_O2_LOGERRORS)) |
| 1030 | level = LOG_ERR; |
| 1031 | |
| 1032 | send_log(prx_log, level, "%s\n", tmpline); |
| 1033 | |
| 1034 | s->logs.logwait = 0; |
| 1035 | } |
| 1036 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1037 | /* |
| 1038 | * send a log for the session when we have enough info about it. |
| 1039 | * Will not log if the frontend has no log defined. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1040 | */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1041 | void http_sess_log(struct session *s) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1042 | { |
| 1043 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 1044 | struct proxy *fe = s->fe; |
| 1045 | struct proxy *be = s->be; |
| 1046 | struct proxy *prx_log; |
| 1047 | struct http_txn *txn = &s->txn; |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1048 | int tolog, level, err; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1049 | char *uri, *h; |
| 1050 | char *svid; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1051 | struct tm tm; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1052 | static char tmpline[MAX_SYSLOG_LEN]; |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1053 | int t_request; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1054 | int hdr; |
| 1055 | |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1056 | /* if we don't want to log normal traffic, return now */ |
| 1057 | err = (s->flags & (SN_ERR_MASK | SN_REDISP)) || |
| 1058 | (s->conn_retries != be->conn_retries) || |
| 1059 | txn->status >= 500; |
| 1060 | if (!err && (fe->options2 & PR_O2_NOLOGNORM)) |
| 1061 | return; |
| 1062 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1063 | if (fe->logfac1 < 0 && fe->logfac2 < 0) |
| 1064 | return; |
| 1065 | prx_log = fe; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1066 | |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 1067 | if (prx_log->options2 & PR_O2_CLFLOG) |
| 1068 | return http_sess_clflog(s); |
| 1069 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1070 | if (s->cli_addr.ss_family == AF_INET) |
| 1071 | inet_ntop(AF_INET, |
| 1072 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 1073 | pn, sizeof(pn)); |
| 1074 | else |
| 1075 | inet_ntop(AF_INET6, |
| 1076 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 1077 | pn, sizeof(pn)); |
| 1078 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 1079 | get_localtime(s->logs.accept_date.tv_sec, &tm); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1080 | |
| 1081 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 1082 | tolog = fe->to_log; |
| 1083 | |
| 1084 | h = tmpline; |
| 1085 | if (fe->to_log & LW_REQHDR && |
| 1086 | txn->req.cap && |
| 1087 | (h < tmpline + sizeof(tmpline) - 10)) { |
| 1088 | *(h++) = ' '; |
| 1089 | *(h++) = '{'; |
| 1090 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 1091 | if (hdr) |
| 1092 | *(h++) = '|'; |
| 1093 | if (txn->req.cap[hdr] != NULL) |
| 1094 | h = encode_string(h, tmpline + sizeof(tmpline) - 7, |
| 1095 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 1096 | } |
| 1097 | *(h++) = '}'; |
| 1098 | } |
| 1099 | |
| 1100 | if (fe->to_log & LW_RSPHDR && |
| 1101 | txn->rsp.cap && |
| 1102 | (h < tmpline + sizeof(tmpline) - 7)) { |
| 1103 | *(h++) = ' '; |
| 1104 | *(h++) = '{'; |
| 1105 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 1106 | if (hdr) |
| 1107 | *(h++) = '|'; |
| 1108 | if (txn->rsp.cap[hdr] != NULL) |
| 1109 | h = encode_string(h, tmpline + sizeof(tmpline) - 4, |
| 1110 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 1111 | } |
| 1112 | *(h++) = '}'; |
| 1113 | } |
| 1114 | |
| 1115 | if (h < tmpline + sizeof(tmpline) - 4) { |
| 1116 | *(h++) = ' '; |
| 1117 | *(h++) = '"'; |
| 1118 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 1119 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 1120 | '#', url_encode_map, uri); |
| 1121 | *(h++) = '"'; |
| 1122 | } |
| 1123 | *h = '\0'; |
| 1124 | |
| 1125 | svid = (tolog & LW_SVID) ? |
| 1126 | (s->data_source != DATA_SRC_STATS) ? |
| 1127 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 1128 | |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1129 | t_request = -1; |
| 1130 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 1131 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 1132 | |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1133 | level = LOG_INFO; |
| 1134 | if (err && (fe->options2 & PR_O2_LOGERRORS)) |
| 1135 | level = LOG_ERR; |
| 1136 | |
| 1137 | send_log(prx_log, level, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1138 | "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]" |
Willy Tarreau | 1772ece | 2009-04-03 14:49:12 +0200 | [diff] [blame] | 1139 | " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld" |
| 1140 | " %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] | 1141 | pn, |
| 1142 | (s->cli_addr.ss_family == AF_INET) ? |
| 1143 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 1144 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1145 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
Willy Tarreau | 1772ece | 2009-04-03 14:49:12 +0200 | [diff] [blame] | 1146 | 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] | 1147 | fe->id, be->id, svid, |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1148 | t_request, |
| 1149 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1150 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 1151 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 1152 | (tolog & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 1153 | txn->status, |
Willy Tarreau | 8b3977f | 2008-01-18 11:16:32 +0100 | [diff] [blame] | 1154 | (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1155 | txn->cli_cookie ? txn->cli_cookie : "-", |
| 1156 | txn->srv_cookie ? txn->srv_cookie : "-", |
| 1157 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 1158 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 1159 | (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 1160 | (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-', |
| 1161 | 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] | 1162 | (s->flags & SN_REDISP)?"+":"", |
| 1163 | (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] | 1164 | s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline); |
| 1165 | |
| 1166 | s->logs.logwait = 0; |
| 1167 | } |
| 1168 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1169 | |
| 1170 | /* |
| 1171 | * Capture headers from message starting at <som> according to header list |
| 1172 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 1173 | */ |
| 1174 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1175 | char **cap, struct cap_hdr *cap_hdr) |
| 1176 | { |
| 1177 | char *eol, *sol, *col, *sov; |
| 1178 | int cur_idx; |
| 1179 | struct cap_hdr *h; |
| 1180 | int len; |
| 1181 | |
| 1182 | sol = som + hdr_idx_first_pos(idx); |
| 1183 | cur_idx = hdr_idx_first_idx(idx); |
| 1184 | |
| 1185 | while (cur_idx) { |
| 1186 | eol = sol + idx->v[cur_idx].len; |
| 1187 | |
| 1188 | col = sol; |
| 1189 | while (col < eol && *col != ':') |
| 1190 | col++; |
| 1191 | |
| 1192 | sov = col + 1; |
| 1193 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 1194 | sov++; |
| 1195 | |
| 1196 | for (h = cap_hdr; h; h = h->next) { |
| 1197 | if ((h->namelen == col - sol) && |
| 1198 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1199 | if (cap[h->index] == NULL) |
| 1200 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1201 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1202 | |
| 1203 | if (cap[h->index] == NULL) { |
| 1204 | Alert("HTTP capture : out of memory.\n"); |
| 1205 | continue; |
| 1206 | } |
| 1207 | |
| 1208 | len = eol - sov; |
| 1209 | if (len > h->len) |
| 1210 | len = h->len; |
| 1211 | |
| 1212 | memcpy(cap[h->index], sov, len); |
| 1213 | cap[h->index][len]=0; |
| 1214 | } |
| 1215 | } |
| 1216 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1217 | cur_idx = idx->v[cur_idx].next; |
| 1218 | } |
| 1219 | } |
| 1220 | |
| 1221 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1222 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1223 | */ |
| 1224 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1225 | |
| 1226 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1227 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1228 | */ |
| 1229 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1230 | ptr++; \ |
| 1231 | if (likely(ptr < end)) \ |
| 1232 | goto good; \ |
| 1233 | else { \ |
| 1234 | state = (st); \ |
| 1235 | goto http_msg_ood; \ |
| 1236 | } \ |
| 1237 | } while (0) |
| 1238 | |
| 1239 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1240 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1241 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1242 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1243 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1244 | * will give undefined results. |
| 1245 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1246 | * and that msg->sol points to the beginning of the response. |
| 1247 | * If a complete line is found (which implies that at least one CR or LF is |
| 1248 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1249 | * returned indicating an incomplete line (which does not mean that parts have |
| 1250 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1251 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1252 | * upon next call. |
| 1253 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1254 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1255 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1256 | * 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] | 1257 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1258 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1259 | const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, |
| 1260 | unsigned int state, const char *ptr, const char *end, |
| 1261 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1262 | { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1263 | switch (state) { |
| 1264 | http_msg_rpver: |
| 1265 | case HTTP_MSG_RPVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1266 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1267 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1268 | |
| 1269 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1270 | msg->sl.st.v_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1271 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1272 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1273 | state = HTTP_MSG_ERROR; |
| 1274 | break; |
| 1275 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1276 | http_msg_rpver_sp: |
| 1277 | case HTTP_MSG_RPVER_SP: |
| 1278 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1279 | msg->sl.st.c = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1280 | goto http_msg_rpcode; |
| 1281 | } |
| 1282 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1283 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1284 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1285 | state = HTTP_MSG_ERROR; |
| 1286 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1287 | |
| 1288 | http_msg_rpcode: |
| 1289 | case HTTP_MSG_RPCODE: |
| 1290 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1291 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1292 | |
| 1293 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1294 | msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1295 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1296 | } |
| 1297 | |
| 1298 | /* so it's a CR/LF, so there is no reason phrase */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1299 | msg->sl.st.c_l = (ptr - msg_buf) - msg->som - msg->sl.st.c; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1300 | http_msg_rsp_reason: |
| 1301 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1302 | msg->sl.st.r = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1303 | msg->sl.st.r_l = 0; |
| 1304 | goto http_msg_rpline_eol; |
| 1305 | |
| 1306 | http_msg_rpcode_sp: |
| 1307 | case HTTP_MSG_RPCODE_SP: |
| 1308 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1309 | msg->sl.st.r = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1310 | goto http_msg_rpreason; |
| 1311 | } |
| 1312 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1313 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1314 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1315 | goto http_msg_rsp_reason; |
| 1316 | |
| 1317 | http_msg_rpreason: |
| 1318 | case HTTP_MSG_RPREASON: |
| 1319 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1320 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1321 | msg->sl.st.r_l = (ptr - msg_buf) - msg->som - msg->sl.st.r; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1322 | http_msg_rpline_eol: |
| 1323 | /* We have seen the end of line. Note that we do not |
| 1324 | * necessarily have the \n yet, but at least we know that we |
| 1325 | * have EITHER \r OR \n, otherwise the response would not be |
| 1326 | * complete. We can then record the response length and return |
| 1327 | * to the caller which will be able to register it. |
| 1328 | */ |
| 1329 | msg->sl.st.l = ptr - msg->sol; |
| 1330 | return ptr; |
| 1331 | |
| 1332 | #ifdef DEBUG_FULL |
| 1333 | default: |
| 1334 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1335 | exit(1); |
| 1336 | #endif |
| 1337 | } |
| 1338 | |
| 1339 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1340 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1341 | if (ret_state) |
| 1342 | *ret_state = state; |
| 1343 | if (ret_ptr) |
| 1344 | *ret_ptr = (char *)ptr; |
| 1345 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1346 | } |
| 1347 | |
| 1348 | |
| 1349 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1350 | * This function parses a request line between <ptr> and <end>, starting with |
| 1351 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1352 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1353 | * will give undefined results. |
| 1354 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1355 | * and that msg->sol points to the beginning of the request. |
| 1356 | * If a complete line is found (which implies that at least one CR or LF is |
| 1357 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1358 | * returned indicating an incomplete line (which does not mean that parts have |
| 1359 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1360 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1361 | * upon next call. |
| 1362 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1363 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1364 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1365 | * 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] | 1366 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1367 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1368 | const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, |
| 1369 | unsigned int state, const char *ptr, const char *end, |
| 1370 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1371 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1372 | switch (state) { |
| 1373 | http_msg_rqmeth: |
| 1374 | case HTTP_MSG_RQMETH: |
| 1375 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1376 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1377 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1378 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1379 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1380 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1381 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1382 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1383 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1384 | /* HTTP 0.9 request */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1385 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1386 | http_msg_req09_uri: |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1387 | msg->sl.rq.u = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1388 | http_msg_req09_uri_e: |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1389 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1390 | http_msg_req09_ver: |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1391 | msg->sl.rq.v = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1392 | msg->sl.rq.v_l = 0; |
| 1393 | goto http_msg_rqline_eol; |
| 1394 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1395 | state = HTTP_MSG_ERROR; |
| 1396 | break; |
| 1397 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1398 | http_msg_rqmeth_sp: |
| 1399 | case HTTP_MSG_RQMETH_SP: |
| 1400 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1401 | msg->sl.rq.u = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1402 | goto http_msg_rquri; |
| 1403 | } |
| 1404 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1405 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1406 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1407 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1408 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1409 | http_msg_rquri: |
| 1410 | case HTTP_MSG_RQURI: |
| 1411 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1412 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1413 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1414 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1415 | msg->sl.rq.u_l = (ptr - msg_buf) - msg->som - msg->sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1416 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1417 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1418 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1419 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1420 | goto http_msg_req09_uri_e; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1421 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1422 | http_msg_rquri_sp: |
| 1423 | case HTTP_MSG_RQURI_SP: |
| 1424 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1425 | msg->sl.rq.v = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1426 | goto http_msg_rqver; |
| 1427 | } |
| 1428 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1429 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1430 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1431 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1432 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1433 | http_msg_rqver: |
| 1434 | case HTTP_MSG_RQVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1435 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1436 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1437 | |
| 1438 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1439 | msg->sl.rq.v_l = (ptr - msg_buf) - msg->som - msg->sl.rq.v; |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1440 | http_msg_rqline_eol: |
| 1441 | /* We have seen the end of line. Note that we do not |
| 1442 | * necessarily have the \n yet, but at least we know that we |
| 1443 | * have EITHER \r OR \n, otherwise the request would not be |
| 1444 | * complete. We can then record the request length and return |
| 1445 | * to the caller which will be able to register it. |
| 1446 | */ |
| 1447 | msg->sl.rq.l = ptr - msg->sol; |
| 1448 | return ptr; |
| 1449 | } |
| 1450 | |
| 1451 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1452 | state = HTTP_MSG_ERROR; |
| 1453 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1454 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1455 | #ifdef DEBUG_FULL |
| 1456 | default: |
| 1457 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1458 | exit(1); |
| 1459 | #endif |
| 1460 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1461 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1462 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1463 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1464 | if (ret_state) |
| 1465 | *ret_state = state; |
| 1466 | if (ret_ptr) |
| 1467 | *ret_ptr = (char *)ptr; |
| 1468 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1469 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1470 | |
| 1471 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1472 | /* |
| 1473 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1474 | * depending on the initial msg->msg_state. It can be preempted everywhere |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1475 | * when data are missing and recalled at the exact same location with no |
| 1476 | * information loss. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1477 | * MSG_R[PQ]BEFORE to MSG_RPVER|MSG_RQMETH. It modifies msg->sol among other |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1478 | * fields. Note that msg->som and msg->sol will be initialized after completing |
| 1479 | * the first state, so that none of the msg pointers has to be initialized |
| 1480 | * prior to the first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1481 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1482 | void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx) |
| 1483 | { |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1484 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1485 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1486 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1487 | state = msg->msg_state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1488 | ptr = buf->lr; |
| 1489 | end = buf->r; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1490 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1491 | if (unlikely(ptr >= end)) |
| 1492 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1493 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1494 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1495 | /* |
| 1496 | * First, states that are specific to the response only. |
| 1497 | * We check them first so that request and headers are |
| 1498 | * closer to each other (accessed more often). |
| 1499 | */ |
| 1500 | http_msg_rpbefore: |
| 1501 | case HTTP_MSG_RPBEFORE: |
| 1502 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1503 | /* we have a start of message, but we have to check |
| 1504 | * first if we need to remove some CRLF. We can only |
| 1505 | * do this when send_max=0. |
| 1506 | */ |
| 1507 | char *beg = buf->w + buf->send_max; |
| 1508 | if (beg >= buf->data + buf->size) |
| 1509 | beg -= buf->size; |
| 1510 | if (unlikely(ptr != beg)) { |
| 1511 | if (buf->send_max) |
| 1512 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1513 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1514 | buffer_ignore(buf, ptr - beg); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1515 | } |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1516 | msg->som = ptr - buf->data; |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 1517 | msg->sol = ptr; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1518 | hdr_idx_init(idx); |
| 1519 | state = HTTP_MSG_RPVER; |
| 1520 | goto http_msg_rpver; |
| 1521 | } |
| 1522 | |
| 1523 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1524 | goto http_msg_invalid; |
| 1525 | |
| 1526 | if (unlikely(*ptr == '\n')) |
| 1527 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1528 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1529 | /* stop here */ |
| 1530 | |
| 1531 | http_msg_rpbefore_cr: |
| 1532 | case HTTP_MSG_RPBEFORE_CR: |
| 1533 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1534 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1535 | /* stop here */ |
| 1536 | |
| 1537 | http_msg_rpver: |
| 1538 | case HTTP_MSG_RPVER: |
| 1539 | case HTTP_MSG_RPVER_SP: |
| 1540 | case HTTP_MSG_RPCODE: |
| 1541 | case HTTP_MSG_RPCODE_SP: |
| 1542 | case HTTP_MSG_RPREASON: |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1543 | ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1544 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1545 | if (unlikely(!ptr)) |
| 1546 | return; |
| 1547 | |
| 1548 | /* we have a full response and we know that we have either a CR |
| 1549 | * or an LF at <ptr>. |
| 1550 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1551 | //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] | 1552 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1553 | |
| 1554 | msg->sol = ptr; |
| 1555 | if (likely(*ptr == '\r')) |
| 1556 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1557 | goto http_msg_rpline_end; |
| 1558 | |
| 1559 | http_msg_rpline_end: |
| 1560 | case HTTP_MSG_RPLINE_END: |
| 1561 | /* msg->sol must point to the first of CR or LF. */ |
| 1562 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1563 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1564 | /* stop here */ |
| 1565 | |
| 1566 | /* |
| 1567 | * Second, states that are specific to the request only |
| 1568 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1569 | http_msg_rqbefore: |
| 1570 | case HTTP_MSG_RQBEFORE: |
| 1571 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1572 | /* we have a start of message, but we have to check |
| 1573 | * first if we need to remove some CRLF. We can only |
| 1574 | * do this when send_max=0. |
| 1575 | */ |
| 1576 | char *beg = buf->w + buf->send_max; |
| 1577 | if (beg >= buf->data + buf->size) |
| 1578 | beg -= buf->size; |
| 1579 | if (likely(ptr != beg)) { |
| 1580 | if (buf->send_max) |
| 1581 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1582 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1583 | buffer_ignore(buf, ptr - beg); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1584 | } |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1585 | msg->som = ptr - buf->data; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1586 | msg->sol = ptr; |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1587 | /* we will need this when keep-alive will be supported |
| 1588 | hdr_idx_init(idx); |
| 1589 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1590 | state = HTTP_MSG_RQMETH; |
| 1591 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1592 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1593 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1594 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1595 | goto http_msg_invalid; |
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 | if (unlikely(*ptr == '\n')) |
| 1598 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1599 | 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] | 1600 | /* stop here */ |
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_rqbefore_cr: |
| 1603 | case HTTP_MSG_RQBEFORE_CR: |
| 1604 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1605 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1606 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1607 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1608 | http_msg_rqmeth: |
| 1609 | case HTTP_MSG_RQMETH: |
| 1610 | case HTTP_MSG_RQMETH_SP: |
| 1611 | case HTTP_MSG_RQURI: |
| 1612 | case HTTP_MSG_RQURI_SP: |
| 1613 | case HTTP_MSG_RQVER: |
| 1614 | ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1615 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1616 | if (unlikely(!ptr)) |
| 1617 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1618 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1619 | /* we have a full request and we know that we have either a CR |
| 1620 | * or an LF at <ptr>. |
| 1621 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1622 | //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] | 1623 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1624 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1625 | msg->sol = ptr; |
| 1626 | if (likely(*ptr == '\r')) |
| 1627 | 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] | 1628 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1629 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1630 | http_msg_rqline_end: |
| 1631 | case HTTP_MSG_RQLINE_END: |
| 1632 | /* check for HTTP/0.9 request : no version information available. |
| 1633 | * msg->sol must point to the first of CR or LF. |
| 1634 | */ |
| 1635 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1636 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1637 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1638 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1639 | 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] | 1640 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1641 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1642 | /* |
| 1643 | * Common states below |
| 1644 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1645 | http_msg_hdr_first: |
| 1646 | case HTTP_MSG_HDR_FIRST: |
| 1647 | msg->sol = ptr; |
| 1648 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1649 | goto http_msg_hdr_name; |
| 1650 | } |
| 1651 | |
| 1652 | if (likely(*ptr == '\r')) |
| 1653 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1654 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1655 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1656 | http_msg_hdr_name: |
| 1657 | case HTTP_MSG_HDR_NAME: |
| 1658 | /* assumes msg->sol points to the first char */ |
| 1659 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1660 | 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] | 1661 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1662 | if (likely(*ptr == ':')) { |
| 1663 | msg->col = ptr - buf->data; |
| 1664 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| 1665 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1666 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1667 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1668 | goto http_msg_invalid; |
| 1669 | |
| 1670 | if (msg->err_pos == -1) /* capture error pointer */ |
| 1671 | msg->err_pos = ptr - buf->data; /* >= 0 now */ |
| 1672 | |
| 1673 | /* and we still accept this non-token character */ |
| 1674 | 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] | 1675 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1676 | http_msg_hdr_l1_sp: |
| 1677 | case HTTP_MSG_HDR_L1_SP: |
| 1678 | /* assumes msg->sol points to the first char and msg->col to the colon */ |
| 1679 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1680 | 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] | 1681 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1682 | /* header value can be basically anything except CR/LF */ |
| 1683 | msg->sov = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1684 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1685 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1686 | goto http_msg_hdr_val; |
| 1687 | } |
| 1688 | |
| 1689 | if (likely(*ptr == '\r')) |
| 1690 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1691 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1692 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1693 | http_msg_hdr_l1_lf: |
| 1694 | case HTTP_MSG_HDR_L1_LF: |
| 1695 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1696 | 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] | 1697 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1698 | http_msg_hdr_l1_lws: |
| 1699 | case HTTP_MSG_HDR_L1_LWS: |
| 1700 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1701 | /* replace HT,CR,LF with spaces */ |
| 1702 | for (; buf->data+msg->sov < ptr; msg->sov++) |
| 1703 | buf->data[msg->sov] = ' '; |
| 1704 | goto http_msg_hdr_l1_sp; |
| 1705 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1706 | /* we had a header consisting only in spaces ! */ |
| 1707 | msg->eol = buf->data + msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1708 | goto http_msg_complete_header; |
| 1709 | |
| 1710 | http_msg_hdr_val: |
| 1711 | case HTTP_MSG_HDR_VAL: |
| 1712 | /* assumes msg->sol points to the first char, msg->col to the |
| 1713 | * colon, and msg->sov points to the first character of the |
| 1714 | * value. |
| 1715 | */ |
| 1716 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1717 | 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] | 1718 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1719 | msg->eol = ptr; |
| 1720 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1721 | * real header end in case it ends with lots of LWS, but is this |
| 1722 | * really needed ? |
| 1723 | */ |
| 1724 | if (likely(*ptr == '\r')) |
| 1725 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1726 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1727 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1728 | http_msg_hdr_l2_lf: |
| 1729 | case HTTP_MSG_HDR_L2_LF: |
| 1730 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1731 | 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] | 1732 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1733 | http_msg_hdr_l2_lws: |
| 1734 | case HTTP_MSG_HDR_L2_LWS: |
| 1735 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1736 | /* LWS: replace HT,CR,LF with spaces */ |
| 1737 | for (; msg->eol < ptr; msg->eol++) |
| 1738 | *msg->eol = ' '; |
| 1739 | goto http_msg_hdr_val; |
| 1740 | } |
| 1741 | http_msg_complete_header: |
| 1742 | /* |
| 1743 | * It was a new header, so the last one is finished. |
| 1744 | * Assumes msg->sol points to the first char, msg->col to the |
| 1745 | * colon, msg->sov points to the first character of the value |
| 1746 | * and msg->eol to the first CR or LF so we know how the line |
| 1747 | * ends. We insert last header into the index. |
| 1748 | */ |
| 1749 | /* |
| 1750 | fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol); |
| 1751 | write(2, msg->sol, msg->eol-msg->sol); |
| 1752 | fprintf(stderr,"\n"); |
| 1753 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1754 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1755 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r', |
| 1756 | idx, idx->tail) < 0)) |
| 1757 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1758 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1759 | msg->sol = ptr; |
| 1760 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1761 | goto http_msg_hdr_name; |
| 1762 | } |
| 1763 | |
| 1764 | if (likely(*ptr == '\r')) |
| 1765 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1766 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1767 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1768 | http_msg_last_lf: |
| 1769 | case HTTP_MSG_LAST_LF: |
| 1770 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1771 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1772 | ptr++; |
| 1773 | buf->lr = ptr; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1774 | msg->col = msg->sov = buf->lr - buf->data; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1775 | msg->eoh = msg->sol - buf->data; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1776 | msg->sol = buf->data + msg->som; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1777 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1778 | return; |
| 1779 | #ifdef DEBUG_FULL |
| 1780 | default: |
| 1781 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1782 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1783 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1784 | } |
| 1785 | http_msg_ood: |
| 1786 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1787 | msg->msg_state = state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1788 | buf->lr = ptr; |
| 1789 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1790 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1791 | http_msg_invalid: |
| 1792 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1793 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1794 | buf->lr = ptr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1795 | return; |
| 1796 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1797 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1798 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1799 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1800 | * nothing is done and 1 is returned. |
| 1801 | */ |
| 1802 | static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn) |
| 1803 | { |
| 1804 | int delta; |
| 1805 | char *cur_end; |
| 1806 | |
| 1807 | if (msg->sl.rq.v_l != 0) |
| 1808 | return 1; |
| 1809 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1810 | cur_end = msg->sol + msg->sl.rq.l; |
| 1811 | delta = 0; |
| 1812 | |
| 1813 | if (msg->sl.rq.u_l == 0) { |
| 1814 | /* if no URI was set, add "/" */ |
| 1815 | delta = buffer_replace2(req, cur_end, cur_end, " /", 2); |
| 1816 | cur_end += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1817 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1818 | } |
| 1819 | /* add HTTP version */ |
| 1820 | delta = buffer_replace2(req, cur_end, cur_end, " HTTP/1.0\r\n", 11); |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1821 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1822 | cur_end += delta; |
| 1823 | cur_end = (char *)http_parse_reqline(msg, req->data, |
| 1824 | HTTP_MSG_RQMETH, |
| 1825 | msg->sol, cur_end + 1, |
| 1826 | NULL, NULL); |
| 1827 | if (unlikely(!cur_end)) |
| 1828 | return 0; |
| 1829 | |
| 1830 | /* we have a full HTTP/1.0 request now and we know that |
| 1831 | * we have either a CR or an LF at <ptr>. |
| 1832 | */ |
| 1833 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1834 | return 1; |
| 1835 | } |
| 1836 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1837 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
| 1838 | * and "keep-alive" values. If a buffer is provided and we already know that |
| 1839 | * some headers may safely be removed, we remove them now. The <to_del> flags |
| 1840 | * are used for that : |
| 1841 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1842 | * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1). |
| 1843 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1844 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1845 | * harmless combinations may be removed. Do not call that after changes have |
| 1846 | * been processed. If unused, the buffer can be NULL, and no data will be |
| 1847 | * changed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1848 | */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1849 | void http_parse_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int to_del) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1850 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1851 | struct hdr_ctx ctx; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1852 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1853 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1854 | return; |
| 1855 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1856 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1857 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1858 | while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1859 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1860 | txn->flags |= TX_HDR_CONN_KAL; |
| 1861 | if ((to_del & 2) && buf) |
| 1862 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
| 1863 | else |
| 1864 | txn->flags |= TX_CON_KAL_SET; |
| 1865 | } |
| 1866 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1867 | txn->flags |= TX_HDR_CONN_CLO; |
| 1868 | if ((to_del & 1) && buf) |
| 1869 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
| 1870 | else |
| 1871 | txn->flags |= TX_CON_CLO_SET; |
| 1872 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1873 | } |
| 1874 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1875 | txn->flags |= TX_HDR_CONN_PRS; |
| 1876 | return; |
| 1877 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1878 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1879 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 1880 | * added depending on the <wanted> flags, which are exclusively composed of |
| 1881 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 1882 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 1883 | */ |
| 1884 | void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int wanted) |
| 1885 | { |
| 1886 | struct hdr_ctx ctx; |
| 1887 | |
| 1888 | ctx.idx = 0; |
| 1889 | |
| 1890 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
| 1891 | while (http_find_header2("Connection", 10, msg->sol, &txn->hdr_idx, &ctx)) { |
| 1892 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1893 | if (wanted & TX_CON_KAL_SET) |
| 1894 | txn->flags |= TX_CON_KAL_SET; |
| 1895 | else |
| 1896 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1897 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1898 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1899 | if (wanted & TX_CON_CLO_SET) |
| 1900 | txn->flags |= TX_CON_CLO_SET; |
| 1901 | else |
| 1902 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 1903 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1904 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1905 | |
| 1906 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 1907 | return; |
| 1908 | |
| 1909 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 1910 | txn->flags |= TX_CON_CLO_SET; |
| 1911 | http_header_add_tail2(buf, msg, &txn->hdr_idx, "Connection: close", 17); |
| 1912 | } |
| 1913 | |
| 1914 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 1915 | txn->flags |= TX_CON_KAL_SET; |
| 1916 | http_header_add_tail2(buf, msg, &txn->hdr_idx, "Connection: keep-alive", 22); |
| 1917 | } |
| 1918 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1919 | } |
| 1920 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1921 | /* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the |
| 1922 | * first byte of body, and increments msg->sov by the number of bytes parsed, |
| 1923 | * so that we know we can forward between ->som and ->sov. Note that due to |
| 1924 | * possible wrapping at the end of the buffer, it is possible that msg->sov is |
| 1925 | * lower than msg->som. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1926 | * Return >0 on success, 0 when some data is missing, <0 on error. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1927 | * Note: this function is designed to parse wrapped CRLF at the end of the buffer. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1928 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1929 | int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1930 | { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1931 | char *ptr = buf->lr; |
| 1932 | char *end = buf->data + buf->size; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1933 | unsigned int chunk = 0; |
| 1934 | |
| 1935 | /* The chunk size is in the following form, though we are only |
| 1936 | * interested in the size and CRLF : |
| 1937 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 1938 | */ |
| 1939 | while (1) { |
| 1940 | int c; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1941 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1942 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1943 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1944 | if (c < 0) /* not a hex digit anymore */ |
| 1945 | break; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1946 | if (++ptr >= end) |
| 1947 | ptr = buf->data; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1948 | if (chunk & 0xF000000) /* overflow will occur */ |
| 1949 | return -1; |
| 1950 | chunk = (chunk << 4) + c; |
| 1951 | } |
| 1952 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1953 | /* empty size not allowed */ |
| 1954 | if (ptr == buf->lr) |
| 1955 | return -1; |
| 1956 | |
| 1957 | while (http_is_spht[(unsigned char)*ptr]) { |
| 1958 | if (++ptr >= end) |
| 1959 | ptr = buf->data; |
| 1960 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1961 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1962 | } |
| 1963 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1964 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 1965 | * for the end of chunk size. |
| 1966 | */ |
| 1967 | while (1) { |
| 1968 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1969 | /* we now have a CR or an LF at ptr */ |
| 1970 | if (likely(*ptr == '\r')) { |
| 1971 | if (++ptr >= end) |
| 1972 | ptr = buf->data; |
| 1973 | if (ptr == buf->r) |
| 1974 | return 0; |
| 1975 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1976 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1977 | if (*ptr != '\n') |
| 1978 | return -1; |
| 1979 | if (++ptr >= end) |
| 1980 | ptr = buf->data; |
| 1981 | /* done */ |
| 1982 | break; |
| 1983 | } |
| 1984 | else if (*ptr == ';') { |
| 1985 | /* chunk extension, ends at next CRLF */ |
| 1986 | if (++ptr >= end) |
| 1987 | ptr = buf->data; |
| 1988 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1989 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1990 | |
| 1991 | while (!HTTP_IS_CRLF(*ptr)) { |
| 1992 | if (++ptr >= end) |
| 1993 | ptr = buf->data; |
| 1994 | if (ptr == buf->r) |
| 1995 | return 0; |
| 1996 | } |
| 1997 | /* we have a CRLF now, loop above */ |
| 1998 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1999 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2000 | else |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2001 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2002 | } |
| 2003 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2004 | /* OK we found our CRLF and now <ptr> points to the next byte, |
| 2005 | * which may or may not be present. We save that into ->lr and |
| 2006 | * ->sov. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2007 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2008 | msg->sov += ptr - buf->lr; |
| 2009 | buf->lr = ptr; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2010 | msg->hdr_content_len = chunk; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2011 | msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2012 | return 1; |
| 2013 | } |
| 2014 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2015 | /* This function skips trailers in the buffer <buf> associated with HTTP |
| 2016 | * message <msg>. The first visited position is buf->lr. If the end of |
| 2017 | * the trailers is found, it is automatically scheduled to be forwarded, |
| 2018 | * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0. |
| 2019 | * If not enough data are available, the function does not change anything |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2020 | * except maybe buf->lr and msg->sov if it could parse some lines, and returns |
| 2021 | * zero. If a parse error is encountered, the function returns < 0 and does not |
| 2022 | * change anything except maybe buf->lr and msg->sov. Note that the message |
| 2023 | * must already be in HTTP_MSG_TRAILERS state before calling this function, |
| 2024 | * which implies that all non-trailers data have already been scheduled for |
| 2025 | * forwarding, and that the difference between msg->som and msg->sov exactly |
| 2026 | * matches the length of trailers already parsed and not forwarded. It is also |
| 2027 | * important to note that this function is designed to be able to parse wrapped |
| 2028 | * headers at end of buffer. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2029 | */ |
| 2030 | int http_forward_trailers(struct buffer *buf, struct http_msg *msg) |
| 2031 | { |
| 2032 | /* we have buf->lr which points to next line. Look for CRLF. */ |
| 2033 | while (1) { |
| 2034 | char *p1 = NULL, *p2 = NULL; |
| 2035 | char *ptr = buf->lr; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2036 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2037 | |
| 2038 | /* scan current line and stop at LF or CRLF */ |
| 2039 | while (1) { |
| 2040 | if (ptr == buf->r) |
| 2041 | return 0; |
| 2042 | |
| 2043 | if (*ptr == '\n') { |
| 2044 | if (!p1) |
| 2045 | p1 = ptr; |
| 2046 | p2 = ptr; |
| 2047 | break; |
| 2048 | } |
| 2049 | |
| 2050 | if (*ptr == '\r') { |
| 2051 | if (p1) |
| 2052 | return -1; |
| 2053 | p1 = ptr; |
| 2054 | } |
| 2055 | |
| 2056 | ptr++; |
| 2057 | if (ptr >= buf->data + buf->size) |
| 2058 | ptr = buf->data; |
| 2059 | } |
| 2060 | |
| 2061 | /* after LF; point to beginning of next line */ |
| 2062 | p2++; |
| 2063 | if (p2 >= buf->data + buf->size) |
| 2064 | p2 = buf->data; |
| 2065 | |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2066 | bytes = p2 - buf->lr; |
| 2067 | if (bytes < 0) |
| 2068 | bytes += buf->size; |
| 2069 | |
| 2070 | /* schedule this line for forwarding */ |
| 2071 | msg->sov += bytes; |
| 2072 | if (msg->sov >= buf->size) |
| 2073 | msg->sov -= buf->size; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2074 | |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2075 | if (p1 == buf->lr) { |
| 2076 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 2077 | * Everything was scheduled for forwarding, there's nothing |
| 2078 | * left from this message. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 2079 | */ |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2080 | buf->lr = p2; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2081 | msg->msg_state = HTTP_MSG_DONE; |
| 2082 | return 1; |
| 2083 | } |
| 2084 | /* OK, next line then */ |
| 2085 | buf->lr = p2; |
| 2086 | } |
| 2087 | } |
| 2088 | |
| 2089 | /* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or |
| 2090 | * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov, |
| 2091 | * ->som, buf->lr in order to include this part into the next forwarding phase. |
| 2092 | * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If |
| 2093 | * not enough data are available, the function does not change anything and |
| 2094 | * returns zero. If a parse error is encountered, the function returns < 0 and |
| 2095 | * does not change anything. Note: this function is designed to parse wrapped |
| 2096 | * CRLF at the end of the buffer. |
| 2097 | */ |
| 2098 | int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg) |
| 2099 | { |
| 2100 | char *ptr; |
| 2101 | int bytes; |
| 2102 | |
| 2103 | /* NB: we'll check data availabilty at the end. It's not a |
| 2104 | * problem because whatever we match first will be checked |
| 2105 | * against the correct length. |
| 2106 | */ |
| 2107 | bytes = 1; |
| 2108 | ptr = buf->lr; |
| 2109 | if (*ptr == '\r') { |
| 2110 | bytes++; |
| 2111 | ptr++; |
| 2112 | if (ptr >= buf->data + buf->size) |
| 2113 | ptr = buf->data; |
| 2114 | } |
| 2115 | |
| 2116 | if (buf->l < bytes) |
| 2117 | return 0; |
| 2118 | |
| 2119 | if (*ptr != '\n') |
| 2120 | return -1; |
| 2121 | |
| 2122 | ptr++; |
| 2123 | if (ptr >= buf->data + buf->size) |
| 2124 | ptr = buf->data; |
| 2125 | buf->lr = ptr; |
| 2126 | /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */ |
| 2127 | msg->sov = ptr - buf->data; |
| 2128 | msg->som = msg->sov - bytes; |
| 2129 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 2130 | return 1; |
| 2131 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2132 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2133 | void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg) |
| 2134 | { |
| 2135 | char *end = buf->data + buf->size; |
| 2136 | int off = buf->data + buf->size - buf->w; |
| 2137 | |
| 2138 | /* two possible cases : |
| 2139 | * - the buffer is in one contiguous block, we move it in-place |
| 2140 | * - the buffer is in two blocks, we move it via the trash |
| 2141 | */ |
| 2142 | if (buf->l) { |
| 2143 | int block1 = buf->l; |
| 2144 | int block2 = 0; |
| 2145 | if (buf->r <= buf->w) { |
| 2146 | /* non-contiguous block */ |
| 2147 | block1 = buf->data + buf->size - buf->w; |
| 2148 | block2 = buf->r - buf->data; |
| 2149 | } |
| 2150 | if (block2) |
| 2151 | memcpy(trash, buf->data, block2); |
| 2152 | memmove(buf->data, buf->w, block1); |
| 2153 | if (block2) |
| 2154 | memcpy(buf->data + block1, trash, block2); |
| 2155 | } |
| 2156 | |
| 2157 | /* adjust all known pointers */ |
| 2158 | buf->w = buf->data; |
| 2159 | buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size; |
| 2160 | buf->r += off; if (buf->r >= end) buf->r -= buf->size; |
| 2161 | msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size; |
| 2162 | msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size; |
| 2163 | |
| 2164 | /* adjust relative pointers */ |
| 2165 | msg->som = 0; |
| 2166 | msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size; |
| 2167 | msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size; |
| 2168 | msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size; |
| 2169 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2170 | if (msg->err_pos >= 0) { |
| 2171 | msg->err_pos += off; |
| 2172 | if (msg->err_pos >= buf->size) |
| 2173 | msg->err_pos -= buf->size; |
| 2174 | } |
| 2175 | |
| 2176 | buf->flags &= ~BF_FULL; |
| 2177 | if (buf->l >= buffer_max_len(buf)) |
| 2178 | buf->flags |= BF_FULL; |
| 2179 | } |
| 2180 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2181 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2182 | * processing can continue on next analysers, or zero if it either needs more |
| 2183 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 2184 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers |
| 2185 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2186 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2187 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2188 | 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] | 2189 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2190 | /* |
| 2191 | * We will parse the partial (or complete) lines. |
| 2192 | * We will check the request syntax, and also join multi-line |
| 2193 | * headers. An index of all the lines will be elaborated while |
| 2194 | * parsing. |
| 2195 | * |
| 2196 | * For the parsing, we use a 28 states FSM. |
| 2197 | * |
| 2198 | * Here is the information we currently have : |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 2199 | * req->data + msg->som = beginning of request |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2200 | * req->data + msg->eoh = end of processed headers / start of current one |
| 2201 | * msg->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2202 | * req->lr = first non-visited byte |
| 2203 | * req->r = end of data |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2204 | * |
| 2205 | * At end of parsing, we may perform a capture of the error (if any), and |
| 2206 | * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE). |
| 2207 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2208 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2209 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2210 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2211 | int cur_idx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2212 | int use_close_only; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2213 | struct http_txn *txn = &s->txn; |
| 2214 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2215 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2216 | |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2217 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 2218 | now_ms, __FUNCTION__, |
| 2219 | s, |
| 2220 | req, |
| 2221 | req->rex, req->wex, |
| 2222 | req->flags, |
| 2223 | req->l, |
| 2224 | req->analysers); |
| 2225 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2226 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2227 | s->srv_error = http_return_srv_error; |
| 2228 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2229 | /* There's a protected area at the end of the buffer for rewriting |
| 2230 | * purposes. We don't want to start to parse the request if the |
| 2231 | * protected area is affected, because we may have to move processed |
| 2232 | * data later, which is much more complicated. |
| 2233 | */ |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2234 | if (req->l && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2235 | if ((txn->flags & TX_NOT_FIRST) && |
| 2236 | unlikely((req->flags & BF_FULL) || |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2237 | req->r < req->lr || |
| 2238 | req->r > req->data + req->size - global.tune.maxrewrite)) { |
| 2239 | if (req->send_max) { |
| 2240 | /* some data has still not left the buffer, wake us once that's done */ |
| 2241 | buffer_dont_connect(req); |
| 2242 | req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 2243 | return 0; |
| 2244 | } |
| 2245 | if (req->l <= req->size - global.tune.maxrewrite) |
| 2246 | http_buffer_heavy_realign(req, msg); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2247 | } |
| 2248 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2249 | /* Note that we have the same problem with the response ; we |
| 2250 | * may want to send a redirect, error or anything which requires |
| 2251 | * some spare space. So we'll ensure that we have at least |
| 2252 | * maxrewrite bytes available in the response buffer before |
| 2253 | * processing that one. This will only affect pipelined |
| 2254 | * keep-alive requests. |
| 2255 | */ |
| 2256 | if ((txn->flags & TX_NOT_FIRST) && |
| 2257 | unlikely((s->rep->flags & BF_FULL) || |
| 2258 | s->rep->r < s->rep->lr || |
| 2259 | s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) { |
| 2260 | if (s->rep->send_max) { |
| 2261 | /* don't let a connection request be initiated */ |
| 2262 | buffer_dont_connect(req); |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2263 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2264 | return 0; |
| 2265 | } |
| 2266 | } |
| 2267 | |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2268 | if (likely(req->lr < req->r)) |
| 2269 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2270 | } |
| 2271 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2272 | /* 1: we might have to print this header in debug mode */ |
| 2273 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2274 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2275 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2276 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2277 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2278 | sol = msg->sol; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2279 | eol = sol + msg->sl.rq.l; |
| 2280 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2281 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2282 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2283 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2284 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2285 | while (cur_idx) { |
| 2286 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2287 | debug_hdr("clihdr", s, sol, eol); |
| 2288 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2289 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2290 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2291 | } |
| 2292 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2293 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2294 | /* |
| 2295 | * Now we quickly check if we have found a full valid request. |
| 2296 | * If not so, we check the FD and buffer states before leaving. |
| 2297 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2298 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2299 | * requests are checked first. When waiting for a second request |
| 2300 | * on a keep-alive session, if we encounter and error, close, t/o, |
| 2301 | * we note the error in the session flags but don't set any state. |
| 2302 | * Since the error will be noted there, it will not be counted by |
| 2303 | * process_session() as a frontend error. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2304 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2305 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2306 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2307 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2308 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2309 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2310 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
| 2311 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2312 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2313 | /* 1: Since we are in header mode, if there's no space |
| 2314 | * left for headers, we won't be able to free more |
| 2315 | * later, so the session will never terminate. We |
| 2316 | * must terminate it now. |
| 2317 | */ |
| 2318 | if (unlikely(req->flags & BF_FULL)) { |
| 2319 | /* FIXME: check if URI is set and return Status |
| 2320 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2321 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2322 | goto return_bad_req; |
| 2323 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2324 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2325 | /* 2: have we encountered a read error ? */ |
| 2326 | else if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2327 | if (!(s->flags & SN_ERR_MASK)) |
| 2328 | s->flags |= SN_ERR_CLICL; |
| 2329 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2330 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2331 | goto failed_keep_alive; |
| 2332 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2333 | /* we cannot return any message on error */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2334 | if (msg->err_pos >= 0) |
| 2335 | 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] | 2336 | msg->msg_state = HTTP_MSG_ERROR; |
| 2337 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2338 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2339 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2340 | if (s->listener->counters) |
| 2341 | s->listener->counters->failed_req++; |
| 2342 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2343 | if (!(s->flags & SN_FINST_MASK)) |
| 2344 | s->flags |= SN_FINST_R; |
| 2345 | return 0; |
| 2346 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2347 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2348 | /* 3: has the read timeout expired ? */ |
| 2349 | else if (req->flags & BF_READ_TIMEOUT || tick_is_expired(req->analyse_exp, now_ms)) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2350 | if (!(s->flags & SN_ERR_MASK)) |
| 2351 | s->flags |= SN_ERR_CLITO; |
| 2352 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2353 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2354 | goto failed_keep_alive; |
| 2355 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2356 | /* read timeout : give up with an error message. */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2357 | if (msg->err_pos >= 0) |
| 2358 | 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] | 2359 | txn->status = 408; |
| 2360 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
| 2361 | msg->msg_state = HTTP_MSG_ERROR; |
| 2362 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2363 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2364 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2365 | if (s->listener->counters) |
| 2366 | s->listener->counters->failed_req++; |
| 2367 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2368 | if (!(s->flags & SN_FINST_MASK)) |
| 2369 | s->flags |= SN_FINST_R; |
| 2370 | return 0; |
| 2371 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2372 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2373 | /* 4: have we encountered a close ? */ |
| 2374 | else if (req->flags & BF_SHUTR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2375 | if (!(s->flags & SN_ERR_MASK)) |
| 2376 | s->flags |= SN_ERR_CLICL; |
| 2377 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2378 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2379 | goto failed_keep_alive; |
| 2380 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2381 | if (msg->err_pos >= 0) |
| 2382 | 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] | 2383 | txn->status = 400; |
| 2384 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 2385 | msg->msg_state = HTTP_MSG_ERROR; |
| 2386 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2387 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2388 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2389 | if (s->listener->counters) |
| 2390 | s->listener->counters->failed_req++; |
| 2391 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2392 | if (!(s->flags & SN_FINST_MASK)) |
| 2393 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2394 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2395 | } |
| 2396 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2397 | buffer_dont_connect(req); |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2398 | req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2399 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2400 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2401 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2402 | /* If the client starts to talk, let's fall back to |
| 2403 | * request timeout processing. |
| 2404 | */ |
| 2405 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2406 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2407 | } |
| 2408 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2409 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2410 | if (!tick_isset(req->analyse_exp)) { |
| 2411 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2412 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2413 | tick_isset(s->be->timeout.httpka)) |
| 2414 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2415 | else |
| 2416 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2417 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2418 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2419 | /* we're not ready yet */ |
| 2420 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2421 | |
| 2422 | failed_keep_alive: |
| 2423 | /* Here we process low-level errors for keep-alive requests. In |
| 2424 | * short, if the request is not the first one and it experiences |
| 2425 | * a timeout, read error or shutdown, we just silently close so |
| 2426 | * that the client can try again. |
| 2427 | */ |
| 2428 | txn->status = 0; |
| 2429 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 2430 | req->analysers = 0; |
| 2431 | s->logs.logwait = 0; |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2432 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 2433 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2434 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2435 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2436 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2437 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2438 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 2439 | * later. At this point, we have the last CRLF at req->data + msg->eoh. |
| 2440 | * If the request is in HTTP/0.9 form, the rule is still true, and eoh |
| 2441 | * points to the CRLF of the request line. req->lr points to the first |
| 2442 | * byte after the last LF. msg->col and msg->sov point to the first |
| 2443 | * byte of data. msg->eol cannot be trusted because it may have been |
| 2444 | * left uninitialized (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2445 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2446 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2447 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2448 | /* kill the pending keep-alive timeout */ |
| 2449 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2450 | req->analyse_exp = TICK_ETERNITY; |
| 2451 | } |
| 2452 | |
| 2453 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2454 | /* Maybe we found in invalid header name while we were configured not |
| 2455 | * to block on that, so we have to capture it now. |
| 2456 | */ |
| 2457 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2458 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
| 2459 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2460 | /* |
| 2461 | * 1: identify the method |
| 2462 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2463 | txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2464 | |
| 2465 | /* we can make use of server redirect on GET and HEAD */ |
| 2466 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2467 | s->flags |= SN_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2468 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2469 | /* |
| 2470 | * 2: check if the URI matches the monitor_uri. |
| 2471 | * We have to do this for every request which gets in, because |
| 2472 | * the monitor-uri is defined by the frontend. |
| 2473 | */ |
| 2474 | if (unlikely((s->fe->monitor_uri_len != 0) && |
| 2475 | (s->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2476 | !memcmp(msg->sol + msg->sl.rq.u, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2477 | s->fe->monitor_uri, |
| 2478 | s->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2479 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2480 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2481 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2482 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2483 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2484 | s->flags |= SN_MONITOR; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2485 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2486 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2487 | list_for_each_entry(cond, &s->fe->mon_fail_cond, list) { |
| 2488 | 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] | 2489 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2490 | ret = acl_pass(ret); |
| 2491 | if (cond->pol == ACL_COND_UNLESS) |
| 2492 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2493 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2494 | if (ret) { |
| 2495 | /* we fail this request, let's return 503 service unavail */ |
| 2496 | txn->status = 503; |
| 2497 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503)); |
| 2498 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2499 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2500 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2501 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2502 | /* nothing to fail, let's reply normaly */ |
| 2503 | txn->status = 200; |
| 2504 | stream_int_retnclose(req->prod, &http_200_chunk); |
| 2505 | goto return_prx_cond; |
| 2506 | } |
| 2507 | |
| 2508 | /* |
| 2509 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2510 | * Note: we cannot log anymore if the request has been |
| 2511 | * classified as invalid. |
| 2512 | */ |
| 2513 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2514 | /* we have a complete HTTP request that we must log */ |
| 2515 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2516 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2517 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2518 | if (urilen >= REQURI_LEN) |
| 2519 | urilen = REQURI_LEN - 1; |
| 2520 | memcpy(txn->uri, &req->data[msg->som], urilen); |
| 2521 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2522 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2523 | if (!(s->logs.logwait &= ~LW_REQ)) |
| 2524 | s->do_log(s); |
| 2525 | } else { |
| 2526 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2527 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2528 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2529 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2530 | /* 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] | 2531 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) |
| 2532 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2533 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2534 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2535 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2536 | ((msg->sol[msg->sl.rq.v + 5] > '1') || |
| 2537 | ((msg->sol[msg->sl.rq.v + 5] == '1') && |
| 2538 | (msg->sol[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2539 | txn->flags |= TX_REQ_VER_11; |
| 2540 | |
| 2541 | /* "connection" has not been parsed yet */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2542 | txn->flags &= ~(TX_HDR_CONN_PRS | TX_HDR_CONN_CLO | TX_HDR_CONN_KAL); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2543 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2544 | /* transfer length unknown*/ |
| 2545 | txn->flags &= ~TX_REQ_XFER_LEN; |
| 2546 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2547 | /* 5: we may need to capture headers */ |
| 2548 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap)) |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2549 | capture_headers(msg->sol, &txn->hdr_idx, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2550 | txn->req.cap, s->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2551 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2552 | /* 6: determine the transfer-length. |
| 2553 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 2554 | * the presence of a message-body in a REQUEST and its transfer length |
| 2555 | * must be determined that way (in order of precedence) : |
| 2556 | * 1. The presence of a message-body in a request is signaled by the |
| 2557 | * inclusion of a Content-Length or Transfer-Encoding header field |
| 2558 | * in the request's header fields. When a request message contains |
| 2559 | * both a message-body of non-zero length and a method that does |
| 2560 | * not define any semantics for that request message-body, then an |
| 2561 | * origin server SHOULD either ignore the message-body or respond |
| 2562 | * with an appropriate error message (e.g., 413). A proxy or |
| 2563 | * gateway, when presented the same request, SHOULD either forward |
| 2564 | * the request inbound with the message- body or ignore the |
| 2565 | * message-body when determining a response. |
| 2566 | * |
| 2567 | * 2. If a Transfer-Encoding header field (Section 9.7) is present |
| 2568 | * and the "chunked" transfer-coding (Section 6.2) is used, the |
| 2569 | * transfer-length is defined by the use of this transfer-coding. |
| 2570 | * If a Transfer-Encoding header field is present and the "chunked" |
| 2571 | * transfer-coding is not present, the transfer-length is defined |
| 2572 | * by the sender closing the connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2573 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2574 | * 3. If a Content-Length header field is present, its decimal value in |
| 2575 | * OCTETs represents both the entity-length and the transfer-length. |
| 2576 | * If a message is received with both a Transfer-Encoding header |
| 2577 | * field and a Content-Length header field, the latter MUST be ignored. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2578 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2579 | * 4. By the server closing the connection. (Closing the connection |
| 2580 | * cannot be used to indicate the end of a request body, since that |
| 2581 | * would leave no possibility for the server to send back a response.) |
| 2582 | * |
| 2583 | * Whenever a transfer-coding is applied to a message-body, the set of |
| 2584 | * transfer-codings MUST include "chunked", unless the message indicates |
| 2585 | * it is terminated by closing the connection. When the "chunked" |
| 2586 | * transfer-coding is used, it MUST be the last transfer-coding applied |
| 2587 | * to the message-body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2588 | */ |
| 2589 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2590 | /* CONNECT sets a tunnel and ignores everything else */ |
| 2591 | if (txn->meth == HTTP_METH_CONNECT) |
| 2592 | goto skip_xfer_len; |
| 2593 | |
| 2594 | use_close_only = 0; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2595 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2596 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 2597 | while ((txn->flags & TX_REQ_VER_11) && |
| 2598 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2599 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
| 2600 | txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN); |
| 2601 | else if (txn->flags & TX_REQ_TE_CHNK) { |
| 2602 | /* bad transfer-encoding (chunked followed by something else) */ |
| 2603 | use_close_only = 1; |
| 2604 | txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN); |
| 2605 | break; |
| 2606 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2607 | } |
| 2608 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2609 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2610 | while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only && |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2611 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) { |
| 2612 | signed long long cl; |
| 2613 | |
| 2614 | if (!ctx.vlen) |
| 2615 | goto return_bad_req; |
| 2616 | |
| 2617 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) |
| 2618 | goto return_bad_req; /* parse failure */ |
| 2619 | |
| 2620 | if (cl < 0) |
| 2621 | goto return_bad_req; |
| 2622 | |
| 2623 | if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl)) |
| 2624 | goto return_bad_req; /* already specified, was different */ |
| 2625 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2626 | txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2627 | msg->hdr_content_len = cl; |
| 2628 | } |
| 2629 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2630 | /* bodyless requests have a known length */ |
| 2631 | if (!use_close_only) |
| 2632 | txn->flags |= TX_REQ_XFER_LEN; |
| 2633 | |
| 2634 | skip_xfer_len: |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2635 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2636 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2637 | req->analyse_exp = TICK_ETERNITY; |
| 2638 | return 1; |
| 2639 | |
| 2640 | return_bad_req: |
| 2641 | /* We centralize bad requests processing here */ |
| 2642 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2643 | /* we detected a parsing error. We want to archive this request |
| 2644 | * in the dedicated proxy area for later troubleshooting. |
| 2645 | */ |
| 2646 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
| 2647 | } |
| 2648 | |
| 2649 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2650 | txn->status = 400; |
| 2651 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2652 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2653 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2654 | if (s->listener->counters) |
| 2655 | s->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2656 | |
| 2657 | return_prx_cond: |
| 2658 | if (!(s->flags & SN_ERR_MASK)) |
| 2659 | s->flags |= SN_ERR_PRXCOND; |
| 2660 | if (!(s->flags & SN_FINST_MASK)) |
| 2661 | s->flags |= SN_FINST_R; |
| 2662 | |
| 2663 | req->analysers = 0; |
| 2664 | req->analyse_exp = TICK_ETERNITY; |
| 2665 | return 0; |
| 2666 | } |
| 2667 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2668 | /* This stream analyser runs all HTTP request processing which is common to |
| 2669 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 2670 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2671 | * 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] | 2672 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 2673 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2674 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2675 | 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] | 2676 | { |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2677 | struct http_txn *txn = &s->txn; |
| 2678 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2679 | struct acl_cond *cond; |
| 2680 | struct redirect_rule *rule; |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 2681 | struct wordlist *wl; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2682 | int del_ka, del_cl; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2683 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2684 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2685 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2686 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2687 | return 0; |
| 2688 | } |
| 2689 | |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2690 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2691 | req->analyse_exp = TICK_ETERNITY; |
| 2692 | |
| 2693 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 2694 | now_ms, __FUNCTION__, |
| 2695 | s, |
| 2696 | req, |
| 2697 | req->rex, req->wex, |
| 2698 | req->flags, |
| 2699 | req->l, |
| 2700 | req->analysers); |
| 2701 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2702 | /* first check whether we have some ACLs set to block this request */ |
| 2703 | list_for_each_entry(cond, &px->block_cond, list) { |
| 2704 | int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ); |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2705 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2706 | ret = acl_pass(ret); |
| 2707 | if (cond->pol == ACL_COND_UNLESS) |
| 2708 | ret = !ret; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2709 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2710 | if (ret) { |
| 2711 | txn->status = 403; |
| 2712 | /* let's log the request time */ |
| 2713 | s->logs.tv_request = now; |
| 2714 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
| 2715 | goto return_prx_cond; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2716 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2717 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2718 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2719 | /* try headers filters */ |
| 2720 | if (px->req_exp != NULL) { |
| 2721 | if (apply_filters_to_request(s, req, px->req_exp) < 0) |
| 2722 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2723 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2724 | /* has the request been denied ? */ |
| 2725 | if (txn->flags & TX_CLDENY) { |
| 2726 | /* no need to go further */ |
| 2727 | txn->status = 403; |
| 2728 | /* let's log the request time */ |
| 2729 | s->logs.tv_request = now; |
| 2730 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
| 2731 | goto return_prx_cond; |
| 2732 | } |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2733 | |
| 2734 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 2735 | * which may be the same as the connect timeout if unspecified. |
| 2736 | * If unset, then set it to zero because we really want it to |
| 2737 | * eventually expire. We build the tarpit as an analyser. |
| 2738 | */ |
| 2739 | if (txn->flags & TX_CLTARPIT) { |
| 2740 | buffer_erase(s->req); |
| 2741 | /* wipe the request out so that we can drop the connection early |
| 2742 | * if the client closes first. |
| 2743 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2744 | buffer_dont_connect(req); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2745 | req->analysers = 0; /* remove switching rules etc... */ |
| 2746 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 2747 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 2748 | if (!req->analyse_exp) |
| 2749 | req->analyse_exp = tick_add(now_ms, 0); |
| 2750 | return 1; |
| 2751 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2752 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2753 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2754 | /* Until set to anything else, the connection mode is set as TUNNEL. It will |
| 2755 | * only change if both the request and the config reference something else. |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2756 | * Option httpclose by itself does not set a mode, it remains a tunnel mode |
| 2757 | * in which headers are mangled. However, if another mode is set, it will |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2758 | * affect it (eg: server-close/keep-alive + httpclose = close). Note that we |
| 2759 | * avoid to redo the same work if FE and BE have the same settings (common). |
| 2760 | * The method consists in checking if options changed between the two calls |
| 2761 | * (implying that either one is non-null, or one of them is non-null and we |
| 2762 | * are there for the first time. |
Willy Tarreau | 4273664 | 2009-10-18 21:04:35 +0200 | [diff] [blame] | 2763 | */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2764 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2765 | del_cl = del_ka = 0; |
| 2766 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2767 | if ((txn->meth != HTTP_METH_CONNECT) && |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2768 | ((!(txn->flags & TX_HDR_CONN_PRS) && |
| 2769 | (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) || |
| 2770 | ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) != |
| 2771 | (s->be->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))))) { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2772 | int tmp = TX_CON_WANT_TUN; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2773 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2774 | if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE) |
| 2775 | tmp = TX_CON_WANT_KAL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2776 | if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO) |
| 2777 | tmp = TX_CON_WANT_SCL; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2778 | if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2779 | tmp = TX_CON_WANT_CLO; |
| 2780 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2781 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 2782 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2783 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2784 | if (!(txn->flags & TX_HDR_CONN_PRS)) { |
| 2785 | /* parse the Connection header and possibly clean it */ |
| 2786 | int to_del = 0; |
| 2787 | if ((txn->flags & TX_REQ_VER_11) || |
| 2788 | (txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL) |
| 2789 | to_del |= 2; /* remove "keep-alive" */ |
| 2790 | if (!(txn->flags & TX_REQ_VER_11)) |
| 2791 | to_del |= 1; /* remove "close" */ |
| 2792 | http_parse_connection_header(txn, msg, req, to_del); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2793 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2794 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2795 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 2796 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 2797 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 2798 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
| 2799 | (txn->flags & (TX_REQ_VER_11|TX_HDR_CONN_KAL)) == 0 || /* no "connection: k-a" in 1.0 */ |
| 2800 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose + any = forceclose */ |
| 2801 | !(txn->flags & TX_REQ_XFER_LEN))) /* no length known => close */ |
| 2802 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 2803 | } |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 2804 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2805 | /* add request headers from the rule sets in the same order */ |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 2806 | list_for_each_entry(wl, &px->req_add, list) { |
| 2807 | if (unlikely(http_header_add_tail(req, &txn->req, &txn->hdr_idx, wl->s) < 0)) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2808 | goto return_bad_req; |
| 2809 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2810 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2811 | /* check if stats URI was requested, and if an auth is needed */ |
| 2812 | if (px->uri_auth != NULL && |
| 2813 | (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) { |
| 2814 | /* we have to check the URI and auth for this request. |
| 2815 | * FIXME!!! that one is rather dangerous, we want to |
| 2816 | * make it follow standard rules (eg: clear req->analysers). |
| 2817 | */ |
| 2818 | if (stats_check_uri_auth(s, px)) { |
| 2819 | req->analysers = 0; |
| 2820 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2821 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2822 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2823 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2824 | /* check whether we have some ACLs set to redirect this request */ |
| 2825 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 2826 | int ret = ACL_PAT_PASS; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2827 | |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 2828 | if (rule->cond) { |
| 2829 | ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ); |
| 2830 | ret = acl_pass(ret); |
| 2831 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2832 | ret = !ret; |
| 2833 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2834 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2835 | if (ret) { |
Willy Tarreau | 3bb9c23 | 2010-01-03 12:24:37 +0100 | [diff] [blame] | 2836 | struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 }; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2837 | const char *msg_fmt; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2838 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2839 | /* build redirect message */ |
| 2840 | switch(rule->code) { |
| 2841 | case 303: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2842 | msg_fmt = HTTP_303; |
| 2843 | break; |
| 2844 | case 301: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2845 | msg_fmt = HTTP_301; |
| 2846 | break; |
| 2847 | case 302: |
| 2848 | default: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2849 | msg_fmt = HTTP_302; |
| 2850 | break; |
| 2851 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2852 | |
Willy Tarreau | 3bb9c23 | 2010-01-03 12:24:37 +0100 | [diff] [blame] | 2853 | if (unlikely(!chunk_strcpy(&rdr, msg_fmt))) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2854 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2855 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2856 | switch(rule->type) { |
| 2857 | case REDIRECT_TYPE_PREFIX: { |
| 2858 | const char *path; |
| 2859 | int pathlen; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2860 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2861 | path = http_get_path(txn); |
| 2862 | /* build message using path */ |
| 2863 | if (path) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2864 | pathlen = txn->req.sl.rq.u_l + (txn->req.sol + txn->req.sl.rq.u) - path; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2865 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 2866 | int qs = 0; |
| 2867 | while (qs < pathlen) { |
| 2868 | if (path[qs] == '?') { |
| 2869 | pathlen = qs; |
| 2870 | break; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2871 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2872 | qs++; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2873 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2874 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2875 | } else { |
| 2876 | path = "/"; |
| 2877 | pathlen = 1; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2878 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2879 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 2880 | if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2881 | goto return_bad_req; |
| 2882 | |
| 2883 | /* add prefix. Note that if prefix == "/", we don't want to |
| 2884 | * add anything, otherwise it makes it hard for the user to |
| 2885 | * configure a self-redirection. |
| 2886 | */ |
| 2887 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2888 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 2889 | rdr.len += rule->rdr_len; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2890 | } |
| 2891 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2892 | /* add path */ |
| 2893 | memcpy(rdr.str + rdr.len, path, pathlen); |
| 2894 | rdr.len += pathlen; |
Willy Tarreau | 81e3b4f | 2010-01-10 00:42:19 +0100 | [diff] [blame] | 2895 | |
| 2896 | /* append a slash at the end of the location is needed and missing */ |
| 2897 | if (rdr.len && rdr.str[rdr.len - 1] != '/' && |
| 2898 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 2899 | if (rdr.len > rdr.size - 5) |
| 2900 | goto return_bad_req; |
| 2901 | rdr.str[rdr.len] = '/'; |
| 2902 | rdr.len++; |
| 2903 | } |
| 2904 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2905 | break; |
| 2906 | } |
| 2907 | case REDIRECT_TYPE_LOCATION: |
| 2908 | default: |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 2909 | if (rdr.len + rule->rdr_len > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2910 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2911 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2912 | /* add location */ |
| 2913 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 2914 | rdr.len += rule->rdr_len; |
| 2915 | break; |
| 2916 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2917 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2918 | if (rule->cookie_len) { |
| 2919 | memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14); |
| 2920 | rdr.len += 14; |
| 2921 | memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len); |
| 2922 | rdr.len += rule->cookie_len; |
| 2923 | memcpy(rdr.str + rdr.len, "\r\n", 2); |
| 2924 | rdr.len += 2; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2925 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2926 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 2927 | /* add end of headers and the keep-alive/close status. |
| 2928 | * We may choose to set keep-alive if the Location begins |
| 2929 | * with a slash, because the client will come back to the |
| 2930 | * same server. |
| 2931 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2932 | txn->status = rule->code; |
| 2933 | /* let's log the request time */ |
| 2934 | s->logs.tv_request = now; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 2935 | |
| 2936 | if (rule->rdr_len >= 1 && *rule->rdr_str == '/' && |
| 2937 | (txn->flags & TX_REQ_XFER_LEN) && |
| 2938 | !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len && |
| 2939 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 2940 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 2941 | /* keep-alive possible */ |
Willy Tarreau | 7566145 | 2010-01-10 10:35:01 +0100 | [diff] [blame] | 2942 | if (!(txn->flags & TX_REQ_VER_11)) { |
| 2943 | memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24); |
| 2944 | rdr.len += 24; |
| 2945 | } |
| 2946 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 2947 | rdr.len += 4; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 2948 | buffer_write(req->prod->ob, rdr.str, rdr.len); |
| 2949 | /* "eat" the request */ |
| 2950 | buffer_ignore(req, msg->sov - msg->som); |
| 2951 | msg->som = msg->sov; |
| 2952 | req->analysers = AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 9300fb2 | 2010-01-05 00:58:24 +0100 | [diff] [blame] | 2953 | s->rep->analysers = AN_RES_HTTP_XFER_BODY; |
| 2954 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 2955 | txn->rsp.msg_state = HTTP_MSG_DONE; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 2956 | break; |
| 2957 | } else { |
| 2958 | /* keep-alive not possible */ |
| 2959 | memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23); |
| 2960 | rdr.len += 23; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 2961 | stream_int_retnclose(req->prod, &rdr); |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 2962 | goto return_prx_cond; |
| 2963 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2964 | } |
| 2965 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2966 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 2967 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 2968 | * If this happens, then the data will not come immediately, so we must |
| 2969 | * send all what we have without waiting. Note that due to the small gain |
| 2970 | * in waiting for the body of the request, it's easier to simply put the |
| 2971 | * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove |
| 2972 | * itself once used. |
| 2973 | */ |
| 2974 | req->flags |= BF_SEND_DONTWAIT; |
| 2975 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2976 | /* that's OK for us now, let's move on to next analysers */ |
| 2977 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2978 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2979 | return_bad_req: |
| 2980 | /* We centralize bad requests processing here */ |
| 2981 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2982 | /* we detected a parsing error. We want to archive this request |
| 2983 | * in the dedicated proxy area for later troubleshooting. |
| 2984 | */ |
| 2985 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
| 2986 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 2987 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2988 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2989 | txn->status = 400; |
| 2990 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2991 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2992 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2993 | if (s->listener->counters) |
| 2994 | s->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 2995 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2996 | return_prx_cond: |
| 2997 | if (!(s->flags & SN_ERR_MASK)) |
| 2998 | s->flags |= SN_ERR_PRXCOND; |
| 2999 | if (!(s->flags & SN_FINST_MASK)) |
| 3000 | s->flags |= SN_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 3001 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3002 | req->analysers = 0; |
| 3003 | req->analyse_exp = TICK_ETERNITY; |
| 3004 | return 0; |
| 3005 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3006 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3007 | /* This function performs all the processing enabled for the current request. |
| 3008 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 3009 | * needs more data, encounters an error, or wants to immediately abort the |
| 3010 | * request. It relies on buffers flags, and updates s->req->analysers. |
| 3011 | */ |
| 3012 | int http_process_request(struct session *s, struct buffer *req, int an_bit) |
| 3013 | { |
| 3014 | struct http_txn *txn = &s->txn; |
| 3015 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3016 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3017 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3018 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3019 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3020 | return 0; |
| 3021 | } |
| 3022 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3023 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 3024 | now_ms, __FUNCTION__, |
| 3025 | s, |
| 3026 | req, |
| 3027 | req->rex, req->wex, |
| 3028 | req->flags, |
| 3029 | req->l, |
| 3030 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3031 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3032 | /* |
| 3033 | * Right now, we know that we have processed the entire headers |
| 3034 | * and that unwanted requests have been filtered out. We can do |
| 3035 | * whatever we want with the remaining request. Also, now we |
| 3036 | * may have separate values for ->fe, ->be. |
| 3037 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3038 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3039 | /* |
| 3040 | * If HTTP PROXY is set we simply get remote server address |
| 3041 | * parsing incoming request. |
| 3042 | */ |
| 3043 | if ((s->be->options & PR_O_HTTP_PROXY) && !(s->flags & SN_ADDR_SET)) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 3044 | url2sa(msg->sol + msg->sl.rq.u, msg->sl.rq.u_l, &s->srv_addr); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3045 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3046 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3047 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3048 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3049 | * Note that doing so might move headers in the request, but |
| 3050 | * the fields will stay coherent and the URI will not move. |
| 3051 | * This should only be performed in the backend. |
| 3052 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 3053 | 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] | 3054 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 3055 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 3056 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3057 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3058 | * 8: the appsession cookie was looked up very early in 1.2, |
| 3059 | * so let's do the same now. |
| 3060 | */ |
| 3061 | |
| 3062 | /* It needs to look into the URI */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 3063 | if ((txn->sessid == NULL) && s->be->appsession_name) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 3064 | get_srv_from_appsession(s, msg->sol + msg->sl.rq.u, msg->sl.rq.u_l); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3065 | } |
| 3066 | |
| 3067 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3068 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 3069 | * asks for it. |
| 3070 | */ |
| 3071 | if ((s->fe->options | s->be->options) & PR_O_FWDFOR) { |
| 3072 | if (s->cli_addr.ss_family == AF_INET) { |
| 3073 | /* Add an X-Forwarded-For header unless the source IP is |
| 3074 | * in the 'except' network range. |
| 3075 | */ |
| 3076 | if ((!s->fe->except_mask.s_addr || |
| 3077 | (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr) |
| 3078 | != s->fe->except_net.s_addr) && |
| 3079 | (!s->be->except_mask.s_addr || |
| 3080 | (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr) |
| 3081 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3082 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3083 | unsigned char *pn; |
| 3084 | pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3085 | |
| 3086 | /* Note: we rely on the backend to get the header name to be used for |
| 3087 | * x-forwarded-for, because the header is really meant for the backends. |
| 3088 | * However, if the backend did not specify any option, we have to rely |
| 3089 | * on the frontend's header name. |
| 3090 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3091 | if (s->be->fwdfor_hdr_len) { |
| 3092 | len = s->be->fwdfor_hdr_len; |
| 3093 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3094 | } else { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3095 | len = s->fe->fwdfor_hdr_len; |
| 3096 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3097 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3098 | 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] | 3099 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3100 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3101 | &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3102 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3103 | } |
| 3104 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3105 | else if (s->cli_addr.ss_family == AF_INET6) { |
| 3106 | /* FIXME: for the sake of completeness, we should also support |
| 3107 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3108 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3109 | int len; |
| 3110 | char pn[INET6_ADDRSTRLEN]; |
| 3111 | inet_ntop(AF_INET6, |
| 3112 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 3113 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3114 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3115 | /* Note: we rely on the backend to get the header name to be used for |
| 3116 | * x-forwarded-for, because the header is really meant for the backends. |
| 3117 | * However, if the backend did not specify any option, we have to rely |
| 3118 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3119 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3120 | if (s->be->fwdfor_hdr_len) { |
| 3121 | len = s->be->fwdfor_hdr_len; |
| 3122 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
| 3123 | } else { |
| 3124 | len = s->fe->fwdfor_hdr_len; |
| 3125 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3126 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3127 | len += sprintf(trash + len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3128 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3129 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3130 | &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3131 | goto return_bad_req; |
| 3132 | } |
| 3133 | } |
| 3134 | |
| 3135 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3136 | * 10: add X-Original-To if either the frontend or the backend |
| 3137 | * asks for it. |
| 3138 | */ |
| 3139 | if ((s->fe->options | s->be->options) & PR_O_ORGTO) { |
| 3140 | |
| 3141 | /* FIXME: don't know if IPv6 can handle that case too. */ |
| 3142 | if (s->cli_addr.ss_family == AF_INET) { |
| 3143 | /* Add an X-Original-To header unless the destination IP is |
| 3144 | * in the 'except' network range. |
| 3145 | */ |
| 3146 | if (!(s->flags & SN_FRT_ADDR_SET)) |
| 3147 | get_frt_addr(s); |
| 3148 | |
| 3149 | if ((!s->fe->except_mask_to.s_addr || |
| 3150 | (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr) |
| 3151 | != s->fe->except_to.s_addr) && |
| 3152 | (!s->be->except_mask_to.s_addr || |
| 3153 | (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr) |
| 3154 | != s->be->except_to.s_addr)) { |
| 3155 | int len; |
| 3156 | unsigned char *pn; |
| 3157 | pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr; |
| 3158 | |
| 3159 | /* Note: we rely on the backend to get the header name to be used for |
| 3160 | * x-original-to, because the header is really meant for the backends. |
| 3161 | * However, if the backend did not specify any option, we have to rely |
| 3162 | * on the frontend's header name. |
| 3163 | */ |
| 3164 | if (s->be->orgto_hdr_len) { |
| 3165 | len = s->be->orgto_hdr_len; |
| 3166 | memcpy(trash, s->be->orgto_hdr_name, len); |
| 3167 | } else { |
| 3168 | len = s->fe->orgto_hdr_len; |
| 3169 | memcpy(trash, s->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3170 | } |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3171 | len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
| 3172 | |
| 3173 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3174 | &txn->hdr_idx, trash, len) < 0)) |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3175 | goto return_bad_req; |
| 3176 | } |
| 3177 | } |
| 3178 | } |
| 3179 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3180 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */ |
| 3181 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
| 3182 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) { |
| 3183 | unsigned int want_flags = 0; |
| 3184 | |
| 3185 | if (txn->flags & TX_REQ_VER_11) { |
| 3186 | if ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
| 3187 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) |
| 3188 | want_flags |= TX_CON_CLO_SET; |
| 3189 | } else { |
| 3190 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) |
| 3191 | want_flags |= TX_CON_KAL_SET; |
| 3192 | } |
| 3193 | |
| 3194 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 3195 | http_change_connection_header(txn, msg, req, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3196 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3197 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3198 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3199 | /* If we have no server assigned yet and we're balancing on url_param |
| 3200 | * with a POST request, we may be interested in checking the body for |
| 3201 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3202 | */ |
| 3203 | if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 3204 | s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3205 | s->be->url_param_post_limit != 0 && |
| 3206 | (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 3207 | memchr(msg->sol + msg->sl.rq.u, '?', msg->sl.rq.u_l) == NULL) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3208 | buffer_dont_connect(req); |
| 3209 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3210 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3211 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3212 | if (txn->flags & TX_REQ_XFER_LEN) |
| 3213 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 3214 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3215 | /************************************************************* |
| 3216 | * OK, that's finished for the headers. We have done what we * |
| 3217 | * could. Let's switch to the DATA state. * |
| 3218 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3219 | req->analyse_exp = TICK_ETERNITY; |
| 3220 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3221 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3222 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3223 | /* OK let's go on with the BODY now */ |
| 3224 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3225 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3226 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3227 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3228 | /* we detected a parsing error. We want to archive this request |
| 3229 | * in the dedicated proxy area for later troubleshooting. |
| 3230 | */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3231 | 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] | 3232 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3233 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3234 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3235 | txn->status = 400; |
| 3236 | req->analysers = 0; |
| 3237 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3238 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3239 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3240 | if (s->listener->counters) |
| 3241 | s->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3242 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3243 | if (!(s->flags & SN_ERR_MASK)) |
| 3244 | s->flags |= SN_ERR_PRXCOND; |
| 3245 | if (!(s->flags & SN_FINST_MASK)) |
| 3246 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3247 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3248 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3249 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3250 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 3251 | * returns zero, at the beginning because it prevents any other processing |
| 3252 | * from occurring, and at the end because it terminates the request. |
| 3253 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3254 | 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] | 3255 | { |
| 3256 | struct http_txn *txn = &s->txn; |
| 3257 | |
| 3258 | /* This connection is being tarpitted. The CLIENT side has |
| 3259 | * already set the connect expiration date to the right |
| 3260 | * timeout. We just have to check that the client is still |
| 3261 | * there and that the timeout has not expired. |
| 3262 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3263 | buffer_dont_connect(req); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3264 | if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 && |
| 3265 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 3266 | return 0; |
| 3267 | |
| 3268 | /* We will set the queue timer to the time spent, just for |
| 3269 | * logging purposes. We fake a 500 server error, so that the |
| 3270 | * attacker will not suspect his connection has been tarpitted. |
| 3271 | * It will not cause trouble to the logs because we can exclude |
| 3272 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 3273 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3274 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3275 | |
| 3276 | txn->status = 500; |
| 3277 | if (req->flags != BF_READ_ERROR) |
| 3278 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500)); |
| 3279 | |
| 3280 | req->analysers = 0; |
| 3281 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3282 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3283 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3284 | if (s->listener->counters) |
| 3285 | s->listener->counters->failed_req++; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3286 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3287 | if (!(s->flags & SN_ERR_MASK)) |
| 3288 | s->flags |= SN_ERR_PRXCOND; |
| 3289 | if (!(s->flags & SN_FINST_MASK)) |
| 3290 | s->flags |= SN_FINST_T; |
| 3291 | return 0; |
| 3292 | } |
| 3293 | |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3294 | /* This function is an analyser which processes the HTTP request body. It looks |
| 3295 | * for parameters to be used for the load balancing algorithm (url_param). It |
| 3296 | * must only be called after the standard HTTP request processing has occurred, |
| 3297 | * because it expects the request to be parsed. It returns zero if it needs to |
| 3298 | * read more data, or 1 once it has completed its analysis. |
| 3299 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3300 | 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] | 3301 | { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3302 | struct http_txn *txn = &s->txn; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3303 | struct http_msg *msg = &s->txn.req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3304 | long long limit = s->be->url_param_post_limit; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3305 | |
| 3306 | /* We have to parse the HTTP request body to find any required data. |
| 3307 | * "balance url_param check_post" should have been the only way to get |
| 3308 | * into this. We were brought here after HTTP header analysis, so all |
| 3309 | * related structures are ready. |
| 3310 | */ |
| 3311 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3312 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 3313 | goto missing_data; |
| 3314 | |
| 3315 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 3316 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 3317 | * send an HTTP/1.1 100 Continue intermediate response. |
| 3318 | */ |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 3319 | if (txn->flags & TX_REQ_VER_11) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3320 | struct hdr_ctx ctx; |
| 3321 | ctx.idx = 0; |
| 3322 | /* Expect is allowed in 1.1, look for it */ |
| 3323 | if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) && |
| 3324 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
| 3325 | buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len); |
| 3326 | } |
| 3327 | } |
| 3328 | msg->msg_state = HTTP_MSG_100_SENT; |
| 3329 | } |
| 3330 | |
| 3331 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3332 | /* we have msg->col and msg->sov which both point to the first |
| 3333 | * byte of message body. msg->som still points to the beginning |
| 3334 | * of the message. We must save the body in req->lr because it |
| 3335 | * survives buffer re-alignments. |
| 3336 | */ |
| 3337 | req->lr = req->data + msg->sov; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3338 | if (txn->flags & TX_REQ_TE_CHNK) |
| 3339 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 3340 | else |
| 3341 | msg->msg_state = HTTP_MSG_DATA; |
| 3342 | } |
| 3343 | |
| 3344 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3345 | /* read the chunk size and assign it to ->hdr_content_len, then |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3346 | * set ->sov and ->lr to point to the body and switch to DATA or |
| 3347 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3348 | */ |
| 3349 | int ret = http_parse_chunk_size(req, msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3350 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3351 | if (!ret) |
| 3352 | goto missing_data; |
| 3353 | else if (ret < 0) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3354 | goto return_bad_req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3355 | } |
| 3356 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3357 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3358 | * We have the first non-header byte in msg->col, which is either the |
| 3359 | * beginning of the chunk size or of the data. The first data byte is in |
| 3360 | * msg->sov, which is equal to msg->col when not using transfer-encoding. |
| 3361 | * We're waiting for at least <url_param_post_limit> bytes after msg->sov. |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3362 | */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3363 | |
| 3364 | if (msg->hdr_content_len < limit) |
| 3365 | limit = msg->hdr_content_len; |
| 3366 | |
Willy Tarreau | 7c96f67 | 2009-12-27 22:47:25 +0100 | [diff] [blame] | 3367 | if (req->l - (msg->sov - msg->som) >= limit) /* we have enough bytes now */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3368 | goto http_end; |
| 3369 | |
| 3370 | missing_data: |
| 3371 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3372 | if (req->flags & BF_FULL) |
| 3373 | goto return_bad_req; |
| 3374 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3375 | if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) { |
| 3376 | txn->status = 408; |
| 3377 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
| 3378 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3379 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3380 | |
| 3381 | /* we get here if we need to wait for more data */ |
| 3382 | if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3383 | /* Not enough data. We'll re-use the http-request |
| 3384 | * timeout here. Ideally, we should set the timeout |
| 3385 | * relative to the accept() date. We just set the |
| 3386 | * request timeout once at the beginning of the |
| 3387 | * request. |
| 3388 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3389 | buffer_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3390 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 3391 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3392 | return 0; |
| 3393 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3394 | |
| 3395 | http_end: |
| 3396 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 3397 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 3398 | req->analysers &= ~an_bit; |
| 3399 | req->analyse_exp = TICK_ETERNITY; |
| 3400 | return 1; |
| 3401 | |
| 3402 | return_bad_req: /* let's centralize all bad requests */ |
| 3403 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3404 | txn->status = 400; |
| 3405 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 3406 | |
| 3407 | return_err_msg: |
| 3408 | req->analysers = 0; |
| 3409 | s->fe->counters.failed_req++; |
| 3410 | if (s->listener->counters) |
| 3411 | s->listener->counters->failed_req++; |
| 3412 | |
| 3413 | if (!(s->flags & SN_ERR_MASK)) |
| 3414 | s->flags |= SN_ERR_PRXCOND; |
| 3415 | if (!(s->flags & SN_FINST_MASK)) |
| 3416 | s->flags |= SN_FINST_R; |
| 3417 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3418 | } |
| 3419 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3420 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 3421 | * right now but it works. |
| 3422 | */ |
| 3423 | void http_end_txn_clean_session(struct session *s) |
| 3424 | { |
| 3425 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 3426 | * server's connections. We need a safer way to reinitialize buffer |
| 3427 | * flags. We also need a more accurate method for computing per-request |
| 3428 | * data. |
| 3429 | */ |
| 3430 | http_silent_debug(__LINE__, s); |
| 3431 | |
| 3432 | s->req->cons->flags |= SI_FL_NOLINGER; |
| 3433 | s->req->cons->shutr(s->req->cons); |
| 3434 | s->req->cons->shutw(s->req->cons); |
| 3435 | |
| 3436 | http_silent_debug(__LINE__, s); |
| 3437 | |
| 3438 | if (s->flags & SN_BE_ASSIGNED) |
| 3439 | s->be->beconn--; |
| 3440 | |
| 3441 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3442 | session_process_counters(s); |
| 3443 | |
| 3444 | if (s->txn.status) { |
| 3445 | int n; |
| 3446 | |
| 3447 | n = s->txn.status / 100; |
| 3448 | if (n < 1 || n > 5) |
| 3449 | n = 0; |
| 3450 | |
| 3451 | if (s->fe->mode == PR_MODE_HTTP) |
| 3452 | s->fe->counters.p.http.rsp[n]++; |
| 3453 | |
| 3454 | if ((s->flags & SN_BE_ASSIGNED) && (s->fe != s->be) && |
| 3455 | (s->be->mode == PR_MODE_HTTP)) |
| 3456 | s->be->counters.p.http.rsp[n]++; |
| 3457 | } |
| 3458 | |
| 3459 | /* don't count other requests' data */ |
| 3460 | s->logs.bytes_in -= s->req->l - s->req->send_max; |
| 3461 | s->logs.bytes_out -= s->rep->l - s->rep->send_max; |
| 3462 | |
| 3463 | /* let's do a final log if we need it */ |
| 3464 | if (s->logs.logwait && |
| 3465 | !(s->flags & SN_MONITOR) && |
| 3466 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 3467 | s->do_log(s); |
| 3468 | } |
| 3469 | |
| 3470 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 3471 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 3472 | tv_zero(&s->logs.tv_request); |
| 3473 | s->logs.t_queue = -1; |
| 3474 | s->logs.t_connect = -1; |
| 3475 | s->logs.t_data = -1; |
| 3476 | s->logs.t_close = 0; |
| 3477 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 3478 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 3479 | |
| 3480 | s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max; |
| 3481 | s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max; |
| 3482 | |
| 3483 | if (s->pend_pos) |
| 3484 | pendconn_free(s->pend_pos); |
| 3485 | |
| 3486 | if (s->srv) { |
| 3487 | if (s->flags & SN_CURR_SESS) { |
| 3488 | s->flags &= ~SN_CURR_SESS; |
| 3489 | s->srv->cur_sess--; |
| 3490 | } |
| 3491 | if (may_dequeue_tasks(s->srv, s->be)) |
| 3492 | process_srv_queue(s->srv); |
| 3493 | } |
| 3494 | |
| 3495 | if (unlikely(s->srv_conn)) |
| 3496 | sess_change_server(s, NULL); |
| 3497 | s->srv = NULL; |
| 3498 | |
| 3499 | s->req->cons->state = s->req->cons->prev_state = SI_ST_INI; |
| 3500 | s->req->cons->fd = -1; /* just to help with debugging */ |
| 3501 | s->req->cons->err_type = SI_ET_NONE; |
| 3502 | s->req->cons->err_loc = NULL; |
| 3503 | s->req->cons->exp = TICK_ETERNITY; |
| 3504 | s->req->cons->flags = SI_FL_NONE; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3505 | s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST); |
| 3506 | s->rep->flags &= ~(BF_SHUTR|BF_SHUTR_NOW|BF_READ_ATTACHED|BF_READ_ERROR|BF_READ_NOEXP|BF_STREAMER|BF_STREAMER_FAST|BF_WRITE_PARTIAL); |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame^] | 3507 | s->flags &= ~(SN_DIRECT|SN_ASSIGNED|SN_ADDR_SET|SN_BE_ASSIGNED|SN_FORCE_PRST); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3508 | s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE); |
| 3509 | s->txn.meth = 0; |
| 3510 | http_reset_txn(s); |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 3511 | s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3512 | if (s->be->options2 & PR_O2_INDEPSTR) |
| 3513 | s->req->cons->flags |= SI_FL_INDEP_STR; |
| 3514 | |
| 3515 | /* if the request buffer is not empty, it means we're |
| 3516 | * about to process another request, so send pending |
| 3517 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 3518 | * Just don't do this if the buffer is close to be full, |
| 3519 | * because the request will wait for it to flush a little |
| 3520 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3521 | */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 3522 | if (s->req->l > s->req->send_max) { |
| 3523 | if (s->rep->send_max && |
| 3524 | !(s->rep->flags & BF_FULL) && |
| 3525 | s->rep->lr <= s->rep->r && |
| 3526 | s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite) |
| 3527 | s->rep->flags |= BF_EXPECT_MORE; |
| 3528 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3529 | |
| 3530 | /* we're removing the analysers, we MUST re-enable events detection */ |
| 3531 | buffer_auto_read(s->req); |
| 3532 | buffer_auto_close(s->req); |
| 3533 | buffer_auto_read(s->rep); |
| 3534 | buffer_auto_close(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3535 | |
| 3536 | /* make ->lr point to the first non-forwarded byte */ |
| 3537 | s->req->lr = s->req->w + s->req->send_max; |
| 3538 | if (s->req->lr >= s->req->data + s->req->size) |
| 3539 | s->req->lr -= s->req->size; |
| 3540 | s->rep->lr = s->rep->w + s->rep->send_max; |
| 3541 | if (s->rep->lr >= s->rep->data + s->rep->size) |
| 3542 | s->rep->lr -= s->req->size; |
| 3543 | |
| 3544 | s->req->analysers |= s->fe->fe_req_ana; |
| 3545 | s->rep->analysers = 0; |
| 3546 | |
| 3547 | http_silent_debug(__LINE__, s); |
| 3548 | } |
| 3549 | |
| 3550 | |
| 3551 | /* This function updates the request state machine according to the response |
| 3552 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 3553 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 3554 | * it is only used to find when a request/response couple is complete. Both |
| 3555 | * this function and its equivalent should loop until both return zero. It |
| 3556 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 3557 | */ |
| 3558 | int http_sync_req_state(struct session *s) |
| 3559 | { |
| 3560 | struct buffer *buf = s->req; |
| 3561 | struct http_txn *txn = &s->txn; |
| 3562 | unsigned int old_flags = buf->flags; |
| 3563 | unsigned int old_state = txn->req.msg_state; |
| 3564 | |
| 3565 | http_silent_debug(__LINE__, s); |
| 3566 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 3567 | return 0; |
| 3568 | |
| 3569 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3570 | /* No need to read anymore, the request was completely parsed. |
| 3571 | * We can shut the read side unless we want to abort_on_close. |
| 3572 | */ |
| 3573 | if (buf->cons->state == SI_ST_EST || !(s->be->options & PR_O_ABRT_CLOSE)) |
| 3574 | buffer_dont_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3575 | |
| 3576 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 3577 | goto wait_other_side; |
| 3578 | |
| 3579 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 3580 | /* The server has not finished to respond, so we |
| 3581 | * don't want to move in order not to upset it. |
| 3582 | */ |
| 3583 | goto wait_other_side; |
| 3584 | } |
| 3585 | |
| 3586 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 3587 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3588 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3589 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 3590 | goto wait_other_side; |
| 3591 | } |
| 3592 | |
| 3593 | /* When we get here, it means that both the request and the |
| 3594 | * response have finished receiving. Depending on the connection |
| 3595 | * mode, we'll have to wait for the last bytes to leave in either |
| 3596 | * direction, and sometimes for a close to be effective. |
| 3597 | */ |
| 3598 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3599 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 3600 | /* Server-close mode : queue a connection close to the server */ |
| 3601 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3602 | buffer_shutw_now(buf); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3603 | } |
| 3604 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 3605 | /* Option forceclose is set, or either side wants to close, |
| 3606 | * let's enforce it now that we're not expecting any new |
| 3607 | * data to come. The caller knows the session is complete |
| 3608 | * once both states are CLOSED. |
| 3609 | */ |
| 3610 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3611 | buffer_shutr_now(buf); |
| 3612 | buffer_shutw_now(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3613 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3614 | } |
| 3615 | else { |
| 3616 | /* The last possible modes are keep-alive and tunnel. Since tunnel |
| 3617 | * mode does not set the body analyser, we can't reach this place |
| 3618 | * in tunnel mode, so we're left with keep-alive only. |
| 3619 | * This mode is currently not implemented, we switch to tunnel mode. |
| 3620 | */ |
| 3621 | buffer_auto_read(buf); |
| 3622 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3623 | } |
| 3624 | |
| 3625 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) { |
| 3626 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3627 | buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
| 3628 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3629 | if (!(buf->flags & BF_OUT_EMPTY)) { |
| 3630 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 3631 | goto http_msg_closing; |
| 3632 | } |
| 3633 | else { |
| 3634 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3635 | goto http_msg_closed; |
| 3636 | } |
| 3637 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3638 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3639 | } |
| 3640 | |
| 3641 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 3642 | http_msg_closing: |
| 3643 | /* nothing else to forward, just waiting for the output buffer |
| 3644 | * to be empty and for the shutw_now to take effect. |
| 3645 | */ |
| 3646 | if (buf->flags & BF_OUT_EMPTY) { |
| 3647 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3648 | goto http_msg_closed; |
| 3649 | } |
| 3650 | else if (buf->flags & BF_SHUTW) { |
| 3651 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3652 | goto wait_other_side; |
| 3653 | } |
| 3654 | } |
| 3655 | |
| 3656 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 3657 | http_msg_closed: |
| 3658 | goto wait_other_side; |
| 3659 | } |
| 3660 | |
| 3661 | wait_other_side: |
| 3662 | http_silent_debug(__LINE__, s); |
| 3663 | return txn->req.msg_state != old_state || buf->flags != old_flags; |
| 3664 | } |
| 3665 | |
| 3666 | |
| 3667 | /* This function updates the response state machine according to the request |
| 3668 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 3669 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 3670 | * it is only used to find when a request/response couple is complete. Both |
| 3671 | * this function and its equivalent should loop until both return zero. It |
| 3672 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 3673 | */ |
| 3674 | int http_sync_res_state(struct session *s) |
| 3675 | { |
| 3676 | struct buffer *buf = s->rep; |
| 3677 | struct http_txn *txn = &s->txn; |
| 3678 | unsigned int old_flags = buf->flags; |
| 3679 | unsigned int old_state = txn->rsp.msg_state; |
| 3680 | |
| 3681 | http_silent_debug(__LINE__, s); |
| 3682 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 3683 | return 0; |
| 3684 | |
| 3685 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 3686 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3687 | * still monitor the server connection for a possible close |
| 3688 | * while the request is being uploaded, so we don't disable |
| 3689 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3690 | */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3691 | /* buffer_dont_read(buf); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3692 | |
| 3693 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 3694 | goto wait_other_side; |
| 3695 | |
| 3696 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 3697 | /* The client seems to still be sending data, probably |
| 3698 | * because we got an error response during an upload. |
| 3699 | * We have the choice of either breaking the connection |
| 3700 | * or letting it pass through. Let's do the later. |
| 3701 | */ |
| 3702 | goto wait_other_side; |
| 3703 | } |
| 3704 | |
| 3705 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 3706 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3707 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3708 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
| 3709 | goto wait_other_side; |
| 3710 | } |
| 3711 | |
| 3712 | /* When we get here, it means that both the request and the |
| 3713 | * response have finished receiving. Depending on the connection |
| 3714 | * mode, we'll have to wait for the last bytes to leave in either |
| 3715 | * direction, and sometimes for a close to be effective. |
| 3716 | */ |
| 3717 | |
| 3718 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 3719 | /* Server-close mode : shut read and wait for the request |
| 3720 | * side to close its output buffer. The caller will detect |
| 3721 | * when we're in DONE and the other is in CLOSED and will |
| 3722 | * catch that for the final cleanup. |
| 3723 | */ |
| 3724 | if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW))) |
| 3725 | buffer_shutr_now(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3726 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3727 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 3728 | /* Option forceclose is set, or either side wants to close, |
| 3729 | * let's enforce it now that we're not expecting any new |
| 3730 | * data to come. The caller knows the session is complete |
| 3731 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3732 | */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3733 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) { |
| 3734 | buffer_shutr_now(buf); |
| 3735 | buffer_shutw_now(buf); |
| 3736 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3737 | } |
| 3738 | else { |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3739 | /* The last possible modes are keep-alive and tunnel. Since tunnel |
| 3740 | * mode does not set the body analyser, we can't reach this place |
| 3741 | * in tunnel mode, so we're left with keep-alive only. |
| 3742 | * This mode is currently not implemented, we switch to tunnel mode. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3743 | */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3744 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3745 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3746 | } |
| 3747 | |
| 3748 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) { |
| 3749 | /* if we've just closed an output, let's switch */ |
| 3750 | if (!(buf->flags & BF_OUT_EMPTY)) { |
| 3751 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 3752 | goto http_msg_closing; |
| 3753 | } |
| 3754 | else { |
| 3755 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 3756 | goto http_msg_closed; |
| 3757 | } |
| 3758 | } |
| 3759 | goto wait_other_side; |
| 3760 | } |
| 3761 | |
| 3762 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 3763 | http_msg_closing: |
| 3764 | /* nothing else to forward, just waiting for the output buffer |
| 3765 | * to be empty and for the shutw_now to take effect. |
| 3766 | */ |
| 3767 | if (buf->flags & BF_OUT_EMPTY) { |
| 3768 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 3769 | goto http_msg_closed; |
| 3770 | } |
| 3771 | else if (buf->flags & BF_SHUTW) { |
| 3772 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 3773 | goto wait_other_side; |
| 3774 | } |
| 3775 | } |
| 3776 | |
| 3777 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 3778 | http_msg_closed: |
| 3779 | /* drop any pending data */ |
| 3780 | buffer_ignore(buf, buf->l - buf->send_max); |
| 3781 | buffer_auto_close(buf); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3782 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3783 | goto wait_other_side; |
| 3784 | } |
| 3785 | |
| 3786 | wait_other_side: |
| 3787 | http_silent_debug(__LINE__, s); |
| 3788 | return txn->rsp.msg_state != old_state || buf->flags != old_flags; |
| 3789 | } |
| 3790 | |
| 3791 | |
| 3792 | /* Resync the request and response state machines. Return 1 if either state |
| 3793 | * changes. |
| 3794 | */ |
| 3795 | int http_resync_states(struct session *s) |
| 3796 | { |
| 3797 | struct http_txn *txn = &s->txn; |
| 3798 | int old_req_state = txn->req.msg_state; |
| 3799 | int old_res_state = txn->rsp.msg_state; |
| 3800 | |
| 3801 | http_silent_debug(__LINE__, s); |
| 3802 | http_sync_req_state(s); |
| 3803 | while (1) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3804 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3805 | if (!http_sync_res_state(s)) |
| 3806 | break; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3807 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3808 | if (!http_sync_req_state(s)) |
| 3809 | break; |
| 3810 | } |
| 3811 | http_silent_debug(__LINE__, s); |
| 3812 | /* OK, both state machines agree on a compatible state. |
| 3813 | * There are a few cases we're interested in : |
| 3814 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 3815 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 3816 | * directions, so let's simply disable both analysers. |
| 3817 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 3818 | * request. |
| 3819 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 3820 | * with server-close mode means we've completed one request and we |
| 3821 | * must re-initialize the server connection. |
| 3822 | */ |
| 3823 | |
| 3824 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 3825 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 3826 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 3827 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
| 3828 | s->req->analysers = 0; |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 3829 | buffer_auto_close(s->req); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3830 | buffer_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3831 | s->rep->analysers = 0; |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 3832 | buffer_auto_close(s->rep); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3833 | buffer_auto_read(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3834 | } |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 3835 | else if (txn->rsp.msg_state == HTTP_MSG_CLOSED || |
| 3836 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
| 3837 | (s->rep->flags & BF_SHUTW)) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3838 | s->rep->analysers = 0; |
| 3839 | buffer_auto_close(s->rep); |
| 3840 | buffer_auto_read(s->rep); |
| 3841 | s->req->analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3842 | buffer_abort(s->req); |
| 3843 | buffer_auto_close(s->req); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3844 | buffer_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3845 | buffer_ignore(s->req, s->req->l - s->req->send_max); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3846 | } |
| 3847 | else if (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 3848 | txn->rsp.msg_state == HTTP_MSG_DONE && |
| 3849 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) { |
| 3850 | /* server-close: terminate this server connection and |
| 3851 | * reinitialize a fresh-new transaction. |
| 3852 | */ |
| 3853 | http_end_txn_clean_session(s); |
| 3854 | } |
| 3855 | |
| 3856 | http_silent_debug(__LINE__, s); |
| 3857 | return txn->req.msg_state != old_req_state || |
| 3858 | txn->rsp.msg_state != old_res_state; |
| 3859 | } |
| 3860 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3861 | /* This function is an analyser which forwards request body (including chunk |
| 3862 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 3863 | * zero byte. The only situation where it must not be called is when we're in |
| 3864 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 3865 | * remaining data and to resync after end of body. It expects the msg_state to |
| 3866 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 3867 | * read more data, or 1 once we can go on with next request or end the session. |
| 3868 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len |
| 3869 | * bytes of pending data + the headers if not already done (between som and sov). |
| 3870 | * It eventually adjusts som to match sov after the data in between have been sent. |
| 3871 | */ |
| 3872 | int http_request_forward_body(struct session *s, struct buffer *req, int an_bit) |
| 3873 | { |
| 3874 | struct http_txn *txn = &s->txn; |
| 3875 | struct http_msg *msg = &s->txn.req; |
| 3876 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 3877 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 3878 | return 0; |
| 3879 | |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 3880 | if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) || |
| 3881 | ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) { |
| 3882 | /* Output closed while we were sending data. We must abort. */ |
| 3883 | buffer_ignore(req, req->l - req->send_max); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3884 | buffer_auto_read(req); |
| 3885 | buffer_auto_close(req); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 3886 | req->analysers &= ~an_bit; |
| 3887 | return 1; |
| 3888 | } |
| 3889 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 3890 | buffer_dont_close(req); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3891 | |
| 3892 | /* Note that we don't have to send 100-continue back because we don't |
| 3893 | * need the data to complete our job, and it's up to the server to |
| 3894 | * decide whether to return 100, 417 or anything else in return of |
| 3895 | * an "Expect: 100-continue" header. |
| 3896 | */ |
| 3897 | |
| 3898 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 3899 | /* we have msg->col and msg->sov which both point to the first |
| 3900 | * byte of message body. msg->som still points to the beginning |
| 3901 | * of the message. We must save the body in req->lr because it |
| 3902 | * survives buffer re-alignments. |
| 3903 | */ |
| 3904 | req->lr = req->data + msg->sov; |
| 3905 | if (txn->flags & TX_REQ_TE_CHNK) |
| 3906 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 3907 | else { |
| 3908 | msg->msg_state = HTTP_MSG_DATA; |
| 3909 | } |
| 3910 | } |
| 3911 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3912 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3913 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 3914 | /* we may have some data pending */ |
| 3915 | if (msg->hdr_content_len || msg->som != msg->sov) { |
| 3916 | int bytes = msg->sov - msg->som; |
| 3917 | if (bytes < 0) /* sov may have wrapped at the end */ |
| 3918 | bytes += req->size; |
| 3919 | buffer_forward(req, bytes + msg->hdr_content_len); |
| 3920 | msg->hdr_content_len = 0; /* don't forward that again */ |
| 3921 | msg->som = msg->sov; |
| 3922 | } |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 3923 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 3924 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 3925 | /* must still forward */ |
| 3926 | if (req->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 3927 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 3928 | |
| 3929 | /* nothing left to forward */ |
| 3930 | if (txn->flags & TX_REQ_TE_CHNK) |
| 3931 | msg->msg_state = HTTP_MSG_DATA_CRLF; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3932 | else |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 3933 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 3934 | } |
| 3935 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3936 | /* read the chunk size and assign it to ->hdr_content_len, then |
| 3937 | * set ->sov and ->lr to point to the body and switch to DATA or |
| 3938 | * TRAILERS state. |
| 3939 | */ |
| 3940 | int ret = http_parse_chunk_size(req, msg); |
| 3941 | |
| 3942 | if (!ret) |
| 3943 | goto missing_data; |
| 3944 | else if (ret < 0) |
| 3945 | goto return_bad_req; |
| 3946 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3947 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3948 | else if (msg->msg_state == HTTP_MSG_DATA_CRLF) { |
| 3949 | /* we want the CRLF after the data */ |
| 3950 | int ret; |
| 3951 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 3952 | req->lr = req->w + req->send_max; |
| 3953 | if (req->lr >= req->data + req->size) |
| 3954 | req->lr -= req->size; |
| 3955 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3956 | ret = http_skip_chunk_crlf(req, msg); |
| 3957 | |
| 3958 | if (ret == 0) |
| 3959 | goto missing_data; |
| 3960 | else if (ret < 0) |
| 3961 | goto return_bad_req; |
| 3962 | /* we're in MSG_CHUNK_SIZE now */ |
| 3963 | } |
| 3964 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
| 3965 | int ret = http_forward_trailers(req, msg); |
| 3966 | |
| 3967 | if (ret == 0) |
| 3968 | goto missing_data; |
| 3969 | else if (ret < 0) |
| 3970 | goto return_bad_req; |
| 3971 | /* we're in HTTP_MSG_DONE now */ |
| 3972 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3973 | else { |
| 3974 | /* other states, DONE...TUNNEL */ |
| 3975 | if (http_resync_states(s)) { |
| 3976 | /* some state changes occurred, maybe the analyser |
| 3977 | * was disabled too. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 3978 | */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3979 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
| 3980 | goto return_bad_req; |
| 3981 | return 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 3982 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3983 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3984 | } |
| 3985 | } |
| 3986 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3987 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 3988 | /* stop waiting for data if the input is closed before the end */ |
| 3989 | if (req->flags & BF_SHUTR) |
| 3990 | goto return_bad_req; |
| 3991 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 3992 | /* waiting for the last bits to leave the buffer */ |
| 3993 | if (req->flags & BF_SHUTW) |
| 3994 | goto return_bad_req; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3995 | |
| 3996 | http_silent_debug(__LINE__, s); |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 3997 | return 0; |
| 3998 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3999 | return_bad_req: /* let's centralize all bad requests */ |
| 4000 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4001 | txn->status = 400; |
| 4002 | /* Note: we don't send any error if some data were already sent */ |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 4003 | stream_int_retnclose(req->prod, (txn->rsp.msg_state < HTTP_MSG_BODY) ? error_message(s, HTTP_ERR_400) : NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4004 | req->analysers = 0; |
| 4005 | s->fe->counters.failed_req++; |
| 4006 | if (s->listener->counters) |
| 4007 | s->listener->counters->failed_req++; |
| 4008 | |
| 4009 | if (!(s->flags & SN_ERR_MASK)) |
| 4010 | s->flags |= SN_ERR_PRXCOND; |
| 4011 | if (!(s->flags & SN_FINST_MASK)) |
| 4012 | s->flags |= SN_FINST_R; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4013 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4014 | return 0; |
| 4015 | } |
| 4016 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4017 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 4018 | * processing can continue on next analysers, or zero if it either needs more |
| 4019 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 4020 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers |
| 4021 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 4022 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4023 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4024 | int http_wait_for_response(struct session *s, struct buffer *rep, int an_bit) |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4025 | { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4026 | struct http_txn *txn = &s->txn; |
| 4027 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4028 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4029 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4030 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4031 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4032 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4033 | 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] | 4034 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4035 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4036 | rep, |
| 4037 | rep->rex, rep->wex, |
| 4038 | rep->flags, |
| 4039 | rep->l, |
| 4040 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 4041 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4042 | /* |
| 4043 | * Now parse the partial (or complete) lines. |
| 4044 | * We will check the response syntax, and also join multi-line |
| 4045 | * headers. An index of all the lines will be elaborated while |
| 4046 | * parsing. |
| 4047 | * |
| 4048 | * For the parsing, we use a 28 states FSM. |
| 4049 | * |
| 4050 | * Here is the information we currently have : |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4051 | * rep->data + msg->som = beginning of response |
| 4052 | * rep->data + msg->eoh = end of processed headers / start of current one |
| 4053 | * msg->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4054 | * rep->lr = first non-visited byte |
| 4055 | * rep->r = end of data |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4056 | * Once we reach MSG_BODY, rep->sol = rep->data + msg->som |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4057 | */ |
| 4058 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4059 | /* There's a protected area at the end of the buffer for rewriting |
| 4060 | * purposes. We don't want to start to parse the request if the |
| 4061 | * protected area is affected, because we may have to move processed |
| 4062 | * data later, which is much more complicated. |
| 4063 | */ |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4064 | if (rep->l && msg->msg_state < HTTP_MSG_ERROR) { |
| 4065 | if (unlikely((rep->flags & BF_FULL) || |
| 4066 | rep->r < rep->lr || |
| 4067 | rep->r > rep->data + rep->size - global.tune.maxrewrite)) { |
| 4068 | if (rep->send_max) { |
| 4069 | /* some data has still not left the buffer, wake us once that's done */ |
| 4070 | buffer_dont_close(rep); |
| 4071 | rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 4072 | return 0; |
| 4073 | } |
| 4074 | if (rep->l <= rep->size - global.tune.maxrewrite) |
| 4075 | http_buffer_heavy_realign(rep, msg); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4076 | } |
| 4077 | |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4078 | if (likely(rep->lr < rep->r)) |
| 4079 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4080 | } |
| 4081 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4082 | /* 1: we might have to print this header in debug mode */ |
| 4083 | if (unlikely((global.mode & MODE_DEBUG) && |
| 4084 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4085 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4086 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4087 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4088 | sol = msg->sol; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4089 | eol = sol + msg->sl.rq.l; |
| 4090 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4091 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4092 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 4093 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4094 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4095 | while (cur_idx) { |
| 4096 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 4097 | debug_hdr("srvhdr", s, sol, eol); |
| 4098 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 4099 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 4100 | } |
| 4101 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4102 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4103 | /* |
| 4104 | * Now we quickly check if we have found a full valid response. |
| 4105 | * If not so, we check the FD and buffer states before leaving. |
| 4106 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4107 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4108 | * responses are checked first. |
| 4109 | * |
| 4110 | * Depending on whether the client is still there or not, we |
| 4111 | * may send an error response back or not. Note that normally |
| 4112 | * we should only check for HTTP status there, and check I/O |
| 4113 | * errors somewhere else. |
| 4114 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4115 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4116 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4117 | /* Invalid response */ |
| 4118 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4119 | /* we detected a parsing error. We want to archive this response |
| 4120 | * in the dedicated proxy area for later troubleshooting. |
| 4121 | */ |
| 4122 | hdr_response_bad: |
| 4123 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
| 4124 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe); |
| 4125 | |
| 4126 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4127 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4128 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4129 | health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP); |
| 4130 | } |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4131 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4132 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4133 | rep->analysers = 0; |
| 4134 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4135 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4136 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
| 4137 | |
| 4138 | if (!(s->flags & SN_ERR_MASK)) |
| 4139 | s->flags |= SN_ERR_PRXCOND; |
| 4140 | if (!(s->flags & SN_FINST_MASK)) |
| 4141 | s->flags |= SN_FINST_H; |
| 4142 | |
| 4143 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4144 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4145 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4146 | /* too large response does not fit in buffer. */ |
| 4147 | else if (rep->flags & BF_FULL) { |
| 4148 | goto hdr_response_bad; |
| 4149 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4150 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4151 | /* read error */ |
| 4152 | else if (rep->flags & BF_READ_ERROR) { |
| 4153 | if (msg->err_pos >= 0) |
| 4154 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4155 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4156 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4157 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4158 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4159 | health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR); |
| 4160 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 4161 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4162 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4163 | rep->analysers = 0; |
| 4164 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4165 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4166 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 4167 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4168 | if (!(s->flags & SN_ERR_MASK)) |
| 4169 | s->flags |= SN_ERR_SRVCL; |
| 4170 | if (!(s->flags & SN_FINST_MASK)) |
| 4171 | s->flags |= SN_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 4172 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4173 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4174 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4175 | /* read timeout : return a 504 to the client. */ |
| 4176 | else if (rep->flags & BF_READ_TIMEOUT) { |
| 4177 | if (msg->err_pos >= 0) |
| 4178 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4179 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4180 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4181 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4182 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4183 | health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT); |
| 4184 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4185 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4186 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4187 | rep->analysers = 0; |
| 4188 | txn->status = 504; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4189 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4190 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4191 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4192 | if (!(s->flags & SN_ERR_MASK)) |
| 4193 | s->flags |= SN_ERR_SRVTO; |
| 4194 | if (!(s->flags & SN_FINST_MASK)) |
| 4195 | s->flags |= SN_FINST_H; |
| 4196 | return 0; |
| 4197 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 4198 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4199 | /* close from server */ |
| 4200 | else if (rep->flags & BF_SHUTR) { |
| 4201 | if (msg->err_pos >= 0) |
| 4202 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4203 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4204 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4205 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4206 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4207 | health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE); |
| 4208 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4209 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4210 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4211 | rep->analysers = 0; |
| 4212 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4213 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4214 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4215 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4216 | if (!(s->flags & SN_ERR_MASK)) |
| 4217 | s->flags |= SN_ERR_SRVCL; |
| 4218 | if (!(s->flags & SN_FINST_MASK)) |
| 4219 | s->flags |= SN_FINST_H; |
| 4220 | return 0; |
| 4221 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4222 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4223 | /* write error to client (we don't send any message then) */ |
| 4224 | else if (rep->flags & BF_WRITE_ERROR) { |
| 4225 | if (msg->err_pos >= 0) |
| 4226 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe); |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4227 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4228 | s->be->counters.failed_resp++; |
| 4229 | rep->analysers = 0; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4230 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4231 | |
| 4232 | if (!(s->flags & SN_ERR_MASK)) |
| 4233 | s->flags |= SN_ERR_CLICL; |
| 4234 | if (!(s->flags & SN_FINST_MASK)) |
| 4235 | s->flags |= SN_FINST_H; |
| 4236 | |
| 4237 | /* process_session() will take care of the error */ |
| 4238 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4239 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4240 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4241 | buffer_dont_close(rep); |
| 4242 | return 0; |
| 4243 | } |
| 4244 | |
| 4245 | /* More interesting part now : we know that we have a complete |
| 4246 | * response which at least looks like HTTP. We have an indicator |
| 4247 | * of each header's length, so we can parse them quickly. |
| 4248 | */ |
| 4249 | |
| 4250 | if (unlikely(msg->err_pos >= 0)) |
| 4251 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe); |
| 4252 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4253 | /* |
| 4254 | * 1: get the status code |
| 4255 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4256 | n = msg->sol[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4257 | if (n < 1 || n > 5) |
| 4258 | n = 0; |
| 4259 | s->srv->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4260 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4261 | /* check if the response is HTTP/1.1 or above */ |
| 4262 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4263 | ((msg->sol[5] > '1') || |
| 4264 | ((msg->sol[5] == '1') && |
| 4265 | (msg->sol[7] >= '1')))) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4266 | txn->flags |= TX_RES_VER_11; |
| 4267 | |
| 4268 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4269 | txn->flags &= ~(TX_HDR_CONN_PRS|TX_HDR_CONN_CLO|TX_HDR_CONN_KAL|TX_CON_CLO_SET|TX_CON_KAL_SET); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4270 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4271 | /* transfer length unknown*/ |
| 4272 | txn->flags &= ~TX_RES_XFER_LEN; |
| 4273 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4274 | txn->status = strl2ui(msg->sol + msg->sl.st.c, msg->sl.st.c_l); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4275 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4276 | if (txn->status >= 100 && txn->status < 500) |
| 4277 | health_adjust(s->srv, HANA_STATUS_HTTP_OK); |
| 4278 | else |
| 4279 | health_adjust(s->srv, HANA_STATUS_HTTP_STS); |
| 4280 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4281 | /* |
| 4282 | * 2: check for cacheability. |
| 4283 | */ |
| 4284 | |
| 4285 | switch (txn->status) { |
| 4286 | case 200: |
| 4287 | case 203: |
| 4288 | case 206: |
| 4289 | case 300: |
| 4290 | case 301: |
| 4291 | case 410: |
| 4292 | /* RFC2616 @13.4: |
| 4293 | * "A response received with a status code of |
| 4294 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 4295 | * by a cache (...) unless a cache-control |
| 4296 | * directive prohibits caching." |
| 4297 | * |
| 4298 | * RFC2616 @9.5: POST method : |
| 4299 | * "Responses to this method are not cacheable, |
| 4300 | * unless the response includes appropriate |
| 4301 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4302 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4303 | if (likely(txn->meth != HTTP_METH_POST) && |
| 4304 | (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
| 4305 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 4306 | break; |
| 4307 | default: |
| 4308 | break; |
| 4309 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4310 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4311 | /* |
| 4312 | * 3: we may need to capture headers |
| 4313 | */ |
| 4314 | s->logs.logwait &= ~LW_RESP; |
| 4315 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap)) |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4316 | capture_headers(msg->sol, &txn->hdr_idx, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4317 | txn->rsp.cap, s->fe->rsp_cap); |
| 4318 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4319 | /* 4: determine the transfer-length. |
| 4320 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 4321 | * the presence of a message-body in a RESPONSE and its transfer length |
| 4322 | * must be determined that way : |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4323 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4324 | * All responses to the HEAD request method MUST NOT include a |
| 4325 | * message-body, even though the presence of entity-header fields |
| 4326 | * might lead one to believe they do. All 1xx (informational), 204 |
| 4327 | * (No Content), and 304 (Not Modified) responses MUST NOT include a |
| 4328 | * message-body. All other responses do include a message-body, |
| 4329 | * although it MAY be of zero length. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4330 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4331 | * 1. Any response which "MUST NOT" include a message-body (such as the |
| 4332 | * 1xx, 204 and 304 responses and any response to a HEAD request) is |
| 4333 | * always terminated by the first empty line after the header fields, |
| 4334 | * regardless of the entity-header fields present in the message. |
| 4335 | * |
| 4336 | * 2. If a Transfer-Encoding header field (Section 9.7) is present and |
| 4337 | * the "chunked" transfer-coding (Section 6.2) is used, the |
| 4338 | * transfer-length is defined by the use of this transfer-coding. |
| 4339 | * If a Transfer-Encoding header field is present and the "chunked" |
| 4340 | * transfer-coding is not present, the transfer-length is defined by |
| 4341 | * the sender closing the connection. |
| 4342 | * |
| 4343 | * 3. If a Content-Length header field is present, its decimal value in |
| 4344 | * OCTETs represents both the entity-length and the transfer-length. |
| 4345 | * If a message is received with both a Transfer-Encoding header |
| 4346 | * field and a Content-Length header field, the latter MUST be ignored. |
| 4347 | * |
| 4348 | * 4. If the message uses the media type "multipart/byteranges", and |
| 4349 | * the transfer-length is not otherwise specified, then this self- |
| 4350 | * delimiting media type defines the transfer-length. This media |
| 4351 | * type MUST NOT be used unless the sender knows that the recipient |
| 4352 | * can parse it; the presence in a request of a Range header with |
| 4353 | * multiple byte-range specifiers from a 1.1 client implies that the |
| 4354 | * client can parse multipart/byteranges responses. |
| 4355 | * |
| 4356 | * 5. By the server closing the connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4357 | */ |
| 4358 | |
| 4359 | /* Skip parsing if no content length is possible. The response flags |
| 4360 | * remain 0 as well as the hdr_content_len, which may or may not mirror |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4361 | * the real header value, and we note that we know the response's length. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4362 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 4363 | */ |
| 4364 | if (txn->meth == HTTP_METH_HEAD || |
| 4365 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4366 | txn->status == 204 || txn->status == 304) { |
| 4367 | txn->flags |= TX_RES_XFER_LEN; |
| 4368 | goto skip_content_length; |
| 4369 | } |
| 4370 | |
| 4371 | if (txn->meth == HTTP_METH_CONNECT) |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4372 | goto skip_content_length; |
| 4373 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4374 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4375 | ctx.idx = 0; |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 4376 | while ((txn->flags & TX_RES_VER_11) && |
| 4377 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4378 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
| 4379 | txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN); |
| 4380 | else if (txn->flags & TX_RES_TE_CHNK) { |
| 4381 | /* bad transfer-encoding (chunked followed by something else) */ |
| 4382 | use_close_only = 1; |
| 4383 | txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN); |
| 4384 | break; |
| 4385 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4386 | } |
| 4387 | |
| 4388 | /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */ |
| 4389 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4390 | while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only && |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4391 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) { |
| 4392 | signed long long cl; |
| 4393 | |
| 4394 | if (!ctx.vlen) |
| 4395 | goto hdr_response_bad; |
| 4396 | |
| 4397 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) |
| 4398 | goto hdr_response_bad; /* parse failure */ |
| 4399 | |
| 4400 | if (cl < 0) |
| 4401 | goto hdr_response_bad; |
| 4402 | |
| 4403 | if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl)) |
| 4404 | goto hdr_response_bad; /* already specified, was different */ |
| 4405 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4406 | txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4407 | msg->hdr_content_len = cl; |
| 4408 | } |
| 4409 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4410 | /* FIXME: we should also implement the multipart/byterange method. |
| 4411 | * For now on, we resort to close mode in this case (unknown length). |
| 4412 | */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4413 | skip_content_length: |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4414 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4415 | /* end of job, return OK */ |
| 4416 | rep->analysers &= ~an_bit; |
| 4417 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4418 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4419 | return 1; |
| 4420 | } |
| 4421 | |
| 4422 | /* This function performs all the processing enabled for the current response. |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 4423 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 4424 | * and updates t->rep->analysers. It might make sense to explode it into several |
| 4425 | * other functions. It works like process_request (see indications above). |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4426 | */ |
| 4427 | int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px) |
| 4428 | { |
| 4429 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4430 | struct http_msg *msg = &txn->rsp; |
| 4431 | struct proxy *cur_proxy; |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 4432 | struct wordlist *wl; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4433 | |
| 4434 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 4435 | now_ms, __FUNCTION__, |
| 4436 | t, |
| 4437 | rep, |
| 4438 | rep->rex, rep->wex, |
| 4439 | rep->flags, |
| 4440 | rep->l, |
| 4441 | rep->analysers); |
| 4442 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4443 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4444 | return 0; |
| 4445 | |
| 4446 | rep->analysers &= ~an_bit; |
| 4447 | rep->analyse_exp = TICK_ETERNITY; |
| 4448 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4449 | /* Now we have to check if we need to modify the Connection header. |
| 4450 | * This is more difficult on the response than it is on the request, |
| 4451 | * because we can have two different HTTP versions and we don't know |
| 4452 | * how the client will interprete a response. For instance, let's say |
| 4453 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 4454 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 4455 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 4456 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 4457 | * the lack of header as a keep-alive. Thus we will use two flags |
| 4458 | * indicating how a request MAY be understood by the client. In case |
| 4459 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 4460 | * ambiguous cases such as both close and keepalive are seen, then we |
| 4461 | * will fall back to explicit close. Note that we won't take risks with |
| 4462 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4463 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4464 | */ |
| 4465 | |
| 4466 | if ((txn->meth != HTTP_METH_CONNECT) && |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4467 | (txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 4468 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
| 4469 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4470 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4471 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4472 | /* on unknown transfer length, we must close */ |
| 4473 | if (!(txn->flags & TX_RES_XFER_LEN) && |
| 4474 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 4475 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4476 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4477 | /* now adjust header transformations depending on current state */ |
| 4478 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 4479 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4480 | to_del |= 2; /* remove "keep-alive" on any response */ |
| 4481 | if (!(txn->flags & TX_RES_VER_11)) |
| 4482 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4483 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4484 | else { /* SCL / KAL */ |
| 4485 | to_del |= 1; /* remove "close" on any response */ |
| 4486 | if ((txn->flags & (TX_RES_VER_11|TX_REQ_VER_11)) == (TX_RES_VER_11|TX_REQ_VER_11)) |
| 4487 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4488 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4489 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4490 | /* Parse and remove some headers from the connection header */ |
| 4491 | http_parse_connection_header(txn, msg, rep, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4492 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4493 | /* Some keep-alive responses are converted to Server-close if |
| 4494 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4495 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4496 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 4497 | if ((txn->flags & TX_HDR_CONN_CLO) || |
| 4498 | (txn->flags & (TX_HDR_CONN_KAL|TX_RES_VER_11)) == 0) |
| 4499 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4500 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4501 | } |
| 4502 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4503 | if (1) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4504 | /* |
| 4505 | * 3: we will have to evaluate the filters. |
| 4506 | * As opposed to version 1.2, now they will be evaluated in the |
| 4507 | * filters order and not in the header order. This means that |
| 4508 | * each filter has to be validated among all headers. |
| 4509 | * |
| 4510 | * Filters are tried with ->be first, then with ->fe if it is |
| 4511 | * different from ->be. |
| 4512 | */ |
| 4513 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4514 | cur_proxy = t->be; |
| 4515 | while (1) { |
| 4516 | struct proxy *rule_set = cur_proxy; |
| 4517 | |
| 4518 | /* try headers filters */ |
| 4519 | if (rule_set->rsp_exp != NULL) { |
| 4520 | if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) { |
| 4521 | return_bad_resp: |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4522 | if (t->srv) { |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4523 | t->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4524 | health_adjust(t->srv, HANA_STATUS_HTTP_RSP); |
| 4525 | } |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4526 | cur_proxy->counters.failed_resp++; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4527 | return_srv_prx_502: |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 4528 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4529 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4530 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 8e89b84 | 2009-10-18 23:56:35 +0200 | [diff] [blame] | 4531 | stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4532 | if (!(t->flags & SN_ERR_MASK)) |
| 4533 | t->flags |= SN_ERR_PRXCOND; |
| 4534 | if (!(t->flags & SN_FINST_MASK)) |
| 4535 | t->flags |= SN_FINST_H; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 4536 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4537 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4538 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4539 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4540 | /* has the response been denied ? */ |
| 4541 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | 8365f93 | 2009-03-15 23:11:49 +0100 | [diff] [blame] | 4542 | if (t->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4543 | t->srv->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4544 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4545 | cur_proxy->counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4546 | if (t->listener->counters) |
| 4547 | t->listener->counters->denied_resp++; |
| 4548 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4549 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 4550 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4551 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4552 | /* add response headers from the rule sets in the same order */ |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 4553 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 4554 | if (txn->status < 200) |
| 4555 | break; |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 4556 | if (unlikely(http_header_add_tail(rep, &txn->rsp, &txn->hdr_idx, wl->s) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4557 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 4558 | } |
| 4559 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4560 | /* check whether we're already working on the frontend */ |
| 4561 | if (cur_proxy == t->fe) |
| 4562 | break; |
| 4563 | cur_proxy = t->fe; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4564 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4565 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4566 | /* |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4567 | * We may be facing a 1xx response (100 continue, 101 switching protocols), |
| 4568 | * in which case this is not the right response, and we're waiting for the |
| 4569 | * next one. Let's allow this response to go to the client and wait for the |
| 4570 | * next one. |
| 4571 | */ |
| 4572 | if (txn->status < 200) { |
| 4573 | hdr_idx_init(&txn->hdr_idx); |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4574 | buffer_forward(rep, rep->lr - msg->sol); |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4575 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 4576 | txn->status = 0; |
| 4577 | rep->analysers |= AN_RES_WAIT_HTTP | an_bit; |
| 4578 | return 1; |
| 4579 | } |
| 4580 | |
| 4581 | /* we don't have any 1xx status code now */ |
| 4582 | |
| 4583 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4584 | * 4: check for server cookie. |
| 4585 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4586 | if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name || |
| 4587 | (t->be->options & PR_O_CHK_CACHE)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4588 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4589 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4590 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4591 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4592 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4593 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4594 | if ((t->be->options & (PR_O_COOK_NOC | PR_O_CHK_CACHE)) != 0) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4595 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4596 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4597 | /* |
| 4598 | * 6: add server cookie in the response if needed |
| 4599 | */ |
| 4600 | if ((t->srv) && !(t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_INS) && |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4601 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4602 | int len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4603 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4604 | /* the server is known, it's not the one the client requested, we have to |
| 4605 | * insert a set-cookie here, except if we want to insert only on POST |
| 4606 | * requests and this one isn't. Note that servers which don't have cookies |
| 4607 | * (eg: some backup servers) will return a full cookie removal request. |
| 4608 | */ |
| 4609 | len = sprintf(trash, "Set-Cookie: %s=%s; path=/", |
| 4610 | t->be->cookie_name, |
| 4611 | 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] | 4612 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4613 | if (t->be->cookie_domain) |
| 4614 | len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4615 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4616 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 4617 | trash, len) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4618 | goto return_bad_resp; |
| 4619 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4620 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4621 | /* Here, we will tell an eventual cache on the client side that we don't |
| 4622 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 4623 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 4624 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 4625 | */ |
| 4626 | if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4627 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4628 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 4629 | |
| 4630 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 4631 | "Cache-control: private", 22) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4632 | goto return_bad_resp; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4633 | } |
| 4634 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4635 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4636 | /* |
| 4637 | * 7: check if result will be cacheable with a cookie. |
| 4638 | * We'll block the response if security checks have caught |
| 4639 | * nasty things such as a cacheable cookie. |
| 4640 | */ |
| 4641 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) == |
| 4642 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) && |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4643 | (t->be->options & PR_O_CHK_CACHE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4644 | |
| 4645 | /* we're in presence of a cacheable response containing |
| 4646 | * a set-cookie header. We'll block it as requested by |
| 4647 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4648 | */ |
Willy Tarreau | 8365f93 | 2009-03-15 23:11:49 +0100 | [diff] [blame] | 4649 | if (t->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4650 | t->srv->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4651 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4652 | cur_proxy->counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4653 | if (t->listener->counters) |
| 4654 | t->listener->counters->denied_resp++; |
| 4655 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4656 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 4657 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 4658 | send_log(t->be, LOG_ALERT, |
| 4659 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 4660 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 4661 | goto return_srv_prx_502; |
| 4662 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4663 | |
| 4664 | /* |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4665 | * 8: adjust "Connection: close" or "Connection: keep-alive" if needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4666 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4667 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
| 4668 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) { |
| 4669 | unsigned int want_flags = 0; |
| 4670 | |
| 4671 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 4672 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4673 | /* we want a keep-alive response here. Keep-alive header |
| 4674 | * required if either side is not 1.1. |
| 4675 | */ |
| 4676 | if ((txn->flags & (TX_REQ_VER_11|TX_RES_VER_11)) != (TX_REQ_VER_11|TX_RES_VER_11)) |
| 4677 | want_flags |= TX_CON_KAL_SET; |
| 4678 | } |
| 4679 | else { |
| 4680 | /* we want a close response here. Close header required if |
| 4681 | * the server is 1.1, regardless of the client. |
| 4682 | */ |
| 4683 | if (txn->flags & TX_RES_VER_11) |
| 4684 | want_flags |= TX_CON_CLO_SET; |
| 4685 | } |
| 4686 | |
| 4687 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 4688 | http_change_connection_header(txn, msg, rep, want_flags); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4689 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4690 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4691 | if (txn->flags & TX_RES_XFER_LEN) |
| 4692 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 4693 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4694 | /************************************************************* |
| 4695 | * OK, that's finished for the headers. We have done what we * |
| 4696 | * could. Let's switch to the DATA state. * |
| 4697 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4698 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4699 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4700 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4701 | /* if the user wants to log as soon as possible, without counting |
| 4702 | * bytes from the server, then this is the right moment. We have |
| 4703 | * to temporarily assign bytes_out to log what we currently have. |
| 4704 | */ |
| 4705 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 4706 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 4707 | t->logs.bytes_out = txn->rsp.eoh; |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 4708 | t->do_log(t); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4709 | t->logs.bytes_out = 0; |
| 4710 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4711 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4712 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 4713 | * otherwise we would not let the client side wake up. |
| 4714 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4715 | |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 4716 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4717 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 4718 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4719 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4720 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4721 | /* This function is an analyser which forwards response body (including chunk |
| 4722 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 4723 | * zero byte. The only situation where it must not be called is when we're in |
| 4724 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 4725 | * remaining data and to resync after end of body. It expects the msg_state to |
| 4726 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 4727 | * read more data, or 1 once we can go on with next request or end the session. |
| 4728 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len |
| 4729 | * bytes of pending data + the headers if not already done (between som and sov). |
| 4730 | * It eventually adjusts som to match sov after the data in between have been sent. |
| 4731 | */ |
| 4732 | int http_response_forward_body(struct session *s, struct buffer *res, int an_bit) |
| 4733 | { |
| 4734 | struct http_txn *txn = &s->txn; |
| 4735 | struct http_msg *msg = &s->txn.rsp; |
| 4736 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4737 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4738 | return 0; |
| 4739 | |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 4740 | if ((res->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) || |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4741 | ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) || |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 4742 | !s->req->analysers) { |
| 4743 | /* in case of error or if the other analyser went away, we can't analyse HTTP anymore */ |
| 4744 | buffer_ignore(res, res->l - res->send_max); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4745 | buffer_auto_read(res); |
| 4746 | buffer_auto_close(res); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 4747 | res->analysers &= ~an_bit; |
| 4748 | return 1; |
| 4749 | } |
| 4750 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 4751 | buffer_dont_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4752 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4753 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4754 | /* we have msg->col and msg->sov which both point to the first |
| 4755 | * byte of message body. msg->som still points to the beginning |
| 4756 | * of the message. We must save the body in req->lr because it |
| 4757 | * survives buffer re-alignments. |
| 4758 | */ |
| 4759 | res->lr = res->data + msg->sov; |
| 4760 | if (txn->flags & TX_RES_TE_CHNK) |
| 4761 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4762 | else { |
| 4763 | msg->msg_state = HTTP_MSG_DATA; |
| 4764 | } |
| 4765 | } |
| 4766 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4767 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4768 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4769 | /* we may have some data pending */ |
| 4770 | if (msg->hdr_content_len || msg->som != msg->sov) { |
| 4771 | int bytes = msg->sov - msg->som; |
| 4772 | if (bytes < 0) /* sov may have wrapped at the end */ |
| 4773 | bytes += res->size; |
| 4774 | buffer_forward(res, bytes + msg->hdr_content_len); |
| 4775 | msg->hdr_content_len = 0; /* don't forward that again */ |
| 4776 | msg->som = msg->sov; |
| 4777 | } |
| 4778 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4779 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 4780 | /* must still forward */ |
| 4781 | if (res->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4782 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4783 | |
| 4784 | /* nothing left to forward */ |
| 4785 | if (txn->flags & TX_RES_TE_CHNK) |
| 4786 | msg->msg_state = HTTP_MSG_DATA_CRLF; |
| 4787 | else |
| 4788 | msg->msg_state = HTTP_MSG_DONE; |
| 4789 | } |
| 4790 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4791 | /* read the chunk size and assign it to ->hdr_content_len, then |
| 4792 | * set ->sov to point to the body and switch to DATA or TRAILERS state. |
| 4793 | */ |
| 4794 | int ret = http_parse_chunk_size(res, msg); |
| 4795 | |
| 4796 | if (!ret) |
| 4797 | goto missing_data; |
| 4798 | else if (ret < 0) |
| 4799 | goto return_bad_res; |
| 4800 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4801 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4802 | else if (msg->msg_state == HTTP_MSG_DATA_CRLF) { |
| 4803 | /* we want the CRLF after the data */ |
| 4804 | int ret; |
| 4805 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 4806 | res->lr = res->w + res->send_max; |
| 4807 | if (res->lr >= res->data + res->size) |
| 4808 | res->lr -= res->size; |
| 4809 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4810 | ret = http_skip_chunk_crlf(res, msg); |
| 4811 | |
| 4812 | if (!ret) |
| 4813 | goto missing_data; |
| 4814 | else if (ret < 0) |
| 4815 | goto return_bad_res; |
| 4816 | /* we're in MSG_CHUNK_SIZE now */ |
| 4817 | } |
| 4818 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
| 4819 | int ret = http_forward_trailers(res, msg); |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 4820 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4821 | if (ret == 0) |
| 4822 | goto missing_data; |
| 4823 | else if (ret < 0) |
| 4824 | goto return_bad_res; |
| 4825 | /* we're in HTTP_MSG_DONE now */ |
| 4826 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4827 | else { |
| 4828 | /* other states, DONE...TUNNEL */ |
| 4829 | if (http_resync_states(s)) { |
| 4830 | http_silent_debug(__LINE__, s); |
| 4831 | /* some state changes occurred, maybe the analyser |
| 4832 | * was disabled too. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 4833 | */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4834 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
| 4835 | goto return_bad_res; |
| 4836 | return 1; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 4837 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4838 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4839 | } |
| 4840 | } |
| 4841 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4842 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4843 | /* stop waiting for data if the input is closed before the end */ |
| 4844 | if (res->flags & BF_SHUTR) |
| 4845 | goto return_bad_res; |
| 4846 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4847 | if (!s->req->analysers) |
| 4848 | goto return_bad_res; |
| 4849 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4850 | /* forward the chunk size as well as any pending data */ |
| 4851 | if (msg->hdr_content_len || msg->som != msg->sov) { |
| 4852 | buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len); |
| 4853 | msg->hdr_content_len = 0; /* don't forward that again */ |
| 4854 | msg->som = msg->sov; |
| 4855 | } |
| 4856 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4857 | /* the session handler will take care of timeouts and errors */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4858 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4859 | return 0; |
| 4860 | |
| 4861 | return_bad_res: /* let's centralize all bad resuests */ |
| 4862 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 4863 | txn->status = 502; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 4864 | /* don't send any error message as we're in the body */ |
| 4865 | stream_int_retnclose(res->cons, NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4866 | res->analysers = 0; |
| 4867 | s->be->counters.failed_resp++; |
| 4868 | if (s->srv) { |
| 4869 | s->srv->counters.failed_resp++; |
| 4870 | health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP); |
| 4871 | } |
| 4872 | |
| 4873 | if (!(s->flags & SN_ERR_MASK)) |
| 4874 | s->flags |= SN_ERR_PRXCOND; |
| 4875 | if (!(s->flags & SN_FINST_MASK)) |
| 4876 | s->flags |= SN_FINST_R; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4877 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4878 | return 0; |
| 4879 | } |
| 4880 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4881 | /* Iterate the same filter through all request headers. |
| 4882 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4883 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 4884 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4885 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4886 | 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] | 4887 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4888 | char term; |
| 4889 | char *cur_ptr, *cur_end, *cur_next; |
| 4890 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4891 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4892 | struct hdr_idx_elem *cur_hdr; |
| 4893 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 4894 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4895 | last_hdr = 0; |
| 4896 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4897 | cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4898 | old_idx = 0; |
| 4899 | |
| 4900 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4901 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4902 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4903 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4904 | (exp->action == ACT_ALLOW || |
| 4905 | exp->action == ACT_DENY || |
| 4906 | exp->action == ACT_TARPIT)) |
| 4907 | return 0; |
| 4908 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4909 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4910 | if (!cur_idx) |
| 4911 | break; |
| 4912 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4913 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4914 | cur_ptr = cur_next; |
| 4915 | cur_end = cur_ptr + cur_hdr->len; |
| 4916 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4917 | |
| 4918 | /* Now we have one header between cur_ptr and cur_end, |
| 4919 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4920 | */ |
| 4921 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4922 | /* The annoying part is that pattern matching needs |
| 4923 | * that we modify the contents to null-terminate all |
| 4924 | * strings before testing them. |
| 4925 | */ |
| 4926 | |
| 4927 | term = *cur_end; |
| 4928 | *cur_end = '\0'; |
| 4929 | |
| 4930 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4931 | switch (exp->action) { |
| 4932 | case ACT_SETBE: |
| 4933 | /* It is not possible to jump a second time. |
| 4934 | * FIXME: should we return an HTTP/500 here so that |
| 4935 | * the admin knows there's a problem ? |
| 4936 | */ |
| 4937 | if (t->be != t->fe) |
| 4938 | break; |
| 4939 | |
| 4940 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4941 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4942 | last_hdr = 1; |
| 4943 | break; |
| 4944 | |
| 4945 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4946 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4947 | last_hdr = 1; |
| 4948 | break; |
| 4949 | |
| 4950 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4951 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4952 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4953 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4954 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4955 | if (t->listener->counters) |
| 4956 | t->listener->counters->denied_resp++; |
| 4957 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4958 | break; |
| 4959 | |
| 4960 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4961 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4962 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4963 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4964 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4965 | if (t->listener->counters) |
| 4966 | t->listener->counters->denied_resp++; |
| 4967 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4968 | break; |
| 4969 | |
| 4970 | case ACT_REPLACE: |
| 4971 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 4972 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 4973 | /* FIXME: if the user adds a newline in the replacement, the |
| 4974 | * index will not be recalculated for now, and the new line |
| 4975 | * will not be counted as a new header. |
| 4976 | */ |
| 4977 | |
| 4978 | cur_end += delta; |
| 4979 | cur_next += delta; |
| 4980 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 4981 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4982 | break; |
| 4983 | |
| 4984 | case ACT_REMOVE: |
| 4985 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 4986 | cur_next += delta; |
| 4987 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 4988 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4989 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 4990 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4991 | cur_hdr->len = 0; |
| 4992 | cur_end = NULL; /* null-term has been rewritten */ |
| 4993 | break; |
| 4994 | |
| 4995 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4996 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4997 | if (cur_end) |
| 4998 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4999 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5000 | /* keep the link from this header to next one in case of later |
| 5001 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5002 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5003 | old_idx = cur_idx; |
| 5004 | } |
| 5005 | return 0; |
| 5006 | } |
| 5007 | |
| 5008 | |
| 5009 | /* Apply the filter to the request line. |
| 5010 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 5011 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5012 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5013 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5014 | */ |
| 5015 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 5016 | { |
| 5017 | char term; |
| 5018 | char *cur_ptr, *cur_end; |
| 5019 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5020 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5021 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5022 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5023 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5024 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5025 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5026 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5027 | (exp->action == ACT_ALLOW || |
| 5028 | exp->action == ACT_DENY || |
| 5029 | exp->action == ACT_TARPIT)) |
| 5030 | return 0; |
| 5031 | else if (exp->action == ACT_REMOVE) |
| 5032 | return 0; |
| 5033 | |
| 5034 | done = 0; |
| 5035 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5036 | cur_ptr = txn->req.sol; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5037 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5038 | |
| 5039 | /* Now we have the request line between cur_ptr and cur_end */ |
| 5040 | |
| 5041 | /* The annoying part is that pattern matching needs |
| 5042 | * that we modify the contents to null-terminate all |
| 5043 | * strings before testing them. |
| 5044 | */ |
| 5045 | |
| 5046 | term = *cur_end; |
| 5047 | *cur_end = '\0'; |
| 5048 | |
| 5049 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5050 | switch (exp->action) { |
| 5051 | case ACT_SETBE: |
| 5052 | /* It is not possible to jump a second time. |
| 5053 | * FIXME: should we return an HTTP/500 here so that |
| 5054 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5055 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5056 | if (t->be != t->fe) |
| 5057 | break; |
| 5058 | |
| 5059 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 5060 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5061 | done = 1; |
| 5062 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5063 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5064 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5065 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5066 | done = 1; |
| 5067 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5068 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5069 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5070 | txn->flags |= TX_CLDENY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5071 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 5072 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5073 | if (t->listener->counters) |
| 5074 | t->listener->counters->denied_resp++; |
| 5075 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5076 | done = 1; |
| 5077 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5078 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5079 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5080 | txn->flags |= TX_CLTARPIT; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5081 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 5082 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5083 | if (t->listener->counters) |
| 5084 | t->listener->counters->denied_resp++; |
| 5085 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5086 | done = 1; |
| 5087 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5088 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5089 | case ACT_REPLACE: |
| 5090 | *cur_end = term; /* restore the string terminator */ |
| 5091 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5092 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 5093 | /* FIXME: if the user adds a newline in the replacement, the |
| 5094 | * index will not be recalculated for now, and the new line |
| 5095 | * will not be counted as a new header. |
| 5096 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5097 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5098 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5099 | cur_end += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5100 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5101 | HTTP_MSG_RQMETH, |
| 5102 | cur_ptr, cur_end + 1, |
| 5103 | NULL, NULL); |
| 5104 | if (unlikely(!cur_end)) |
| 5105 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5106 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5107 | /* we have a full request and we know that we have either a CR |
| 5108 | * or an LF at <ptr>. |
| 5109 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5110 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 5111 | 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] | 5112 | /* there is no point trying this regex on headers */ |
| 5113 | return 1; |
| 5114 | } |
| 5115 | } |
| 5116 | *cur_end = term; /* restore the string terminator */ |
| 5117 | return done; |
| 5118 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 5119 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5120 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5121 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5122 | /* |
| 5123 | * Apply all the req filters <exp> to all headers in buffer <req> of session <t>. |
| 5124 | * 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] | 5125 | * unparsable request. Since it can manage the switch to another backend, it |
| 5126 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5127 | */ |
| 5128 | int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 5129 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5130 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5131 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5132 | while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5133 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5134 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5135 | /* |
| 5136 | * The interleaving of transformations and verdicts |
| 5137 | * makes it difficult to decide to continue or stop |
| 5138 | * the evaluation. |
| 5139 | */ |
| 5140 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5141 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5142 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 5143 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) { |
| 5144 | exp = exp->next; |
| 5145 | continue; |
| 5146 | } |
| 5147 | |
| 5148 | /* Apply the filter to the request line. */ |
| 5149 | ret = apply_filter_to_req_line(t, req, exp); |
| 5150 | if (unlikely(ret < 0)) |
| 5151 | return -1; |
| 5152 | |
| 5153 | if (likely(ret == 0)) { |
| 5154 | /* The filter did not match the request, it can be |
| 5155 | * iterated through all headers. |
| 5156 | */ |
| 5157 | apply_filter_to_req_headers(t, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5158 | } |
| 5159 | exp = exp->next; |
| 5160 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5161 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5162 | } |
| 5163 | |
| 5164 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5165 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5166 | /* |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5167 | * Try to retrieve the server associated to the appsession. |
| 5168 | * If the server is found, it's assigned to the session. |
| 5169 | */ |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5170 | void manage_client_side_appsession(struct session *t, const char *buf, int len) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5171 | struct http_txn *txn = &t->txn; |
| 5172 | appsess *asession = NULL; |
| 5173 | char *sessid_temp = NULL; |
| 5174 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5175 | if (len > t->be->appsession_len) { |
| 5176 | len = t->be->appsession_len; |
| 5177 | } |
| 5178 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5179 | if (t->be->options2 & PR_O2_AS_REQL) { |
| 5180 | /* request-learn option is enabled : store the sessid in the session for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5181 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5182 | /* free previously allocated memory as we don't need the session id found in the URL anymore */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5183 | pool_free2(apools.sessid, txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5184 | } |
| 5185 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5186 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5187 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5188 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5189 | return; |
| 5190 | } |
| 5191 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5192 | memcpy(txn->sessid, buf, len); |
| 5193 | txn->sessid[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5194 | } |
| 5195 | |
| 5196 | if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) { |
| 5197 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5198 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5199 | return; |
| 5200 | } |
| 5201 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5202 | memcpy(sessid_temp, buf, len); |
| 5203 | sessid_temp[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5204 | |
| 5205 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp); |
| 5206 | /* free previously allocated memory */ |
| 5207 | pool_free2(apools.sessid, sessid_temp); |
| 5208 | |
| 5209 | if (asession != NULL) { |
| 5210 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 5211 | if (!(t->be->options2 & PR_O2_AS_REQL)) |
| 5212 | asession->request_count++; |
| 5213 | |
| 5214 | if (asession->serverid != NULL) { |
| 5215 | struct server *srv = t->be->srv; |
| 5216 | while (srv) { |
| 5217 | if (strcmp(srv->id, asession->serverid) == 0) { |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame^] | 5218 | if ((srv->state & SRV_RUNNING) || |
| 5219 | (t->be->options & PR_O_PERSIST) || |
| 5220 | (t->flags & SN_FORCE_PRST)) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5221 | /* we found the server and it's usable */ |
| 5222 | txn->flags &= ~TX_CK_MASK; |
| 5223 | txn->flags |= TX_CK_VALID; |
| 5224 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
| 5225 | t->srv = srv; |
| 5226 | break; |
| 5227 | } else { |
| 5228 | txn->flags &= ~TX_CK_MASK; |
| 5229 | txn->flags |= TX_CK_DOWN; |
| 5230 | } |
| 5231 | } |
| 5232 | srv = srv->next; |
| 5233 | } |
| 5234 | } |
| 5235 | } |
| 5236 | } |
| 5237 | |
| 5238 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 5239 | * Manage client-side cookie. It can impact performance by about 2% so it is |
| 5240 | * desirable to call it only when needed. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5241 | */ |
| 5242 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 5243 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5244 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5245 | char *p1, *p2, *p3, *p4, *p5; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5246 | char *del_colon, *del_cookie, *colon; |
| 5247 | int app_cookies; |
| 5248 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5249 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5250 | int cur_idx, old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5251 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 5252 | /* Iterate through the headers. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5253 | * we start with the start line. |
| 5254 | */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 5255 | old_idx = 0; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5256 | cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5257 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5258 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5259 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5260 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5261 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5262 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5263 | cur_ptr = cur_next; |
| 5264 | cur_end = cur_ptr + cur_hdr->len; |
| 5265 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5266 | |
| 5267 | /* We have one full header between cur_ptr and cur_end, and the |
| 5268 | * next header starts at cur_next. We're only interested in |
| 5269 | * "Cookie:" headers. |
| 5270 | */ |
| 5271 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5272 | val = http_header_match2(cur_ptr, cur_end, "Cookie", 6); |
| 5273 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5274 | old_idx = cur_idx; |
| 5275 | continue; |
| 5276 | } |
| 5277 | |
| 5278 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 5279 | * attributes whose name begin with a '$', and associate them with |
| 5280 | * the right cookie, if we want to delete this cookie. |
| 5281 | * So there are 3 cases for each cookie read : |
| 5282 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 5283 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 5284 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 5285 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 5286 | * "special" cookie. |
| 5287 | * At the end of loop, if a "special" cookie remains, we may have to |
| 5288 | * remove it. If no application cookie persists in the header, we |
| 5289 | * *MUST* delete it |
| 5290 | */ |
| 5291 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5292 | colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5293 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5294 | /* del_cookie == NULL => nothing to be deleted */ |
| 5295 | del_colon = del_cookie = NULL; |
| 5296 | app_cookies = 0; |
| 5297 | |
| 5298 | while (p1 < cur_end) { |
| 5299 | /* skip spaces and colons, but keep an eye on these ones */ |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5300 | resync_name: |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5301 | while (p1 < cur_end) { |
| 5302 | if (*p1 == ';' || *p1 == ',') |
| 5303 | colon = p1; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 5304 | else if (!isspace((unsigned char)*p1)) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5305 | break; |
| 5306 | p1++; |
| 5307 | } |
| 5308 | |
| 5309 | if (p1 == cur_end) |
| 5310 | break; |
| 5311 | |
| 5312 | /* p1 is at the beginning of the cookie name */ |
| 5313 | p2 = p1; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5314 | while (p2 < cur_end && *p2 != '=') { |
| 5315 | if (*p2 == ',' || *p2 == ';' || isspace((unsigned char)*p2)) { |
| 5316 | /* oops, the cookie name was truncated, resync */ |
| 5317 | p1 = p2; |
| 5318 | goto resync_name; |
| 5319 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5320 | p2++; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5321 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5322 | |
| 5323 | if (p2 == cur_end) |
| 5324 | break; |
| 5325 | |
| 5326 | p3 = p2 + 1; /* skips the '=' sign */ |
| 5327 | if (p3 == cur_end) |
| 5328 | break; |
| 5329 | |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5330 | /* parse the value, stripping leading and trailing spaces but keeping insiders. */ |
| 5331 | p5 = p4 = p3; |
| 5332 | while (p5 < cur_end && *p5 != ';' && *p5 != ',') { |
| 5333 | if (!isspace((unsigned char)*p5)) |
| 5334 | p4 = p5 + 1; |
| 5335 | p5++; |
| 5336 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5337 | |
| 5338 | /* here, we have the cookie name between p1 and p2, |
| 5339 | * and its value between p3 and p4. |
| 5340 | * we can process it : |
| 5341 | * |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5342 | * Cookie: NAME=VALUE ; |
| 5343 | * | || || |+-> p5 |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5344 | * | || || +--> p4 |
| 5345 | * | || |+-------> p3 |
| 5346 | * | || +--------> p2 |
| 5347 | * | |+------------> p1 |
| 5348 | * | +-------------> colon |
| 5349 | * +--------------------> cur_ptr |
| 5350 | */ |
| 5351 | |
| 5352 | if (*p1 == '$') { |
| 5353 | /* skip this one */ |
| 5354 | } |
| 5355 | else { |
| 5356 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5357 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5358 | txn->cli_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5359 | (p4 - p1 >= t->fe->capture_namelen) && |
| 5360 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5361 | int log_len = p4 - p1; |
| 5362 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 5363 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5364 | Alert("HTTP logging : out of memory.\n"); |
| 5365 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5366 | if (log_len > t->fe->capture_len) |
| 5367 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5368 | memcpy(txn->cli_cookie, p1, log_len); |
| 5369 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5370 | } |
| 5371 | } |
| 5372 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5373 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 5374 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5375 | /* Cool... it's the right one */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5376 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5377 | char *delim; |
| 5378 | |
| 5379 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 5380 | * have the server ID betweek p3 and delim, and the original cookie between |
| 5381 | * delim+1 and p4. Otherwise, delim==p4 : |
| 5382 | * |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5383 | * Cookie: NAME=SRV~VALUE ; |
| 5384 | * | || || | |+-> p5 |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5385 | * | || || | +--> p4 |
| 5386 | * | || || +--------> delim |
| 5387 | * | || |+-----------> p3 |
| 5388 | * | || +------------> p2 |
| 5389 | * | |+----------------> p1 |
| 5390 | * | +-----------------> colon |
| 5391 | * +------------------------> cur_ptr |
| 5392 | */ |
| 5393 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5394 | if (t->be->options & PR_O_COOK_PFX) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5395 | for (delim = p3; delim < p4; delim++) |
| 5396 | if (*delim == COOKIE_DELIM) |
| 5397 | break; |
| 5398 | } |
| 5399 | else |
| 5400 | delim = p4; |
| 5401 | |
| 5402 | |
| 5403 | /* Here, we'll look for the first running server which supports the cookie. |
| 5404 | * This allows to share a same cookie between several servers, for example |
| 5405 | * to dedicate backup servers to specific servers only. |
| 5406 | * However, to prevent clients from sticking to cookie-less backup server |
| 5407 | * when they have incidentely learned an empty cookie, we simply ignore |
| 5408 | * empty cookies and mark them as invalid. |
| 5409 | */ |
| 5410 | if (delim == p3) |
| 5411 | srv = NULL; |
| 5412 | |
| 5413 | while (srv) { |
Willy Tarreau | 92f2ab1 | 2007-02-02 22:14:47 +0100 | [diff] [blame] | 5414 | if (srv->cookie && (srv->cklen == delim - p3) && |
| 5415 | !memcmp(p3, srv->cookie, delim - p3)) { |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame^] | 5416 | if ((srv->state & SRV_RUNNING) || |
| 5417 | (t->be->options & PR_O_PERSIST) || |
| 5418 | (t->flags & SN_FORCE_PRST)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5419 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5420 | txn->flags &= ~TX_CK_MASK; |
| 5421 | txn->flags |= TX_CK_VALID; |
| 5422 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5423 | t->srv = srv; |
| 5424 | break; |
| 5425 | } else { |
| 5426 | /* we found a server, but it's down */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5427 | txn->flags &= ~TX_CK_MASK; |
| 5428 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5429 | } |
| 5430 | } |
| 5431 | srv = srv->next; |
| 5432 | } |
| 5433 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5434 | if (!srv && !(txn->flags & TX_CK_DOWN)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5435 | /* no server matched this cookie */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5436 | txn->flags &= ~TX_CK_MASK; |
| 5437 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5438 | } |
| 5439 | |
| 5440 | /* depending on the cookie mode, we may have to either : |
| 5441 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 5442 | * the server never sees it ; |
| 5443 | * - remove the server id from the cookie value, and tag the cookie as an |
| 5444 | * application cookie so that it does not get accidentely removed later, |
| 5445 | * if we're in cookie prefix mode |
| 5446 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5447 | if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5448 | int delta; /* negative */ |
| 5449 | |
| 5450 | delta = buffer_replace2(req, p3, delim + 1, NULL, 0); |
| 5451 | p4 += delta; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5452 | p5 += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5453 | cur_end += delta; |
| 5454 | cur_next += delta; |
| 5455 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5456 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5457 | |
| 5458 | del_cookie = del_colon = NULL; |
| 5459 | app_cookies++; /* protect the header from deletion */ |
| 5460 | } |
| 5461 | else if (del_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5462 | (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] | 5463 | del_cookie = p1; |
| 5464 | del_colon = colon; |
| 5465 | } |
| 5466 | } else { |
| 5467 | /* now we know that we must keep this cookie since it's |
| 5468 | * not ours. But if we wanted to delete our cookie |
| 5469 | * earlier, we cannot remove the complete header, but we |
| 5470 | * can remove the previous block itself. |
| 5471 | */ |
| 5472 | app_cookies++; |
| 5473 | |
| 5474 | if (del_cookie != NULL) { |
| 5475 | int delta; /* negative */ |
| 5476 | |
| 5477 | delta = buffer_replace2(req, del_cookie, p1, NULL, 0); |
| 5478 | p4 += delta; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5479 | p5 += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5480 | cur_end += delta; |
| 5481 | cur_next += delta; |
| 5482 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5483 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5484 | del_cookie = del_colon = NULL; |
| 5485 | } |
| 5486 | } |
| 5487 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5488 | if (t->be->appsession_name != NULL) { |
| 5489 | int cmp_len, value_len; |
| 5490 | char *value_begin; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5491 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5492 | if (t->be->options2 & PR_O2_AS_PFX) { |
| 5493 | cmp_len = MIN(p4 - p1, t->be->appsession_name_len); |
| 5494 | value_begin = p1 + t->be->appsession_name_len; |
| 5495 | value_len = p4 - p1 - t->be->appsession_name_len; |
| 5496 | } else { |
| 5497 | cmp_len = p2 - p1; |
| 5498 | value_begin = p3; |
| 5499 | value_len = p4 - p3; |
| 5500 | } |
| 5501 | |
| 5502 | /* let's see if the cookie is our appcookie */ |
| 5503 | if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) { |
| 5504 | /* Cool... it's the right one */ |
| 5505 | manage_client_side_appsession(t, value_begin, value_len); |
| 5506 | } |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5507 | #if defined(DEBUG_HASH) |
| 5508 | Alert("manage_client_side_cookies\n"); |
| 5509 | appsession_hash_dump(&(t->be->htbl_proxy)); |
| 5510 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5511 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 5512 | } |
| 5513 | |
| 5514 | /* we'll have to look for another cookie ... */ |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5515 | p1 = p5; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5516 | } /* while (p1 < cur_end) */ |
| 5517 | |
| 5518 | /* There's no more cookie on this line. |
| 5519 | * We may have marked the last one(s) for deletion. |
| 5520 | * We must do this now in two ways : |
| 5521 | * - if there is no app cookie, we simply delete the header ; |
| 5522 | * - if there are app cookies, we must delete the end of the |
| 5523 | * string properly, including the colon/semi-colon before |
| 5524 | * the cookie name. |
| 5525 | */ |
| 5526 | if (del_cookie != NULL) { |
| 5527 | int delta; |
| 5528 | if (app_cookies) { |
| 5529 | delta = buffer_replace2(req, del_colon, cur_end, NULL, 0); |
| 5530 | cur_end = del_colon; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5531 | cur_hdr->len += delta; |
| 5532 | } else { |
| 5533 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5534 | |
| 5535 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5536 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5537 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5538 | cur_hdr->len = 0; |
| 5539 | } |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 5540 | cur_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5541 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5542 | } |
| 5543 | |
| 5544 | /* keep the link from this header to next one */ |
| 5545 | old_idx = cur_idx; |
| 5546 | } /* end of cookie processing on this header */ |
| 5547 | } |
| 5548 | |
| 5549 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5550 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 5551 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 5552 | */ |
| 5553 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 5554 | { |
| 5555 | char term; |
| 5556 | char *cur_ptr, *cur_end, *cur_next; |
| 5557 | int cur_idx, old_idx, last_hdr; |
| 5558 | struct http_txn *txn = &t->txn; |
| 5559 | struct hdr_idx_elem *cur_hdr; |
| 5560 | int len, delta; |
| 5561 | |
| 5562 | last_hdr = 0; |
| 5563 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5564 | cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5565 | old_idx = 0; |
| 5566 | |
| 5567 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5568 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5569 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5570 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5571 | (exp->action == ACT_ALLOW || |
| 5572 | exp->action == ACT_DENY)) |
| 5573 | return 0; |
| 5574 | |
| 5575 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 5576 | if (!cur_idx) |
| 5577 | break; |
| 5578 | |
| 5579 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 5580 | cur_ptr = cur_next; |
| 5581 | cur_end = cur_ptr + cur_hdr->len; |
| 5582 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5583 | |
| 5584 | /* Now we have one header between cur_ptr and cur_end, |
| 5585 | * and the next header starts at cur_next. |
| 5586 | */ |
| 5587 | |
| 5588 | /* The annoying part is that pattern matching needs |
| 5589 | * that we modify the contents to null-terminate all |
| 5590 | * strings before testing them. |
| 5591 | */ |
| 5592 | |
| 5593 | term = *cur_end; |
| 5594 | *cur_end = '\0'; |
| 5595 | |
| 5596 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5597 | switch (exp->action) { |
| 5598 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5599 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5600 | last_hdr = 1; |
| 5601 | break; |
| 5602 | |
| 5603 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5604 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5605 | last_hdr = 1; |
| 5606 | break; |
| 5607 | |
| 5608 | case ACT_REPLACE: |
| 5609 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5610 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 5611 | /* FIXME: if the user adds a newline in the replacement, the |
| 5612 | * index will not be recalculated for now, and the new line |
| 5613 | * will not be counted as a new header. |
| 5614 | */ |
| 5615 | |
| 5616 | cur_end += delta; |
| 5617 | cur_next += delta; |
| 5618 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5619 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5620 | break; |
| 5621 | |
| 5622 | case ACT_REMOVE: |
| 5623 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 5624 | cur_next += delta; |
| 5625 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5626 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5627 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5628 | txn->hdr_idx.used--; |
| 5629 | cur_hdr->len = 0; |
| 5630 | cur_end = NULL; /* null-term has been rewritten */ |
| 5631 | break; |
| 5632 | |
| 5633 | } |
| 5634 | } |
| 5635 | if (cur_end) |
| 5636 | *cur_end = term; /* restore the string terminator */ |
| 5637 | |
| 5638 | /* keep the link from this header to next one in case of later |
| 5639 | * removal of next header. |
| 5640 | */ |
| 5641 | old_idx = cur_idx; |
| 5642 | } |
| 5643 | return 0; |
| 5644 | } |
| 5645 | |
| 5646 | |
| 5647 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 5648 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 5649 | * or -1 if a replacement resulted in an invalid status line. |
| 5650 | */ |
| 5651 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 5652 | { |
| 5653 | char term; |
| 5654 | char *cur_ptr, *cur_end; |
| 5655 | int done; |
| 5656 | struct http_txn *txn = &t->txn; |
| 5657 | int len, delta; |
| 5658 | |
| 5659 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5660 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5661 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5662 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5663 | (exp->action == ACT_ALLOW || |
| 5664 | exp->action == ACT_DENY)) |
| 5665 | return 0; |
| 5666 | else if (exp->action == ACT_REMOVE) |
| 5667 | return 0; |
| 5668 | |
| 5669 | done = 0; |
| 5670 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5671 | cur_ptr = txn->rsp.sol; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5672 | cur_end = cur_ptr + txn->rsp.sl.rq.l; |
| 5673 | |
| 5674 | /* Now we have the status line between cur_ptr and cur_end */ |
| 5675 | |
| 5676 | /* The annoying part is that pattern matching needs |
| 5677 | * that we modify the contents to null-terminate all |
| 5678 | * strings before testing them. |
| 5679 | */ |
| 5680 | |
| 5681 | term = *cur_end; |
| 5682 | *cur_end = '\0'; |
| 5683 | |
| 5684 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5685 | switch (exp->action) { |
| 5686 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5687 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5688 | done = 1; |
| 5689 | break; |
| 5690 | |
| 5691 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5692 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5693 | done = 1; |
| 5694 | break; |
| 5695 | |
| 5696 | case ACT_REPLACE: |
| 5697 | *cur_end = term; /* restore the string terminator */ |
| 5698 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5699 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 5700 | /* FIXME: if the user adds a newline in the replacement, the |
| 5701 | * index will not be recalculated for now, and the new line |
| 5702 | * will not be counted as a new header. |
| 5703 | */ |
| 5704 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5705 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5706 | cur_end += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5707 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 5708 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5709 | cur_ptr, cur_end + 1, |
| 5710 | NULL, NULL); |
| 5711 | if (unlikely(!cur_end)) |
| 5712 | return -1; |
| 5713 | |
| 5714 | /* we have a full respnse and we know that we have either a CR |
| 5715 | * or an LF at <ptr>. |
| 5716 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5717 | txn->status = strl2ui(txn->rsp.sol + txn->rsp.sl.st.c, txn->rsp.sl.st.c_l); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5718 | hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r'); |
| 5719 | /* there is no point trying this regex on headers */ |
| 5720 | return 1; |
| 5721 | } |
| 5722 | } |
| 5723 | *cur_end = term; /* restore the string terminator */ |
| 5724 | return done; |
| 5725 | } |
| 5726 | |
| 5727 | |
| 5728 | |
| 5729 | /* |
| 5730 | * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>. |
| 5731 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 5732 | * unparsable response. |
| 5733 | */ |
| 5734 | int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 5735 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5736 | struct http_txn *txn = &t->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5737 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5738 | while (exp && !(txn->flags & TX_SVDENY)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5739 | int ret; |
| 5740 | |
| 5741 | /* |
| 5742 | * The interleaving of transformations and verdicts |
| 5743 | * makes it difficult to decide to continue or stop |
| 5744 | * the evaluation. |
| 5745 | */ |
| 5746 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5747 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5748 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 5749 | exp->action == ACT_PASS)) { |
| 5750 | exp = exp->next; |
| 5751 | continue; |
| 5752 | } |
| 5753 | |
| 5754 | /* Apply the filter to the status line. */ |
| 5755 | ret = apply_filter_to_sts_line(t, rtr, exp); |
| 5756 | if (unlikely(ret < 0)) |
| 5757 | return -1; |
| 5758 | |
| 5759 | if (likely(ret == 0)) { |
| 5760 | /* The filter did not match the response, it can be |
| 5761 | * iterated through all headers. |
| 5762 | */ |
| 5763 | apply_filter_to_resp_headers(t, rtr, exp); |
| 5764 | } |
| 5765 | exp = exp->next; |
| 5766 | } |
| 5767 | return 0; |
| 5768 | } |
| 5769 | |
| 5770 | |
| 5771 | |
| 5772 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 5773 | * Manage server-side cookies. It can impact performance by about 2% so it is |
| 5774 | * desirable to call it only when needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5775 | */ |
| 5776 | void manage_server_side_cookies(struct session *t, struct buffer *rtr) |
| 5777 | { |
| 5778 | struct http_txn *txn = &t->txn; |
| 5779 | char *p1, *p2, *p3, *p4; |
| 5780 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5781 | char *cur_ptr, *cur_end, *cur_next; |
| 5782 | int cur_idx, old_idx, delta; |
| 5783 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5784 | /* Iterate through the headers. |
| 5785 | * we start with the start line. |
| 5786 | */ |
| 5787 | old_idx = 0; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5788 | cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5789 | |
| 5790 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 5791 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5792 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5793 | |
| 5794 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 5795 | cur_ptr = cur_next; |
| 5796 | cur_end = cur_ptr + cur_hdr->len; |
| 5797 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5798 | |
| 5799 | /* We have one full header between cur_ptr and cur_end, and the |
| 5800 | * next header starts at cur_next. We're only interested in |
| 5801 | * "Cookie:" headers. |
| 5802 | */ |
| 5803 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5804 | val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10); |
| 5805 | if (!val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5806 | old_idx = cur_idx; |
| 5807 | continue; |
| 5808 | } |
| 5809 | |
| 5810 | /* 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] | 5811 | txn->flags |= TX_SCK_ANY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5812 | |
| 5813 | |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 5814 | /* maybe we only wanted to see if there was a set-cookie. Note that |
| 5815 | * the cookie capture is declared in the fronend. |
| 5816 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5817 | if (t->be->cookie_name == NULL && |
| 5818 | t->be->appsession_name == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 5819 | t->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5820 | return; |
| 5821 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5822 | p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5823 | |
| 5824 | 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] | 5825 | if (p1 == cur_end || *p1 == ';') /* end of cookie */ |
| 5826 | break; |
| 5827 | |
| 5828 | /* p1 is at the beginning of the cookie name */ |
| 5829 | p2 = p1; |
| 5830 | |
| 5831 | while (p2 < cur_end && *p2 != '=' && *p2 != ';') |
| 5832 | p2++; |
| 5833 | |
| 5834 | if (p2 == cur_end || *p2 == ';') /* next cookie */ |
| 5835 | break; |
| 5836 | |
| 5837 | p3 = p2 + 1; /* skip the '=' sign */ |
| 5838 | if (p3 == cur_end) |
| 5839 | break; |
| 5840 | |
| 5841 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 5842 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';') |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5843 | p4++; |
| 5844 | |
| 5845 | /* here, we have the cookie name between p1 and p2, |
| 5846 | * and its value between p3 and p4. |
| 5847 | * we can process it. |
| 5848 | */ |
| 5849 | |
| 5850 | /* first, let's see if we want to capture it */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 5851 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5852 | txn->srv_cookie == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 5853 | (p4 - p1 >= t->fe->capture_namelen) && |
| 5854 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5855 | int log_len = p4 - p1; |
| 5856 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 5857 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5858 | Alert("HTTP logging : out of memory.\n"); |
| 5859 | } |
| 5860 | |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 5861 | if (log_len > t->fe->capture_len) |
| 5862 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5863 | memcpy(txn->srv_cookie, p1, log_len); |
| 5864 | txn->srv_cookie[log_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5865 | } |
| 5866 | |
| 5867 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5868 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 5869 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5870 | /* Cool... it's the right one */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5871 | txn->flags |= TX_SCK_SEEN; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5872 | |
| 5873 | /* If the cookie is in insert mode on a known server, we'll delete |
| 5874 | * this occurrence because we'll insert another one later. |
| 5875 | * We'll delete it too if the "indirect" option is set and we're in |
| 5876 | * a direct access. */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5877 | if (((t->srv) && (t->be->options & PR_O_COOK_INS)) || |
| 5878 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5879 | /* this header must be deleted */ |
| 5880 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 5881 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5882 | txn->hdr_idx.used--; |
| 5883 | cur_hdr->len = 0; |
| 5884 | cur_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5885 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5886 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5887 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5888 | } |
| 5889 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5890 | (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5891 | /* replace bytes p3->p4 with the cookie name associated |
| 5892 | * with this server since we know it. |
| 5893 | */ |
| 5894 | delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen); |
| 5895 | cur_hdr->len += delta; |
| 5896 | cur_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5897 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5898 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5899 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5900 | } |
| 5901 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5902 | (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5903 | /* insert the cookie name associated with this server |
| 5904 | * before existing cookie, and insert a delimitor between them.. |
| 5905 | */ |
| 5906 | delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1); |
| 5907 | cur_hdr->len += delta; |
| 5908 | cur_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5909 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5910 | |
| 5911 | p3[t->srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5912 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5913 | } |
| 5914 | } |
| 5915 | /* next, let's see if the cookie is our appcookie */ |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5916 | else if (t->be->appsession_name != NULL) { |
| 5917 | int cmp_len, value_len; |
| 5918 | char *value_begin; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5919 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5920 | if (t->be->options2 & PR_O2_AS_PFX) { |
| 5921 | cmp_len = MIN(p4 - p1, t->be->appsession_name_len); |
| 5922 | value_begin = p1 + t->be->appsession_name_len; |
| 5923 | value_len = MIN(t->be->appsession_len, p4 - p1 - t->be->appsession_name_len); |
| 5924 | } else { |
| 5925 | cmp_len = p2 - p1; |
| 5926 | value_begin = p3; |
| 5927 | value_len = MIN(t->be->appsession_len, p4 - p3); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5928 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5929 | |
| 5930 | if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) { |
| 5931 | /* Cool... it's the right one */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5932 | if (txn->sessid != NULL) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5933 | /* free previously allocated memory as we don't need it anymore */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5934 | pool_free2(apools.sessid, txn->sessid); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5935 | } |
| 5936 | /* Store the sessid in the session for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5937 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5938 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5939 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5940 | return; |
| 5941 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5942 | memcpy(txn->sessid, value_begin, value_len); |
| 5943 | txn->sessid[value_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5944 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5945 | } /* end if ((t->be->appsession_name != NULL) ... */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5946 | break; /* we don't want to loop again since there cannot be another cookie on the same line */ |
| 5947 | } /* we're now at the end of the cookie value */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5948 | /* keep the link from this header to next one */ |
| 5949 | old_idx = cur_idx; |
| 5950 | } /* end of cookie processing on this header */ |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5951 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5952 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5953 | appsess *asession = NULL; |
| 5954 | /* only do insert, if lookup fails */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5955 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5956 | if (asession == NULL) { |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 5957 | size_t server_id_len; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5958 | if ((asession = pool_alloc2(pool2_appsess)) == NULL) { |
| 5959 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 5960 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 5961 | return; |
| 5962 | } |
| 5963 | if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 5964 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5965 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
Cyril Bonté | 41689c2 | 2010-01-10 00:30:14 +0100 | [diff] [blame] | 5966 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5967 | return; |
| 5968 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5969 | memcpy(asession->sessid, txn->sessid, t->be->appsession_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5970 | asession->sessid[t->be->appsession_len] = 0; |
| 5971 | |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 5972 | server_id_len = strlen(t->srv->id) + 1; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5973 | if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) { |
| 5974 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5975 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
Cyril Bonté | 41689c2 | 2010-01-10 00:30:14 +0100 | [diff] [blame] | 5976 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5977 | return; |
| 5978 | } |
| 5979 | asession->serverid[0] = '\0'; |
| 5980 | memcpy(asession->serverid, t->srv->id, server_id_len); |
| 5981 | |
| 5982 | asession->request_count = 0; |
| 5983 | appsession_hash_insert(&(t->be->htbl_proxy), asession); |
| 5984 | } |
| 5985 | |
| 5986 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 5987 | asession->request_count++; |
| 5988 | } |
| 5989 | |
| 5990 | #if defined(DEBUG_HASH) |
| 5991 | Alert("manage_server_side_cookies\n"); |
| 5992 | appsession_hash_dump(&(t->be->htbl_proxy)); |
| 5993 | #endif |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5994 | } |
| 5995 | |
| 5996 | |
| 5997 | |
| 5998 | /* |
| 5999 | * Check if response is cacheable or not. Updates t->flags. |
| 6000 | */ |
| 6001 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 6002 | { |
| 6003 | struct http_txn *txn = &t->txn; |
| 6004 | char *p1, *p2; |
| 6005 | |
| 6006 | char *cur_ptr, *cur_end, *cur_next; |
| 6007 | int cur_idx; |
| 6008 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 6009 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6010 | return; |
| 6011 | |
| 6012 | /* Iterate through the headers. |
| 6013 | * we start with the start line. |
| 6014 | */ |
| 6015 | cur_idx = 0; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6016 | cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6017 | |
| 6018 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 6019 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6020 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6021 | |
| 6022 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 6023 | cur_ptr = cur_next; |
| 6024 | cur_end = cur_ptr + cur_hdr->len; |
| 6025 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6026 | |
| 6027 | /* We have one full header between cur_ptr and cur_end, and the |
| 6028 | * next header starts at cur_next. We're only interested in |
| 6029 | * "Cookie:" headers. |
| 6030 | */ |
| 6031 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6032 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 6033 | if (val) { |
| 6034 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 6035 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 6036 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 6037 | return; |
| 6038 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6039 | } |
| 6040 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6041 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 6042 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6043 | continue; |
| 6044 | |
| 6045 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 6046 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6047 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6048 | |
| 6049 | if (p1 >= cur_end) /* no more info */ |
| 6050 | continue; |
| 6051 | |
| 6052 | /* p1 is at the beginning of the value */ |
| 6053 | p2 = p1; |
| 6054 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 6055 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6056 | p2++; |
| 6057 | |
| 6058 | /* we have a complete value between p1 and p2 */ |
| 6059 | if (p2 < cur_end && *p2 == '=') { |
| 6060 | /* we have something of the form no-cache="set-cookie" */ |
| 6061 | if ((cur_end - p1 >= 21) && |
| 6062 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 6063 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6064 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6065 | continue; |
| 6066 | } |
| 6067 | |
| 6068 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 6069 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 6070 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 6071 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 6072 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6073 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6074 | return; |
| 6075 | } |
| 6076 | |
| 6077 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6078 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6079 | continue; |
| 6080 | } |
| 6081 | } |
| 6082 | } |
| 6083 | |
| 6084 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6085 | /* |
| 6086 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 6087 | * If the server is found, it's assigned to the session. |
| 6088 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6089 | 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] | 6090 | { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6091 | char *end_params, *first_param, *cur_param, *next_param; |
| 6092 | char separator; |
| 6093 | int value_len; |
| 6094 | |
| 6095 | int mode = t->be->options2 & PR_O2_AS_M_ANY; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6096 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6097 | if (t->be->appsession_name == NULL || |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6098 | (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6099 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6100 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6101 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6102 | first_param = NULL; |
| 6103 | switch (mode) { |
| 6104 | case PR_O2_AS_M_PP: |
| 6105 | first_param = memchr(begin, ';', len); |
| 6106 | break; |
| 6107 | case PR_O2_AS_M_QS: |
| 6108 | first_param = memchr(begin, '?', len); |
| 6109 | break; |
| 6110 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6111 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6112 | if (first_param == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6113 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6114 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6115 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6116 | switch (mode) { |
| 6117 | case PR_O2_AS_M_PP: |
| 6118 | if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) { |
| 6119 | end_params = (char *) begin + len; |
| 6120 | } |
| 6121 | separator = ';'; |
| 6122 | break; |
| 6123 | case PR_O2_AS_M_QS: |
| 6124 | end_params = (char *) begin + len; |
| 6125 | separator = '&'; |
| 6126 | break; |
| 6127 | default: |
| 6128 | /* unknown mode, shouldn't happen */ |
| 6129 | return; |
| 6130 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6131 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6132 | cur_param = next_param = end_params; |
| 6133 | while (cur_param > first_param) { |
| 6134 | cur_param--; |
| 6135 | if ((cur_param[0] == separator) || (cur_param == first_param)) { |
| 6136 | /* let's see if this is the appsession parameter */ |
| 6137 | if ((cur_param + t->be->appsession_name_len + 1 < next_param) && |
| 6138 | ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') && |
| 6139 | (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 6140 | /* Cool... it's the right one */ |
| 6141 | cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2); |
| 6142 | value_len = MIN(t->be->appsession_len, next_param - cur_param); |
| 6143 | if (value_len > 0) { |
| 6144 | manage_client_side_appsession(t, cur_param, value_len); |
| 6145 | } |
| 6146 | break; |
| 6147 | } |
| 6148 | next_param = cur_param; |
| 6149 | } |
| 6150 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6151 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 6152 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 6153 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6154 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6155 | } |
| 6156 | |
| 6157 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6158 | /* |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 6159 | * In a GET or HEAD request, check if the requested URI matches the stats uri |
| 6160 | * 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] | 6161 | * |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 6162 | * 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] | 6163 | * 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] | 6164 | * the stats I/O handler will be registered to start sending data. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6165 | * |
| 6166 | * Returns 1 if the session's state changes, otherwise 0. |
| 6167 | */ |
| 6168 | int stats_check_uri_auth(struct session *t, struct proxy *backend) |
| 6169 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6170 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6171 | struct uri_auth *uri_auth = backend->uri_auth; |
| 6172 | struct user_auth *user; |
| 6173 | int authenticated, cur_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6174 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6175 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6176 | memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats)); |
| 6177 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6178 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6179 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6180 | return 0; |
| 6181 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6182 | h = txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6183 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 6184 | /* the URI is in h */ |
| 6185 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6186 | return 0; |
| 6187 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 6188 | h += uri_auth->uri_len; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6189 | while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 3) { |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 6190 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6191 | t->data_ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 6192 | break; |
| 6193 | } |
| 6194 | h++; |
| 6195 | } |
| 6196 | |
| 6197 | if (uri_auth->refresh) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6198 | h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len; |
| 6199 | while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 10) { |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 6200 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6201 | t->data_ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 6202 | break; |
| 6203 | } |
| 6204 | h++; |
| 6205 | } |
| 6206 | } |
| 6207 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6208 | h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len; |
| 6209 | while (h <= txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l - 4) { |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 6210 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6211 | t->data_ctx.stats.flags |= STAT_FMT_CSV; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 6212 | break; |
| 6213 | } |
| 6214 | h++; |
| 6215 | } |
| 6216 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6217 | t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO; |
| 6218 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6219 | /* we are in front of a interceptable URI. Let's check |
| 6220 | * if there's an authentication and if it's valid. |
| 6221 | */ |
| 6222 | user = uri_auth->users; |
| 6223 | if (!user) { |
| 6224 | /* no user auth required, it's OK */ |
| 6225 | authenticated = 1; |
| 6226 | } else { |
| 6227 | authenticated = 0; |
| 6228 | |
| 6229 | /* a user list is defined, we have to check. |
| 6230 | * skip 21 chars for "Authorization: Basic ". |
| 6231 | */ |
| 6232 | |
| 6233 | /* FIXME: this should move to an earlier place */ |
| 6234 | cur_idx = 0; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6235 | h = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6236 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 6237 | int len = txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6238 | if (len > 14 && |
| 6239 | !strncasecmp("Authorization:", h, 14)) { |
Krzysztof Piotr Oledzki | 6f61b21 | 2009-10-04 23:34:15 +0200 | [diff] [blame] | 6240 | chunk_initlen(&txn->auth_hdr, h, 0, len); |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6241 | break; |
| 6242 | } |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6243 | h += len + txn->hdr_idx.v[cur_idx].cr + 1; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6244 | } |
| 6245 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6246 | if (txn->auth_hdr.len < 21 || |
| 6247 | memcmp(txn->auth_hdr.str + 14, " Basic ", 7)) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6248 | user = NULL; |
| 6249 | |
| 6250 | while (user) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6251 | if ((txn->auth_hdr.len == user->user_len + 14 + 7) |
| 6252 | && !memcmp(txn->auth_hdr.str + 14 + 7, |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6253 | user->user_pwd, user->user_len)) { |
| 6254 | authenticated = 1; |
| 6255 | break; |
| 6256 | } |
| 6257 | user = user->next; |
| 6258 | } |
| 6259 | } |
| 6260 | |
| 6261 | if (!authenticated) { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 6262 | struct chunk msg; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6263 | |
| 6264 | /* no need to go further */ |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 6265 | sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm); |
| 6266 | chunk_initlen(&msg, trash, sizeof(trash), strlen(trash)); |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 6267 | txn->status = 401; |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 6268 | stream_int_retnclose(t->req->prod, &msg); |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 6269 | t->req->analysers = 0; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6270 | if (!(t->flags & SN_ERR_MASK)) |
| 6271 | t->flags |= SN_ERR_PRXCOND; |
| 6272 | if (!(t->flags & SN_FINST_MASK)) |
| 6273 | t->flags |= SN_FINST_R; |
| 6274 | return 1; |
| 6275 | } |
| 6276 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6277 | /* The request is valid, the user is authenticated. Let's start sending |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6278 | * data. |
| 6279 | */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 6280 | t->logs.tv_request = now; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6281 | t->data_source = DATA_SRC_STATS; |
| 6282 | t->data_state = DATA_ST_INIT; |
Willy Tarreau | 91e9993 | 2008-06-30 07:51:00 +0200 | [diff] [blame] | 6283 | t->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | b0c9bc4 | 2009-10-04 15:56:38 +0200 | [diff] [blame] | 6284 | stream_int_register_handler(t->rep->prod, http_stats_io_handler); |
| 6285 | t->rep->prod->private = t; |
| 6286 | t->rep->prod->st0 = t->rep->prod->st1 = 0; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6287 | return 1; |
| 6288 | } |
| 6289 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6290 | /* |
| 6291 | * Capture a bad request or response and archive it in the proxy's structure. |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6292 | * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not |
| 6293 | * assume that msg->sol = buf->data + msg->som. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6294 | */ |
| 6295 | void http_capture_bad_message(struct error_snapshot *es, struct session *s, |
| 6296 | struct buffer *buf, struct http_msg *msg, |
| 6297 | struct proxy *other_end) |
| 6298 | { |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 6299 | es->len = buf->r - (buf->data + msg->som); |
| 6300 | 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] | 6301 | if (msg->err_pos >= 0) |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 6302 | es->pos = msg->err_pos - msg->som; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6303 | else |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 6304 | es->pos = buf->lr - (buf->data + msg->som); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6305 | es->when = date; // user-visible date |
| 6306 | es->sid = s->uniq_id; |
| 6307 | es->srv = s->srv; |
| 6308 | es->oe = other_end; |
| 6309 | es->src = s->cli_addr; |
| 6310 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6311 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6312 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6313 | * Print a debug line with a header |
| 6314 | */ |
| 6315 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 6316 | { |
| 6317 | int len, max; |
| 6318 | 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] | 6319 | 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] | 6320 | max = end - start; |
| 6321 | UBOUND(max, sizeof(trash) - len - 1); |
| 6322 | len += strlcpy2(trash + len, start, max + 1); |
| 6323 | trash[len++] = '\n'; |
| 6324 | write(1, trash, len); |
| 6325 | } |
| 6326 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6327 | /* |
| 6328 | * Initialize a new HTTP transaction for session <s>. It is assumed that all |
| 6329 | * the required fields are properly allocated and that we only need to (re)init |
| 6330 | * them. This should be used before processing any new request. |
| 6331 | */ |
| 6332 | void http_init_txn(struct session *s) |
| 6333 | { |
| 6334 | struct http_txn *txn = &s->txn; |
| 6335 | struct proxy *fe = s->fe; |
| 6336 | |
| 6337 | txn->flags = 0; |
| 6338 | txn->status = -1; |
| 6339 | |
| 6340 | txn->req.sol = txn->req.eol = NULL; |
| 6341 | txn->req.som = txn->req.eoh = 0; /* relative to the buffer */ |
| 6342 | txn->rsp.sol = txn->rsp.eol = NULL; |
| 6343 | txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */ |
| 6344 | txn->req.hdr_content_len = 0LL; |
| 6345 | txn->rsp.hdr_content_len = 0LL; |
| 6346 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 6347 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
| 6348 | chunk_reset(&txn->auth_hdr); |
| 6349 | |
| 6350 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 6351 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 6352 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 6353 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 6354 | if (txn->req.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6355 | memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *)); |
| 6356 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 6357 | if (txn->rsp.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6358 | memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
| 6359 | |
| 6360 | if (txn->hdr_idx.v) |
| 6361 | hdr_idx_init(&txn->hdr_idx); |
| 6362 | } |
| 6363 | |
| 6364 | /* to be used at the end of a transaction */ |
| 6365 | void http_end_txn(struct session *s) |
| 6366 | { |
| 6367 | struct http_txn *txn = &s->txn; |
| 6368 | |
| 6369 | /* these ones will have been dynamically allocated */ |
| 6370 | pool_free2(pool2_requri, txn->uri); |
| 6371 | pool_free2(pool2_capture, txn->cli_cookie); |
| 6372 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6373 | pool_free2(apools.sessid, txn->sessid); |
| 6374 | txn->sessid = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6375 | txn->uri = NULL; |
| 6376 | txn->srv_cookie = NULL; |
| 6377 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 6378 | |
| 6379 | if (txn->req.cap) { |
| 6380 | struct cap_hdr *h; |
| 6381 | for (h = s->fe->req_cap; h; h = h->next) |
| 6382 | pool_free2(h->pool, txn->req.cap[h->index]); |
| 6383 | memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *)); |
| 6384 | } |
| 6385 | |
| 6386 | if (txn->rsp.cap) { |
| 6387 | struct cap_hdr *h; |
| 6388 | for (h = s->fe->rsp_cap; h; h = h->next) |
| 6389 | pool_free2(h->pool, txn->rsp.cap[h->index]); |
| 6390 | memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *)); |
| 6391 | } |
| 6392 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6393 | } |
| 6394 | |
| 6395 | /* to be used at the end of a transaction to prepare a new one */ |
| 6396 | void http_reset_txn(struct session *s) |
| 6397 | { |
| 6398 | http_end_txn(s); |
| 6399 | http_init_txn(s); |
| 6400 | |
| 6401 | s->be = s->fe; |
| 6402 | s->req->analysers = s->listener->analysers; |
| 6403 | s->logs.logwait = s->fe->to_log; |
| 6404 | s->srv = s->prev_srv = s->srv_conn = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 6405 | /* re-init store persistence */ |
| 6406 | s->store_count = 0; |
| 6407 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6408 | s->pend_pos = NULL; |
| 6409 | s->conn_retries = s->be->conn_retries; |
| 6410 | |
| 6411 | s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */ |
| 6412 | |
| 6413 | s->req->rto = s->fe->timeout.client; |
| 6414 | s->req->wto = s->be->timeout.server; |
| 6415 | s->req->cto = s->be->timeout.connect; |
| 6416 | |
| 6417 | s->rep->rto = s->be->timeout.server; |
| 6418 | s->rep->wto = s->fe->timeout.client; |
| 6419 | s->rep->cto = TICK_ETERNITY; |
| 6420 | |
| 6421 | s->req->rex = TICK_ETERNITY; |
| 6422 | s->req->wex = TICK_ETERNITY; |
| 6423 | s->req->analyse_exp = TICK_ETERNITY; |
| 6424 | s->rep->rex = TICK_ETERNITY; |
| 6425 | s->rep->wex = TICK_ETERNITY; |
| 6426 | s->rep->analyse_exp = TICK_ETERNITY; |
| 6427 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6428 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6429 | /************************************************************************/ |
| 6430 | /* The code below is dedicated to ACL parsing and matching */ |
| 6431 | /************************************************************************/ |
| 6432 | |
| 6433 | |
| 6434 | |
| 6435 | |
| 6436 | /* 1. Check on METHOD |
| 6437 | * We use the pre-parsed method if it is known, and store its number as an |
| 6438 | * integer. If it is unknown, we use the pointer and the length. |
| 6439 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6440 | 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] | 6441 | { |
| 6442 | int len, meth; |
| 6443 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6444 | len = strlen(*text); |
| 6445 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6446 | |
| 6447 | pattern->val.i = meth; |
| 6448 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6449 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6450 | if (!pattern->ptr.str) |
| 6451 | return 0; |
| 6452 | pattern->len = len; |
| 6453 | } |
| 6454 | return 1; |
| 6455 | } |
| 6456 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6457 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6458 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6459 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6460 | { |
| 6461 | int meth; |
| 6462 | struct http_txn *txn = l7; |
| 6463 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6464 | if (!txn) |
| 6465 | return 0; |
| 6466 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6467 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6468 | return 0; |
| 6469 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6470 | meth = txn->meth; |
| 6471 | test->i = meth; |
| 6472 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6473 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6474 | /* ensure the indexes are not affected */ |
| 6475 | return 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6476 | test->len = txn->req.sl.rq.m_l; |
| 6477 | test->ptr = txn->req.sol; |
| 6478 | } |
| 6479 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 6480 | return 1; |
| 6481 | } |
| 6482 | |
| 6483 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 6484 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 6485 | int icase; |
| 6486 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6487 | if (test->i != pattern->val.i) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6488 | return ACL_PAT_FAIL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6489 | |
| 6490 | if (test->i != HTTP_METH_OTHER) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6491 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6492 | |
| 6493 | /* Other method, we must compare the strings */ |
| 6494 | if (pattern->len != test->len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6495 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 6496 | |
| 6497 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 6498 | if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) || |
| 6499 | (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6500 | return ACL_PAT_FAIL; |
| 6501 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6502 | } |
| 6503 | |
| 6504 | /* 2. Check on Request/Status Version |
| 6505 | * We simply compare strings here. |
| 6506 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6507 | 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] | 6508 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6509 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6510 | if (!pattern->ptr.str) |
| 6511 | return 0; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6512 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6513 | return 1; |
| 6514 | } |
| 6515 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6516 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6517 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6518 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6519 | { |
| 6520 | struct http_txn *txn = l7; |
| 6521 | char *ptr; |
| 6522 | int len; |
| 6523 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6524 | if (!txn) |
| 6525 | return 0; |
| 6526 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6527 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6528 | return 0; |
| 6529 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6530 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6531 | ptr = txn->req.sol + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6532 | |
| 6533 | while ((len-- > 0) && (*ptr++ != '/')); |
| 6534 | if (len <= 0) |
| 6535 | return 0; |
| 6536 | |
| 6537 | test->ptr = ptr; |
| 6538 | test->len = len; |
| 6539 | |
| 6540 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 6541 | return 1; |
| 6542 | } |
| 6543 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6544 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6545 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6546 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6547 | { |
| 6548 | struct http_txn *txn = l7; |
| 6549 | char *ptr; |
| 6550 | int len; |
| 6551 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6552 | if (!txn) |
| 6553 | return 0; |
| 6554 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6555 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6556 | return 0; |
| 6557 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6558 | len = txn->rsp.sl.st.v_l; |
| 6559 | ptr = txn->rsp.sol; |
| 6560 | |
| 6561 | while ((len-- > 0) && (*ptr++ != '/')); |
| 6562 | if (len <= 0) |
| 6563 | return 0; |
| 6564 | |
| 6565 | test->ptr = ptr; |
| 6566 | test->len = len; |
| 6567 | |
| 6568 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 6569 | return 1; |
| 6570 | } |
| 6571 | |
| 6572 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6573 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6574 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6575 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6576 | { |
| 6577 | struct http_txn *txn = l7; |
| 6578 | char *ptr; |
| 6579 | int len; |
| 6580 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6581 | if (!txn) |
| 6582 | return 0; |
| 6583 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6584 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6585 | return 0; |
| 6586 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6587 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6588 | ptr = txn->rsp.sol + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6589 | |
| 6590 | test->i = __strl2ui(ptr, len); |
| 6591 | test->flags = ACL_TEST_F_VOL_1ST; |
| 6592 | return 1; |
| 6593 | } |
| 6594 | |
| 6595 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6596 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6597 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6598 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6599 | { |
| 6600 | struct http_txn *txn = l7; |
| 6601 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6602 | if (!txn) |
| 6603 | return 0; |
| 6604 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6605 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6606 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6607 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6608 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6609 | /* ensure the indexes are not affected */ |
| 6610 | return 0; |
| 6611 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6612 | test->len = txn->req.sl.rq.u_l; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6613 | test->ptr = txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6614 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 6615 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 6616 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6617 | return 1; |
| 6618 | } |
| 6619 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6620 | static int |
| 6621 | acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6622 | struct acl_expr *expr, struct acl_test *test) |
| 6623 | { |
| 6624 | struct http_txn *txn = l7; |
| 6625 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6626 | if (!txn) |
| 6627 | return 0; |
| 6628 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6629 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6630 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6631 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6632 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6633 | /* ensure the indexes are not affected */ |
| 6634 | return 0; |
| 6635 | |
| 6636 | /* Parse HTTP request */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6637 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6638 | test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr; |
| 6639 | test->i = AF_INET; |
| 6640 | |
| 6641 | /* |
| 6642 | * If we are parsing url in frontend space, we prepare backend stage |
| 6643 | * to not parse again the same url ! optimization lazyness... |
| 6644 | */ |
| 6645 | if (px->options & PR_O_HTTP_PROXY) |
| 6646 | l4->flags |= SN_ADDR_SET; |
| 6647 | |
| 6648 | test->flags = ACL_TEST_F_READ_ONLY; |
| 6649 | return 1; |
| 6650 | } |
| 6651 | |
| 6652 | static int |
| 6653 | acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6654 | struct acl_expr *expr, struct acl_test *test) |
| 6655 | { |
| 6656 | struct http_txn *txn = l7; |
| 6657 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6658 | if (!txn) |
| 6659 | return 0; |
| 6660 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6661 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6662 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6663 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6664 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6665 | /* ensure the indexes are not affected */ |
| 6666 | return 0; |
| 6667 | |
| 6668 | /* Same optimization as url_ip */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6669 | url2sa(txn->req.sol + txn->req.sl.rq.u, txn->req.sl.rq.u_l, &l4->srv_addr); |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6670 | test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port); |
| 6671 | |
| 6672 | if (px->options & PR_O_HTTP_PROXY) |
| 6673 | l4->flags |= SN_ADDR_SET; |
| 6674 | |
| 6675 | test->flags = ACL_TEST_F_READ_ONLY; |
| 6676 | return 1; |
| 6677 | } |
| 6678 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6679 | /* 5. Check on HTTP header. A pointer to the beginning of the value is returned. |
| 6680 | * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr(). |
| 6681 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6682 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6683 | 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] | 6684 | struct acl_expr *expr, struct acl_test *test) |
| 6685 | { |
| 6686 | struct http_txn *txn = l7; |
| 6687 | struct hdr_idx *idx = &txn->hdr_idx; |
| 6688 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6689 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6690 | if (!txn) |
| 6691 | return 0; |
| 6692 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6693 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 6694 | /* search for header from the beginning */ |
| 6695 | ctx->idx = 0; |
| 6696 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6697 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 6698 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 6699 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6700 | test->len = ctx->vlen; |
| 6701 | test->ptr = (char *)ctx->line + ctx->val; |
| 6702 | return 1; |
| 6703 | } |
| 6704 | |
| 6705 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 6706 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6707 | return 0; |
| 6708 | } |
| 6709 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6710 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6711 | acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6712 | struct acl_expr *expr, struct acl_test *test) |
| 6713 | { |
| 6714 | struct http_txn *txn = l7; |
| 6715 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6716 | if (!txn) |
| 6717 | return 0; |
| 6718 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6719 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6720 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6721 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6722 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6723 | /* ensure the indexes are not affected */ |
| 6724 | return 0; |
| 6725 | |
| 6726 | return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test); |
| 6727 | } |
| 6728 | |
| 6729 | static int |
| 6730 | acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6731 | struct acl_expr *expr, struct acl_test *test) |
| 6732 | { |
| 6733 | struct http_txn *txn = l7; |
| 6734 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6735 | if (!txn) |
| 6736 | return 0; |
| 6737 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6738 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6739 | return 0; |
| 6740 | |
| 6741 | return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test); |
| 6742 | } |
| 6743 | |
| 6744 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 6745 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 6746 | */ |
| 6747 | static int |
| 6748 | 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] | 6749 | struct acl_expr *expr, struct acl_test *test) |
| 6750 | { |
| 6751 | struct http_txn *txn = l7; |
| 6752 | struct hdr_idx *idx = &txn->hdr_idx; |
| 6753 | struct hdr_ctx ctx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6754 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6755 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6756 | if (!txn) |
| 6757 | return 0; |
| 6758 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6759 | ctx.idx = 0; |
| 6760 | cnt = 0; |
| 6761 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx)) |
| 6762 | cnt++; |
| 6763 | |
| 6764 | test->i = cnt; |
| 6765 | test->flags = ACL_TEST_F_VOL_HDR; |
| 6766 | return 1; |
| 6767 | } |
| 6768 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6769 | static int |
| 6770 | acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6771 | struct acl_expr *expr, struct acl_test *test) |
| 6772 | { |
| 6773 | struct http_txn *txn = l7; |
| 6774 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6775 | if (!txn) |
| 6776 | return 0; |
| 6777 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6778 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6779 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6780 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6781 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6782 | /* ensure the indexes are not affected */ |
| 6783 | return 0; |
| 6784 | |
| 6785 | return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test); |
| 6786 | } |
| 6787 | |
| 6788 | static int |
| 6789 | acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6790 | struct acl_expr *expr, struct acl_test *test) |
| 6791 | { |
| 6792 | struct http_txn *txn = l7; |
| 6793 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6794 | if (!txn) |
| 6795 | return 0; |
| 6796 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6797 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6798 | return 0; |
| 6799 | |
| 6800 | return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test); |
| 6801 | } |
| 6802 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6803 | /* 7. Check on HTTP header's integer value. The integer value is returned. |
| 6804 | * FIXME: the type is 'int', it may not be appropriate for everything. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6805 | * 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] | 6806 | */ |
| 6807 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6808 | 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] | 6809 | struct acl_expr *expr, struct acl_test *test) |
| 6810 | { |
| 6811 | struct http_txn *txn = l7; |
| 6812 | struct hdr_idx *idx = &txn->hdr_idx; |
| 6813 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6814 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6815 | if (!txn) |
| 6816 | return 0; |
| 6817 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6818 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 6819 | /* search for header from the beginning */ |
| 6820 | ctx->idx = 0; |
| 6821 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6822 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 6823 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 6824 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6825 | test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen); |
| 6826 | return 1; |
| 6827 | } |
| 6828 | |
| 6829 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 6830 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6831 | return 0; |
| 6832 | } |
| 6833 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6834 | static int |
| 6835 | acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6836 | struct acl_expr *expr, struct acl_test *test) |
| 6837 | { |
| 6838 | struct http_txn *txn = l7; |
| 6839 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6840 | if (!txn) |
| 6841 | return 0; |
| 6842 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6843 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6844 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6845 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6846 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6847 | /* ensure the indexes are not affected */ |
| 6848 | return 0; |
| 6849 | |
| 6850 | return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test); |
| 6851 | } |
| 6852 | |
| 6853 | static int |
| 6854 | acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6855 | struct acl_expr *expr, struct acl_test *test) |
| 6856 | { |
| 6857 | struct http_txn *txn = l7; |
| 6858 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6859 | if (!txn) |
| 6860 | return 0; |
| 6861 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6862 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6863 | return 0; |
| 6864 | |
| 6865 | return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test); |
| 6866 | } |
| 6867 | |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 6868 | /* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned. |
| 6869 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 6870 | */ |
| 6871 | static int |
| 6872 | acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol, |
| 6873 | struct acl_expr *expr, struct acl_test *test) |
| 6874 | { |
| 6875 | struct http_txn *txn = l7; |
| 6876 | struct hdr_idx *idx = &txn->hdr_idx; |
| 6877 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
| 6878 | |
| 6879 | if (!txn) |
| 6880 | return 0; |
| 6881 | |
| 6882 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 6883 | /* search for header from the beginning */ |
| 6884 | ctx->idx = 0; |
| 6885 | |
| 6886 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 6887 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 6888 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6889 | /* Same optimization as url_ip */ |
| 6890 | memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr)); |
| 6891 | url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr); |
| 6892 | test->ptr = (void *)&l4->srv_addr.sin_addr; |
| 6893 | test->i = AF_INET; |
| 6894 | return 1; |
| 6895 | } |
| 6896 | |
| 6897 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 6898 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6899 | return 0; |
| 6900 | } |
| 6901 | |
| 6902 | static int |
| 6903 | acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6904 | struct acl_expr *expr, struct acl_test *test) |
| 6905 | { |
| 6906 | struct http_txn *txn = l7; |
| 6907 | |
| 6908 | if (!txn) |
| 6909 | return 0; |
| 6910 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6911 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 6912 | return 0; |
| 6913 | |
| 6914 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6915 | /* ensure the indexes are not affected */ |
| 6916 | return 0; |
| 6917 | |
| 6918 | return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test); |
| 6919 | } |
| 6920 | |
| 6921 | static int |
| 6922 | acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6923 | struct acl_expr *expr, struct acl_test *test) |
| 6924 | { |
| 6925 | struct http_txn *txn = l7; |
| 6926 | |
| 6927 | if (!txn) |
| 6928 | return 0; |
| 6929 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6930 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 6931 | return 0; |
| 6932 | |
| 6933 | return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test); |
| 6934 | } |
| 6935 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 6936 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 6937 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 6938 | */ |
| 6939 | static int |
| 6940 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6941 | struct acl_expr *expr, struct acl_test *test) |
| 6942 | { |
| 6943 | struct http_txn *txn = l7; |
| 6944 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6945 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6946 | if (!txn) |
| 6947 | return 0; |
| 6948 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6949 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6950 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6951 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6952 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6953 | /* ensure the indexes are not affected */ |
| 6954 | return 0; |
| 6955 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6956 | end = txn->req.sol + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 6957 | ptr = http_get_path(txn); |
| 6958 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 6959 | return 0; |
| 6960 | |
| 6961 | /* OK, we got the '/' ! */ |
| 6962 | test->ptr = ptr; |
| 6963 | |
| 6964 | while (ptr < end && *ptr != '?') |
| 6965 | ptr++; |
| 6966 | |
| 6967 | test->len = ptr - test->ptr; |
| 6968 | |
| 6969 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 6970 | test->flags = ACL_TEST_F_VOL_1ST; |
| 6971 | return 1; |
| 6972 | } |
| 6973 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 6974 | static int |
| 6975 | acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir, |
| 6976 | struct acl_expr *expr, struct acl_test *test) |
| 6977 | { |
| 6978 | struct buffer *req = s->req; |
| 6979 | struct http_txn *txn = &s->txn; |
| 6980 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 6981 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 6982 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 6983 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 6984 | */ |
| 6985 | |
| 6986 | if (!s || !req) |
| 6987 | return 0; |
| 6988 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6989 | if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 6990 | /* Already decoded as OK */ |
| 6991 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 6992 | return 1; |
| 6993 | } |
| 6994 | |
| 6995 | /* Try to decode HTTP request */ |
| 6996 | if (likely(req->lr < req->r)) |
| 6997 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
| 6998 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6999 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 7000 | if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) { |
| 7001 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 7002 | return 1; |
| 7003 | } |
| 7004 | /* wait for final state */ |
| 7005 | test->flags |= ACL_TEST_F_MAY_CHANGE; |
| 7006 | return 0; |
| 7007 | } |
| 7008 | |
| 7009 | /* OK we got a valid HTTP request. We have some minor preparation to |
| 7010 | * perform so that further checks can rely on HTTP tests. |
| 7011 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7012 | txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 7013 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 7014 | s->flags |= SN_REDIRECTABLE; |
| 7015 | |
| 7016 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) { |
| 7017 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 7018 | return 1; |
| 7019 | } |
| 7020 | |
| 7021 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 7022 | return 1; |
| 7023 | } |
| 7024 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7025 | |
| 7026 | /************************************************************************/ |
| 7027 | /* All supported keywords must be declared here. */ |
| 7028 | /************************************************************************/ |
| 7029 | |
| 7030 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 7031 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 7032 | { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT }, |
| 7033 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 7034 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT }, |
| 7035 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 7036 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 7037 | { "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] | 7038 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 7039 | { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 7040 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 7041 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 7042 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 7043 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 7044 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 7045 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 7046 | { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE }, |
| 7047 | { "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] | 7048 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 7049 | /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */ |
| 7050 | { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 7051 | { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 7052 | { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 7053 | { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 7054 | { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 7055 | { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 7056 | { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 7057 | { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
| 7058 | { "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] | 7059 | { "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] | 7060 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 7061 | { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 7062 | { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE }, |
| 7063 | { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE }, |
| 7064 | { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE }, |
| 7065 | { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE }, |
| 7066 | { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE }, |
| 7067 | { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE }, |
| 7068 | { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
| 7069 | { "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] | 7070 | { "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] | 7071 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 7072 | { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 7073 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 7074 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 7075 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 7076 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 7077 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 7078 | { "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] | 7079 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 7080 | { NULL, NULL, NULL, NULL }, |
| 7081 | |
| 7082 | #if 0 |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7083 | { "line", acl_parse_str, acl_fetch_line, acl_match_str }, |
| 7084 | { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg }, |
| 7085 | { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg }, |
| 7086 | { "line_end", acl_parse_str, acl_fetch_line, acl_match_end }, |
| 7087 | { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub }, |
| 7088 | { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir }, |
| 7089 | { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom }, |
| 7090 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7091 | { "cook", acl_parse_str, acl_fetch_cook, acl_match_str }, |
| 7092 | { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg }, |
| 7093 | { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg }, |
| 7094 | { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end }, |
| 7095 | { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub }, |
| 7096 | { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir }, |
| 7097 | { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom }, |
| 7098 | { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst }, |
| 7099 | |
| 7100 | { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str }, |
| 7101 | { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg }, |
| 7102 | { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str }, |
| 7103 | { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 }, |
| 7104 | { NULL, NULL, NULL, NULL }, |
| 7105 | #endif |
| 7106 | }}; |
| 7107 | |
| 7108 | |
| 7109 | __attribute__((constructor)) |
| 7110 | static void __http_protocol_init(void) |
| 7111 | { |
| 7112 | acl_register_keywords(&acl_kws); |
| 7113 | } |
| 7114 | |
| 7115 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7116 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 7117 | * Local variables: |
| 7118 | * c-indent-level: 8 |
| 7119 | * c-basic-offset: 8 |
| 7120 | * End: |
| 7121 | */ |