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 | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 764 | |
| 765 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 766 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 767 | rdr.len += 29; |
| 768 | } else { |
| 769 | memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23); |
| 770 | rdr.len += 23; |
| 771 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 772 | |
| 773 | /* prepare to return without error. */ |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame] | 774 | si->shutr(si); |
| 775 | si->shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 776 | si->err_type = SI_ET_NONE; |
| 777 | si->err_loc = NULL; |
| 778 | si->state = SI_ST_CLO; |
| 779 | |
| 780 | /* send the message */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 781 | 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] | 782 | |
| 783 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
| 784 | if (s->srv) |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 785 | srv_inc_sess_ctr(s->srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 786 | } |
| 787 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 788 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 789 | * that the server side is closed. Note that err_type is actually a |
| 790 | * bitmask, where almost only aborts may be cumulated with other |
| 791 | * values. We consider that aborted operations are more important |
| 792 | * than timeouts or errors due to the fact that nobody else in the |
| 793 | * logs might explain incomplete retries. All others should avoid |
| 794 | * being cumulated. It should normally not be possible to have multiple |
| 795 | * aborts at once, but just in case, the first one in sequence is reported. |
| 796 | */ |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 797 | void http_return_srv_error(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 798 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 799 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 800 | |
| 801 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 802 | http_server_error(s, si, SN_ERR_CLICL, 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_CONN_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 805 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C, |
| 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_QUEUE_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 808 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q, |
| 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_QUEUE_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 811 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q, |
| 812 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 813 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 814 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C, |
| 815 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 816 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 817 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C, |
| 818 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 819 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 820 | http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C, |
| 821 | 500, error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 822 | } |
| 823 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 824 | extern const char sess_term_cond[8]; |
| 825 | extern const char sess_fin_state[8]; |
| 826 | extern const char *monthname[12]; |
| 827 | const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */ |
| 828 | const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown, |
| 829 | Set-cookie seen and left unchanged (passive), Set-cookie Deleted, |
| 830 | unknown, Set-cookie Rewritten */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 831 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 832 | struct pool_head *pool2_capture; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 833 | |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 834 | void http_sess_clflog(struct session *s) |
| 835 | { |
| 836 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 837 | struct proxy *fe = s->fe; |
| 838 | struct proxy *be = s->be; |
| 839 | struct proxy *prx_log; |
| 840 | struct http_txn *txn = &s->txn; |
| 841 | int tolog, level, err; |
| 842 | char *uri, *h; |
| 843 | char *svid; |
| 844 | struct tm tm; |
| 845 | static char tmpline[MAX_SYSLOG_LEN]; |
| 846 | int hdr; |
| 847 | size_t w; |
| 848 | int t_request; |
| 849 | |
| 850 | prx_log = fe; |
| 851 | err = (s->flags & (SN_ERR_MASK | SN_REDISP)) || |
| 852 | (s->conn_retries != be->conn_retries) || |
| 853 | txn->status >= 500; |
| 854 | |
| 855 | if (s->cli_addr.ss_family == AF_INET) |
| 856 | inet_ntop(AF_INET, |
| 857 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 858 | pn, sizeof(pn)); |
| 859 | else |
| 860 | inet_ntop(AF_INET6, |
| 861 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 862 | pn, sizeof(pn)); |
| 863 | |
| 864 | get_gmtime(s->logs.accept_date.tv_sec, &tm); |
| 865 | |
| 866 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 867 | tolog = fe->to_log; |
| 868 | |
| 869 | h = tmpline; |
| 870 | |
| 871 | w = snprintf(h, sizeof(tmpline), |
| 872 | "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]", |
| 873 | pn, |
| 874 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 875 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
| 876 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 877 | goto trunc; |
| 878 | h += w; |
| 879 | |
| 880 | if (h >= tmpline + sizeof(tmpline) - 4) |
| 881 | goto trunc; |
| 882 | |
| 883 | *(h++) = ' '; |
| 884 | *(h++) = '\"'; |
| 885 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 886 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 887 | '#', url_encode_map, uri); |
| 888 | *(h++) = '\"'; |
| 889 | |
| 890 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out); |
| 891 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 892 | goto trunc; |
| 893 | h += w; |
| 894 | |
| 895 | if (h >= tmpline + sizeof(tmpline) - 9) |
| 896 | goto trunc; |
| 897 | memcpy(h, " \"-\" \"-\"", 8); |
| 898 | h += 8; |
| 899 | |
| 900 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 901 | " %d %03d", |
| 902 | (s->cli_addr.ss_family == AF_INET) ? |
| 903 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 904 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
| 905 | (int)s->logs.accept_date.tv_usec/1000); |
| 906 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 907 | goto trunc; |
| 908 | h += w; |
| 909 | |
| 910 | w = strlen(fe->id); |
| 911 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 912 | goto trunc; |
| 913 | *(h++) = ' '; |
| 914 | *(h++) = '\"'; |
| 915 | memcpy(h, fe->id, w); |
| 916 | h += w; |
| 917 | *(h++) = '\"'; |
| 918 | |
| 919 | w = strlen(be->id); |
| 920 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 921 | goto trunc; |
| 922 | *(h++) = ' '; |
| 923 | *(h++) = '\"'; |
| 924 | memcpy(h, be->id, w); |
| 925 | h += w; |
| 926 | *(h++) = '\"'; |
| 927 | |
| 928 | svid = (tolog & LW_SVID) ? |
| 929 | (s->data_source != DATA_SRC_STATS) ? |
| 930 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 931 | |
| 932 | w = strlen(svid); |
| 933 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 934 | goto trunc; |
| 935 | *(h++) = ' '; |
| 936 | *(h++) = '\"'; |
| 937 | memcpy(h, svid, w); |
| 938 | h += w; |
| 939 | *(h++) = '\"'; |
| 940 | |
| 941 | t_request = -1; |
| 942 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 943 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 944 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 945 | " %d %ld %ld %ld %ld", |
| 946 | t_request, |
| 947 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
| 948 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 949 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 950 | s->logs.t_close); |
| 951 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 952 | goto trunc; |
| 953 | h += w; |
| 954 | |
| 955 | if (h >= tmpline + sizeof(tmpline) - 8) |
| 956 | goto trunc; |
| 957 | *(h++) = ' '; |
| 958 | *(h++) = '\"'; |
| 959 | *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT]; |
| 960 | *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT]; |
| 961 | *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 962 | *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-'; |
| 963 | *(h++) = '\"'; |
| 964 | |
| 965 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 966 | " %d %d %d %d %d %ld %ld", |
| 967 | actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0, |
| 968 | (s->conn_retries > 0) ? (be->conn_retries - s->conn_retries) : be->conn_retries, |
| 969 | s->logs.srv_queue_size, s->logs.prx_queue_size); |
| 970 | |
| 971 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 972 | goto trunc; |
| 973 | h += w; |
| 974 | |
| 975 | if (txn->cli_cookie) { |
| 976 | w = strlen(txn->cli_cookie); |
| 977 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 978 | goto trunc; |
| 979 | *(h++) = ' '; |
| 980 | *(h++) = '\"'; |
| 981 | memcpy(h, txn->cli_cookie, w); |
| 982 | h += w; |
| 983 | *(h++) = '\"'; |
| 984 | } else { |
| 985 | if (h >= tmpline + sizeof(tmpline) - 5) |
| 986 | goto trunc; |
| 987 | memcpy(h, " \"-\"", 4); |
| 988 | h += 4; |
| 989 | } |
| 990 | |
| 991 | if (txn->srv_cookie) { |
| 992 | w = strlen(txn->srv_cookie); |
| 993 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 994 | goto trunc; |
| 995 | *(h++) = ' '; |
| 996 | *(h++) = '\"'; |
| 997 | memcpy(h, txn->srv_cookie, w); |
| 998 | h += w; |
| 999 | *(h++) = '\"'; |
| 1000 | } else { |
| 1001 | if (h >= tmpline + sizeof(tmpline) - 5) |
| 1002 | goto trunc; |
| 1003 | memcpy(h, " \"-\"", 4); |
| 1004 | h += 4; |
| 1005 | } |
| 1006 | |
| 1007 | if ((fe->to_log & LW_REQHDR) && txn->req.cap) { |
| 1008 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 1009 | if (h >= sizeof (tmpline) + tmpline - 4) |
| 1010 | goto trunc; |
| 1011 | *(h++) = ' '; |
| 1012 | *(h++) = '\"'; |
| 1013 | h = encode_string(h, tmpline + sizeof(tmpline) - 2, |
| 1014 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 1015 | *(h++) = '\"'; |
| 1016 | } |
| 1017 | } |
| 1018 | |
| 1019 | if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) { |
| 1020 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 1021 | if (h >= sizeof (tmpline) + tmpline - 4) |
| 1022 | goto trunc; |
| 1023 | *(h++) = ' '; |
| 1024 | *(h++) = '\"'; |
| 1025 | h = encode_string(h, tmpline + sizeof(tmpline) - 2, |
| 1026 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 1027 | *(h++) = '\"'; |
| 1028 | } |
| 1029 | } |
| 1030 | |
| 1031 | trunc: |
| 1032 | *h = '\0'; |
| 1033 | |
| 1034 | level = LOG_INFO; |
| 1035 | if (err && (fe->options2 & PR_O2_LOGERRORS)) |
| 1036 | level = LOG_ERR; |
| 1037 | |
| 1038 | send_log(prx_log, level, "%s\n", tmpline); |
| 1039 | |
| 1040 | s->logs.logwait = 0; |
| 1041 | } |
| 1042 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1043 | /* |
| 1044 | * send a log for the session when we have enough info about it. |
| 1045 | * Will not log if the frontend has no log defined. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1046 | */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1047 | void http_sess_log(struct session *s) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1048 | { |
| 1049 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 1050 | struct proxy *fe = s->fe; |
| 1051 | struct proxy *be = s->be; |
| 1052 | struct proxy *prx_log; |
| 1053 | struct http_txn *txn = &s->txn; |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1054 | int tolog, level, err; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1055 | char *uri, *h; |
| 1056 | char *svid; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1057 | struct tm tm; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1058 | static char tmpline[MAX_SYSLOG_LEN]; |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1059 | int t_request; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1060 | int hdr; |
| 1061 | |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1062 | /* if we don't want to log normal traffic, return now */ |
| 1063 | err = (s->flags & (SN_ERR_MASK | SN_REDISP)) || |
| 1064 | (s->conn_retries != be->conn_retries) || |
| 1065 | txn->status >= 500; |
| 1066 | if (!err && (fe->options2 & PR_O2_NOLOGNORM)) |
| 1067 | return; |
| 1068 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1069 | if (fe->logfac1 < 0 && fe->logfac2 < 0) |
| 1070 | return; |
| 1071 | prx_log = fe; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1072 | |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 1073 | if (prx_log->options2 & PR_O2_CLFLOG) |
| 1074 | return http_sess_clflog(s); |
| 1075 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1076 | if (s->cli_addr.ss_family == AF_INET) |
| 1077 | inet_ntop(AF_INET, |
| 1078 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 1079 | pn, sizeof(pn)); |
| 1080 | else |
| 1081 | inet_ntop(AF_INET6, |
| 1082 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 1083 | pn, sizeof(pn)); |
| 1084 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 1085 | get_localtime(s->logs.accept_date.tv_sec, &tm); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1086 | |
| 1087 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 1088 | tolog = fe->to_log; |
| 1089 | |
| 1090 | h = tmpline; |
| 1091 | if (fe->to_log & LW_REQHDR && |
| 1092 | txn->req.cap && |
| 1093 | (h < tmpline + sizeof(tmpline) - 10)) { |
| 1094 | *(h++) = ' '; |
| 1095 | *(h++) = '{'; |
| 1096 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 1097 | if (hdr) |
| 1098 | *(h++) = '|'; |
| 1099 | if (txn->req.cap[hdr] != NULL) |
| 1100 | h = encode_string(h, tmpline + sizeof(tmpline) - 7, |
| 1101 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 1102 | } |
| 1103 | *(h++) = '}'; |
| 1104 | } |
| 1105 | |
| 1106 | if (fe->to_log & LW_RSPHDR && |
| 1107 | txn->rsp.cap && |
| 1108 | (h < tmpline + sizeof(tmpline) - 7)) { |
| 1109 | *(h++) = ' '; |
| 1110 | *(h++) = '{'; |
| 1111 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 1112 | if (hdr) |
| 1113 | *(h++) = '|'; |
| 1114 | if (txn->rsp.cap[hdr] != NULL) |
| 1115 | h = encode_string(h, tmpline + sizeof(tmpline) - 4, |
| 1116 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 1117 | } |
| 1118 | *(h++) = '}'; |
| 1119 | } |
| 1120 | |
| 1121 | if (h < tmpline + sizeof(tmpline) - 4) { |
| 1122 | *(h++) = ' '; |
| 1123 | *(h++) = '"'; |
| 1124 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 1125 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 1126 | '#', url_encode_map, uri); |
| 1127 | *(h++) = '"'; |
| 1128 | } |
| 1129 | *h = '\0'; |
| 1130 | |
| 1131 | svid = (tolog & LW_SVID) ? |
| 1132 | (s->data_source != DATA_SRC_STATS) ? |
| 1133 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 1134 | |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1135 | t_request = -1; |
| 1136 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 1137 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 1138 | |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1139 | level = LOG_INFO; |
| 1140 | if (err && (fe->options2 & PR_O2_LOGERRORS)) |
| 1141 | level = LOG_ERR; |
| 1142 | |
| 1143 | send_log(prx_log, level, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1144 | "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]" |
Willy Tarreau | 1772ece | 2009-04-03 14:49:12 +0200 | [diff] [blame] | 1145 | " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld" |
| 1146 | " %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] | 1147 | pn, |
| 1148 | (s->cli_addr.ss_family == AF_INET) ? |
| 1149 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 1150 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1151 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
Willy Tarreau | 1772ece | 2009-04-03 14:49:12 +0200 | [diff] [blame] | 1152 | 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] | 1153 | fe->id, be->id, svid, |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1154 | t_request, |
| 1155 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1156 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 1157 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 1158 | (tolog & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 1159 | txn->status, |
Willy Tarreau | 8b3977f | 2008-01-18 11:16:32 +0100 | [diff] [blame] | 1160 | (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1161 | txn->cli_cookie ? txn->cli_cookie : "-", |
| 1162 | txn->srv_cookie ? txn->srv_cookie : "-", |
| 1163 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 1164 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 1165 | (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 1166 | (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-', |
| 1167 | 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] | 1168 | (s->flags & SN_REDISP)?"+":"", |
| 1169 | (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] | 1170 | s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline); |
| 1171 | |
| 1172 | s->logs.logwait = 0; |
| 1173 | } |
| 1174 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1175 | |
| 1176 | /* |
| 1177 | * Capture headers from message starting at <som> according to header list |
| 1178 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 1179 | */ |
| 1180 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1181 | char **cap, struct cap_hdr *cap_hdr) |
| 1182 | { |
| 1183 | char *eol, *sol, *col, *sov; |
| 1184 | int cur_idx; |
| 1185 | struct cap_hdr *h; |
| 1186 | int len; |
| 1187 | |
| 1188 | sol = som + hdr_idx_first_pos(idx); |
| 1189 | cur_idx = hdr_idx_first_idx(idx); |
| 1190 | |
| 1191 | while (cur_idx) { |
| 1192 | eol = sol + idx->v[cur_idx].len; |
| 1193 | |
| 1194 | col = sol; |
| 1195 | while (col < eol && *col != ':') |
| 1196 | col++; |
| 1197 | |
| 1198 | sov = col + 1; |
| 1199 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 1200 | sov++; |
| 1201 | |
| 1202 | for (h = cap_hdr; h; h = h->next) { |
| 1203 | if ((h->namelen == col - sol) && |
| 1204 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1205 | if (cap[h->index] == NULL) |
| 1206 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1207 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1208 | |
| 1209 | if (cap[h->index] == NULL) { |
| 1210 | Alert("HTTP capture : out of memory.\n"); |
| 1211 | continue; |
| 1212 | } |
| 1213 | |
| 1214 | len = eol - sov; |
| 1215 | if (len > h->len) |
| 1216 | len = h->len; |
| 1217 | |
| 1218 | memcpy(cap[h->index], sov, len); |
| 1219 | cap[h->index][len]=0; |
| 1220 | } |
| 1221 | } |
| 1222 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1223 | cur_idx = idx->v[cur_idx].next; |
| 1224 | } |
| 1225 | } |
| 1226 | |
| 1227 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1228 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1229 | */ |
| 1230 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1231 | |
| 1232 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1233 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1234 | */ |
| 1235 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1236 | ptr++; \ |
| 1237 | if (likely(ptr < end)) \ |
| 1238 | goto good; \ |
| 1239 | else { \ |
| 1240 | state = (st); \ |
| 1241 | goto http_msg_ood; \ |
| 1242 | } \ |
| 1243 | } while (0) |
| 1244 | |
| 1245 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1246 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1247 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1248 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1249 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1250 | * will give undefined results. |
| 1251 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1252 | * and that msg->sol points to the beginning of the response. |
| 1253 | * If a complete line is found (which implies that at least one CR or LF is |
| 1254 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1255 | * returned indicating an incomplete line (which does not mean that parts have |
| 1256 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1257 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1258 | * upon next call. |
| 1259 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1260 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1261 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1262 | * within its state machine and use the same macros, hence the need for same |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1263 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1264 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1265 | const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, |
| 1266 | unsigned int state, const char *ptr, const char *end, |
| 1267 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1268 | { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1269 | switch (state) { |
| 1270 | http_msg_rpver: |
| 1271 | case HTTP_MSG_RPVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1272 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1273 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1274 | |
| 1275 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1276 | msg->sl.st.v_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1277 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1278 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1279 | state = HTTP_MSG_ERROR; |
| 1280 | break; |
| 1281 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1282 | http_msg_rpver_sp: |
| 1283 | case HTTP_MSG_RPVER_SP: |
| 1284 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1285 | msg->sl.st.c = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1286 | goto http_msg_rpcode; |
| 1287 | } |
| 1288 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1289 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1290 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1291 | state = HTTP_MSG_ERROR; |
| 1292 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1293 | |
| 1294 | http_msg_rpcode: |
| 1295 | case HTTP_MSG_RPCODE: |
| 1296 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1297 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1298 | |
| 1299 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1300 | 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] | 1301 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1302 | } |
| 1303 | |
| 1304 | /* so it's a CR/LF, so there is no reason phrase */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1305 | 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] | 1306 | http_msg_rsp_reason: |
| 1307 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1308 | msg->sl.st.r = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1309 | msg->sl.st.r_l = 0; |
| 1310 | goto http_msg_rpline_eol; |
| 1311 | |
| 1312 | http_msg_rpcode_sp: |
| 1313 | case HTTP_MSG_RPCODE_SP: |
| 1314 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1315 | msg->sl.st.r = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1316 | goto http_msg_rpreason; |
| 1317 | } |
| 1318 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1319 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1320 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1321 | goto http_msg_rsp_reason; |
| 1322 | |
| 1323 | http_msg_rpreason: |
| 1324 | case HTTP_MSG_RPREASON: |
| 1325 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1326 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1327 | 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] | 1328 | http_msg_rpline_eol: |
| 1329 | /* We have seen the end of line. Note that we do not |
| 1330 | * necessarily have the \n yet, but at least we know that we |
| 1331 | * have EITHER \r OR \n, otherwise the response would not be |
| 1332 | * complete. We can then record the response length and return |
| 1333 | * to the caller which will be able to register it. |
| 1334 | */ |
| 1335 | msg->sl.st.l = ptr - msg->sol; |
| 1336 | return ptr; |
| 1337 | |
| 1338 | #ifdef DEBUG_FULL |
| 1339 | default: |
| 1340 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1341 | exit(1); |
| 1342 | #endif |
| 1343 | } |
| 1344 | |
| 1345 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1346 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1347 | if (ret_state) |
| 1348 | *ret_state = state; |
| 1349 | if (ret_ptr) |
| 1350 | *ret_ptr = (char *)ptr; |
| 1351 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | |
| 1355 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1356 | * This function parses a request line between <ptr> and <end>, starting with |
| 1357 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1358 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1359 | * will give undefined results. |
| 1360 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1361 | * and that msg->sol points to the beginning of the request. |
| 1362 | * If a complete line is found (which implies that at least one CR or LF is |
| 1363 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1364 | * returned indicating an incomplete line (which does not mean that parts have |
| 1365 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1366 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1367 | * upon next call. |
| 1368 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1369 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1370 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1371 | * 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] | 1372 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1373 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1374 | const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, |
| 1375 | unsigned int state, const char *ptr, const char *end, |
| 1376 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1377 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1378 | switch (state) { |
| 1379 | http_msg_rqmeth: |
| 1380 | case HTTP_MSG_RQMETH: |
| 1381 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1382 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1383 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1384 | if (likely(HTTP_IS_SPHT(*ptr))) { |
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 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1387 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1388 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1389 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1390 | /* HTTP 0.9 request */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1391 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1392 | http_msg_req09_uri: |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1393 | msg->sl.rq.u = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1394 | http_msg_req09_uri_e: |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1395 | 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] | 1396 | http_msg_req09_ver: |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1397 | msg->sl.rq.v = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1398 | msg->sl.rq.v_l = 0; |
| 1399 | goto http_msg_rqline_eol; |
| 1400 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1401 | state = HTTP_MSG_ERROR; |
| 1402 | break; |
| 1403 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1404 | http_msg_rqmeth_sp: |
| 1405 | case HTTP_MSG_RQMETH_SP: |
| 1406 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1407 | msg->sl.rq.u = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1408 | goto http_msg_rquri; |
| 1409 | } |
| 1410 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1411 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1412 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1413 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1414 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1415 | http_msg_rquri: |
| 1416 | case HTTP_MSG_RQURI: |
| 1417 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1418 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1419 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1420 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1421 | 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] | 1422 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1423 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1424 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1425 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1426 | goto http_msg_req09_uri_e; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1427 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1428 | http_msg_rquri_sp: |
| 1429 | case HTTP_MSG_RQURI_SP: |
| 1430 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1431 | msg->sl.rq.v = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1432 | goto http_msg_rqver; |
| 1433 | } |
| 1434 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1435 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1436 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1437 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1438 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1439 | http_msg_rqver: |
| 1440 | case HTTP_MSG_RQVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1441 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1442 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1443 | |
| 1444 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1445 | 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] | 1446 | http_msg_rqline_eol: |
| 1447 | /* We have seen the end of line. Note that we do not |
| 1448 | * necessarily have the \n yet, but at least we know that we |
| 1449 | * have EITHER \r OR \n, otherwise the request would not be |
| 1450 | * complete. We can then record the request length and return |
| 1451 | * to the caller which will be able to register it. |
| 1452 | */ |
| 1453 | msg->sl.rq.l = ptr - msg->sol; |
| 1454 | return ptr; |
| 1455 | } |
| 1456 | |
| 1457 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1458 | state = HTTP_MSG_ERROR; |
| 1459 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1460 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1461 | #ifdef DEBUG_FULL |
| 1462 | default: |
| 1463 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1464 | exit(1); |
| 1465 | #endif |
| 1466 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1467 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1468 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1469 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1470 | if (ret_state) |
| 1471 | *ret_state = state; |
| 1472 | if (ret_ptr) |
| 1473 | *ret_ptr = (char *)ptr; |
| 1474 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1475 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1476 | |
| 1477 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1478 | /* |
| 1479 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1480 | * depending on the initial msg->msg_state. It can be preempted everywhere |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1481 | * when data are missing and recalled at the exact same location with no |
| 1482 | * information loss. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1483 | * 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] | 1484 | * fields. Note that msg->som and msg->sol will be initialized after completing |
| 1485 | * the first state, so that none of the msg pointers has to be initialized |
| 1486 | * prior to the first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1487 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1488 | void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx) |
| 1489 | { |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1490 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1491 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1492 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1493 | state = msg->msg_state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1494 | ptr = buf->lr; |
| 1495 | end = buf->r; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1496 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1497 | if (unlikely(ptr >= end)) |
| 1498 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1499 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1500 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1501 | /* |
| 1502 | * First, states that are specific to the response only. |
| 1503 | * We check them first so that request and headers are |
| 1504 | * closer to each other (accessed more often). |
| 1505 | */ |
| 1506 | http_msg_rpbefore: |
| 1507 | case HTTP_MSG_RPBEFORE: |
| 1508 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1509 | /* we have a start of message, but we have to check |
| 1510 | * first if we need to remove some CRLF. We can only |
| 1511 | * do this when send_max=0. |
| 1512 | */ |
| 1513 | char *beg = buf->w + buf->send_max; |
| 1514 | if (beg >= buf->data + buf->size) |
| 1515 | beg -= buf->size; |
| 1516 | if (unlikely(ptr != beg)) { |
| 1517 | if (buf->send_max) |
| 1518 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1519 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1520 | buffer_ignore(buf, ptr - beg); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1521 | } |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1522 | msg->som = ptr - buf->data; |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 1523 | msg->sol = ptr; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1524 | hdr_idx_init(idx); |
| 1525 | state = HTTP_MSG_RPVER; |
| 1526 | goto http_msg_rpver; |
| 1527 | } |
| 1528 | |
| 1529 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1530 | goto http_msg_invalid; |
| 1531 | |
| 1532 | if (unlikely(*ptr == '\n')) |
| 1533 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1534 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1535 | /* stop here */ |
| 1536 | |
| 1537 | http_msg_rpbefore_cr: |
| 1538 | case HTTP_MSG_RPBEFORE_CR: |
| 1539 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1540 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1541 | /* stop here */ |
| 1542 | |
| 1543 | http_msg_rpver: |
| 1544 | case HTTP_MSG_RPVER: |
| 1545 | case HTTP_MSG_RPVER_SP: |
| 1546 | case HTTP_MSG_RPCODE: |
| 1547 | case HTTP_MSG_RPCODE_SP: |
| 1548 | case HTTP_MSG_RPREASON: |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1549 | ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1550 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1551 | if (unlikely(!ptr)) |
| 1552 | return; |
| 1553 | |
| 1554 | /* we have a full response and we know that we have either a CR |
| 1555 | * or an LF at <ptr>. |
| 1556 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1557 | //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] | 1558 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1559 | |
| 1560 | msg->sol = ptr; |
| 1561 | if (likely(*ptr == '\r')) |
| 1562 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1563 | goto http_msg_rpline_end; |
| 1564 | |
| 1565 | http_msg_rpline_end: |
| 1566 | case HTTP_MSG_RPLINE_END: |
| 1567 | /* msg->sol must point to the first of CR or LF. */ |
| 1568 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1569 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1570 | /* stop here */ |
| 1571 | |
| 1572 | /* |
| 1573 | * Second, states that are specific to the request only |
| 1574 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1575 | http_msg_rqbefore: |
| 1576 | case HTTP_MSG_RQBEFORE: |
| 1577 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1578 | /* we have a start of message, but we have to check |
| 1579 | * first if we need to remove some CRLF. We can only |
| 1580 | * do this when send_max=0. |
| 1581 | */ |
| 1582 | char *beg = buf->w + buf->send_max; |
| 1583 | if (beg >= buf->data + buf->size) |
| 1584 | beg -= buf->size; |
| 1585 | if (likely(ptr != beg)) { |
| 1586 | if (buf->send_max) |
| 1587 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1588 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1589 | buffer_ignore(buf, ptr - beg); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1590 | } |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1591 | msg->som = ptr - buf->data; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1592 | msg->sol = ptr; |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1593 | /* we will need this when keep-alive will be supported |
| 1594 | hdr_idx_init(idx); |
| 1595 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1596 | state = HTTP_MSG_RQMETH; |
| 1597 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1598 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1599 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1600 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1601 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1602 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1603 | if (unlikely(*ptr == '\n')) |
| 1604 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1605 | 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] | 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_rqbefore_cr: |
| 1609 | case HTTP_MSG_RQBEFORE_CR: |
| 1610 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1611 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1612 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1613 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1614 | http_msg_rqmeth: |
| 1615 | case HTTP_MSG_RQMETH: |
| 1616 | case HTTP_MSG_RQMETH_SP: |
| 1617 | case HTTP_MSG_RQURI: |
| 1618 | case HTTP_MSG_RQURI_SP: |
| 1619 | case HTTP_MSG_RQVER: |
| 1620 | ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1621 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1622 | if (unlikely(!ptr)) |
| 1623 | return; |
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 | /* we have a full request and we know that we have either a CR |
| 1626 | * or an LF at <ptr>. |
| 1627 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1628 | //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] | 1629 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1630 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1631 | msg->sol = ptr; |
| 1632 | if (likely(*ptr == '\r')) |
| 1633 | 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] | 1634 | goto http_msg_rqline_end; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1635 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1636 | http_msg_rqline_end: |
| 1637 | case HTTP_MSG_RQLINE_END: |
| 1638 | /* check for HTTP/0.9 request : no version information available. |
| 1639 | * msg->sol must point to the first of CR or LF. |
| 1640 | */ |
| 1641 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1642 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1643 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1644 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1645 | 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] | 1646 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1647 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1648 | /* |
| 1649 | * Common states below |
| 1650 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1651 | http_msg_hdr_first: |
| 1652 | case HTTP_MSG_HDR_FIRST: |
| 1653 | msg->sol = ptr; |
| 1654 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1655 | goto http_msg_hdr_name; |
| 1656 | } |
| 1657 | |
| 1658 | if (likely(*ptr == '\r')) |
| 1659 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1660 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1661 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1662 | http_msg_hdr_name: |
| 1663 | case HTTP_MSG_HDR_NAME: |
| 1664 | /* assumes msg->sol points to the first char */ |
| 1665 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1666 | 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] | 1667 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1668 | if (likely(*ptr == ':')) { |
| 1669 | msg->col = ptr - buf->data; |
| 1670 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| 1671 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1672 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1673 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1674 | goto http_msg_invalid; |
| 1675 | |
| 1676 | if (msg->err_pos == -1) /* capture error pointer */ |
| 1677 | msg->err_pos = ptr - buf->data; /* >= 0 now */ |
| 1678 | |
| 1679 | /* and we still accept this non-token character */ |
| 1680 | 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] | 1681 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1682 | http_msg_hdr_l1_sp: |
| 1683 | case HTTP_MSG_HDR_L1_SP: |
| 1684 | /* assumes msg->sol points to the first char and msg->col to the colon */ |
| 1685 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1686 | 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] | 1687 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1688 | /* header value can be basically anything except CR/LF */ |
| 1689 | msg->sov = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1690 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1691 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1692 | goto http_msg_hdr_val; |
| 1693 | } |
| 1694 | |
| 1695 | if (likely(*ptr == '\r')) |
| 1696 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1697 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1698 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1699 | http_msg_hdr_l1_lf: |
| 1700 | case HTTP_MSG_HDR_L1_LF: |
| 1701 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1702 | 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] | 1703 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1704 | http_msg_hdr_l1_lws: |
| 1705 | case HTTP_MSG_HDR_L1_LWS: |
| 1706 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1707 | /* replace HT,CR,LF with spaces */ |
| 1708 | for (; buf->data+msg->sov < ptr; msg->sov++) |
| 1709 | buf->data[msg->sov] = ' '; |
| 1710 | goto http_msg_hdr_l1_sp; |
| 1711 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1712 | /* we had a header consisting only in spaces ! */ |
| 1713 | msg->eol = buf->data + msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1714 | goto http_msg_complete_header; |
| 1715 | |
| 1716 | http_msg_hdr_val: |
| 1717 | case HTTP_MSG_HDR_VAL: |
| 1718 | /* assumes msg->sol points to the first char, msg->col to the |
| 1719 | * colon, and msg->sov points to the first character of the |
| 1720 | * value. |
| 1721 | */ |
| 1722 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1723 | 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] | 1724 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1725 | msg->eol = ptr; |
| 1726 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1727 | * real header end in case it ends with lots of LWS, but is this |
| 1728 | * really needed ? |
| 1729 | */ |
| 1730 | if (likely(*ptr == '\r')) |
| 1731 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1732 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1733 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1734 | http_msg_hdr_l2_lf: |
| 1735 | case HTTP_MSG_HDR_L2_LF: |
| 1736 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1737 | 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] | 1738 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1739 | http_msg_hdr_l2_lws: |
| 1740 | case HTTP_MSG_HDR_L2_LWS: |
| 1741 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1742 | /* LWS: replace HT,CR,LF with spaces */ |
| 1743 | for (; msg->eol < ptr; msg->eol++) |
| 1744 | *msg->eol = ' '; |
| 1745 | goto http_msg_hdr_val; |
| 1746 | } |
| 1747 | http_msg_complete_header: |
| 1748 | /* |
| 1749 | * It was a new header, so the last one is finished. |
| 1750 | * Assumes msg->sol points to the first char, msg->col to the |
| 1751 | * colon, msg->sov points to the first character of the value |
| 1752 | * and msg->eol to the first CR or LF so we know how the line |
| 1753 | * ends. We insert last header into the index. |
| 1754 | */ |
| 1755 | /* |
| 1756 | fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol); |
| 1757 | write(2, msg->sol, msg->eol-msg->sol); |
| 1758 | fprintf(stderr,"\n"); |
| 1759 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1760 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1761 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r', |
| 1762 | idx, idx->tail) < 0)) |
| 1763 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1764 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1765 | msg->sol = ptr; |
| 1766 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1767 | goto http_msg_hdr_name; |
| 1768 | } |
| 1769 | |
| 1770 | if (likely(*ptr == '\r')) |
| 1771 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1772 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1773 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1774 | http_msg_last_lf: |
| 1775 | case HTTP_MSG_LAST_LF: |
| 1776 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1777 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1778 | ptr++; |
| 1779 | buf->lr = ptr; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1780 | msg->col = msg->sov = buf->lr - buf->data; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1781 | msg->eoh = msg->sol - buf->data; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1782 | msg->sol = buf->data + msg->som; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1783 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1784 | return; |
| 1785 | #ifdef DEBUG_FULL |
| 1786 | default: |
| 1787 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1788 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1789 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1790 | } |
| 1791 | http_msg_ood: |
| 1792 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1793 | msg->msg_state = state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1794 | buf->lr = ptr; |
| 1795 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1796 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1797 | http_msg_invalid: |
| 1798 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1799 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1800 | buf->lr = ptr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1801 | return; |
| 1802 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1803 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1804 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1805 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1806 | * nothing is done and 1 is returned. |
| 1807 | */ |
| 1808 | static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn) |
| 1809 | { |
| 1810 | int delta; |
| 1811 | char *cur_end; |
| 1812 | |
| 1813 | if (msg->sl.rq.v_l != 0) |
| 1814 | return 1; |
| 1815 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1816 | cur_end = msg->sol + msg->sl.rq.l; |
| 1817 | delta = 0; |
| 1818 | |
| 1819 | if (msg->sl.rq.u_l == 0) { |
| 1820 | /* if no URI was set, add "/" */ |
| 1821 | delta = buffer_replace2(req, cur_end, cur_end, " /", 2); |
| 1822 | cur_end += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1823 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1824 | } |
| 1825 | /* add HTTP version */ |
| 1826 | 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] | 1827 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1828 | cur_end += delta; |
| 1829 | cur_end = (char *)http_parse_reqline(msg, req->data, |
| 1830 | HTTP_MSG_RQMETH, |
| 1831 | msg->sol, cur_end + 1, |
| 1832 | NULL, NULL); |
| 1833 | if (unlikely(!cur_end)) |
| 1834 | return 0; |
| 1835 | |
| 1836 | /* we have a full HTTP/1.0 request now and we know that |
| 1837 | * we have either a CR or an LF at <ptr>. |
| 1838 | */ |
| 1839 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1840 | return 1; |
| 1841 | } |
| 1842 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1843 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
| 1844 | * and "keep-alive" values. If a buffer is provided and we already know that |
| 1845 | * some headers may safely be removed, we remove them now. The <to_del> flags |
| 1846 | * are used for that : |
| 1847 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1848 | * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1). |
| 1849 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1850 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1851 | * harmless combinations may be removed. Do not call that after changes have |
| 1852 | * been processed. If unused, the buffer can be NULL, and no data will be |
| 1853 | * changed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1854 | */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1855 | 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] | 1856 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1857 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1858 | const char *hdr_val = "Connection"; |
| 1859 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1860 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1861 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1862 | return; |
| 1863 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1864 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1865 | hdr_val = "Proxy-Connection"; |
| 1866 | hdr_len = 16; |
| 1867 | } |
| 1868 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1869 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1870 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1871 | while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1872 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1873 | txn->flags |= TX_HDR_CONN_KAL; |
| 1874 | if ((to_del & 2) && buf) |
| 1875 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
| 1876 | else |
| 1877 | txn->flags |= TX_CON_KAL_SET; |
| 1878 | } |
| 1879 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1880 | txn->flags |= TX_HDR_CONN_CLO; |
| 1881 | if ((to_del & 1) && buf) |
| 1882 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
| 1883 | else |
| 1884 | txn->flags |= TX_CON_CLO_SET; |
| 1885 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1886 | } |
| 1887 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1888 | txn->flags |= TX_HDR_CONN_PRS; |
| 1889 | return; |
| 1890 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1891 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1892 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 1893 | * added depending on the <wanted> flags, which are exclusively composed of |
| 1894 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 1895 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 1896 | */ |
| 1897 | void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int wanted) |
| 1898 | { |
| 1899 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1900 | const char *hdr_val = "Connection"; |
| 1901 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1902 | |
| 1903 | ctx.idx = 0; |
| 1904 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1905 | |
| 1906 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1907 | hdr_val = "Proxy-Connection"; |
| 1908 | hdr_len = 16; |
| 1909 | } |
| 1910 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1911 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1912 | while (http_find_header2(hdr_val, hdr_len, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1913 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1914 | if (wanted & TX_CON_KAL_SET) |
| 1915 | txn->flags |= TX_CON_KAL_SET; |
| 1916 | else |
| 1917 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1918 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1919 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1920 | if (wanted & TX_CON_CLO_SET) |
| 1921 | txn->flags |= TX_CON_CLO_SET; |
| 1922 | else |
| 1923 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 1924 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1925 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1926 | |
| 1927 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 1928 | return; |
| 1929 | |
| 1930 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 1931 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1932 | hdr_val = "Connection: close"; |
| 1933 | hdr_len = 17; |
| 1934 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1935 | hdr_val = "Proxy-Connection: close"; |
| 1936 | hdr_len = 23; |
| 1937 | } |
| 1938 | http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1939 | } |
| 1940 | |
| 1941 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 1942 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1943 | hdr_val = "Connection: keep-alive"; |
| 1944 | hdr_len = 22; |
| 1945 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1946 | hdr_val = "Proxy-Connection: keep-alive"; |
| 1947 | hdr_len = 28; |
| 1948 | } |
| 1949 | http_header_add_tail2(buf, msg, &txn->hdr_idx, hdr_val, hdr_len); |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1950 | } |
| 1951 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1952 | } |
| 1953 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1954 | /* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the |
| 1955 | * first byte of body, and increments msg->sov by the number of bytes parsed, |
| 1956 | * so that we know we can forward between ->som and ->sov. Note that due to |
| 1957 | * possible wrapping at the end of the buffer, it is possible that msg->sov is |
| 1958 | * lower than msg->som. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1959 | * 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] | 1960 | * 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] | 1961 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1962 | int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1963 | { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1964 | char *ptr = buf->lr; |
| 1965 | char *end = buf->data + buf->size; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1966 | unsigned int chunk = 0; |
| 1967 | |
| 1968 | /* The chunk size is in the following form, though we are only |
| 1969 | * interested in the size and CRLF : |
| 1970 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 1971 | */ |
| 1972 | while (1) { |
| 1973 | int c; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1974 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1975 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1976 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1977 | if (c < 0) /* not a hex digit anymore */ |
| 1978 | break; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1979 | if (++ptr >= end) |
| 1980 | ptr = buf->data; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1981 | if (chunk & 0xF000000) /* overflow will occur */ |
| 1982 | return -1; |
| 1983 | chunk = (chunk << 4) + c; |
| 1984 | } |
| 1985 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1986 | /* empty size not allowed */ |
| 1987 | if (ptr == buf->lr) |
| 1988 | return -1; |
| 1989 | |
| 1990 | while (http_is_spht[(unsigned char)*ptr]) { |
| 1991 | if (++ptr >= end) |
| 1992 | ptr = buf->data; |
| 1993 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1994 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 1995 | } |
| 1996 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 1997 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 1998 | * for the end of chunk size. |
| 1999 | */ |
| 2000 | while (1) { |
| 2001 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 2002 | /* we now have a CR or an LF at ptr */ |
| 2003 | if (likely(*ptr == '\r')) { |
| 2004 | if (++ptr >= end) |
| 2005 | ptr = buf->data; |
| 2006 | if (ptr == buf->r) |
| 2007 | return 0; |
| 2008 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2009 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2010 | if (*ptr != '\n') |
| 2011 | return -1; |
| 2012 | if (++ptr >= end) |
| 2013 | ptr = buf->data; |
| 2014 | /* done */ |
| 2015 | break; |
| 2016 | } |
| 2017 | else if (*ptr == ';') { |
| 2018 | /* chunk extension, ends at next CRLF */ |
| 2019 | if (++ptr >= end) |
| 2020 | ptr = buf->data; |
| 2021 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2022 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2023 | |
| 2024 | while (!HTTP_IS_CRLF(*ptr)) { |
| 2025 | if (++ptr >= end) |
| 2026 | ptr = buf->data; |
| 2027 | if (ptr == buf->r) |
| 2028 | return 0; |
| 2029 | } |
| 2030 | /* we have a CRLF now, loop above */ |
| 2031 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2032 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2033 | else |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2034 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2035 | } |
| 2036 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2037 | /* OK we found our CRLF and now <ptr> points to the next byte, |
| 2038 | * which may or may not be present. We save that into ->lr and |
| 2039 | * ->sov. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2040 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2041 | msg->sov += ptr - buf->lr; |
| 2042 | buf->lr = ptr; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2043 | msg->hdr_content_len = chunk; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2044 | msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2045 | return 1; |
| 2046 | } |
| 2047 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2048 | /* This function skips trailers in the buffer <buf> associated with HTTP |
| 2049 | * message <msg>. The first visited position is buf->lr. If the end of |
| 2050 | * the trailers is found, it is automatically scheduled to be forwarded, |
| 2051 | * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0. |
| 2052 | * If not enough data are available, the function does not change anything |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2053 | * except maybe buf->lr and msg->sov if it could parse some lines, and returns |
| 2054 | * zero. If a parse error is encountered, the function returns < 0 and does not |
| 2055 | * change anything except maybe buf->lr and msg->sov. Note that the message |
| 2056 | * must already be in HTTP_MSG_TRAILERS state before calling this function, |
| 2057 | * which implies that all non-trailers data have already been scheduled for |
| 2058 | * forwarding, and that the difference between msg->som and msg->sov exactly |
| 2059 | * matches the length of trailers already parsed and not forwarded. It is also |
| 2060 | * important to note that this function is designed to be able to parse wrapped |
| 2061 | * headers at end of buffer. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2062 | */ |
| 2063 | int http_forward_trailers(struct buffer *buf, struct http_msg *msg) |
| 2064 | { |
| 2065 | /* we have buf->lr which points to next line. Look for CRLF. */ |
| 2066 | while (1) { |
| 2067 | char *p1 = NULL, *p2 = NULL; |
| 2068 | char *ptr = buf->lr; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2069 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2070 | |
| 2071 | /* scan current line and stop at LF or CRLF */ |
| 2072 | while (1) { |
| 2073 | if (ptr == buf->r) |
| 2074 | return 0; |
| 2075 | |
| 2076 | if (*ptr == '\n') { |
| 2077 | if (!p1) |
| 2078 | p1 = ptr; |
| 2079 | p2 = ptr; |
| 2080 | break; |
| 2081 | } |
| 2082 | |
| 2083 | if (*ptr == '\r') { |
| 2084 | if (p1) |
| 2085 | return -1; |
| 2086 | p1 = ptr; |
| 2087 | } |
| 2088 | |
| 2089 | ptr++; |
| 2090 | if (ptr >= buf->data + buf->size) |
| 2091 | ptr = buf->data; |
| 2092 | } |
| 2093 | |
| 2094 | /* after LF; point to beginning of next line */ |
| 2095 | p2++; |
| 2096 | if (p2 >= buf->data + buf->size) |
| 2097 | p2 = buf->data; |
| 2098 | |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2099 | bytes = p2 - buf->lr; |
| 2100 | if (bytes < 0) |
| 2101 | bytes += buf->size; |
| 2102 | |
| 2103 | /* schedule this line for forwarding */ |
| 2104 | msg->sov += bytes; |
| 2105 | if (msg->sov >= buf->size) |
| 2106 | msg->sov -= buf->size; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2107 | |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2108 | if (p1 == buf->lr) { |
| 2109 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 2110 | * Everything was scheduled for forwarding, there's nothing |
| 2111 | * left from this message. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 2112 | */ |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2113 | buf->lr = p2; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2114 | msg->msg_state = HTTP_MSG_DONE; |
| 2115 | return 1; |
| 2116 | } |
| 2117 | /* OK, next line then */ |
| 2118 | buf->lr = p2; |
| 2119 | } |
| 2120 | } |
| 2121 | |
| 2122 | /* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or |
| 2123 | * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov, |
| 2124 | * ->som, buf->lr in order to include this part into the next forwarding phase. |
| 2125 | * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If |
| 2126 | * not enough data are available, the function does not change anything and |
| 2127 | * returns zero. If a parse error is encountered, the function returns < 0 and |
| 2128 | * does not change anything. Note: this function is designed to parse wrapped |
| 2129 | * CRLF at the end of the buffer. |
| 2130 | */ |
| 2131 | int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg) |
| 2132 | { |
| 2133 | char *ptr; |
| 2134 | int bytes; |
| 2135 | |
| 2136 | /* NB: we'll check data availabilty at the end. It's not a |
| 2137 | * problem because whatever we match first will be checked |
| 2138 | * against the correct length. |
| 2139 | */ |
| 2140 | bytes = 1; |
| 2141 | ptr = buf->lr; |
| 2142 | if (*ptr == '\r') { |
| 2143 | bytes++; |
| 2144 | ptr++; |
| 2145 | if (ptr >= buf->data + buf->size) |
| 2146 | ptr = buf->data; |
| 2147 | } |
| 2148 | |
| 2149 | if (buf->l < bytes) |
| 2150 | return 0; |
| 2151 | |
| 2152 | if (*ptr != '\n') |
| 2153 | return -1; |
| 2154 | |
| 2155 | ptr++; |
| 2156 | if (ptr >= buf->data + buf->size) |
| 2157 | ptr = buf->data; |
| 2158 | buf->lr = ptr; |
| 2159 | /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */ |
| 2160 | msg->sov = ptr - buf->data; |
| 2161 | msg->som = msg->sov - bytes; |
| 2162 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 2163 | return 1; |
| 2164 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2165 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2166 | void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg) |
| 2167 | { |
| 2168 | char *end = buf->data + buf->size; |
| 2169 | int off = buf->data + buf->size - buf->w; |
| 2170 | |
| 2171 | /* two possible cases : |
| 2172 | * - the buffer is in one contiguous block, we move it in-place |
| 2173 | * - the buffer is in two blocks, we move it via the trash |
| 2174 | */ |
| 2175 | if (buf->l) { |
| 2176 | int block1 = buf->l; |
| 2177 | int block2 = 0; |
| 2178 | if (buf->r <= buf->w) { |
| 2179 | /* non-contiguous block */ |
| 2180 | block1 = buf->data + buf->size - buf->w; |
| 2181 | block2 = buf->r - buf->data; |
| 2182 | } |
| 2183 | if (block2) |
| 2184 | memcpy(trash, buf->data, block2); |
| 2185 | memmove(buf->data, buf->w, block1); |
| 2186 | if (block2) |
| 2187 | memcpy(buf->data + block1, trash, block2); |
| 2188 | } |
| 2189 | |
| 2190 | /* adjust all known pointers */ |
| 2191 | buf->w = buf->data; |
| 2192 | buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size; |
| 2193 | buf->r += off; if (buf->r >= end) buf->r -= buf->size; |
| 2194 | msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size; |
| 2195 | msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size; |
| 2196 | |
| 2197 | /* adjust relative pointers */ |
| 2198 | msg->som = 0; |
| 2199 | msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size; |
| 2200 | msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size; |
| 2201 | msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size; |
| 2202 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2203 | if (msg->err_pos >= 0) { |
| 2204 | msg->err_pos += off; |
| 2205 | if (msg->err_pos >= buf->size) |
| 2206 | msg->err_pos -= buf->size; |
| 2207 | } |
| 2208 | |
| 2209 | buf->flags &= ~BF_FULL; |
| 2210 | if (buf->l >= buffer_max_len(buf)) |
| 2211 | buf->flags |= BF_FULL; |
| 2212 | } |
| 2213 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2214 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2215 | * processing can continue on next analysers, or zero if it either needs more |
| 2216 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 2217 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers |
| 2218 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2219 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2220 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2221 | 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] | 2222 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2223 | /* |
| 2224 | * We will parse the partial (or complete) lines. |
| 2225 | * We will check the request syntax, and also join multi-line |
| 2226 | * headers. An index of all the lines will be elaborated while |
| 2227 | * parsing. |
| 2228 | * |
| 2229 | * For the parsing, we use a 28 states FSM. |
| 2230 | * |
| 2231 | * Here is the information we currently have : |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 2232 | * req->data + msg->som = beginning of request |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2233 | * req->data + msg->eoh = end of processed headers / start of current one |
| 2234 | * msg->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2235 | * req->lr = first non-visited byte |
| 2236 | * req->r = end of data |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2237 | * |
| 2238 | * At end of parsing, we may perform a capture of the error (if any), and |
| 2239 | * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE). |
| 2240 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2241 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2242 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2243 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2244 | int cur_idx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2245 | int use_close_only; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2246 | struct http_txn *txn = &s->txn; |
| 2247 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2248 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2249 | |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2250 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 2251 | now_ms, __FUNCTION__, |
| 2252 | s, |
| 2253 | req, |
| 2254 | req->rex, req->wex, |
| 2255 | req->flags, |
| 2256 | req->l, |
| 2257 | req->analysers); |
| 2258 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2259 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2260 | s->srv_error = http_return_srv_error; |
| 2261 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2262 | /* There's a protected area at the end of the buffer for rewriting |
| 2263 | * purposes. We don't want to start to parse the request if the |
| 2264 | * protected area is affected, because we may have to move processed |
| 2265 | * data later, which is much more complicated. |
| 2266 | */ |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2267 | if (req->l && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2268 | if ((txn->flags & TX_NOT_FIRST) && |
| 2269 | unlikely((req->flags & BF_FULL) || |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2270 | req->r < req->lr || |
| 2271 | req->r > req->data + req->size - global.tune.maxrewrite)) { |
| 2272 | if (req->send_max) { |
| 2273 | /* some data has still not left the buffer, wake us once that's done */ |
| 2274 | buffer_dont_connect(req); |
| 2275 | req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 2276 | return 0; |
| 2277 | } |
| 2278 | if (req->l <= req->size - global.tune.maxrewrite) |
| 2279 | http_buffer_heavy_realign(req, msg); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2280 | } |
| 2281 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2282 | /* Note that we have the same problem with the response ; we |
| 2283 | * may want to send a redirect, error or anything which requires |
| 2284 | * some spare space. So we'll ensure that we have at least |
| 2285 | * maxrewrite bytes available in the response buffer before |
| 2286 | * processing that one. This will only affect pipelined |
| 2287 | * keep-alive requests. |
| 2288 | */ |
| 2289 | if ((txn->flags & TX_NOT_FIRST) && |
| 2290 | unlikely((s->rep->flags & BF_FULL) || |
| 2291 | s->rep->r < s->rep->lr || |
| 2292 | s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) { |
| 2293 | if (s->rep->send_max) { |
| 2294 | /* don't let a connection request be initiated */ |
| 2295 | buffer_dont_connect(req); |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2296 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2297 | return 0; |
| 2298 | } |
| 2299 | } |
| 2300 | |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2301 | if (likely(req->lr < req->r)) |
| 2302 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2303 | } |
| 2304 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2305 | /* 1: we might have to print this header in debug mode */ |
| 2306 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2307 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2308 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2309 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2310 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2311 | sol = msg->sol; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2312 | eol = sol + msg->sl.rq.l; |
| 2313 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2314 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2315 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2316 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2317 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2318 | while (cur_idx) { |
| 2319 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2320 | debug_hdr("clihdr", s, sol, eol); |
| 2321 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2322 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2323 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2324 | } |
| 2325 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2326 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2327 | /* |
| 2328 | * Now we quickly check if we have found a full valid request. |
| 2329 | * If not so, we check the FD and buffer states before leaving. |
| 2330 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2331 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2332 | * requests are checked first. When waiting for a second request |
| 2333 | * on a keep-alive session, if we encounter and error, close, t/o, |
| 2334 | * we note the error in the session flags but don't set any state. |
| 2335 | * Since the error will be noted there, it will not be counted by |
| 2336 | * process_session() as a frontend error. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2337 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2338 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2339 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2340 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2341 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2342 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2343 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
| 2344 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2345 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2346 | /* 1: Since we are in header mode, if there's no space |
| 2347 | * left for headers, we won't be able to free more |
| 2348 | * later, so the session will never terminate. We |
| 2349 | * must terminate it now. |
| 2350 | */ |
| 2351 | if (unlikely(req->flags & BF_FULL)) { |
| 2352 | /* FIXME: check if URI is set and return Status |
| 2353 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2354 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2355 | goto return_bad_req; |
| 2356 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2357 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2358 | /* 2: have we encountered a read error ? */ |
| 2359 | else if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2360 | if (!(s->flags & SN_ERR_MASK)) |
| 2361 | s->flags |= SN_ERR_CLICL; |
| 2362 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2363 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2364 | goto failed_keep_alive; |
| 2365 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2366 | /* we cannot return any message on error */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2367 | if (msg->err_pos >= 0) |
| 2368 | 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] | 2369 | msg->msg_state = HTTP_MSG_ERROR; |
| 2370 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2371 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2372 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2373 | if (s->listener->counters) |
| 2374 | s->listener->counters->failed_req++; |
| 2375 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2376 | if (!(s->flags & SN_FINST_MASK)) |
| 2377 | s->flags |= SN_FINST_R; |
| 2378 | return 0; |
| 2379 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2380 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2381 | /* 3: has the read timeout expired ? */ |
| 2382 | 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] | 2383 | if (!(s->flags & SN_ERR_MASK)) |
| 2384 | s->flags |= SN_ERR_CLITO; |
| 2385 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2386 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2387 | goto failed_keep_alive; |
| 2388 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2389 | /* read timeout : give up with an error message. */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2390 | if (msg->err_pos >= 0) |
| 2391 | 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] | 2392 | txn->status = 408; |
| 2393 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
| 2394 | msg->msg_state = HTTP_MSG_ERROR; |
| 2395 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2396 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2397 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2398 | if (s->listener->counters) |
| 2399 | s->listener->counters->failed_req++; |
| 2400 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2401 | if (!(s->flags & SN_FINST_MASK)) |
| 2402 | s->flags |= SN_FINST_R; |
| 2403 | return 0; |
| 2404 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2405 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2406 | /* 4: have we encountered a close ? */ |
| 2407 | else if (req->flags & BF_SHUTR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2408 | if (!(s->flags & SN_ERR_MASK)) |
| 2409 | s->flags |= SN_ERR_CLICL; |
| 2410 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2411 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2412 | goto failed_keep_alive; |
| 2413 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2414 | if (msg->err_pos >= 0) |
| 2415 | 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] | 2416 | txn->status = 400; |
| 2417 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 2418 | msg->msg_state = HTTP_MSG_ERROR; |
| 2419 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2420 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2421 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2422 | if (s->listener->counters) |
| 2423 | s->listener->counters->failed_req++; |
| 2424 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2425 | if (!(s->flags & SN_FINST_MASK)) |
| 2426 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2427 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2428 | } |
| 2429 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2430 | buffer_dont_connect(req); |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2431 | req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
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 | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2433 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2434 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2435 | /* If the client starts to talk, let's fall back to |
| 2436 | * request timeout processing. |
| 2437 | */ |
| 2438 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2439 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2440 | } |
| 2441 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2442 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2443 | if (!tick_isset(req->analyse_exp)) { |
| 2444 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2445 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2446 | tick_isset(s->be->timeout.httpka)) |
| 2447 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2448 | else |
| 2449 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2450 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2451 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2452 | /* we're not ready yet */ |
| 2453 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2454 | |
| 2455 | failed_keep_alive: |
| 2456 | /* Here we process low-level errors for keep-alive requests. In |
| 2457 | * short, if the request is not the first one and it experiences |
| 2458 | * a timeout, read error or shutdown, we just silently close so |
| 2459 | * that the client can try again. |
| 2460 | */ |
| 2461 | txn->status = 0; |
| 2462 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 2463 | req->analysers = 0; |
| 2464 | s->logs.logwait = 0; |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2465 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 2466 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2467 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2468 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2469 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2470 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2471 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 2472 | * later. At this point, we have the last CRLF at req->data + msg->eoh. |
| 2473 | * If the request is in HTTP/0.9 form, the rule is still true, and eoh |
| 2474 | * points to the CRLF of the request line. req->lr points to the first |
| 2475 | * byte after the last LF. msg->col and msg->sov point to the first |
| 2476 | * byte of data. msg->eol cannot be trusted because it may have been |
| 2477 | * left uninitialized (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2478 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2479 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2480 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2481 | /* kill the pending keep-alive timeout */ |
| 2482 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2483 | req->analyse_exp = TICK_ETERNITY; |
| 2484 | } |
| 2485 | |
| 2486 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2487 | /* Maybe we found in invalid header name while we were configured not |
| 2488 | * to block on that, so we have to capture it now. |
| 2489 | */ |
| 2490 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2491 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
| 2492 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2493 | /* |
| 2494 | * 1: identify the method |
| 2495 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2496 | txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2497 | |
| 2498 | /* we can make use of server redirect on GET and HEAD */ |
| 2499 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2500 | s->flags |= SN_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2501 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2502 | /* |
| 2503 | * 2: check if the URI matches the monitor_uri. |
| 2504 | * We have to do this for every request which gets in, because |
| 2505 | * the monitor-uri is defined by the frontend. |
| 2506 | */ |
| 2507 | if (unlikely((s->fe->monitor_uri_len != 0) && |
| 2508 | (s->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2509 | !memcmp(msg->sol + msg->sl.rq.u, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2510 | s->fe->monitor_uri, |
| 2511 | s->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2512 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2513 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2514 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2515 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2516 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2517 | s->flags |= SN_MONITOR; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2518 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2519 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2520 | list_for_each_entry(cond, &s->fe->mon_fail_cond, list) { |
| 2521 | 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] | 2522 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2523 | ret = acl_pass(ret); |
| 2524 | if (cond->pol == ACL_COND_UNLESS) |
| 2525 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2526 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2527 | if (ret) { |
| 2528 | /* we fail this request, let's return 503 service unavail */ |
| 2529 | txn->status = 503; |
| 2530 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503)); |
| 2531 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2532 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2533 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2534 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2535 | /* nothing to fail, let's reply normaly */ |
| 2536 | txn->status = 200; |
| 2537 | stream_int_retnclose(req->prod, &http_200_chunk); |
| 2538 | goto return_prx_cond; |
| 2539 | } |
| 2540 | |
| 2541 | /* |
| 2542 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2543 | * Note: we cannot log anymore if the request has been |
| 2544 | * classified as invalid. |
| 2545 | */ |
| 2546 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2547 | /* we have a complete HTTP request that we must log */ |
| 2548 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2549 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2550 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2551 | if (urilen >= REQURI_LEN) |
| 2552 | urilen = REQURI_LEN - 1; |
| 2553 | memcpy(txn->uri, &req->data[msg->som], urilen); |
| 2554 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2555 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2556 | if (!(s->logs.logwait &= ~LW_REQ)) |
| 2557 | s->do_log(s); |
| 2558 | } else { |
| 2559 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2560 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2561 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2562 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2563 | /* 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] | 2564 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) |
| 2565 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2566 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2567 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2568 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2569 | ((msg->sol[msg->sl.rq.v + 5] > '1') || |
| 2570 | ((msg->sol[msg->sl.rq.v + 5] == '1') && |
| 2571 | (msg->sol[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2572 | txn->flags |= TX_REQ_VER_11; |
| 2573 | |
| 2574 | /* "connection" has not been parsed yet */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2575 | 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] | 2576 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2577 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 2578 | * we have what looks like a proxied connection instead of a connection, |
| 2579 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 2580 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 2581 | * happen to do that despite being non-standard :-( |
| 2582 | * We consider that a request not beginning with either '/' or '*' is |
| 2583 | * a proxied connection, which covers both "scheme://location" and |
| 2584 | * CONNECT ip:port. |
| 2585 | */ |
| 2586 | if ((s->fe->options2 & PR_O2_USE_PXHDR) && |
| 2587 | msg->sol[msg->sl.rq.u] != '/' && msg->sol[msg->sl.rq.u] != '*') |
| 2588 | txn->flags |= TX_USE_PX_CONN; |
| 2589 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2590 | /* transfer length unknown*/ |
| 2591 | txn->flags &= ~TX_REQ_XFER_LEN; |
| 2592 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2593 | /* 5: we may need to capture headers */ |
| 2594 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap)) |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2595 | capture_headers(msg->sol, &txn->hdr_idx, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2596 | txn->req.cap, s->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2597 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2598 | /* 6: determine the transfer-length. |
| 2599 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 2600 | * the presence of a message-body in a REQUEST and its transfer length |
| 2601 | * must be determined that way (in order of precedence) : |
| 2602 | * 1. The presence of a message-body in a request is signaled by the |
| 2603 | * inclusion of a Content-Length or Transfer-Encoding header field |
| 2604 | * in the request's header fields. When a request message contains |
| 2605 | * both a message-body of non-zero length and a method that does |
| 2606 | * not define any semantics for that request message-body, then an |
| 2607 | * origin server SHOULD either ignore the message-body or respond |
| 2608 | * with an appropriate error message (e.g., 413). A proxy or |
| 2609 | * gateway, when presented the same request, SHOULD either forward |
| 2610 | * the request inbound with the message- body or ignore the |
| 2611 | * message-body when determining a response. |
| 2612 | * |
| 2613 | * 2. If a Transfer-Encoding header field (Section 9.7) is present |
| 2614 | * and the "chunked" transfer-coding (Section 6.2) is used, the |
| 2615 | * transfer-length is defined by the use of this transfer-coding. |
| 2616 | * If a Transfer-Encoding header field is present and the "chunked" |
| 2617 | * transfer-coding is not present, the transfer-length is defined |
| 2618 | * by the sender closing the connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2619 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2620 | * 3. If a Content-Length header field is present, its decimal value in |
| 2621 | * OCTETs represents both the entity-length and the transfer-length. |
| 2622 | * If a message is received with both a Transfer-Encoding header |
| 2623 | * field and a Content-Length header field, the latter MUST be ignored. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2624 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2625 | * 4. By the server closing the connection. (Closing the connection |
| 2626 | * cannot be used to indicate the end of a request body, since that |
| 2627 | * would leave no possibility for the server to send back a response.) |
| 2628 | * |
| 2629 | * Whenever a transfer-coding is applied to a message-body, the set of |
| 2630 | * transfer-codings MUST include "chunked", unless the message indicates |
| 2631 | * it is terminated by closing the connection. When the "chunked" |
| 2632 | * transfer-coding is used, it MUST be the last transfer-coding applied |
| 2633 | * to the message-body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2634 | */ |
| 2635 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2636 | /* CONNECT sets a tunnel and ignores everything else */ |
| 2637 | if (txn->meth == HTTP_METH_CONNECT) |
| 2638 | goto skip_xfer_len; |
| 2639 | |
| 2640 | use_close_only = 0; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2641 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2642 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 2643 | while ((txn->flags & TX_REQ_VER_11) && |
| 2644 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2645 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
| 2646 | txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN); |
| 2647 | else if (txn->flags & TX_REQ_TE_CHNK) { |
| 2648 | /* bad transfer-encoding (chunked followed by something else) */ |
| 2649 | use_close_only = 1; |
| 2650 | txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN); |
| 2651 | break; |
| 2652 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2653 | } |
| 2654 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2655 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2656 | while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only && |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2657 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) { |
| 2658 | signed long long cl; |
| 2659 | |
| 2660 | if (!ctx.vlen) |
| 2661 | goto return_bad_req; |
| 2662 | |
| 2663 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) |
| 2664 | goto return_bad_req; /* parse failure */ |
| 2665 | |
| 2666 | if (cl < 0) |
| 2667 | goto return_bad_req; |
| 2668 | |
| 2669 | if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl)) |
| 2670 | goto return_bad_req; /* already specified, was different */ |
| 2671 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2672 | txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2673 | msg->hdr_content_len = cl; |
| 2674 | } |
| 2675 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2676 | /* bodyless requests have a known length */ |
| 2677 | if (!use_close_only) |
| 2678 | txn->flags |= TX_REQ_XFER_LEN; |
| 2679 | |
| 2680 | skip_xfer_len: |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2681 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2682 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2683 | req->analyse_exp = TICK_ETERNITY; |
| 2684 | return 1; |
| 2685 | |
| 2686 | return_bad_req: |
| 2687 | /* We centralize bad requests processing here */ |
| 2688 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2689 | /* we detected a parsing error. We want to archive this request |
| 2690 | * in the dedicated proxy area for later troubleshooting. |
| 2691 | */ |
| 2692 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
| 2693 | } |
| 2694 | |
| 2695 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2696 | txn->status = 400; |
| 2697 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2698 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2699 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2700 | if (s->listener->counters) |
| 2701 | s->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2702 | |
| 2703 | return_prx_cond: |
| 2704 | if (!(s->flags & SN_ERR_MASK)) |
| 2705 | s->flags |= SN_ERR_PRXCOND; |
| 2706 | if (!(s->flags & SN_FINST_MASK)) |
| 2707 | s->flags |= SN_FINST_R; |
| 2708 | |
| 2709 | req->analysers = 0; |
| 2710 | req->analyse_exp = TICK_ETERNITY; |
| 2711 | return 0; |
| 2712 | } |
| 2713 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2714 | /* This stream analyser runs all HTTP request processing which is common to |
| 2715 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 2716 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2717 | * 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] | 2718 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 2719 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2720 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2721 | 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] | 2722 | { |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2723 | struct http_txn *txn = &s->txn; |
| 2724 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2725 | struct acl_cond *cond; |
| 2726 | struct redirect_rule *rule; |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 2727 | struct wordlist *wl; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2728 | int del_ka, del_cl; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2729 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2730 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2731 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2732 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2733 | return 0; |
| 2734 | } |
| 2735 | |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2736 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2737 | req->analyse_exp = TICK_ETERNITY; |
| 2738 | |
| 2739 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 2740 | now_ms, __FUNCTION__, |
| 2741 | s, |
| 2742 | req, |
| 2743 | req->rex, req->wex, |
| 2744 | req->flags, |
| 2745 | req->l, |
| 2746 | req->analysers); |
| 2747 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2748 | /* first check whether we have some ACLs set to block this request */ |
| 2749 | list_for_each_entry(cond, &px->block_cond, list) { |
| 2750 | int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ); |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2751 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2752 | ret = acl_pass(ret); |
| 2753 | if (cond->pol == ACL_COND_UNLESS) |
| 2754 | ret = !ret; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2755 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2756 | if (ret) { |
| 2757 | txn->status = 403; |
| 2758 | /* let's log the request time */ |
| 2759 | s->logs.tv_request = now; |
| 2760 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
| 2761 | goto return_prx_cond; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2762 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2763 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2764 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2765 | /* try headers filters */ |
| 2766 | if (px->req_exp != NULL) { |
| 2767 | if (apply_filters_to_request(s, req, px->req_exp) < 0) |
| 2768 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2769 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2770 | /* has the request been denied ? */ |
| 2771 | if (txn->flags & TX_CLDENY) { |
| 2772 | /* no need to go further */ |
| 2773 | txn->status = 403; |
| 2774 | /* let's log the request time */ |
| 2775 | s->logs.tv_request = now; |
| 2776 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
| 2777 | goto return_prx_cond; |
| 2778 | } |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2779 | |
| 2780 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 2781 | * which may be the same as the connect timeout if unspecified. |
| 2782 | * If unset, then set it to zero because we really want it to |
| 2783 | * eventually expire. We build the tarpit as an analyser. |
| 2784 | */ |
| 2785 | if (txn->flags & TX_CLTARPIT) { |
| 2786 | buffer_erase(s->req); |
| 2787 | /* wipe the request out so that we can drop the connection early |
| 2788 | * if the client closes first. |
| 2789 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2790 | buffer_dont_connect(req); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2791 | req->analysers = 0; /* remove switching rules etc... */ |
| 2792 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 2793 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 2794 | if (!req->analyse_exp) |
| 2795 | req->analyse_exp = tick_add(now_ms, 0); |
| 2796 | return 1; |
| 2797 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2798 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2799 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2800 | /* Until set to anything else, the connection mode is set as TUNNEL. It will |
| 2801 | * only change if both the request and the config reference something else. |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2802 | * Option httpclose by itself does not set a mode, it remains a tunnel mode |
| 2803 | * 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] | 2804 | * affect it (eg: server-close/keep-alive + httpclose = close). Note that we |
| 2805 | * avoid to redo the same work if FE and BE have the same settings (common). |
| 2806 | * The method consists in checking if options changed between the two calls |
| 2807 | * (implying that either one is non-null, or one of them is non-null and we |
| 2808 | * are there for the first time. |
Willy Tarreau | 4273664 | 2009-10-18 21:04:35 +0200 | [diff] [blame] | 2809 | */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2810 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2811 | del_cl = del_ka = 0; |
| 2812 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2813 | if ((txn->meth != HTTP_METH_CONNECT) && |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2814 | ((!(txn->flags & TX_HDR_CONN_PRS) && |
| 2815 | (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) || |
| 2816 | ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) != |
| 2817 | (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] | 2818 | int tmp = TX_CON_WANT_TUN; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2819 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2820 | if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE) |
| 2821 | tmp = TX_CON_WANT_KAL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2822 | if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO) |
| 2823 | tmp = TX_CON_WANT_SCL; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2824 | if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2825 | tmp = TX_CON_WANT_CLO; |
| 2826 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2827 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 2828 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2829 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2830 | if (!(txn->flags & TX_HDR_CONN_PRS)) { |
| 2831 | /* parse the Connection header and possibly clean it */ |
| 2832 | int to_del = 0; |
| 2833 | if ((txn->flags & TX_REQ_VER_11) || |
| 2834 | (txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL) |
| 2835 | to_del |= 2; /* remove "keep-alive" */ |
| 2836 | if (!(txn->flags & TX_REQ_VER_11)) |
| 2837 | to_del |= 1; /* remove "close" */ |
| 2838 | http_parse_connection_header(txn, msg, req, to_del); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2839 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2840 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2841 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 2842 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 2843 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 2844 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
| 2845 | (txn->flags & (TX_REQ_VER_11|TX_HDR_CONN_KAL)) == 0 || /* no "connection: k-a" in 1.0 */ |
| 2846 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE) || /* httpclose + any = forceclose */ |
Willy Tarreau | c3e8b25 | 2010-01-28 15:01:20 +0100 | [diff] [blame^] | 2847 | !(txn->flags & TX_REQ_XFER_LEN) || /* no length known => close */ |
| 2848 | s->fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2849 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 2850 | } |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 2851 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2852 | /* add request headers from the rule sets in the same order */ |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 2853 | list_for_each_entry(wl, &px->req_add, list) { |
| 2854 | 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] | 2855 | goto return_bad_req; |
| 2856 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2857 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2858 | /* check if stats URI was requested, and if an auth is needed */ |
| 2859 | if (px->uri_auth != NULL && |
| 2860 | (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD)) { |
| 2861 | /* we have to check the URI and auth for this request. |
| 2862 | * FIXME!!! that one is rather dangerous, we want to |
| 2863 | * make it follow standard rules (eg: clear req->analysers). |
| 2864 | */ |
| 2865 | if (stats_check_uri_auth(s, px)) { |
| 2866 | req->analysers = 0; |
| 2867 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2868 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2869 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2870 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2871 | /* check whether we have some ACLs set to redirect this request */ |
| 2872 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 2873 | int ret = ACL_PAT_PASS; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2874 | |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 2875 | if (rule->cond) { |
| 2876 | ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ); |
| 2877 | ret = acl_pass(ret); |
| 2878 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 2879 | ret = !ret; |
| 2880 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2881 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2882 | if (ret) { |
Willy Tarreau | 3bb9c23 | 2010-01-03 12:24:37 +0100 | [diff] [blame] | 2883 | struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 }; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2884 | const char *msg_fmt; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2885 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2886 | /* build redirect message */ |
| 2887 | switch(rule->code) { |
| 2888 | case 303: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2889 | msg_fmt = HTTP_303; |
| 2890 | break; |
| 2891 | case 301: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2892 | msg_fmt = HTTP_301; |
| 2893 | break; |
| 2894 | case 302: |
| 2895 | default: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2896 | msg_fmt = HTTP_302; |
| 2897 | break; |
| 2898 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2899 | |
Willy Tarreau | 3bb9c23 | 2010-01-03 12:24:37 +0100 | [diff] [blame] | 2900 | if (unlikely(!chunk_strcpy(&rdr, msg_fmt))) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2901 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2902 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2903 | switch(rule->type) { |
| 2904 | case REDIRECT_TYPE_PREFIX: { |
| 2905 | const char *path; |
| 2906 | int pathlen; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2907 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2908 | path = http_get_path(txn); |
| 2909 | /* build message using path */ |
| 2910 | if (path) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2911 | 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] | 2912 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 2913 | int qs = 0; |
| 2914 | while (qs < pathlen) { |
| 2915 | if (path[qs] == '?') { |
| 2916 | pathlen = qs; |
| 2917 | break; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2918 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2919 | qs++; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2920 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2921 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2922 | } else { |
| 2923 | path = "/"; |
| 2924 | pathlen = 1; |
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 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 2927 | if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2928 | goto return_bad_req; |
| 2929 | |
| 2930 | /* add prefix. Note that if prefix == "/", we don't want to |
| 2931 | * add anything, otherwise it makes it hard for the user to |
| 2932 | * configure a self-redirection. |
| 2933 | */ |
| 2934 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2935 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 2936 | rdr.len += rule->rdr_len; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2937 | } |
| 2938 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2939 | /* add path */ |
| 2940 | memcpy(rdr.str + rdr.len, path, pathlen); |
| 2941 | rdr.len += pathlen; |
Willy Tarreau | 81e3b4f | 2010-01-10 00:42:19 +0100 | [diff] [blame] | 2942 | |
| 2943 | /* append a slash at the end of the location is needed and missing */ |
| 2944 | if (rdr.len && rdr.str[rdr.len - 1] != '/' && |
| 2945 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 2946 | if (rdr.len > rdr.size - 5) |
| 2947 | goto return_bad_req; |
| 2948 | rdr.str[rdr.len] = '/'; |
| 2949 | rdr.len++; |
| 2950 | } |
| 2951 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2952 | break; |
| 2953 | } |
| 2954 | case REDIRECT_TYPE_LOCATION: |
| 2955 | default: |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 2956 | if (rdr.len + rule->rdr_len > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2957 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2958 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2959 | /* add location */ |
| 2960 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 2961 | rdr.len += rule->rdr_len; |
| 2962 | break; |
| 2963 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2964 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2965 | if (rule->cookie_len) { |
| 2966 | memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14); |
| 2967 | rdr.len += 14; |
| 2968 | memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len); |
| 2969 | rdr.len += rule->cookie_len; |
| 2970 | memcpy(rdr.str + rdr.len, "\r\n", 2); |
| 2971 | rdr.len += 2; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2972 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 2973 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 2974 | /* add end of headers and the keep-alive/close status. |
| 2975 | * We may choose to set keep-alive if the Location begins |
| 2976 | * with a slash, because the client will come back to the |
| 2977 | * same server. |
| 2978 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2979 | txn->status = rule->code; |
| 2980 | /* let's log the request time */ |
| 2981 | s->logs.tv_request = now; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 2982 | |
| 2983 | if (rule->rdr_len >= 1 && *rule->rdr_str == '/' && |
| 2984 | (txn->flags & TX_REQ_XFER_LEN) && |
| 2985 | !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len && |
| 2986 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 2987 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 2988 | /* keep-alive possible */ |
Willy Tarreau | 7566145 | 2010-01-10 10:35:01 +0100 | [diff] [blame] | 2989 | if (!(txn->flags & TX_REQ_VER_11)) { |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2990 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2991 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30); |
| 2992 | rdr.len += 30; |
| 2993 | } else { |
| 2994 | memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24); |
| 2995 | rdr.len += 24; |
| 2996 | } |
Willy Tarreau | 7566145 | 2010-01-10 10:35:01 +0100 | [diff] [blame] | 2997 | } |
| 2998 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 2999 | rdr.len += 4; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3000 | buffer_write(req->prod->ob, rdr.str, rdr.len); |
| 3001 | /* "eat" the request */ |
| 3002 | buffer_ignore(req, msg->sov - msg->som); |
| 3003 | msg->som = msg->sov; |
| 3004 | req->analysers = AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 9300fb2 | 2010-01-05 00:58:24 +0100 | [diff] [blame] | 3005 | s->rep->analysers = AN_RES_HTTP_XFER_BODY; |
| 3006 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3007 | txn->rsp.msg_state = HTTP_MSG_DONE; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3008 | break; |
| 3009 | } else { |
| 3010 | /* keep-alive not possible */ |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3011 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3012 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 3013 | rdr.len += 29; |
| 3014 | } else { |
| 3015 | memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23); |
| 3016 | rdr.len += 23; |
| 3017 | } |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 3018 | stream_int_retnclose(req->prod, &rdr); |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3019 | goto return_prx_cond; |
| 3020 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3021 | } |
| 3022 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3023 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3024 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 3025 | * If this happens, then the data will not come immediately, so we must |
| 3026 | * send all what we have without waiting. Note that due to the small gain |
| 3027 | * in waiting for the body of the request, it's easier to simply put the |
| 3028 | * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove |
| 3029 | * itself once used. |
| 3030 | */ |
| 3031 | req->flags |= BF_SEND_DONTWAIT; |
| 3032 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3033 | /* that's OK for us now, let's move on to next analysers */ |
| 3034 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 3035 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3036 | return_bad_req: |
| 3037 | /* We centralize bad requests processing here */ |
| 3038 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3039 | /* we detected a parsing error. We want to archive this request |
| 3040 | * in the dedicated proxy area for later troubleshooting. |
| 3041 | */ |
| 3042 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
| 3043 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3044 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3045 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3046 | txn->status = 400; |
| 3047 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3048 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3049 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3050 | if (s->listener->counters) |
| 3051 | s->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3052 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3053 | return_prx_cond: |
| 3054 | if (!(s->flags & SN_ERR_MASK)) |
| 3055 | s->flags |= SN_ERR_PRXCOND; |
| 3056 | if (!(s->flags & SN_FINST_MASK)) |
| 3057 | s->flags |= SN_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 3058 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3059 | req->analysers = 0; |
| 3060 | req->analyse_exp = TICK_ETERNITY; |
| 3061 | return 0; |
| 3062 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3063 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3064 | /* This function performs all the processing enabled for the current request. |
| 3065 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 3066 | * needs more data, encounters an error, or wants to immediately abort the |
| 3067 | * request. It relies on buffers flags, and updates s->req->analysers. |
| 3068 | */ |
| 3069 | int http_process_request(struct session *s, struct buffer *req, int an_bit) |
| 3070 | { |
| 3071 | struct http_txn *txn = &s->txn; |
| 3072 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3073 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3074 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3075 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3076 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3077 | return 0; |
| 3078 | } |
| 3079 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3080 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 3081 | now_ms, __FUNCTION__, |
| 3082 | s, |
| 3083 | req, |
| 3084 | req->rex, req->wex, |
| 3085 | req->flags, |
| 3086 | req->l, |
| 3087 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3088 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3089 | /* |
| 3090 | * Right now, we know that we have processed the entire headers |
| 3091 | * and that unwanted requests have been filtered out. We can do |
| 3092 | * whatever we want with the remaining request. Also, now we |
| 3093 | * may have separate values for ->fe, ->be. |
| 3094 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3095 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3096 | /* |
| 3097 | * If HTTP PROXY is set we simply get remote server address |
| 3098 | * parsing incoming request. |
| 3099 | */ |
| 3100 | 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] | 3101 | 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] | 3102 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3103 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3104 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3105 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3106 | * Note that doing so might move headers in the request, but |
| 3107 | * the fields will stay coherent and the URI will not move. |
| 3108 | * This should only be performed in the backend. |
| 3109 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 3110 | 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] | 3111 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 3112 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 3113 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3114 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3115 | * 8: the appsession cookie was looked up very early in 1.2, |
| 3116 | * so let's do the same now. |
| 3117 | */ |
| 3118 | |
| 3119 | /* It needs to look into the URI */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 3120 | if ((txn->sessid == NULL) && s->be->appsession_name) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 3121 | 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] | 3122 | } |
| 3123 | |
| 3124 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3125 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 3126 | * asks for it. |
| 3127 | */ |
| 3128 | if ((s->fe->options | s->be->options) & PR_O_FWDFOR) { |
| 3129 | if (s->cli_addr.ss_family == AF_INET) { |
| 3130 | /* Add an X-Forwarded-For header unless the source IP is |
| 3131 | * in the 'except' network range. |
| 3132 | */ |
| 3133 | if ((!s->fe->except_mask.s_addr || |
| 3134 | (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr) |
| 3135 | != s->fe->except_net.s_addr) && |
| 3136 | (!s->be->except_mask.s_addr || |
| 3137 | (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr) |
| 3138 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3139 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3140 | unsigned char *pn; |
| 3141 | pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3142 | |
| 3143 | /* Note: we rely on the backend to get the header name to be used for |
| 3144 | * x-forwarded-for, because the header is really meant for the backends. |
| 3145 | * However, if the backend did not specify any option, we have to rely |
| 3146 | * on the frontend's header name. |
| 3147 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3148 | if (s->be->fwdfor_hdr_len) { |
| 3149 | len = s->be->fwdfor_hdr_len; |
| 3150 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3151 | } else { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3152 | len = s->fe->fwdfor_hdr_len; |
| 3153 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3154 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3155 | 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] | 3156 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3157 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3158 | &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3159 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3160 | } |
| 3161 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3162 | else if (s->cli_addr.ss_family == AF_INET6) { |
| 3163 | /* FIXME: for the sake of completeness, we should also support |
| 3164 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3165 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3166 | int len; |
| 3167 | char pn[INET6_ADDRSTRLEN]; |
| 3168 | inet_ntop(AF_INET6, |
| 3169 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 3170 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3171 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3172 | /* Note: we rely on the backend to get the header name to be used for |
| 3173 | * x-forwarded-for, because the header is really meant for the backends. |
| 3174 | * However, if the backend did not specify any option, we have to rely |
| 3175 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3176 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3177 | if (s->be->fwdfor_hdr_len) { |
| 3178 | len = s->be->fwdfor_hdr_len; |
| 3179 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
| 3180 | } else { |
| 3181 | len = s->fe->fwdfor_hdr_len; |
| 3182 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3183 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3184 | len += sprintf(trash + len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3185 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3186 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3187 | &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3188 | goto return_bad_req; |
| 3189 | } |
| 3190 | } |
| 3191 | |
| 3192 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3193 | * 10: add X-Original-To if either the frontend or the backend |
| 3194 | * asks for it. |
| 3195 | */ |
| 3196 | if ((s->fe->options | s->be->options) & PR_O_ORGTO) { |
| 3197 | |
| 3198 | /* FIXME: don't know if IPv6 can handle that case too. */ |
| 3199 | if (s->cli_addr.ss_family == AF_INET) { |
| 3200 | /* Add an X-Original-To header unless the destination IP is |
| 3201 | * in the 'except' network range. |
| 3202 | */ |
| 3203 | if (!(s->flags & SN_FRT_ADDR_SET)) |
| 3204 | get_frt_addr(s); |
| 3205 | |
| 3206 | if ((!s->fe->except_mask_to.s_addr || |
| 3207 | (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr) |
| 3208 | != s->fe->except_to.s_addr) && |
| 3209 | (!s->be->except_mask_to.s_addr || |
| 3210 | (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr) |
| 3211 | != s->be->except_to.s_addr)) { |
| 3212 | int len; |
| 3213 | unsigned char *pn; |
| 3214 | pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr; |
| 3215 | |
| 3216 | /* Note: we rely on the backend to get the header name to be used for |
| 3217 | * x-original-to, because the header is really meant for the backends. |
| 3218 | * However, if the backend did not specify any option, we have to rely |
| 3219 | * on the frontend's header name. |
| 3220 | */ |
| 3221 | if (s->be->orgto_hdr_len) { |
| 3222 | len = s->be->orgto_hdr_len; |
| 3223 | memcpy(trash, s->be->orgto_hdr_name, len); |
| 3224 | } else { |
| 3225 | len = s->fe->orgto_hdr_len; |
| 3226 | memcpy(trash, s->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3227 | } |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3228 | len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
| 3229 | |
| 3230 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3231 | &txn->hdr_idx, trash, len) < 0)) |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3232 | goto return_bad_req; |
| 3233 | } |
| 3234 | } |
| 3235 | } |
| 3236 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3237 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */ |
| 3238 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
| 3239 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) { |
| 3240 | unsigned int want_flags = 0; |
| 3241 | |
| 3242 | if (txn->flags & TX_REQ_VER_11) { |
| 3243 | if ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
| 3244 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) |
| 3245 | want_flags |= TX_CON_CLO_SET; |
| 3246 | } else { |
| 3247 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) |
| 3248 | want_flags |= TX_CON_KAL_SET; |
| 3249 | } |
| 3250 | |
| 3251 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 3252 | http_change_connection_header(txn, msg, req, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3253 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3254 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3255 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3256 | /* If we have no server assigned yet and we're balancing on url_param |
| 3257 | * with a POST request, we may be interested in checking the body for |
| 3258 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3259 | */ |
| 3260 | if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 3261 | s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3262 | s->be->url_param_post_limit != 0 && |
| 3263 | (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 3264 | 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] | 3265 | buffer_dont_connect(req); |
| 3266 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3267 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3268 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3269 | if (txn->flags & TX_REQ_XFER_LEN) |
| 3270 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 3271 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3272 | /************************************************************* |
| 3273 | * OK, that's finished for the headers. We have done what we * |
| 3274 | * could. Let's switch to the DATA state. * |
| 3275 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3276 | req->analyse_exp = TICK_ETERNITY; |
| 3277 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3278 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3279 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3280 | /* OK let's go on with the BODY now */ |
| 3281 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3282 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3283 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3284 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3285 | /* we detected a parsing error. We want to archive this request |
| 3286 | * in the dedicated proxy area for later troubleshooting. |
| 3287 | */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3288 | 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] | 3289 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3290 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3291 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3292 | txn->status = 400; |
| 3293 | req->analysers = 0; |
| 3294 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3295 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3296 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3297 | if (s->listener->counters) |
| 3298 | s->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3299 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3300 | if (!(s->flags & SN_ERR_MASK)) |
| 3301 | s->flags |= SN_ERR_PRXCOND; |
| 3302 | if (!(s->flags & SN_FINST_MASK)) |
| 3303 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3304 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3305 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3306 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3307 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 3308 | * returns zero, at the beginning because it prevents any other processing |
| 3309 | * from occurring, and at the end because it terminates the request. |
| 3310 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3311 | 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] | 3312 | { |
| 3313 | struct http_txn *txn = &s->txn; |
| 3314 | |
| 3315 | /* This connection is being tarpitted. The CLIENT side has |
| 3316 | * already set the connect expiration date to the right |
| 3317 | * timeout. We just have to check that the client is still |
| 3318 | * there and that the timeout has not expired. |
| 3319 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3320 | buffer_dont_connect(req); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3321 | if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 && |
| 3322 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 3323 | return 0; |
| 3324 | |
| 3325 | /* We will set the queue timer to the time spent, just for |
| 3326 | * logging purposes. We fake a 500 server error, so that the |
| 3327 | * attacker will not suspect his connection has been tarpitted. |
| 3328 | * It will not cause trouble to the logs because we can exclude |
| 3329 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 3330 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3331 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3332 | |
| 3333 | txn->status = 500; |
| 3334 | if (req->flags != BF_READ_ERROR) |
| 3335 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500)); |
| 3336 | |
| 3337 | req->analysers = 0; |
| 3338 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3339 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3340 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3341 | if (s->listener->counters) |
| 3342 | s->listener->counters->failed_req++; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3343 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3344 | if (!(s->flags & SN_ERR_MASK)) |
| 3345 | s->flags |= SN_ERR_PRXCOND; |
| 3346 | if (!(s->flags & SN_FINST_MASK)) |
| 3347 | s->flags |= SN_FINST_T; |
| 3348 | return 0; |
| 3349 | } |
| 3350 | |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3351 | /* This function is an analyser which processes the HTTP request body. It looks |
| 3352 | * for parameters to be used for the load balancing algorithm (url_param). It |
| 3353 | * must only be called after the standard HTTP request processing has occurred, |
| 3354 | * because it expects the request to be parsed. It returns zero if it needs to |
| 3355 | * read more data, or 1 once it has completed its analysis. |
| 3356 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3357 | 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] | 3358 | { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3359 | struct http_txn *txn = &s->txn; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3360 | struct http_msg *msg = &s->txn.req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3361 | long long limit = s->be->url_param_post_limit; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3362 | |
| 3363 | /* We have to parse the HTTP request body to find any required data. |
| 3364 | * "balance url_param check_post" should have been the only way to get |
| 3365 | * into this. We were brought here after HTTP header analysis, so all |
| 3366 | * related structures are ready. |
| 3367 | */ |
| 3368 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3369 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 3370 | goto missing_data; |
| 3371 | |
| 3372 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 3373 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 3374 | * send an HTTP/1.1 100 Continue intermediate response. |
| 3375 | */ |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 3376 | if (txn->flags & TX_REQ_VER_11) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3377 | struct hdr_ctx ctx; |
| 3378 | ctx.idx = 0; |
| 3379 | /* Expect is allowed in 1.1, look for it */ |
| 3380 | if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) && |
| 3381 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
| 3382 | buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len); |
| 3383 | } |
| 3384 | } |
| 3385 | msg->msg_state = HTTP_MSG_100_SENT; |
| 3386 | } |
| 3387 | |
| 3388 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3389 | /* we have msg->col and msg->sov which both point to the first |
| 3390 | * byte of message body. msg->som still points to the beginning |
| 3391 | * of the message. We must save the body in req->lr because it |
| 3392 | * survives buffer re-alignments. |
| 3393 | */ |
| 3394 | req->lr = req->data + msg->sov; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3395 | if (txn->flags & TX_REQ_TE_CHNK) |
| 3396 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 3397 | else |
| 3398 | msg->msg_state = HTTP_MSG_DATA; |
| 3399 | } |
| 3400 | |
| 3401 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3402 | /* read the chunk size and assign it to ->hdr_content_len, then |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3403 | * set ->sov and ->lr to point to the body and switch to DATA or |
| 3404 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3405 | */ |
| 3406 | int ret = http_parse_chunk_size(req, msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3407 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3408 | if (!ret) |
| 3409 | goto missing_data; |
| 3410 | else if (ret < 0) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3411 | goto return_bad_req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3412 | } |
| 3413 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3414 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3415 | * We have the first non-header byte in msg->col, which is either the |
| 3416 | * beginning of the chunk size or of the data. The first data byte is in |
| 3417 | * msg->sov, which is equal to msg->col when not using transfer-encoding. |
| 3418 | * 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] | 3419 | */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3420 | |
| 3421 | if (msg->hdr_content_len < limit) |
| 3422 | limit = msg->hdr_content_len; |
| 3423 | |
Willy Tarreau | 7c96f67 | 2009-12-27 22:47:25 +0100 | [diff] [blame] | 3424 | 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] | 3425 | goto http_end; |
| 3426 | |
| 3427 | missing_data: |
| 3428 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3429 | if (req->flags & BF_FULL) |
| 3430 | goto return_bad_req; |
| 3431 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3432 | if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) { |
| 3433 | txn->status = 408; |
| 3434 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
| 3435 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3436 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3437 | |
| 3438 | /* we get here if we need to wait for more data */ |
| 3439 | if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3440 | /* Not enough data. We'll re-use the http-request |
| 3441 | * timeout here. Ideally, we should set the timeout |
| 3442 | * relative to the accept() date. We just set the |
| 3443 | * request timeout once at the beginning of the |
| 3444 | * request. |
| 3445 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3446 | buffer_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3447 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 3448 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3449 | return 0; |
| 3450 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3451 | |
| 3452 | http_end: |
| 3453 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 3454 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 3455 | req->analysers &= ~an_bit; |
| 3456 | req->analyse_exp = TICK_ETERNITY; |
| 3457 | return 1; |
| 3458 | |
| 3459 | return_bad_req: /* let's centralize all bad requests */ |
| 3460 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3461 | txn->status = 400; |
| 3462 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 3463 | |
| 3464 | return_err_msg: |
| 3465 | req->analysers = 0; |
| 3466 | s->fe->counters.failed_req++; |
| 3467 | if (s->listener->counters) |
| 3468 | s->listener->counters->failed_req++; |
| 3469 | |
| 3470 | if (!(s->flags & SN_ERR_MASK)) |
| 3471 | s->flags |= SN_ERR_PRXCOND; |
| 3472 | if (!(s->flags & SN_FINST_MASK)) |
| 3473 | s->flags |= SN_FINST_R; |
| 3474 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3475 | } |
| 3476 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3477 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 3478 | * right now but it works. |
| 3479 | */ |
| 3480 | void http_end_txn_clean_session(struct session *s) |
| 3481 | { |
| 3482 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 3483 | * server's connections. We need a safer way to reinitialize buffer |
| 3484 | * flags. We also need a more accurate method for computing per-request |
| 3485 | * data. |
| 3486 | */ |
| 3487 | http_silent_debug(__LINE__, s); |
| 3488 | |
| 3489 | s->req->cons->flags |= SI_FL_NOLINGER; |
| 3490 | s->req->cons->shutr(s->req->cons); |
| 3491 | s->req->cons->shutw(s->req->cons); |
| 3492 | |
| 3493 | http_silent_debug(__LINE__, s); |
| 3494 | |
| 3495 | if (s->flags & SN_BE_ASSIGNED) |
| 3496 | s->be->beconn--; |
| 3497 | |
| 3498 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3499 | session_process_counters(s); |
| 3500 | |
| 3501 | if (s->txn.status) { |
| 3502 | int n; |
| 3503 | |
| 3504 | n = s->txn.status / 100; |
| 3505 | if (n < 1 || n > 5) |
| 3506 | n = 0; |
| 3507 | |
| 3508 | if (s->fe->mode == PR_MODE_HTTP) |
| 3509 | s->fe->counters.p.http.rsp[n]++; |
| 3510 | |
| 3511 | if ((s->flags & SN_BE_ASSIGNED) && (s->fe != s->be) && |
| 3512 | (s->be->mode == PR_MODE_HTTP)) |
| 3513 | s->be->counters.p.http.rsp[n]++; |
| 3514 | } |
| 3515 | |
| 3516 | /* don't count other requests' data */ |
| 3517 | s->logs.bytes_in -= s->req->l - s->req->send_max; |
| 3518 | s->logs.bytes_out -= s->rep->l - s->rep->send_max; |
| 3519 | |
| 3520 | /* let's do a final log if we need it */ |
| 3521 | if (s->logs.logwait && |
| 3522 | !(s->flags & SN_MONITOR) && |
| 3523 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 3524 | s->do_log(s); |
| 3525 | } |
| 3526 | |
| 3527 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 3528 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 3529 | tv_zero(&s->logs.tv_request); |
| 3530 | s->logs.t_queue = -1; |
| 3531 | s->logs.t_connect = -1; |
| 3532 | s->logs.t_data = -1; |
| 3533 | s->logs.t_close = 0; |
| 3534 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 3535 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 3536 | |
| 3537 | s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max; |
| 3538 | s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max; |
| 3539 | |
| 3540 | if (s->pend_pos) |
| 3541 | pendconn_free(s->pend_pos); |
| 3542 | |
| 3543 | if (s->srv) { |
| 3544 | if (s->flags & SN_CURR_SESS) { |
| 3545 | s->flags &= ~SN_CURR_SESS; |
| 3546 | s->srv->cur_sess--; |
| 3547 | } |
| 3548 | if (may_dequeue_tasks(s->srv, s->be)) |
| 3549 | process_srv_queue(s->srv); |
| 3550 | } |
| 3551 | |
| 3552 | if (unlikely(s->srv_conn)) |
| 3553 | sess_change_server(s, NULL); |
| 3554 | s->srv = NULL; |
| 3555 | |
| 3556 | s->req->cons->state = s->req->cons->prev_state = SI_ST_INI; |
| 3557 | s->req->cons->fd = -1; /* just to help with debugging */ |
| 3558 | s->req->cons->err_type = SI_ET_NONE; |
| 3559 | s->req->cons->err_loc = NULL; |
| 3560 | s->req->cons->exp = TICK_ETERNITY; |
| 3561 | s->req->cons->flags = SI_FL_NONE; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3562 | s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST); |
| 3563 | 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] | 3564 | 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] | 3565 | s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE); |
| 3566 | s->txn.meth = 0; |
| 3567 | http_reset_txn(s); |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 3568 | s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3569 | if (s->be->options2 & PR_O2_INDEPSTR) |
| 3570 | s->req->cons->flags |= SI_FL_INDEP_STR; |
| 3571 | |
| 3572 | /* if the request buffer is not empty, it means we're |
| 3573 | * about to process another request, so send pending |
| 3574 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 3575 | * Just don't do this if the buffer is close to be full, |
| 3576 | * because the request will wait for it to flush a little |
| 3577 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3578 | */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 3579 | if (s->req->l > s->req->send_max) { |
| 3580 | if (s->rep->send_max && |
| 3581 | !(s->rep->flags & BF_FULL) && |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 3582 | s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite) |
| 3583 | s->rep->flags |= BF_EXPECT_MORE; |
| 3584 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3585 | |
| 3586 | /* we're removing the analysers, we MUST re-enable events detection */ |
| 3587 | buffer_auto_read(s->req); |
| 3588 | buffer_auto_close(s->req); |
| 3589 | buffer_auto_read(s->rep); |
| 3590 | buffer_auto_close(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3591 | |
| 3592 | /* make ->lr point to the first non-forwarded byte */ |
| 3593 | s->req->lr = s->req->w + s->req->send_max; |
| 3594 | if (s->req->lr >= s->req->data + s->req->size) |
| 3595 | s->req->lr -= s->req->size; |
| 3596 | s->rep->lr = s->rep->w + s->rep->send_max; |
| 3597 | if (s->rep->lr >= s->rep->data + s->rep->size) |
| 3598 | s->rep->lr -= s->req->size; |
| 3599 | |
| 3600 | s->req->analysers |= s->fe->fe_req_ana; |
| 3601 | s->rep->analysers = 0; |
| 3602 | |
| 3603 | http_silent_debug(__LINE__, s); |
| 3604 | } |
| 3605 | |
| 3606 | |
| 3607 | /* This function updates the request state machine according to the response |
| 3608 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 3609 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 3610 | * it is only used to find when a request/response couple is complete. Both |
| 3611 | * this function and its equivalent should loop until both return zero. It |
| 3612 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 3613 | */ |
| 3614 | int http_sync_req_state(struct session *s) |
| 3615 | { |
| 3616 | struct buffer *buf = s->req; |
| 3617 | struct http_txn *txn = &s->txn; |
| 3618 | unsigned int old_flags = buf->flags; |
| 3619 | unsigned int old_state = txn->req.msg_state; |
| 3620 | |
| 3621 | http_silent_debug(__LINE__, s); |
| 3622 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 3623 | return 0; |
| 3624 | |
| 3625 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3626 | /* No need to read anymore, the request was completely parsed. |
| 3627 | * We can shut the read side unless we want to abort_on_close. |
| 3628 | */ |
| 3629 | if (buf->cons->state == SI_ST_EST || !(s->be->options & PR_O_ABRT_CLOSE)) |
| 3630 | buffer_dont_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3631 | |
| 3632 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 3633 | goto wait_other_side; |
| 3634 | |
| 3635 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 3636 | /* The server has not finished to respond, so we |
| 3637 | * don't want to move in order not to upset it. |
| 3638 | */ |
| 3639 | goto wait_other_side; |
| 3640 | } |
| 3641 | |
| 3642 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 3643 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3644 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3645 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 3646 | goto wait_other_side; |
| 3647 | } |
| 3648 | |
| 3649 | /* When we get here, it means that both the request and the |
| 3650 | * response have finished receiving. Depending on the connection |
| 3651 | * mode, we'll have to wait for the last bytes to leave in either |
| 3652 | * direction, and sometimes for a close to be effective. |
| 3653 | */ |
| 3654 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3655 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 3656 | /* Server-close mode : queue a connection close to the server */ |
| 3657 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3658 | buffer_shutw_now(buf); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3659 | } |
| 3660 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 3661 | /* Option forceclose is set, or either side wants to close, |
| 3662 | * let's enforce it now that we're not expecting any new |
| 3663 | * data to come. The caller knows the session is complete |
| 3664 | * once both states are CLOSED. |
| 3665 | */ |
| 3666 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3667 | buffer_shutr_now(buf); |
| 3668 | buffer_shutw_now(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3669 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3670 | } |
| 3671 | else { |
| 3672 | /* The last possible modes are keep-alive and tunnel. Since tunnel |
| 3673 | * mode does not set the body analyser, we can't reach this place |
| 3674 | * in tunnel mode, so we're left with keep-alive only. |
| 3675 | * This mode is currently not implemented, we switch to tunnel mode. |
| 3676 | */ |
| 3677 | buffer_auto_read(buf); |
| 3678 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3679 | } |
| 3680 | |
| 3681 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) { |
| 3682 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3683 | buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
| 3684 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3685 | if (!(buf->flags & BF_OUT_EMPTY)) { |
| 3686 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 3687 | goto http_msg_closing; |
| 3688 | } |
| 3689 | else { |
| 3690 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3691 | goto http_msg_closed; |
| 3692 | } |
| 3693 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3694 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3695 | } |
| 3696 | |
| 3697 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 3698 | http_msg_closing: |
| 3699 | /* nothing else to forward, just waiting for the output buffer |
| 3700 | * to be empty and for the shutw_now to take effect. |
| 3701 | */ |
| 3702 | if (buf->flags & BF_OUT_EMPTY) { |
| 3703 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3704 | goto http_msg_closed; |
| 3705 | } |
| 3706 | else if (buf->flags & BF_SHUTW) { |
| 3707 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3708 | goto wait_other_side; |
| 3709 | } |
| 3710 | } |
| 3711 | |
| 3712 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 3713 | http_msg_closed: |
| 3714 | goto wait_other_side; |
| 3715 | } |
| 3716 | |
| 3717 | wait_other_side: |
| 3718 | http_silent_debug(__LINE__, s); |
| 3719 | return txn->req.msg_state != old_state || buf->flags != old_flags; |
| 3720 | } |
| 3721 | |
| 3722 | |
| 3723 | /* This function updates the response state machine according to the request |
| 3724 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 3725 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 3726 | * it is only used to find when a request/response couple is complete. Both |
| 3727 | * this function and its equivalent should loop until both return zero. It |
| 3728 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 3729 | */ |
| 3730 | int http_sync_res_state(struct session *s) |
| 3731 | { |
| 3732 | struct buffer *buf = s->rep; |
| 3733 | struct http_txn *txn = &s->txn; |
| 3734 | unsigned int old_flags = buf->flags; |
| 3735 | unsigned int old_state = txn->rsp.msg_state; |
| 3736 | |
| 3737 | http_silent_debug(__LINE__, s); |
| 3738 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 3739 | return 0; |
| 3740 | |
| 3741 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 3742 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3743 | * still monitor the server connection for a possible close |
| 3744 | * while the request is being uploaded, so we don't disable |
| 3745 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3746 | */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3747 | /* buffer_dont_read(buf); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3748 | |
| 3749 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 3750 | goto wait_other_side; |
| 3751 | |
| 3752 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 3753 | /* The client seems to still be sending data, probably |
| 3754 | * because we got an error response during an upload. |
| 3755 | * We have the choice of either breaking the connection |
| 3756 | * or letting it pass through. Let's do the later. |
| 3757 | */ |
| 3758 | goto wait_other_side; |
| 3759 | } |
| 3760 | |
| 3761 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 3762 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3763 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3764 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
| 3765 | goto wait_other_side; |
| 3766 | } |
| 3767 | |
| 3768 | /* When we get here, it means that both the request and the |
| 3769 | * response have finished receiving. Depending on the connection |
| 3770 | * mode, we'll have to wait for the last bytes to leave in either |
| 3771 | * direction, and sometimes for a close to be effective. |
| 3772 | */ |
| 3773 | |
| 3774 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 3775 | /* Server-close mode : shut read and wait for the request |
| 3776 | * side to close its output buffer. The caller will detect |
| 3777 | * when we're in DONE and the other is in CLOSED and will |
| 3778 | * catch that for the final cleanup. |
| 3779 | */ |
| 3780 | if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW))) |
| 3781 | buffer_shutr_now(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3782 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3783 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 3784 | /* Option forceclose is set, or either side wants to close, |
| 3785 | * let's enforce it now that we're not expecting any new |
| 3786 | * data to come. The caller knows the session is complete |
| 3787 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3788 | */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3789 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) { |
| 3790 | buffer_shutr_now(buf); |
| 3791 | buffer_shutw_now(buf); |
| 3792 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3793 | } |
| 3794 | else { |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3795 | /* The last possible modes are keep-alive and tunnel. Since tunnel |
| 3796 | * mode does not set the body analyser, we can't reach this place |
| 3797 | * in tunnel mode, so we're left with keep-alive only. |
| 3798 | * This mode is currently not implemented, we switch to tunnel mode. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3799 | */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3800 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3801 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3802 | } |
| 3803 | |
| 3804 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) { |
| 3805 | /* if we've just closed an output, let's switch */ |
| 3806 | if (!(buf->flags & BF_OUT_EMPTY)) { |
| 3807 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 3808 | goto http_msg_closing; |
| 3809 | } |
| 3810 | else { |
| 3811 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 3812 | goto http_msg_closed; |
| 3813 | } |
| 3814 | } |
| 3815 | goto wait_other_side; |
| 3816 | } |
| 3817 | |
| 3818 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 3819 | http_msg_closing: |
| 3820 | /* nothing else to forward, just waiting for the output buffer |
| 3821 | * to be empty and for the shutw_now to take effect. |
| 3822 | */ |
| 3823 | if (buf->flags & BF_OUT_EMPTY) { |
| 3824 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 3825 | goto http_msg_closed; |
| 3826 | } |
| 3827 | else if (buf->flags & BF_SHUTW) { |
| 3828 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 3829 | goto wait_other_side; |
| 3830 | } |
| 3831 | } |
| 3832 | |
| 3833 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 3834 | http_msg_closed: |
| 3835 | /* drop any pending data */ |
| 3836 | buffer_ignore(buf, buf->l - buf->send_max); |
| 3837 | buffer_auto_close(buf); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3838 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3839 | goto wait_other_side; |
| 3840 | } |
| 3841 | |
| 3842 | wait_other_side: |
| 3843 | http_silent_debug(__LINE__, s); |
| 3844 | return txn->rsp.msg_state != old_state || buf->flags != old_flags; |
| 3845 | } |
| 3846 | |
| 3847 | |
| 3848 | /* Resync the request and response state machines. Return 1 if either state |
| 3849 | * changes. |
| 3850 | */ |
| 3851 | int http_resync_states(struct session *s) |
| 3852 | { |
| 3853 | struct http_txn *txn = &s->txn; |
| 3854 | int old_req_state = txn->req.msg_state; |
| 3855 | int old_res_state = txn->rsp.msg_state; |
| 3856 | |
| 3857 | http_silent_debug(__LINE__, s); |
| 3858 | http_sync_req_state(s); |
| 3859 | while (1) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3860 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3861 | if (!http_sync_res_state(s)) |
| 3862 | break; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3863 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3864 | if (!http_sync_req_state(s)) |
| 3865 | break; |
| 3866 | } |
| 3867 | http_silent_debug(__LINE__, s); |
| 3868 | /* OK, both state machines agree on a compatible state. |
| 3869 | * There are a few cases we're interested in : |
| 3870 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 3871 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 3872 | * directions, so let's simply disable both analysers. |
| 3873 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 3874 | * request. |
| 3875 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 3876 | * with server-close mode means we've completed one request and we |
| 3877 | * must re-initialize the server connection. |
| 3878 | */ |
| 3879 | |
| 3880 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 3881 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 3882 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 3883 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
| 3884 | s->req->analysers = 0; |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 3885 | buffer_auto_close(s->req); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3886 | buffer_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3887 | s->rep->analysers = 0; |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 3888 | buffer_auto_close(s->rep); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3889 | buffer_auto_read(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3890 | } |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 3891 | else if (txn->rsp.msg_state == HTTP_MSG_CLOSED || |
| 3892 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
| 3893 | (s->rep->flags & BF_SHUTW)) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3894 | s->rep->analysers = 0; |
| 3895 | buffer_auto_close(s->rep); |
| 3896 | buffer_auto_read(s->rep); |
| 3897 | s->req->analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3898 | buffer_abort(s->req); |
| 3899 | buffer_auto_close(s->req); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3900 | buffer_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3901 | buffer_ignore(s->req, s->req->l - s->req->send_max); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3902 | } |
| 3903 | else if (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 3904 | txn->rsp.msg_state == HTTP_MSG_DONE && |
| 3905 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) { |
| 3906 | /* server-close: terminate this server connection and |
| 3907 | * reinitialize a fresh-new transaction. |
| 3908 | */ |
| 3909 | http_end_txn_clean_session(s); |
| 3910 | } |
| 3911 | |
| 3912 | http_silent_debug(__LINE__, s); |
| 3913 | return txn->req.msg_state != old_req_state || |
| 3914 | txn->rsp.msg_state != old_res_state; |
| 3915 | } |
| 3916 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3917 | /* This function is an analyser which forwards request body (including chunk |
| 3918 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 3919 | * zero byte. The only situation where it must not be called is when we're in |
| 3920 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 3921 | * remaining data and to resync after end of body. It expects the msg_state to |
| 3922 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 3923 | * read more data, or 1 once we can go on with next request or end the session. |
| 3924 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len |
| 3925 | * bytes of pending data + the headers if not already done (between som and sov). |
| 3926 | * It eventually adjusts som to match sov after the data in between have been sent. |
| 3927 | */ |
| 3928 | int http_request_forward_body(struct session *s, struct buffer *req, int an_bit) |
| 3929 | { |
| 3930 | struct http_txn *txn = &s->txn; |
| 3931 | struct http_msg *msg = &s->txn.req; |
| 3932 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 3933 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 3934 | return 0; |
| 3935 | |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 3936 | if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) || |
| 3937 | ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) { |
| 3938 | /* Output closed while we were sending data. We must abort. */ |
| 3939 | buffer_ignore(req, req->l - req->send_max); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3940 | buffer_auto_read(req); |
| 3941 | buffer_auto_close(req); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 3942 | req->analysers &= ~an_bit; |
| 3943 | return 1; |
| 3944 | } |
| 3945 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 3946 | buffer_dont_close(req); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3947 | |
| 3948 | /* Note that we don't have to send 100-continue back because we don't |
| 3949 | * need the data to complete our job, and it's up to the server to |
| 3950 | * decide whether to return 100, 417 or anything else in return of |
| 3951 | * an "Expect: 100-continue" header. |
| 3952 | */ |
| 3953 | |
| 3954 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 3955 | /* we have msg->col and msg->sov which both point to the first |
| 3956 | * byte of message body. msg->som still points to the beginning |
| 3957 | * of the message. We must save the body in req->lr because it |
| 3958 | * survives buffer re-alignments. |
| 3959 | */ |
| 3960 | req->lr = req->data + msg->sov; |
| 3961 | if (txn->flags & TX_REQ_TE_CHNK) |
| 3962 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 3963 | else { |
| 3964 | msg->msg_state = HTTP_MSG_DATA; |
| 3965 | } |
| 3966 | } |
| 3967 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3968 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3969 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 3970 | /* we may have some data pending */ |
| 3971 | if (msg->hdr_content_len || msg->som != msg->sov) { |
| 3972 | int bytes = msg->sov - msg->som; |
| 3973 | if (bytes < 0) /* sov may have wrapped at the end */ |
| 3974 | bytes += req->size; |
| 3975 | buffer_forward(req, bytes + msg->hdr_content_len); |
| 3976 | msg->hdr_content_len = 0; /* don't forward that again */ |
| 3977 | msg->som = msg->sov; |
| 3978 | } |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 3979 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 3980 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 3981 | /* must still forward */ |
| 3982 | if (req->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 3983 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 3984 | |
| 3985 | /* nothing left to forward */ |
| 3986 | if (txn->flags & TX_REQ_TE_CHNK) |
| 3987 | msg->msg_state = HTTP_MSG_DATA_CRLF; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3988 | else |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 3989 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 3990 | } |
| 3991 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3992 | /* read the chunk size and assign it to ->hdr_content_len, then |
| 3993 | * set ->sov and ->lr to point to the body and switch to DATA or |
| 3994 | * TRAILERS state. |
| 3995 | */ |
| 3996 | int ret = http_parse_chunk_size(req, msg); |
| 3997 | |
| 3998 | if (!ret) |
| 3999 | goto missing_data; |
| 4000 | else if (ret < 0) |
| 4001 | goto return_bad_req; |
| 4002 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4003 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4004 | else if (msg->msg_state == HTTP_MSG_DATA_CRLF) { |
| 4005 | /* we want the CRLF after the data */ |
| 4006 | int ret; |
| 4007 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 4008 | req->lr = req->w + req->send_max; |
| 4009 | if (req->lr >= req->data + req->size) |
| 4010 | req->lr -= req->size; |
| 4011 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4012 | ret = http_skip_chunk_crlf(req, msg); |
| 4013 | |
| 4014 | if (ret == 0) |
| 4015 | goto missing_data; |
| 4016 | else if (ret < 0) |
| 4017 | goto return_bad_req; |
| 4018 | /* we're in MSG_CHUNK_SIZE now */ |
| 4019 | } |
| 4020 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
| 4021 | int ret = http_forward_trailers(req, msg); |
| 4022 | |
| 4023 | if (ret == 0) |
| 4024 | goto missing_data; |
| 4025 | else if (ret < 0) |
| 4026 | goto return_bad_req; |
| 4027 | /* we're in HTTP_MSG_DONE now */ |
| 4028 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4029 | else { |
| 4030 | /* other states, DONE...TUNNEL */ |
| 4031 | if (http_resync_states(s)) { |
| 4032 | /* some state changes occurred, maybe the analyser |
| 4033 | * was disabled too. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 4034 | */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4035 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
| 4036 | goto return_bad_req; |
| 4037 | return 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4038 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4039 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4040 | } |
| 4041 | } |
| 4042 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4043 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4044 | /* stop waiting for data if the input is closed before the end */ |
| 4045 | if (req->flags & BF_SHUTR) |
| 4046 | goto return_bad_req; |
| 4047 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4048 | /* waiting for the last bits to leave the buffer */ |
| 4049 | if (req->flags & BF_SHUTW) |
| 4050 | goto return_bad_req; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4051 | |
| 4052 | http_silent_debug(__LINE__, s); |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4053 | return 0; |
| 4054 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4055 | return_bad_req: /* let's centralize all bad requests */ |
| 4056 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4057 | txn->status = 400; |
| 4058 | /* 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] | 4059 | 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] | 4060 | req->analysers = 0; |
| 4061 | s->fe->counters.failed_req++; |
| 4062 | if (s->listener->counters) |
| 4063 | s->listener->counters->failed_req++; |
| 4064 | |
| 4065 | if (!(s->flags & SN_ERR_MASK)) |
| 4066 | s->flags |= SN_ERR_PRXCOND; |
| 4067 | if (!(s->flags & SN_FINST_MASK)) |
| 4068 | s->flags |= SN_FINST_R; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4069 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4070 | return 0; |
| 4071 | } |
| 4072 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4073 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 4074 | * processing can continue on next analysers, or zero if it either needs more |
| 4075 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 4076 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers |
| 4077 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 4078 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4079 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4080 | 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] | 4081 | { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4082 | struct http_txn *txn = &s->txn; |
| 4083 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4084 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4085 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4086 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4087 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4088 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4089 | 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] | 4090 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4091 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4092 | rep, |
| 4093 | rep->rex, rep->wex, |
| 4094 | rep->flags, |
| 4095 | rep->l, |
| 4096 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 4097 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4098 | /* |
| 4099 | * Now parse the partial (or complete) lines. |
| 4100 | * We will check the response syntax, and also join multi-line |
| 4101 | * headers. An index of all the lines will be elaborated while |
| 4102 | * parsing. |
| 4103 | * |
| 4104 | * For the parsing, we use a 28 states FSM. |
| 4105 | * |
| 4106 | * Here is the information we currently have : |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4107 | * rep->data + msg->som = beginning of response |
| 4108 | * rep->data + msg->eoh = end of processed headers / start of current one |
| 4109 | * msg->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4110 | * rep->lr = first non-visited byte |
| 4111 | * rep->r = end of data |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4112 | * Once we reach MSG_BODY, rep->sol = rep->data + msg->som |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4113 | */ |
| 4114 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4115 | /* There's a protected area at the end of the buffer for rewriting |
| 4116 | * purposes. We don't want to start to parse the request if the |
| 4117 | * protected area is affected, because we may have to move processed |
| 4118 | * data later, which is much more complicated. |
| 4119 | */ |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4120 | if (rep->l && msg->msg_state < HTTP_MSG_ERROR) { |
| 4121 | if (unlikely((rep->flags & BF_FULL) || |
| 4122 | rep->r < rep->lr || |
| 4123 | rep->r > rep->data + rep->size - global.tune.maxrewrite)) { |
| 4124 | if (rep->send_max) { |
| 4125 | /* some data has still not left the buffer, wake us once that's done */ |
| 4126 | buffer_dont_close(rep); |
| 4127 | rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 4128 | return 0; |
| 4129 | } |
| 4130 | if (rep->l <= rep->size - global.tune.maxrewrite) |
| 4131 | http_buffer_heavy_realign(rep, msg); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4132 | } |
| 4133 | |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4134 | if (likely(rep->lr < rep->r)) |
| 4135 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4136 | } |
| 4137 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4138 | /* 1: we might have to print this header in debug mode */ |
| 4139 | if (unlikely((global.mode & MODE_DEBUG) && |
| 4140 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4141 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4142 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4143 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4144 | sol = msg->sol; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4145 | eol = sol + msg->sl.rq.l; |
| 4146 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4147 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4148 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 4149 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
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 | while (cur_idx) { |
| 4152 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 4153 | debug_hdr("srvhdr", s, sol, eol); |
| 4154 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 4155 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 4156 | } |
| 4157 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4158 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4159 | /* |
| 4160 | * Now we quickly check if we have found a full valid response. |
| 4161 | * If not so, we check the FD and buffer states before leaving. |
| 4162 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4163 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4164 | * responses are checked first. |
| 4165 | * |
| 4166 | * Depending on whether the client is still there or not, we |
| 4167 | * may send an error response back or not. Note that normally |
| 4168 | * we should only check for HTTP status there, and check I/O |
| 4169 | * errors somewhere else. |
| 4170 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4171 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4172 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4173 | /* Invalid response */ |
| 4174 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4175 | /* we detected a parsing error. We want to archive this response |
| 4176 | * in the dedicated proxy area for later troubleshooting. |
| 4177 | */ |
| 4178 | hdr_response_bad: |
| 4179 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
| 4180 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe); |
| 4181 | |
| 4182 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4183 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4184 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4185 | health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP); |
| 4186 | } |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4187 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4188 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4189 | rep->analysers = 0; |
| 4190 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4191 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4192 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
| 4193 | |
| 4194 | if (!(s->flags & SN_ERR_MASK)) |
| 4195 | s->flags |= SN_ERR_PRXCOND; |
| 4196 | if (!(s->flags & SN_FINST_MASK)) |
| 4197 | s->flags |= SN_FINST_H; |
| 4198 | |
| 4199 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4200 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4201 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4202 | /* too large response does not fit in buffer. */ |
| 4203 | else if (rep->flags & BF_FULL) { |
| 4204 | goto hdr_response_bad; |
| 4205 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4206 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4207 | /* read error */ |
| 4208 | else if (rep->flags & BF_READ_ERROR) { |
| 4209 | if (msg->err_pos >= 0) |
| 4210 | 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] | 4211 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4212 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4213 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4214 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4215 | health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR); |
| 4216 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 4217 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4218 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4219 | rep->analysers = 0; |
| 4220 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4221 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4222 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 4223 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4224 | if (!(s->flags & SN_ERR_MASK)) |
| 4225 | s->flags |= SN_ERR_SRVCL; |
| 4226 | if (!(s->flags & SN_FINST_MASK)) |
| 4227 | s->flags |= SN_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 4228 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4229 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4230 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4231 | /* read timeout : return a 504 to the client. */ |
| 4232 | else if (rep->flags & BF_READ_TIMEOUT) { |
| 4233 | if (msg->err_pos >= 0) |
| 4234 | 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] | 4235 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4236 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4237 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4238 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4239 | health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT); |
| 4240 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4241 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4242 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4243 | rep->analysers = 0; |
| 4244 | txn->status = 504; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4245 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4246 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4247 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4248 | if (!(s->flags & SN_ERR_MASK)) |
| 4249 | s->flags |= SN_ERR_SRVTO; |
| 4250 | if (!(s->flags & SN_FINST_MASK)) |
| 4251 | s->flags |= SN_FINST_H; |
| 4252 | return 0; |
| 4253 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 4254 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4255 | /* close from server */ |
| 4256 | else if (rep->flags & BF_SHUTR) { |
| 4257 | if (msg->err_pos >= 0) |
| 4258 | 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] | 4259 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4260 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4261 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4262 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4263 | health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE); |
| 4264 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4265 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4266 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4267 | rep->analysers = 0; |
| 4268 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4269 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4270 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4271 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4272 | if (!(s->flags & SN_ERR_MASK)) |
| 4273 | s->flags |= SN_ERR_SRVCL; |
| 4274 | if (!(s->flags & SN_FINST_MASK)) |
| 4275 | s->flags |= SN_FINST_H; |
| 4276 | return 0; |
| 4277 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4278 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4279 | /* write error to client (we don't send any message then) */ |
| 4280 | else if (rep->flags & BF_WRITE_ERROR) { |
| 4281 | if (msg->err_pos >= 0) |
| 4282 | 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] | 4283 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4284 | s->be->counters.failed_resp++; |
| 4285 | rep->analysers = 0; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4286 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4287 | |
| 4288 | if (!(s->flags & SN_ERR_MASK)) |
| 4289 | s->flags |= SN_ERR_CLICL; |
| 4290 | if (!(s->flags & SN_FINST_MASK)) |
| 4291 | s->flags |= SN_FINST_H; |
| 4292 | |
| 4293 | /* process_session() will take care of the error */ |
| 4294 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4295 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4296 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4297 | buffer_dont_close(rep); |
| 4298 | return 0; |
| 4299 | } |
| 4300 | |
| 4301 | /* More interesting part now : we know that we have a complete |
| 4302 | * response which at least looks like HTTP. We have an indicator |
| 4303 | * of each header's length, so we can parse them quickly. |
| 4304 | */ |
| 4305 | |
| 4306 | if (unlikely(msg->err_pos >= 0)) |
| 4307 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe); |
| 4308 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4309 | /* |
| 4310 | * 1: get the status code |
| 4311 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4312 | n = msg->sol[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4313 | if (n < 1 || n > 5) |
| 4314 | n = 0; |
| 4315 | s->srv->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4316 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4317 | /* check if the response is HTTP/1.1 or above */ |
| 4318 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4319 | ((msg->sol[5] > '1') || |
| 4320 | ((msg->sol[5] == '1') && |
| 4321 | (msg->sol[7] >= '1')))) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4322 | txn->flags |= TX_RES_VER_11; |
| 4323 | |
| 4324 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4325 | 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] | 4326 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4327 | /* transfer length unknown*/ |
| 4328 | txn->flags &= ~TX_RES_XFER_LEN; |
| 4329 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4330 | 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] | 4331 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4332 | if (txn->status >= 100 && txn->status < 500) |
| 4333 | health_adjust(s->srv, HANA_STATUS_HTTP_OK); |
| 4334 | else |
| 4335 | health_adjust(s->srv, HANA_STATUS_HTTP_STS); |
| 4336 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4337 | /* |
| 4338 | * 2: check for cacheability. |
| 4339 | */ |
| 4340 | |
| 4341 | switch (txn->status) { |
| 4342 | case 200: |
| 4343 | case 203: |
| 4344 | case 206: |
| 4345 | case 300: |
| 4346 | case 301: |
| 4347 | case 410: |
| 4348 | /* RFC2616 @13.4: |
| 4349 | * "A response received with a status code of |
| 4350 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 4351 | * by a cache (...) unless a cache-control |
| 4352 | * directive prohibits caching." |
| 4353 | * |
| 4354 | * RFC2616 @9.5: POST method : |
| 4355 | * "Responses to this method are not cacheable, |
| 4356 | * unless the response includes appropriate |
| 4357 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4358 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4359 | if (likely(txn->meth != HTTP_METH_POST) && |
| 4360 | (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
| 4361 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 4362 | break; |
| 4363 | default: |
| 4364 | break; |
| 4365 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4366 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4367 | /* |
| 4368 | * 3: we may need to capture headers |
| 4369 | */ |
| 4370 | s->logs.logwait &= ~LW_RESP; |
| 4371 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap)) |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4372 | capture_headers(msg->sol, &txn->hdr_idx, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4373 | txn->rsp.cap, s->fe->rsp_cap); |
| 4374 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4375 | /* 4: determine the transfer-length. |
| 4376 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 4377 | * the presence of a message-body in a RESPONSE and its transfer length |
| 4378 | * must be determined that way : |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4379 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4380 | * All responses to the HEAD request method MUST NOT include a |
| 4381 | * message-body, even though the presence of entity-header fields |
| 4382 | * might lead one to believe they do. All 1xx (informational), 204 |
| 4383 | * (No Content), and 304 (Not Modified) responses MUST NOT include a |
| 4384 | * message-body. All other responses do include a message-body, |
| 4385 | * although it MAY be of zero length. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4386 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4387 | * 1. Any response which "MUST NOT" include a message-body (such as the |
| 4388 | * 1xx, 204 and 304 responses and any response to a HEAD request) is |
| 4389 | * always terminated by the first empty line after the header fields, |
| 4390 | * regardless of the entity-header fields present in the message. |
| 4391 | * |
| 4392 | * 2. If a Transfer-Encoding header field (Section 9.7) is present and |
| 4393 | * the "chunked" transfer-coding (Section 6.2) is used, the |
| 4394 | * transfer-length is defined by the use of this transfer-coding. |
| 4395 | * If a Transfer-Encoding header field is present and the "chunked" |
| 4396 | * transfer-coding is not present, the transfer-length is defined by |
| 4397 | * the sender closing the connection. |
| 4398 | * |
| 4399 | * 3. If a Content-Length header field is present, its decimal value in |
| 4400 | * OCTETs represents both the entity-length and the transfer-length. |
| 4401 | * If a message is received with both a Transfer-Encoding header |
| 4402 | * field and a Content-Length header field, the latter MUST be ignored. |
| 4403 | * |
| 4404 | * 4. If the message uses the media type "multipart/byteranges", and |
| 4405 | * the transfer-length is not otherwise specified, then this self- |
| 4406 | * delimiting media type defines the transfer-length. This media |
| 4407 | * type MUST NOT be used unless the sender knows that the recipient |
| 4408 | * can parse it; the presence in a request of a Range header with |
| 4409 | * multiple byte-range specifiers from a 1.1 client implies that the |
| 4410 | * client can parse multipart/byteranges responses. |
| 4411 | * |
| 4412 | * 5. By the server closing the connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4413 | */ |
| 4414 | |
| 4415 | /* Skip parsing if no content length is possible. The response flags |
| 4416 | * 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] | 4417 | * 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] | 4418 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 4419 | */ |
| 4420 | if (txn->meth == HTTP_METH_HEAD || |
| 4421 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4422 | txn->status == 204 || txn->status == 304) { |
| 4423 | txn->flags |= TX_RES_XFER_LEN; |
| 4424 | goto skip_content_length; |
| 4425 | } |
| 4426 | |
| 4427 | if (txn->meth == HTTP_METH_CONNECT) |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4428 | goto skip_content_length; |
| 4429 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4430 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4431 | ctx.idx = 0; |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 4432 | while ((txn->flags & TX_RES_VER_11) && |
| 4433 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4434 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
| 4435 | txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN); |
| 4436 | else if (txn->flags & TX_RES_TE_CHNK) { |
| 4437 | /* bad transfer-encoding (chunked followed by something else) */ |
| 4438 | use_close_only = 1; |
| 4439 | txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN); |
| 4440 | break; |
| 4441 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4442 | } |
| 4443 | |
| 4444 | /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */ |
| 4445 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4446 | while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only && |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4447 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) { |
| 4448 | signed long long cl; |
| 4449 | |
| 4450 | if (!ctx.vlen) |
| 4451 | goto hdr_response_bad; |
| 4452 | |
| 4453 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) |
| 4454 | goto hdr_response_bad; /* parse failure */ |
| 4455 | |
| 4456 | if (cl < 0) |
| 4457 | goto hdr_response_bad; |
| 4458 | |
| 4459 | if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl)) |
| 4460 | goto hdr_response_bad; /* already specified, was different */ |
| 4461 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4462 | txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4463 | msg->hdr_content_len = cl; |
| 4464 | } |
| 4465 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4466 | /* FIXME: we should also implement the multipart/byterange method. |
| 4467 | * For now on, we resort to close mode in this case (unknown length). |
| 4468 | */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4469 | skip_content_length: |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4470 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4471 | /* end of job, return OK */ |
| 4472 | rep->analysers &= ~an_bit; |
| 4473 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4474 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4475 | return 1; |
| 4476 | } |
| 4477 | |
| 4478 | /* This function performs all the processing enabled for the current response. |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 4479 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 4480 | * and updates t->rep->analysers. It might make sense to explode it into several |
| 4481 | * other functions. It works like process_request (see indications above). |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4482 | */ |
| 4483 | int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px) |
| 4484 | { |
| 4485 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4486 | struct http_msg *msg = &txn->rsp; |
| 4487 | struct proxy *cur_proxy; |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 4488 | struct wordlist *wl; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4489 | |
| 4490 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 4491 | now_ms, __FUNCTION__, |
| 4492 | t, |
| 4493 | rep, |
| 4494 | rep->rex, rep->wex, |
| 4495 | rep->flags, |
| 4496 | rep->l, |
| 4497 | rep->analysers); |
| 4498 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4499 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4500 | return 0; |
| 4501 | |
| 4502 | rep->analysers &= ~an_bit; |
| 4503 | rep->analyse_exp = TICK_ETERNITY; |
| 4504 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4505 | /* Now we have to check if we need to modify the Connection header. |
| 4506 | * This is more difficult on the response than it is on the request, |
| 4507 | * because we can have two different HTTP versions and we don't know |
| 4508 | * how the client will interprete a response. For instance, let's say |
| 4509 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 4510 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 4511 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 4512 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 4513 | * the lack of header as a keep-alive. Thus we will use two flags |
| 4514 | * indicating how a request MAY be understood by the client. In case |
| 4515 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 4516 | * ambiguous cases such as both close and keepalive are seen, then we |
| 4517 | * will fall back to explicit close. Note that we won't take risks with |
| 4518 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4519 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4520 | */ |
| 4521 | |
| 4522 | if ((txn->meth != HTTP_METH_CONNECT) && |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 4523 | (txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 4524 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
| 4525 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4526 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4527 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4528 | /* on unknown transfer length, we must close */ |
| 4529 | if (!(txn->flags & TX_RES_XFER_LEN) && |
| 4530 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 4531 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4532 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4533 | /* now adjust header transformations depending on current state */ |
| 4534 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 4535 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4536 | to_del |= 2; /* remove "keep-alive" on any response */ |
| 4537 | if (!(txn->flags & TX_RES_VER_11)) |
| 4538 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4539 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4540 | else { /* SCL / KAL */ |
| 4541 | to_del |= 1; /* remove "close" on any response */ |
| 4542 | if ((txn->flags & (TX_RES_VER_11|TX_REQ_VER_11)) == (TX_RES_VER_11|TX_REQ_VER_11)) |
| 4543 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4544 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4545 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4546 | /* Parse and remove some headers from the connection header */ |
| 4547 | http_parse_connection_header(txn, msg, rep, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4548 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4549 | /* Some keep-alive responses are converted to Server-close if |
| 4550 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4551 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4552 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 4553 | if ((txn->flags & TX_HDR_CONN_CLO) || |
| 4554 | (txn->flags & (TX_HDR_CONN_KAL|TX_RES_VER_11)) == 0) |
| 4555 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4556 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4557 | } |
| 4558 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4559 | if (1) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4560 | /* |
| 4561 | * 3: we will have to evaluate the filters. |
| 4562 | * As opposed to version 1.2, now they will be evaluated in the |
| 4563 | * filters order and not in the header order. This means that |
| 4564 | * each filter has to be validated among all headers. |
| 4565 | * |
| 4566 | * Filters are tried with ->be first, then with ->fe if it is |
| 4567 | * different from ->be. |
| 4568 | */ |
| 4569 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4570 | cur_proxy = t->be; |
| 4571 | while (1) { |
| 4572 | struct proxy *rule_set = cur_proxy; |
| 4573 | |
| 4574 | /* try headers filters */ |
| 4575 | if (rule_set->rsp_exp != NULL) { |
| 4576 | if (apply_filters_to_response(t, rep, rule_set->rsp_exp) < 0) { |
| 4577 | return_bad_resp: |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4578 | if (t->srv) { |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4579 | t->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4580 | health_adjust(t->srv, HANA_STATUS_HTTP_RSP); |
| 4581 | } |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4582 | cur_proxy->counters.failed_resp++; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4583 | return_srv_prx_502: |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 4584 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4585 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4586 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 8e89b84 | 2009-10-18 23:56:35 +0200 | [diff] [blame] | 4587 | stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4588 | if (!(t->flags & SN_ERR_MASK)) |
| 4589 | t->flags |= SN_ERR_PRXCOND; |
| 4590 | if (!(t->flags & SN_FINST_MASK)) |
| 4591 | t->flags |= SN_FINST_H; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 4592 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4593 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4594 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4595 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4596 | /* has the response been denied ? */ |
| 4597 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | 8365f93 | 2009-03-15 23:11:49 +0100 | [diff] [blame] | 4598 | if (t->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4599 | t->srv->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4600 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4601 | cur_proxy->counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4602 | if (t->listener->counters) |
| 4603 | t->listener->counters->denied_resp++; |
| 4604 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4605 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 4606 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4607 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4608 | /* add response headers from the rule sets in the same order */ |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 4609 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 4610 | if (txn->status < 200) |
| 4611 | break; |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 4612 | 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] | 4613 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 4614 | } |
| 4615 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4616 | /* check whether we're already working on the frontend */ |
| 4617 | if (cur_proxy == t->fe) |
| 4618 | break; |
| 4619 | cur_proxy = t->fe; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4620 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4621 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4622 | /* |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4623 | * We may be facing a 1xx response (100 continue, 101 switching protocols), |
| 4624 | * in which case this is not the right response, and we're waiting for the |
| 4625 | * next one. Let's allow this response to go to the client and wait for the |
| 4626 | * next one. |
| 4627 | */ |
| 4628 | if (txn->status < 200) { |
| 4629 | hdr_idx_init(&txn->hdr_idx); |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4630 | buffer_forward(rep, rep->lr - msg->sol); |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4631 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 4632 | txn->status = 0; |
| 4633 | rep->analysers |= AN_RES_WAIT_HTTP | an_bit; |
| 4634 | return 1; |
| 4635 | } |
| 4636 | |
| 4637 | /* we don't have any 1xx status code now */ |
| 4638 | |
| 4639 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4640 | * 4: check for server cookie. |
| 4641 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4642 | if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name || |
| 4643 | (t->be->options & PR_O_CHK_CACHE)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4644 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4645 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4646 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4647 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4648 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4649 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4650 | 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] | 4651 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4652 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4653 | /* |
| 4654 | * 6: add server cookie in the response if needed |
| 4655 | */ |
| 4656 | 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] | 4657 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4658 | int len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4659 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4660 | /* the server is known, it's not the one the client requested, we have to |
| 4661 | * insert a set-cookie here, except if we want to insert only on POST |
| 4662 | * requests and this one isn't. Note that servers which don't have cookies |
| 4663 | * (eg: some backup servers) will return a full cookie removal request. |
| 4664 | */ |
| 4665 | len = sprintf(trash, "Set-Cookie: %s=%s; path=/", |
| 4666 | t->be->cookie_name, |
| 4667 | 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] | 4668 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4669 | if (t->be->cookie_domain) |
| 4670 | len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4671 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4672 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 4673 | trash, len) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4674 | goto return_bad_resp; |
| 4675 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4676 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4677 | /* Here, we will tell an eventual cache on the client side that we don't |
| 4678 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 4679 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 4680 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 4681 | */ |
| 4682 | if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4683 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4684 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 4685 | |
| 4686 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 4687 | "Cache-control: private", 22) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4688 | goto return_bad_resp; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4689 | } |
| 4690 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4691 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4692 | /* |
| 4693 | * 7: check if result will be cacheable with a cookie. |
| 4694 | * We'll block the response if security checks have caught |
| 4695 | * nasty things such as a cacheable cookie. |
| 4696 | */ |
| 4697 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) == |
| 4698 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) && |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4699 | (t->be->options & PR_O_CHK_CACHE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4700 | |
| 4701 | /* we're in presence of a cacheable response containing |
| 4702 | * a set-cookie header. We'll block it as requested by |
| 4703 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4704 | */ |
Willy Tarreau | 8365f93 | 2009-03-15 23:11:49 +0100 | [diff] [blame] | 4705 | if (t->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4706 | t->srv->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4707 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4708 | cur_proxy->counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4709 | if (t->listener->counters) |
| 4710 | t->listener->counters->denied_resp++; |
| 4711 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4712 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 4713 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 4714 | send_log(t->be, LOG_ALERT, |
| 4715 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 4716 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 4717 | goto return_srv_prx_502; |
| 4718 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4719 | |
| 4720 | /* |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4721 | * 8: adjust "Connection: close" or "Connection: keep-alive" if needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4722 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4723 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
| 4724 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) { |
| 4725 | unsigned int want_flags = 0; |
| 4726 | |
| 4727 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 4728 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4729 | /* we want a keep-alive response here. Keep-alive header |
| 4730 | * required if either side is not 1.1. |
| 4731 | */ |
| 4732 | if ((txn->flags & (TX_REQ_VER_11|TX_RES_VER_11)) != (TX_REQ_VER_11|TX_RES_VER_11)) |
| 4733 | want_flags |= TX_CON_KAL_SET; |
| 4734 | } |
| 4735 | else { |
| 4736 | /* we want a close response here. Close header required if |
| 4737 | * the server is 1.1, regardless of the client. |
| 4738 | */ |
| 4739 | if (txn->flags & TX_RES_VER_11) |
| 4740 | want_flags |= TX_CON_CLO_SET; |
| 4741 | } |
| 4742 | |
| 4743 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 4744 | http_change_connection_header(txn, msg, rep, want_flags); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4745 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4746 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4747 | if (txn->flags & TX_RES_XFER_LEN) |
| 4748 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 4749 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4750 | /************************************************************* |
| 4751 | * OK, that's finished for the headers. We have done what we * |
| 4752 | * could. Let's switch to the DATA state. * |
| 4753 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4754 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4755 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4756 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4757 | /* if the user wants to log as soon as possible, without counting |
| 4758 | * bytes from the server, then this is the right moment. We have |
| 4759 | * to temporarily assign bytes_out to log what we currently have. |
| 4760 | */ |
| 4761 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 4762 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 4763 | t->logs.bytes_out = txn->rsp.eoh; |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 4764 | t->do_log(t); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4765 | t->logs.bytes_out = 0; |
| 4766 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4767 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4768 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 4769 | * otherwise we would not let the client side wake up. |
| 4770 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4771 | |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 4772 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4773 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 4774 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4775 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4776 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4777 | /* This function is an analyser which forwards response body (including chunk |
| 4778 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 4779 | * zero byte. The only situation where it must not be called is when we're in |
| 4780 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 4781 | * remaining data and to resync after end of body. It expects the msg_state to |
| 4782 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 4783 | * read more data, or 1 once we can go on with next request or end the session. |
| 4784 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len |
| 4785 | * bytes of pending data + the headers if not already done (between som and sov). |
| 4786 | * It eventually adjusts som to match sov after the data in between have been sent. |
| 4787 | */ |
| 4788 | int http_response_forward_body(struct session *s, struct buffer *res, int an_bit) |
| 4789 | { |
| 4790 | struct http_txn *txn = &s->txn; |
| 4791 | struct http_msg *msg = &s->txn.rsp; |
| 4792 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4793 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4794 | return 0; |
| 4795 | |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 4796 | 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] | 4797 | ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) || |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 4798 | !s->req->analysers) { |
| 4799 | /* in case of error or if the other analyser went away, we can't analyse HTTP anymore */ |
| 4800 | buffer_ignore(res, res->l - res->send_max); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4801 | buffer_auto_read(res); |
| 4802 | buffer_auto_close(res); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 4803 | res->analysers &= ~an_bit; |
| 4804 | return 1; |
| 4805 | } |
| 4806 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 4807 | buffer_dont_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4808 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4809 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4810 | /* we have msg->col and msg->sov which both point to the first |
| 4811 | * byte of message body. msg->som still points to the beginning |
| 4812 | * of the message. We must save the body in req->lr because it |
| 4813 | * survives buffer re-alignments. |
| 4814 | */ |
| 4815 | res->lr = res->data + msg->sov; |
| 4816 | if (txn->flags & TX_RES_TE_CHNK) |
| 4817 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4818 | else { |
| 4819 | msg->msg_state = HTTP_MSG_DATA; |
| 4820 | } |
| 4821 | } |
| 4822 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4823 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4824 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4825 | /* we may have some data pending */ |
| 4826 | if (msg->hdr_content_len || msg->som != msg->sov) { |
| 4827 | int bytes = msg->sov - msg->som; |
| 4828 | if (bytes < 0) /* sov may have wrapped at the end */ |
| 4829 | bytes += res->size; |
| 4830 | buffer_forward(res, bytes + msg->hdr_content_len); |
| 4831 | msg->hdr_content_len = 0; /* don't forward that again */ |
| 4832 | msg->som = msg->sov; |
| 4833 | } |
| 4834 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4835 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 4836 | /* must still forward */ |
| 4837 | if (res->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4838 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4839 | |
| 4840 | /* nothing left to forward */ |
| 4841 | if (txn->flags & TX_RES_TE_CHNK) |
| 4842 | msg->msg_state = HTTP_MSG_DATA_CRLF; |
| 4843 | else |
| 4844 | msg->msg_state = HTTP_MSG_DONE; |
| 4845 | } |
| 4846 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4847 | /* read the chunk size and assign it to ->hdr_content_len, then |
| 4848 | * set ->sov to point to the body and switch to DATA or TRAILERS state. |
| 4849 | */ |
| 4850 | int ret = http_parse_chunk_size(res, msg); |
| 4851 | |
| 4852 | if (!ret) |
| 4853 | goto missing_data; |
| 4854 | else if (ret < 0) |
| 4855 | goto return_bad_res; |
| 4856 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4857 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4858 | else if (msg->msg_state == HTTP_MSG_DATA_CRLF) { |
| 4859 | /* we want the CRLF after the data */ |
| 4860 | int ret; |
| 4861 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 4862 | res->lr = res->w + res->send_max; |
| 4863 | if (res->lr >= res->data + res->size) |
| 4864 | res->lr -= res->size; |
| 4865 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4866 | ret = http_skip_chunk_crlf(res, msg); |
| 4867 | |
| 4868 | if (!ret) |
| 4869 | goto missing_data; |
| 4870 | else if (ret < 0) |
| 4871 | goto return_bad_res; |
| 4872 | /* we're in MSG_CHUNK_SIZE now */ |
| 4873 | } |
| 4874 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
| 4875 | int ret = http_forward_trailers(res, msg); |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 4876 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4877 | if (ret == 0) |
| 4878 | goto missing_data; |
| 4879 | else if (ret < 0) |
| 4880 | goto return_bad_res; |
| 4881 | /* we're in HTTP_MSG_DONE now */ |
| 4882 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4883 | else { |
| 4884 | /* other states, DONE...TUNNEL */ |
| 4885 | if (http_resync_states(s)) { |
| 4886 | http_silent_debug(__LINE__, s); |
| 4887 | /* some state changes occurred, maybe the analyser |
| 4888 | * was disabled too. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 4889 | */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4890 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
| 4891 | goto return_bad_res; |
| 4892 | return 1; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 4893 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4894 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4895 | } |
| 4896 | } |
| 4897 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4898 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4899 | /* stop waiting for data if the input is closed before the end */ |
| 4900 | if (res->flags & BF_SHUTR) |
| 4901 | goto return_bad_res; |
| 4902 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4903 | if (!s->req->analysers) |
| 4904 | goto return_bad_res; |
| 4905 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4906 | /* forward the chunk size as well as any pending data */ |
| 4907 | if (msg->hdr_content_len || msg->som != msg->sov) { |
| 4908 | buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len); |
| 4909 | msg->hdr_content_len = 0; /* don't forward that again */ |
| 4910 | msg->som = msg->sov; |
| 4911 | } |
| 4912 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4913 | /* the session handler will take care of timeouts and errors */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4914 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4915 | return 0; |
| 4916 | |
| 4917 | return_bad_res: /* let's centralize all bad resuests */ |
| 4918 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 4919 | txn->status = 502; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 4920 | /* don't send any error message as we're in the body */ |
| 4921 | stream_int_retnclose(res->cons, NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4922 | res->analysers = 0; |
| 4923 | s->be->counters.failed_resp++; |
| 4924 | if (s->srv) { |
| 4925 | s->srv->counters.failed_resp++; |
| 4926 | health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP); |
| 4927 | } |
| 4928 | |
| 4929 | if (!(s->flags & SN_ERR_MASK)) |
| 4930 | s->flags |= SN_ERR_PRXCOND; |
| 4931 | if (!(s->flags & SN_FINST_MASK)) |
| 4932 | s->flags |= SN_FINST_R; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4933 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4934 | return 0; |
| 4935 | } |
| 4936 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4937 | /* Iterate the same filter through all request headers. |
| 4938 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4939 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 4940 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4941 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4942 | 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] | 4943 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4944 | char term; |
| 4945 | char *cur_ptr, *cur_end, *cur_next; |
| 4946 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4947 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4948 | struct hdr_idx_elem *cur_hdr; |
| 4949 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 4950 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4951 | last_hdr = 0; |
| 4952 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4953 | cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4954 | old_idx = 0; |
| 4955 | |
| 4956 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4957 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4958 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 4959 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4960 | (exp->action == ACT_ALLOW || |
| 4961 | exp->action == ACT_DENY || |
| 4962 | exp->action == ACT_TARPIT)) |
| 4963 | return 0; |
| 4964 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4965 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4966 | if (!cur_idx) |
| 4967 | break; |
| 4968 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 4969 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4970 | cur_ptr = cur_next; |
| 4971 | cur_end = cur_ptr + cur_hdr->len; |
| 4972 | cur_next = cur_end + cur_hdr->cr + 1; |
| 4973 | |
| 4974 | /* Now we have one header between cur_ptr and cur_end, |
| 4975 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 4976 | */ |
| 4977 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4978 | /* The annoying part is that pattern matching needs |
| 4979 | * that we modify the contents to null-terminate all |
| 4980 | * strings before testing them. |
| 4981 | */ |
| 4982 | |
| 4983 | term = *cur_end; |
| 4984 | *cur_end = '\0'; |
| 4985 | |
| 4986 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 4987 | switch (exp->action) { |
| 4988 | case ACT_SETBE: |
| 4989 | /* It is not possible to jump a second time. |
| 4990 | * FIXME: should we return an HTTP/500 here so that |
| 4991 | * the admin knows there's a problem ? |
| 4992 | */ |
| 4993 | if (t->be != t->fe) |
| 4994 | break; |
| 4995 | |
| 4996 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 4997 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 4998 | last_hdr = 1; |
| 4999 | break; |
| 5000 | |
| 5001 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5002 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5003 | last_hdr = 1; |
| 5004 | break; |
| 5005 | |
| 5006 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5007 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5008 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5009 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 5010 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5011 | if (t->listener->counters) |
| 5012 | t->listener->counters->denied_resp++; |
| 5013 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5014 | break; |
| 5015 | |
| 5016 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5017 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5018 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5019 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 5020 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5021 | if (t->listener->counters) |
| 5022 | t->listener->counters->denied_resp++; |
| 5023 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5024 | break; |
| 5025 | |
| 5026 | case ACT_REPLACE: |
| 5027 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5028 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 5029 | /* FIXME: if the user adds a newline in the replacement, the |
| 5030 | * index will not be recalculated for now, and the new line |
| 5031 | * will not be counted as a new header. |
| 5032 | */ |
| 5033 | |
| 5034 | cur_end += delta; |
| 5035 | cur_next += delta; |
| 5036 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5037 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5038 | break; |
| 5039 | |
| 5040 | case ACT_REMOVE: |
| 5041 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 5042 | cur_next += delta; |
| 5043 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5044 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5045 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5046 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5047 | cur_hdr->len = 0; |
| 5048 | cur_end = NULL; /* null-term has been rewritten */ |
| 5049 | break; |
| 5050 | |
| 5051 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5052 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5053 | if (cur_end) |
| 5054 | *cur_end = term; /* restore the string terminator */ |
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 | /* keep the link from this header to next one in case of later |
| 5057 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5058 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5059 | old_idx = cur_idx; |
| 5060 | } |
| 5061 | return 0; |
| 5062 | } |
| 5063 | |
| 5064 | |
| 5065 | /* Apply the filter to the request line. |
| 5066 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 5067 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5068 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5069 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5070 | */ |
| 5071 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 5072 | { |
| 5073 | char term; |
| 5074 | char *cur_ptr, *cur_end; |
| 5075 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5076 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5077 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5078 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5079 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5080 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5081 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5082 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5083 | (exp->action == ACT_ALLOW || |
| 5084 | exp->action == ACT_DENY || |
| 5085 | exp->action == ACT_TARPIT)) |
| 5086 | return 0; |
| 5087 | else if (exp->action == ACT_REMOVE) |
| 5088 | return 0; |
| 5089 | |
| 5090 | done = 0; |
| 5091 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5092 | cur_ptr = txn->req.sol; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5093 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5094 | |
| 5095 | /* Now we have the request line between cur_ptr and cur_end */ |
| 5096 | |
| 5097 | /* The annoying part is that pattern matching needs |
| 5098 | * that we modify the contents to null-terminate all |
| 5099 | * strings before testing them. |
| 5100 | */ |
| 5101 | |
| 5102 | term = *cur_end; |
| 5103 | *cur_end = '\0'; |
| 5104 | |
| 5105 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5106 | switch (exp->action) { |
| 5107 | case ACT_SETBE: |
| 5108 | /* It is not possible to jump a second time. |
| 5109 | * FIXME: should we return an HTTP/500 here so that |
| 5110 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5111 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5112 | if (t->be != t->fe) |
| 5113 | break; |
| 5114 | |
| 5115 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 5116 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5117 | done = 1; |
| 5118 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5119 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5120 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5121 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5122 | done = 1; |
| 5123 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5124 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5125 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5126 | txn->flags |= TX_CLDENY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5127 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 5128 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5129 | if (t->listener->counters) |
| 5130 | t->listener->counters->denied_resp++; |
| 5131 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5132 | done = 1; |
| 5133 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5134 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5135 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5136 | txn->flags |= TX_CLTARPIT; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5137 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 5138 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5139 | if (t->listener->counters) |
| 5140 | t->listener->counters->denied_resp++; |
| 5141 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5142 | done = 1; |
| 5143 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5144 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5145 | case ACT_REPLACE: |
| 5146 | *cur_end = term; /* restore the string terminator */ |
| 5147 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5148 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 5149 | /* FIXME: if the user adds a newline in the replacement, the |
| 5150 | * index will not be recalculated for now, and the new line |
| 5151 | * will not be counted as a new header. |
| 5152 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5153 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5154 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5155 | cur_end += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5156 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5157 | HTTP_MSG_RQMETH, |
| 5158 | cur_ptr, cur_end + 1, |
| 5159 | NULL, NULL); |
| 5160 | if (unlikely(!cur_end)) |
| 5161 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5162 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5163 | /* we have a full request and we know that we have either a CR |
| 5164 | * or an LF at <ptr>. |
| 5165 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5166 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 5167 | 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] | 5168 | /* there is no point trying this regex on headers */ |
| 5169 | return 1; |
| 5170 | } |
| 5171 | } |
| 5172 | *cur_end = term; /* restore the string terminator */ |
| 5173 | return done; |
| 5174 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 5175 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5176 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5177 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5178 | /* |
| 5179 | * Apply all the req filters <exp> to all headers in buffer <req> of session <t>. |
| 5180 | * 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] | 5181 | * unparsable request. Since it can manage the switch to another backend, it |
| 5182 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5183 | */ |
| 5184 | int apply_filters_to_request(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 5185 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5186 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5187 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5188 | while (exp && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5189 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5190 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5191 | /* |
| 5192 | * The interleaving of transformations and verdicts |
| 5193 | * makes it difficult to decide to continue or stop |
| 5194 | * the evaluation. |
| 5195 | */ |
| 5196 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5197 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5198 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 5199 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) { |
| 5200 | exp = exp->next; |
| 5201 | continue; |
| 5202 | } |
| 5203 | |
| 5204 | /* Apply the filter to the request line. */ |
| 5205 | ret = apply_filter_to_req_line(t, req, exp); |
| 5206 | if (unlikely(ret < 0)) |
| 5207 | return -1; |
| 5208 | |
| 5209 | if (likely(ret == 0)) { |
| 5210 | /* The filter did not match the request, it can be |
| 5211 | * iterated through all headers. |
| 5212 | */ |
| 5213 | apply_filter_to_req_headers(t, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5214 | } |
| 5215 | exp = exp->next; |
| 5216 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5217 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5218 | } |
| 5219 | |
| 5220 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5221 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5222 | /* |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5223 | * Try to retrieve the server associated to the appsession. |
| 5224 | * If the server is found, it's assigned to the session. |
| 5225 | */ |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5226 | 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] | 5227 | struct http_txn *txn = &t->txn; |
| 5228 | appsess *asession = NULL; |
| 5229 | char *sessid_temp = NULL; |
| 5230 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5231 | if (len > t->be->appsession_len) { |
| 5232 | len = t->be->appsession_len; |
| 5233 | } |
| 5234 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5235 | if (t->be->options2 & PR_O2_AS_REQL) { |
| 5236 | /* 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] | 5237 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5238 | /* 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] | 5239 | pool_free2(apools.sessid, txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5240 | } |
| 5241 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5242 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5243 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5244 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5245 | return; |
| 5246 | } |
| 5247 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5248 | memcpy(txn->sessid, buf, len); |
| 5249 | txn->sessid[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5250 | } |
| 5251 | |
| 5252 | if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) { |
| 5253 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5254 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5255 | return; |
| 5256 | } |
| 5257 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5258 | memcpy(sessid_temp, buf, len); |
| 5259 | sessid_temp[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5260 | |
| 5261 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp); |
| 5262 | /* free previously allocated memory */ |
| 5263 | pool_free2(apools.sessid, sessid_temp); |
| 5264 | |
| 5265 | if (asession != NULL) { |
| 5266 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 5267 | if (!(t->be->options2 & PR_O2_AS_REQL)) |
| 5268 | asession->request_count++; |
| 5269 | |
| 5270 | if (asession->serverid != NULL) { |
| 5271 | struct server *srv = t->be->srv; |
| 5272 | while (srv) { |
| 5273 | if (strcmp(srv->id, asession->serverid) == 0) { |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 5274 | if ((srv->state & SRV_RUNNING) || |
| 5275 | (t->be->options & PR_O_PERSIST) || |
| 5276 | (t->flags & SN_FORCE_PRST)) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5277 | /* we found the server and it's usable */ |
| 5278 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 2a6d88d | 2010-01-24 13:10:43 +0100 | [diff] [blame] | 5279 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5280 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
| 5281 | t->srv = srv; |
| 5282 | break; |
| 5283 | } else { |
| 5284 | txn->flags &= ~TX_CK_MASK; |
| 5285 | txn->flags |= TX_CK_DOWN; |
| 5286 | } |
| 5287 | } |
| 5288 | srv = srv->next; |
| 5289 | } |
| 5290 | } |
| 5291 | } |
| 5292 | } |
| 5293 | |
| 5294 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 5295 | * Manage client-side cookie. It can impact performance by about 2% so it is |
| 5296 | * desirable to call it only when needed. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5297 | */ |
| 5298 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 5299 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5300 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5301 | char *p1, *p2, *p3, *p4, *p5; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5302 | char *del_colon, *del_cookie, *colon; |
| 5303 | int app_cookies; |
| 5304 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5305 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5306 | int cur_idx, old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5307 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 5308 | /* Iterate through the headers. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5309 | * we start with the start line. |
| 5310 | */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 5311 | old_idx = 0; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5312 | cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5313 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5314 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5315 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5316 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5317 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5318 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5319 | cur_ptr = cur_next; |
| 5320 | cur_end = cur_ptr + cur_hdr->len; |
| 5321 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5322 | |
| 5323 | /* We have one full header between cur_ptr and cur_end, and the |
| 5324 | * next header starts at cur_next. We're only interested in |
| 5325 | * "Cookie:" headers. |
| 5326 | */ |
| 5327 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5328 | val = http_header_match2(cur_ptr, cur_end, "Cookie", 6); |
| 5329 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5330 | old_idx = cur_idx; |
| 5331 | continue; |
| 5332 | } |
| 5333 | |
| 5334 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 5335 | * attributes whose name begin with a '$', and associate them with |
| 5336 | * the right cookie, if we want to delete this cookie. |
| 5337 | * So there are 3 cases for each cookie read : |
| 5338 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 5339 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 5340 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 5341 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 5342 | * "special" cookie. |
| 5343 | * At the end of loop, if a "special" cookie remains, we may have to |
| 5344 | * remove it. If no application cookie persists in the header, we |
| 5345 | * *MUST* delete it |
| 5346 | */ |
| 5347 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5348 | colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5349 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5350 | /* del_cookie == NULL => nothing to be deleted */ |
| 5351 | del_colon = del_cookie = NULL; |
| 5352 | app_cookies = 0; |
| 5353 | |
| 5354 | while (p1 < cur_end) { |
| 5355 | /* skip spaces and colons, but keep an eye on these ones */ |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5356 | resync_name: |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5357 | while (p1 < cur_end) { |
| 5358 | if (*p1 == ';' || *p1 == ',') |
| 5359 | colon = p1; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 5360 | else if (!isspace((unsigned char)*p1)) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5361 | break; |
| 5362 | p1++; |
| 5363 | } |
| 5364 | |
| 5365 | if (p1 == cur_end) |
| 5366 | break; |
| 5367 | |
| 5368 | /* p1 is at the beginning of the cookie name */ |
| 5369 | p2 = p1; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5370 | while (p2 < cur_end && *p2 != '=') { |
| 5371 | if (*p2 == ',' || *p2 == ';' || isspace((unsigned char)*p2)) { |
| 5372 | /* oops, the cookie name was truncated, resync */ |
| 5373 | p1 = p2; |
| 5374 | goto resync_name; |
| 5375 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5376 | p2++; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5377 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5378 | |
| 5379 | if (p2 == cur_end) |
| 5380 | break; |
| 5381 | |
| 5382 | p3 = p2 + 1; /* skips the '=' sign */ |
| 5383 | if (p3 == cur_end) |
| 5384 | break; |
| 5385 | |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5386 | /* parse the value, stripping leading and trailing spaces but keeping insiders. */ |
| 5387 | p5 = p4 = p3; |
| 5388 | while (p5 < cur_end && *p5 != ';' && *p5 != ',') { |
| 5389 | if (!isspace((unsigned char)*p5)) |
| 5390 | p4 = p5 + 1; |
| 5391 | p5++; |
| 5392 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5393 | |
| 5394 | /* here, we have the cookie name between p1 and p2, |
| 5395 | * and its value between p3 and p4. |
| 5396 | * we can process it : |
| 5397 | * |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5398 | * Cookie: NAME=VALUE ; |
| 5399 | * | || || |+-> p5 |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5400 | * | || || +--> p4 |
| 5401 | * | || |+-------> p3 |
| 5402 | * | || +--------> p2 |
| 5403 | * | |+------------> p1 |
| 5404 | * | +-------------> colon |
| 5405 | * +--------------------> cur_ptr |
| 5406 | */ |
| 5407 | |
| 5408 | if (*p1 == '$') { |
| 5409 | /* skip this one */ |
| 5410 | } |
| 5411 | else { |
| 5412 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5413 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5414 | txn->cli_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5415 | (p4 - p1 >= t->fe->capture_namelen) && |
| 5416 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5417 | int log_len = p4 - p1; |
| 5418 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 5419 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5420 | Alert("HTTP logging : out of memory.\n"); |
| 5421 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5422 | if (log_len > t->fe->capture_len) |
| 5423 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5424 | memcpy(txn->cli_cookie, p1, log_len); |
| 5425 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5426 | } |
| 5427 | } |
| 5428 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5429 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 5430 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5431 | /* Cool... it's the right one */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5432 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5433 | char *delim; |
| 5434 | |
| 5435 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 5436 | * have the server ID betweek p3 and delim, and the original cookie between |
| 5437 | * delim+1 and p4. Otherwise, delim==p4 : |
| 5438 | * |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5439 | * Cookie: NAME=SRV~VALUE ; |
| 5440 | * | || || | |+-> p5 |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5441 | * | || || | +--> p4 |
| 5442 | * | || || +--------> delim |
| 5443 | * | || |+-----------> p3 |
| 5444 | * | || +------------> p2 |
| 5445 | * | |+----------------> p1 |
| 5446 | * | +-----------------> colon |
| 5447 | * +------------------------> cur_ptr |
| 5448 | */ |
| 5449 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5450 | if (t->be->options & PR_O_COOK_PFX) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5451 | for (delim = p3; delim < p4; delim++) |
| 5452 | if (*delim == COOKIE_DELIM) |
| 5453 | break; |
| 5454 | } |
| 5455 | else |
| 5456 | delim = p4; |
| 5457 | |
| 5458 | |
| 5459 | /* Here, we'll look for the first running server which supports the cookie. |
| 5460 | * This allows to share a same cookie between several servers, for example |
| 5461 | * to dedicate backup servers to specific servers only. |
| 5462 | * However, to prevent clients from sticking to cookie-less backup server |
| 5463 | * when they have incidentely learned an empty cookie, we simply ignore |
| 5464 | * empty cookies and mark them as invalid. |
| 5465 | */ |
| 5466 | if (delim == p3) |
| 5467 | srv = NULL; |
| 5468 | |
| 5469 | while (srv) { |
Willy Tarreau | 92f2ab1 | 2007-02-02 22:14:47 +0100 | [diff] [blame] | 5470 | if (srv->cookie && (srv->cklen == delim - p3) && |
| 5471 | !memcmp(p3, srv->cookie, delim - p3)) { |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 5472 | if ((srv->state & SRV_RUNNING) || |
| 5473 | (t->be->options & PR_O_PERSIST) || |
| 5474 | (t->flags & SN_FORCE_PRST)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5475 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5476 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 2a6d88d | 2010-01-24 13:10:43 +0100 | [diff] [blame] | 5477 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5478 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5479 | t->srv = srv; |
| 5480 | break; |
| 5481 | } else { |
| 5482 | /* we found a server, but it's down */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5483 | txn->flags &= ~TX_CK_MASK; |
| 5484 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5485 | } |
| 5486 | } |
| 5487 | srv = srv->next; |
| 5488 | } |
| 5489 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5490 | if (!srv && !(txn->flags & TX_CK_DOWN)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5491 | /* no server matched this cookie */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5492 | txn->flags &= ~TX_CK_MASK; |
| 5493 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5494 | } |
| 5495 | |
| 5496 | /* depending on the cookie mode, we may have to either : |
| 5497 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 5498 | * the server never sees it ; |
| 5499 | * - remove the server id from the cookie value, and tag the cookie as an |
| 5500 | * application cookie so that it does not get accidentely removed later, |
| 5501 | * if we're in cookie prefix mode |
| 5502 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5503 | if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5504 | int delta; /* negative */ |
| 5505 | |
| 5506 | delta = buffer_replace2(req, p3, delim + 1, NULL, 0); |
| 5507 | p4 += delta; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5508 | p5 += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5509 | cur_end += delta; |
| 5510 | cur_next += delta; |
| 5511 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5512 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5513 | |
| 5514 | del_cookie = del_colon = NULL; |
| 5515 | app_cookies++; /* protect the header from deletion */ |
| 5516 | } |
| 5517 | else if (del_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5518 | (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] | 5519 | del_cookie = p1; |
| 5520 | del_colon = colon; |
| 5521 | } |
| 5522 | } else { |
| 5523 | /* now we know that we must keep this cookie since it's |
| 5524 | * not ours. But if we wanted to delete our cookie |
| 5525 | * earlier, we cannot remove the complete header, but we |
| 5526 | * can remove the previous block itself. |
| 5527 | */ |
| 5528 | app_cookies++; |
| 5529 | |
| 5530 | if (del_cookie != NULL) { |
| 5531 | int delta; /* negative */ |
| 5532 | |
| 5533 | delta = buffer_replace2(req, del_cookie, p1, NULL, 0); |
| 5534 | p4 += delta; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5535 | p5 += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5536 | cur_end += delta; |
| 5537 | cur_next += delta; |
| 5538 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5539 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5540 | del_cookie = del_colon = NULL; |
| 5541 | } |
| 5542 | } |
| 5543 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5544 | if (t->be->appsession_name != NULL) { |
| 5545 | int cmp_len, value_len; |
| 5546 | char *value_begin; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5547 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5548 | if (t->be->options2 & PR_O2_AS_PFX) { |
| 5549 | cmp_len = MIN(p4 - p1, t->be->appsession_name_len); |
| 5550 | value_begin = p1 + t->be->appsession_name_len; |
| 5551 | value_len = p4 - p1 - t->be->appsession_name_len; |
| 5552 | } else { |
| 5553 | cmp_len = p2 - p1; |
| 5554 | value_begin = p3; |
| 5555 | value_len = p4 - p3; |
| 5556 | } |
| 5557 | |
| 5558 | /* let's see if the cookie is our appcookie */ |
| 5559 | if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) { |
| 5560 | /* Cool... it's the right one */ |
| 5561 | manage_client_side_appsession(t, value_begin, value_len); |
| 5562 | } |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5563 | #if defined(DEBUG_HASH) |
| 5564 | Alert("manage_client_side_cookies\n"); |
| 5565 | appsession_hash_dump(&(t->be->htbl_proxy)); |
| 5566 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5567 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 5568 | } |
| 5569 | |
| 5570 | /* we'll have to look for another cookie ... */ |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5571 | p1 = p5; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5572 | } /* while (p1 < cur_end) */ |
| 5573 | |
| 5574 | /* There's no more cookie on this line. |
| 5575 | * We may have marked the last one(s) for deletion. |
| 5576 | * We must do this now in two ways : |
| 5577 | * - if there is no app cookie, we simply delete the header ; |
| 5578 | * - if there are app cookies, we must delete the end of the |
| 5579 | * string properly, including the colon/semi-colon before |
| 5580 | * the cookie name. |
| 5581 | */ |
| 5582 | if (del_cookie != NULL) { |
| 5583 | int delta; |
| 5584 | if (app_cookies) { |
| 5585 | delta = buffer_replace2(req, del_colon, cur_end, NULL, 0); |
| 5586 | cur_end = del_colon; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5587 | cur_hdr->len += delta; |
| 5588 | } else { |
| 5589 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5590 | |
| 5591 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5592 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5593 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5594 | cur_hdr->len = 0; |
| 5595 | } |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 5596 | cur_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5597 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5598 | } |
| 5599 | |
| 5600 | /* keep the link from this header to next one */ |
| 5601 | old_idx = cur_idx; |
| 5602 | } /* end of cookie processing on this header */ |
| 5603 | } |
| 5604 | |
| 5605 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5606 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 5607 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 5608 | */ |
| 5609 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 5610 | { |
| 5611 | char term; |
| 5612 | char *cur_ptr, *cur_end, *cur_next; |
| 5613 | int cur_idx, old_idx, last_hdr; |
| 5614 | struct http_txn *txn = &t->txn; |
| 5615 | struct hdr_idx_elem *cur_hdr; |
| 5616 | int len, delta; |
| 5617 | |
| 5618 | last_hdr = 0; |
| 5619 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5620 | cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5621 | old_idx = 0; |
| 5622 | |
| 5623 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5624 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5625 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5626 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5627 | (exp->action == ACT_ALLOW || |
| 5628 | exp->action == ACT_DENY)) |
| 5629 | return 0; |
| 5630 | |
| 5631 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 5632 | if (!cur_idx) |
| 5633 | break; |
| 5634 | |
| 5635 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 5636 | cur_ptr = cur_next; |
| 5637 | cur_end = cur_ptr + cur_hdr->len; |
| 5638 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5639 | |
| 5640 | /* Now we have one header between cur_ptr and cur_end, |
| 5641 | * and the next header starts at cur_next. |
| 5642 | */ |
| 5643 | |
| 5644 | /* The annoying part is that pattern matching needs |
| 5645 | * that we modify the contents to null-terminate all |
| 5646 | * strings before testing them. |
| 5647 | */ |
| 5648 | |
| 5649 | term = *cur_end; |
| 5650 | *cur_end = '\0'; |
| 5651 | |
| 5652 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5653 | switch (exp->action) { |
| 5654 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5655 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5656 | last_hdr = 1; |
| 5657 | break; |
| 5658 | |
| 5659 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5660 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5661 | last_hdr = 1; |
| 5662 | break; |
| 5663 | |
| 5664 | case ACT_REPLACE: |
| 5665 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5666 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 5667 | /* FIXME: if the user adds a newline in the replacement, the |
| 5668 | * index will not be recalculated for now, and the new line |
| 5669 | * will not be counted as a new header. |
| 5670 | */ |
| 5671 | |
| 5672 | cur_end += delta; |
| 5673 | cur_next += delta; |
| 5674 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5675 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5676 | break; |
| 5677 | |
| 5678 | case ACT_REMOVE: |
| 5679 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 5680 | cur_next += delta; |
| 5681 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5682 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5683 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5684 | txn->hdr_idx.used--; |
| 5685 | cur_hdr->len = 0; |
| 5686 | cur_end = NULL; /* null-term has been rewritten */ |
| 5687 | break; |
| 5688 | |
| 5689 | } |
| 5690 | } |
| 5691 | if (cur_end) |
| 5692 | *cur_end = term; /* restore the string terminator */ |
| 5693 | |
| 5694 | /* keep the link from this header to next one in case of later |
| 5695 | * removal of next header. |
| 5696 | */ |
| 5697 | old_idx = cur_idx; |
| 5698 | } |
| 5699 | return 0; |
| 5700 | } |
| 5701 | |
| 5702 | |
| 5703 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 5704 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 5705 | * or -1 if a replacement resulted in an invalid status line. |
| 5706 | */ |
| 5707 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 5708 | { |
| 5709 | char term; |
| 5710 | char *cur_ptr, *cur_end; |
| 5711 | int done; |
| 5712 | struct http_txn *txn = &t->txn; |
| 5713 | int len, delta; |
| 5714 | |
| 5715 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5716 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5717 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5718 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5719 | (exp->action == ACT_ALLOW || |
| 5720 | exp->action == ACT_DENY)) |
| 5721 | return 0; |
| 5722 | else if (exp->action == ACT_REMOVE) |
| 5723 | return 0; |
| 5724 | |
| 5725 | done = 0; |
| 5726 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5727 | cur_ptr = txn->rsp.sol; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5728 | cur_end = cur_ptr + txn->rsp.sl.rq.l; |
| 5729 | |
| 5730 | /* Now we have the status line between cur_ptr and cur_end */ |
| 5731 | |
| 5732 | /* The annoying part is that pattern matching needs |
| 5733 | * that we modify the contents to null-terminate all |
| 5734 | * strings before testing them. |
| 5735 | */ |
| 5736 | |
| 5737 | term = *cur_end; |
| 5738 | *cur_end = '\0'; |
| 5739 | |
| 5740 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5741 | switch (exp->action) { |
| 5742 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5743 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5744 | done = 1; |
| 5745 | break; |
| 5746 | |
| 5747 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5748 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5749 | done = 1; |
| 5750 | break; |
| 5751 | |
| 5752 | case ACT_REPLACE: |
| 5753 | *cur_end = term; /* restore the string terminator */ |
| 5754 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5755 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 5756 | /* FIXME: if the user adds a newline in the replacement, the |
| 5757 | * index will not be recalculated for now, and the new line |
| 5758 | * will not be counted as a new header. |
| 5759 | */ |
| 5760 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5761 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5762 | cur_end += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5763 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 5764 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5765 | cur_ptr, cur_end + 1, |
| 5766 | NULL, NULL); |
| 5767 | if (unlikely(!cur_end)) |
| 5768 | return -1; |
| 5769 | |
| 5770 | /* we have a full respnse and we know that we have either a CR |
| 5771 | * or an LF at <ptr>. |
| 5772 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5773 | 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] | 5774 | hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r'); |
| 5775 | /* there is no point trying this regex on headers */ |
| 5776 | return 1; |
| 5777 | } |
| 5778 | } |
| 5779 | *cur_end = term; /* restore the string terminator */ |
| 5780 | return done; |
| 5781 | } |
| 5782 | |
| 5783 | |
| 5784 | |
| 5785 | /* |
| 5786 | * Apply all the resp filters <exp> to all headers in buffer <rtr> of session <t>. |
| 5787 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 5788 | * unparsable response. |
| 5789 | */ |
| 5790 | int apply_filters_to_response(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 5791 | { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5792 | struct http_txn *txn = &t->txn; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5793 | /* iterate through the filters in the outer loop */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5794 | while (exp && !(txn->flags & TX_SVDENY)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5795 | int ret; |
| 5796 | |
| 5797 | /* |
| 5798 | * The interleaving of transformations and verdicts |
| 5799 | * makes it difficult to decide to continue or stop |
| 5800 | * the evaluation. |
| 5801 | */ |
| 5802 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5803 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5804 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 5805 | exp->action == ACT_PASS)) { |
| 5806 | exp = exp->next; |
| 5807 | continue; |
| 5808 | } |
| 5809 | |
| 5810 | /* Apply the filter to the status line. */ |
| 5811 | ret = apply_filter_to_sts_line(t, rtr, exp); |
| 5812 | if (unlikely(ret < 0)) |
| 5813 | return -1; |
| 5814 | |
| 5815 | if (likely(ret == 0)) { |
| 5816 | /* The filter did not match the response, it can be |
| 5817 | * iterated through all headers. |
| 5818 | */ |
| 5819 | apply_filter_to_resp_headers(t, rtr, exp); |
| 5820 | } |
| 5821 | exp = exp->next; |
| 5822 | } |
| 5823 | return 0; |
| 5824 | } |
| 5825 | |
| 5826 | |
| 5827 | |
| 5828 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 5829 | * Manage server-side cookies. It can impact performance by about 2% so it is |
| 5830 | * desirable to call it only when needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5831 | */ |
| 5832 | void manage_server_side_cookies(struct session *t, struct buffer *rtr) |
| 5833 | { |
| 5834 | struct http_txn *txn = &t->txn; |
| 5835 | char *p1, *p2, *p3, *p4; |
| 5836 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5837 | char *cur_ptr, *cur_end, *cur_next; |
| 5838 | int cur_idx, old_idx, delta; |
| 5839 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5840 | /* Iterate through the headers. |
| 5841 | * we start with the start line. |
| 5842 | */ |
| 5843 | old_idx = 0; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5844 | cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5845 | |
| 5846 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 5847 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5848 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5849 | |
| 5850 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 5851 | cur_ptr = cur_next; |
| 5852 | cur_end = cur_ptr + cur_hdr->len; |
| 5853 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5854 | |
| 5855 | /* We have one full header between cur_ptr and cur_end, and the |
| 5856 | * next header starts at cur_next. We're only interested in |
| 5857 | * "Cookie:" headers. |
| 5858 | */ |
| 5859 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5860 | val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10); |
| 5861 | if (!val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5862 | old_idx = cur_idx; |
| 5863 | continue; |
| 5864 | } |
| 5865 | |
| 5866 | /* 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] | 5867 | txn->flags |= TX_SCK_ANY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5868 | |
| 5869 | |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 5870 | /* maybe we only wanted to see if there was a set-cookie. Note that |
| 5871 | * the cookie capture is declared in the fronend. |
| 5872 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5873 | if (t->be->cookie_name == NULL && |
| 5874 | t->be->appsession_name == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 5875 | t->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5876 | return; |
| 5877 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5878 | p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5879 | |
| 5880 | 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] | 5881 | if (p1 == cur_end || *p1 == ';') /* end of cookie */ |
| 5882 | break; |
| 5883 | |
| 5884 | /* p1 is at the beginning of the cookie name */ |
| 5885 | p2 = p1; |
| 5886 | |
| 5887 | while (p2 < cur_end && *p2 != '=' && *p2 != ';') |
| 5888 | p2++; |
| 5889 | |
| 5890 | if (p2 == cur_end || *p2 == ';') /* next cookie */ |
| 5891 | break; |
| 5892 | |
| 5893 | p3 = p2 + 1; /* skip the '=' sign */ |
| 5894 | if (p3 == cur_end) |
| 5895 | break; |
| 5896 | |
| 5897 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 5898 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';') |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5899 | p4++; |
| 5900 | |
| 5901 | /* here, we have the cookie name between p1 and p2, |
| 5902 | * and its value between p3 and p4. |
| 5903 | * we can process it. |
| 5904 | */ |
| 5905 | |
| 5906 | /* first, let's see if we want to capture it */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 5907 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5908 | txn->srv_cookie == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 5909 | (p4 - p1 >= t->fe->capture_namelen) && |
| 5910 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5911 | int log_len = p4 - p1; |
| 5912 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 5913 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5914 | Alert("HTTP logging : out of memory.\n"); |
| 5915 | } |
| 5916 | |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 5917 | if (log_len > t->fe->capture_len) |
| 5918 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5919 | memcpy(txn->srv_cookie, p1, log_len); |
| 5920 | txn->srv_cookie[log_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5921 | } |
| 5922 | |
| 5923 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5924 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 5925 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5926 | /* Cool... it's the right one */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5927 | txn->flags |= TX_SCK_SEEN; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5928 | |
| 5929 | /* If the cookie is in insert mode on a known server, we'll delete |
| 5930 | * this occurrence because we'll insert another one later. |
| 5931 | * We'll delete it too if the "indirect" option is set and we're in |
| 5932 | * a direct access. */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5933 | if (((t->srv) && (t->be->options & PR_O_COOK_INS)) || |
| 5934 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5935 | /* this header must be deleted */ |
| 5936 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 5937 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5938 | txn->hdr_idx.used--; |
| 5939 | cur_hdr->len = 0; |
| 5940 | cur_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5941 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5942 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5943 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5944 | } |
| 5945 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5946 | (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5947 | /* replace bytes p3->p4 with the cookie name associated |
| 5948 | * with this server since we know it. |
| 5949 | */ |
| 5950 | delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen); |
| 5951 | cur_hdr->len += delta; |
| 5952 | cur_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5953 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5954 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5955 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5956 | } |
| 5957 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5958 | (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5959 | /* insert the cookie name associated with this server |
| 5960 | * before existing cookie, and insert a delimitor between them.. |
| 5961 | */ |
| 5962 | delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1); |
| 5963 | cur_hdr->len += delta; |
| 5964 | cur_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5965 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5966 | |
| 5967 | p3[t->srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5968 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5969 | } |
| 5970 | } |
| 5971 | /* next, let's see if the cookie is our appcookie */ |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5972 | else if (t->be->appsession_name != NULL) { |
| 5973 | int cmp_len, value_len; |
| 5974 | char *value_begin; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5975 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5976 | if (t->be->options2 & PR_O2_AS_PFX) { |
| 5977 | cmp_len = MIN(p4 - p1, t->be->appsession_name_len); |
| 5978 | value_begin = p1 + t->be->appsession_name_len; |
| 5979 | value_len = MIN(t->be->appsession_len, p4 - p1 - t->be->appsession_name_len); |
| 5980 | } else { |
| 5981 | cmp_len = p2 - p1; |
| 5982 | value_begin = p3; |
| 5983 | value_len = MIN(t->be->appsession_len, p4 - p3); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5984 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5985 | |
| 5986 | if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) { |
| 5987 | /* Cool... it's the right one */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5988 | if (txn->sessid != NULL) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5989 | /* free previously allocated memory as we don't need it anymore */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5990 | pool_free2(apools.sessid, txn->sessid); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5991 | } |
| 5992 | /* Store the sessid in the session for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5993 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5994 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5995 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 5996 | return; |
| 5997 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5998 | memcpy(txn->sessid, value_begin, value_len); |
| 5999 | txn->sessid[value_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6000 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6001 | } /* end if ((t->be->appsession_name != NULL) ... */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6002 | break; /* we don't want to loop again since there cannot be another cookie on the same line */ |
| 6003 | } /* we're now at the end of the cookie value */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6004 | /* keep the link from this header to next one */ |
| 6005 | old_idx = cur_idx; |
| 6006 | } /* end of cookie processing on this header */ |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6007 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6008 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6009 | appsess *asession = NULL; |
| 6010 | /* only do insert, if lookup fails */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6011 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6012 | if (asession == NULL) { |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 6013 | size_t server_id_len; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6014 | if ((asession = pool_alloc2(pool2_appsess)) == NULL) { |
| 6015 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 6016 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 6017 | return; |
| 6018 | } |
| 6019 | if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 6020 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 6021 | 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] | 6022 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6023 | return; |
| 6024 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6025 | memcpy(asession->sessid, txn->sessid, t->be->appsession_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6026 | asession->sessid[t->be->appsession_len] = 0; |
| 6027 | |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 6028 | server_id_len = strlen(t->srv->id) + 1; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6029 | if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) { |
| 6030 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 6031 | 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] | 6032 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6033 | return; |
| 6034 | } |
| 6035 | asession->serverid[0] = '\0'; |
| 6036 | memcpy(asession->serverid, t->srv->id, server_id_len); |
| 6037 | |
| 6038 | asession->request_count = 0; |
| 6039 | appsession_hash_insert(&(t->be->htbl_proxy), asession); |
| 6040 | } |
| 6041 | |
| 6042 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 6043 | asession->request_count++; |
| 6044 | } |
| 6045 | |
| 6046 | #if defined(DEBUG_HASH) |
| 6047 | Alert("manage_server_side_cookies\n"); |
| 6048 | appsession_hash_dump(&(t->be->htbl_proxy)); |
| 6049 | #endif |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6050 | } |
| 6051 | |
| 6052 | |
| 6053 | |
| 6054 | /* |
| 6055 | * Check if response is cacheable or not. Updates t->flags. |
| 6056 | */ |
| 6057 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 6058 | { |
| 6059 | struct http_txn *txn = &t->txn; |
| 6060 | char *p1, *p2; |
| 6061 | |
| 6062 | char *cur_ptr, *cur_end, *cur_next; |
| 6063 | int cur_idx; |
| 6064 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 6065 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6066 | return; |
| 6067 | |
| 6068 | /* Iterate through the headers. |
| 6069 | * we start with the start line. |
| 6070 | */ |
| 6071 | cur_idx = 0; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6072 | cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6073 | |
| 6074 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 6075 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6076 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6077 | |
| 6078 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 6079 | cur_ptr = cur_next; |
| 6080 | cur_end = cur_ptr + cur_hdr->len; |
| 6081 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6082 | |
| 6083 | /* We have one full header between cur_ptr and cur_end, and the |
| 6084 | * next header starts at cur_next. We're only interested in |
| 6085 | * "Cookie:" headers. |
| 6086 | */ |
| 6087 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6088 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 6089 | if (val) { |
| 6090 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 6091 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 6092 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 6093 | return; |
| 6094 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6095 | } |
| 6096 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6097 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 6098 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6099 | continue; |
| 6100 | |
| 6101 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 6102 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6103 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6104 | |
| 6105 | if (p1 >= cur_end) /* no more info */ |
| 6106 | continue; |
| 6107 | |
| 6108 | /* p1 is at the beginning of the value */ |
| 6109 | p2 = p1; |
| 6110 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 6111 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6112 | p2++; |
| 6113 | |
| 6114 | /* we have a complete value between p1 and p2 */ |
| 6115 | if (p2 < cur_end && *p2 == '=') { |
| 6116 | /* we have something of the form no-cache="set-cookie" */ |
| 6117 | if ((cur_end - p1 >= 21) && |
| 6118 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 6119 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6120 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6121 | continue; |
| 6122 | } |
| 6123 | |
| 6124 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 6125 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 6126 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 6127 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 6128 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6129 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6130 | return; |
| 6131 | } |
| 6132 | |
| 6133 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6134 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6135 | continue; |
| 6136 | } |
| 6137 | } |
| 6138 | } |
| 6139 | |
| 6140 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6141 | /* |
| 6142 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 6143 | * If the server is found, it's assigned to the session. |
| 6144 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6145 | 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] | 6146 | { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6147 | char *end_params, *first_param, *cur_param, *next_param; |
| 6148 | char separator; |
| 6149 | int value_len; |
| 6150 | |
| 6151 | int mode = t->be->options2 & PR_O2_AS_M_ANY; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6152 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6153 | if (t->be->appsession_name == NULL || |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6154 | (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6155 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6156 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6157 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6158 | first_param = NULL; |
| 6159 | switch (mode) { |
| 6160 | case PR_O2_AS_M_PP: |
| 6161 | first_param = memchr(begin, ';', len); |
| 6162 | break; |
| 6163 | case PR_O2_AS_M_QS: |
| 6164 | first_param = memchr(begin, '?', len); |
| 6165 | break; |
| 6166 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6167 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6168 | if (first_param == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6169 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6170 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6171 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6172 | switch (mode) { |
| 6173 | case PR_O2_AS_M_PP: |
| 6174 | if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) { |
| 6175 | end_params = (char *) begin + len; |
| 6176 | } |
| 6177 | separator = ';'; |
| 6178 | break; |
| 6179 | case PR_O2_AS_M_QS: |
| 6180 | end_params = (char *) begin + len; |
| 6181 | separator = '&'; |
| 6182 | break; |
| 6183 | default: |
| 6184 | /* unknown mode, shouldn't happen */ |
| 6185 | return; |
| 6186 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6187 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6188 | cur_param = next_param = end_params; |
| 6189 | while (cur_param > first_param) { |
| 6190 | cur_param--; |
| 6191 | if ((cur_param[0] == separator) || (cur_param == first_param)) { |
| 6192 | /* let's see if this is the appsession parameter */ |
| 6193 | if ((cur_param + t->be->appsession_name_len + 1 < next_param) && |
| 6194 | ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') && |
| 6195 | (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 6196 | /* Cool... it's the right one */ |
| 6197 | cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2); |
| 6198 | value_len = MIN(t->be->appsession_len, next_param - cur_param); |
| 6199 | if (value_len > 0) { |
| 6200 | manage_client_side_appsession(t, cur_param, value_len); |
| 6201 | } |
| 6202 | break; |
| 6203 | } |
| 6204 | next_param = cur_param; |
| 6205 | } |
| 6206 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6207 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 6208 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 6209 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6210 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6211 | } |
| 6212 | |
| 6213 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6214 | /* |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 6215 | * In a GET or HEAD request, check if the requested URI matches the stats uri |
| 6216 | * 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] | 6217 | * |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 6218 | * 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] | 6219 | * 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] | 6220 | * the stats I/O handler will be registered to start sending data. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6221 | * |
| 6222 | * Returns 1 if the session's state changes, otherwise 0. |
| 6223 | */ |
| 6224 | int stats_check_uri_auth(struct session *t, struct proxy *backend) |
| 6225 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6226 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6227 | struct uri_auth *uri_auth = backend->uri_auth; |
| 6228 | struct user_auth *user; |
| 6229 | int authenticated, cur_idx; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6230 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6231 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6232 | memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats)); |
| 6233 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6234 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6235 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6236 | return 0; |
| 6237 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6238 | h = txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6239 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 6240 | /* the URI is in h */ |
| 6241 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6242 | return 0; |
| 6243 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 6244 | h += uri_auth->uri_len; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6245 | 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] | 6246 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6247 | t->data_ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 6248 | break; |
| 6249 | } |
| 6250 | h++; |
| 6251 | } |
| 6252 | |
| 6253 | if (uri_auth->refresh) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6254 | h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len; |
| 6255 | 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] | 6256 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6257 | t->data_ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 6258 | break; |
| 6259 | } |
| 6260 | h++; |
| 6261 | } |
| 6262 | } |
| 6263 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6264 | h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len; |
| 6265 | 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] | 6266 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6267 | t->data_ctx.stats.flags |= STAT_FMT_CSV; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 6268 | break; |
| 6269 | } |
| 6270 | h++; |
| 6271 | } |
| 6272 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6273 | t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO; |
| 6274 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6275 | /* we are in front of a interceptable URI. Let's check |
| 6276 | * if there's an authentication and if it's valid. |
| 6277 | */ |
| 6278 | user = uri_auth->users; |
| 6279 | if (!user) { |
| 6280 | /* no user auth required, it's OK */ |
| 6281 | authenticated = 1; |
| 6282 | } else { |
| 6283 | authenticated = 0; |
| 6284 | |
| 6285 | /* a user list is defined, we have to check. |
| 6286 | * skip 21 chars for "Authorization: Basic ". |
| 6287 | */ |
| 6288 | |
| 6289 | /* FIXME: this should move to an earlier place */ |
| 6290 | cur_idx = 0; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6291 | h = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6292 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 6293 | int len = txn->hdr_idx.v[cur_idx].len; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6294 | if (len > 14 && |
| 6295 | !strncasecmp("Authorization:", h, 14)) { |
Krzysztof Piotr Oledzki | 6f61b21 | 2009-10-04 23:34:15 +0200 | [diff] [blame] | 6296 | chunk_initlen(&txn->auth_hdr, h, 0, len); |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6297 | break; |
| 6298 | } |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6299 | h += len + txn->hdr_idx.v[cur_idx].cr + 1; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6300 | } |
| 6301 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6302 | if (txn->auth_hdr.len < 21 || |
| 6303 | memcmp(txn->auth_hdr.str + 14, " Basic ", 7)) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6304 | user = NULL; |
| 6305 | |
| 6306 | while (user) { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6307 | if ((txn->auth_hdr.len == user->user_len + 14 + 7) |
| 6308 | && !memcmp(txn->auth_hdr.str + 14 + 7, |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6309 | user->user_pwd, user->user_len)) { |
| 6310 | authenticated = 1; |
| 6311 | break; |
| 6312 | } |
| 6313 | user = user->next; |
| 6314 | } |
| 6315 | } |
| 6316 | |
| 6317 | if (!authenticated) { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 6318 | struct chunk msg; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6319 | |
| 6320 | /* no need to go further */ |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 6321 | sprintf(trash, HTTP_401_fmt, uri_auth->auth_realm); |
| 6322 | chunk_initlen(&msg, trash, sizeof(trash), strlen(trash)); |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 6323 | txn->status = 401; |
Willy Tarreau | dded32d | 2008-11-30 19:48:07 +0100 | [diff] [blame] | 6324 | stream_int_retnclose(t->req->prod, &msg); |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 6325 | t->req->analysers = 0; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6326 | if (!(t->flags & SN_ERR_MASK)) |
| 6327 | t->flags |= SN_ERR_PRXCOND; |
| 6328 | if (!(t->flags & SN_FINST_MASK)) |
| 6329 | t->flags |= SN_FINST_R; |
| 6330 | return 1; |
| 6331 | } |
| 6332 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6333 | /* The request is valid, the user is authenticated. Let's start sending |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6334 | * data. |
| 6335 | */ |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 6336 | t->logs.tv_request = now; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6337 | t->data_source = DATA_SRC_STATS; |
| 6338 | t->data_state = DATA_ST_INIT; |
Willy Tarreau | 91e9993 | 2008-06-30 07:51:00 +0200 | [diff] [blame] | 6339 | t->task->nice = -32; /* small boost for HTTP statistics */ |
Willy Tarreau | b0c9bc4 | 2009-10-04 15:56:38 +0200 | [diff] [blame] | 6340 | stream_int_register_handler(t->rep->prod, http_stats_io_handler); |
| 6341 | t->rep->prod->private = t; |
| 6342 | t->rep->prod->st0 = t->rep->prod->st1 = 0; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6343 | return 1; |
| 6344 | } |
| 6345 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6346 | /* |
| 6347 | * 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] | 6348 | * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not |
| 6349 | * assume that msg->sol = buf->data + msg->som. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6350 | */ |
| 6351 | void http_capture_bad_message(struct error_snapshot *es, struct session *s, |
| 6352 | struct buffer *buf, struct http_msg *msg, |
| 6353 | struct proxy *other_end) |
| 6354 | { |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 6355 | es->len = buf->r - (buf->data + msg->som); |
| 6356 | 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] | 6357 | if (msg->err_pos >= 0) |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 6358 | es->pos = msg->err_pos - msg->som; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6359 | else |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 6360 | es->pos = buf->lr - (buf->data + msg->som); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6361 | es->when = date; // user-visible date |
| 6362 | es->sid = s->uniq_id; |
| 6363 | es->srv = s->srv; |
| 6364 | es->oe = other_end; |
| 6365 | es->src = s->cli_addr; |
| 6366 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6367 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6368 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6369 | * Print a debug line with a header |
| 6370 | */ |
| 6371 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 6372 | { |
| 6373 | int len, max; |
| 6374 | 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] | 6375 | 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] | 6376 | max = end - start; |
| 6377 | UBOUND(max, sizeof(trash) - len - 1); |
| 6378 | len += strlcpy2(trash + len, start, max + 1); |
| 6379 | trash[len++] = '\n'; |
| 6380 | write(1, trash, len); |
| 6381 | } |
| 6382 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6383 | /* |
| 6384 | * Initialize a new HTTP transaction for session <s>. It is assumed that all |
| 6385 | * the required fields are properly allocated and that we only need to (re)init |
| 6386 | * them. This should be used before processing any new request. |
| 6387 | */ |
| 6388 | void http_init_txn(struct session *s) |
| 6389 | { |
| 6390 | struct http_txn *txn = &s->txn; |
| 6391 | struct proxy *fe = s->fe; |
| 6392 | |
| 6393 | txn->flags = 0; |
| 6394 | txn->status = -1; |
| 6395 | |
| 6396 | txn->req.sol = txn->req.eol = NULL; |
| 6397 | txn->req.som = txn->req.eoh = 0; /* relative to the buffer */ |
| 6398 | txn->rsp.sol = txn->rsp.eol = NULL; |
| 6399 | txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */ |
| 6400 | txn->req.hdr_content_len = 0LL; |
| 6401 | txn->rsp.hdr_content_len = 0LL; |
| 6402 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 6403 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
| 6404 | chunk_reset(&txn->auth_hdr); |
| 6405 | |
| 6406 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 6407 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 6408 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 6409 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 6410 | if (txn->req.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6411 | memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *)); |
| 6412 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 6413 | if (txn->rsp.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6414 | memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
| 6415 | |
| 6416 | if (txn->hdr_idx.v) |
| 6417 | hdr_idx_init(&txn->hdr_idx); |
| 6418 | } |
| 6419 | |
| 6420 | /* to be used at the end of a transaction */ |
| 6421 | void http_end_txn(struct session *s) |
| 6422 | { |
| 6423 | struct http_txn *txn = &s->txn; |
| 6424 | |
| 6425 | /* these ones will have been dynamically allocated */ |
| 6426 | pool_free2(pool2_requri, txn->uri); |
| 6427 | pool_free2(pool2_capture, txn->cli_cookie); |
| 6428 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6429 | pool_free2(apools.sessid, txn->sessid); |
| 6430 | txn->sessid = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6431 | txn->uri = NULL; |
| 6432 | txn->srv_cookie = NULL; |
| 6433 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 6434 | |
| 6435 | if (txn->req.cap) { |
| 6436 | struct cap_hdr *h; |
| 6437 | for (h = s->fe->req_cap; h; h = h->next) |
| 6438 | pool_free2(h->pool, txn->req.cap[h->index]); |
| 6439 | memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *)); |
| 6440 | } |
| 6441 | |
| 6442 | if (txn->rsp.cap) { |
| 6443 | struct cap_hdr *h; |
| 6444 | for (h = s->fe->rsp_cap; h; h = h->next) |
| 6445 | pool_free2(h->pool, txn->rsp.cap[h->index]); |
| 6446 | memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *)); |
| 6447 | } |
| 6448 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6449 | } |
| 6450 | |
| 6451 | /* to be used at the end of a transaction to prepare a new one */ |
| 6452 | void http_reset_txn(struct session *s) |
| 6453 | { |
| 6454 | http_end_txn(s); |
| 6455 | http_init_txn(s); |
| 6456 | |
| 6457 | s->be = s->fe; |
| 6458 | s->req->analysers = s->listener->analysers; |
| 6459 | s->logs.logwait = s->fe->to_log; |
| 6460 | s->srv = s->prev_srv = s->srv_conn = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 6461 | /* re-init store persistence */ |
| 6462 | s->store_count = 0; |
| 6463 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6464 | s->pend_pos = NULL; |
| 6465 | s->conn_retries = s->be->conn_retries; |
| 6466 | |
| 6467 | s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */ |
| 6468 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 6469 | /* We must trim any excess data from the response buffer, because we |
| 6470 | * may have blocked an invalid response from a server that we don't |
| 6471 | * want to accidentely forward once we disable the analysers, nor do |
| 6472 | * we want those data to come along with next response. A typical |
| 6473 | * example of such data would be from a buggy server responding to |
| 6474 | * a HEAD with some data, or sending more than the advertised |
| 6475 | * content-length. |
| 6476 | */ |
| 6477 | if (unlikely(s->rep->l > s->rep->send_max)) { |
| 6478 | s->rep->l = s->rep->send_max; |
| 6479 | s->rep->r = s->rep->w + s->rep->l; |
| 6480 | if (s->rep->r >= s->rep->data + s->rep->size) |
| 6481 | s->rep->r -= s->rep->size; |
| 6482 | } |
| 6483 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6484 | s->req->rto = s->fe->timeout.client; |
| 6485 | s->req->wto = s->be->timeout.server; |
| 6486 | s->req->cto = s->be->timeout.connect; |
| 6487 | |
| 6488 | s->rep->rto = s->be->timeout.server; |
| 6489 | s->rep->wto = s->fe->timeout.client; |
| 6490 | s->rep->cto = TICK_ETERNITY; |
| 6491 | |
| 6492 | s->req->rex = TICK_ETERNITY; |
| 6493 | s->req->wex = TICK_ETERNITY; |
| 6494 | s->req->analyse_exp = TICK_ETERNITY; |
| 6495 | s->rep->rex = TICK_ETERNITY; |
| 6496 | s->rep->wex = TICK_ETERNITY; |
| 6497 | s->rep->analyse_exp = TICK_ETERNITY; |
| 6498 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6499 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6500 | /************************************************************************/ |
| 6501 | /* The code below is dedicated to ACL parsing and matching */ |
| 6502 | /************************************************************************/ |
| 6503 | |
| 6504 | |
| 6505 | |
| 6506 | |
| 6507 | /* 1. Check on METHOD |
| 6508 | * We use the pre-parsed method if it is known, and store its number as an |
| 6509 | * integer. If it is unknown, we use the pointer and the length. |
| 6510 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6511 | 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] | 6512 | { |
| 6513 | int len, meth; |
| 6514 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6515 | len = strlen(*text); |
| 6516 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6517 | |
| 6518 | pattern->val.i = meth; |
| 6519 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6520 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6521 | if (!pattern->ptr.str) |
| 6522 | return 0; |
| 6523 | pattern->len = len; |
| 6524 | } |
| 6525 | return 1; |
| 6526 | } |
| 6527 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6528 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6529 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6530 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6531 | { |
| 6532 | int meth; |
| 6533 | struct http_txn *txn = l7; |
| 6534 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6535 | if (!txn) |
| 6536 | return 0; |
| 6537 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6538 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6539 | return 0; |
| 6540 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6541 | meth = txn->meth; |
| 6542 | test->i = meth; |
| 6543 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6544 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6545 | /* ensure the indexes are not affected */ |
| 6546 | return 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6547 | test->len = txn->req.sl.rq.m_l; |
| 6548 | test->ptr = txn->req.sol; |
| 6549 | } |
| 6550 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 6551 | return 1; |
| 6552 | } |
| 6553 | |
| 6554 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 6555 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 6556 | int icase; |
| 6557 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6558 | if (test->i != pattern->val.i) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6559 | return ACL_PAT_FAIL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6560 | |
| 6561 | if (test->i != HTTP_METH_OTHER) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6562 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6563 | |
| 6564 | /* Other method, we must compare the strings */ |
| 6565 | if (pattern->len != test->len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6566 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 6567 | |
| 6568 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 6569 | if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) || |
| 6570 | (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6571 | return ACL_PAT_FAIL; |
| 6572 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6573 | } |
| 6574 | |
| 6575 | /* 2. Check on Request/Status Version |
| 6576 | * We simply compare strings here. |
| 6577 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6578 | 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] | 6579 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6580 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6581 | if (!pattern->ptr.str) |
| 6582 | return 0; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6583 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6584 | return 1; |
| 6585 | } |
| 6586 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6587 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6588 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6589 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6590 | { |
| 6591 | struct http_txn *txn = l7; |
| 6592 | char *ptr; |
| 6593 | int len; |
| 6594 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6595 | if (!txn) |
| 6596 | return 0; |
| 6597 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6598 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6599 | return 0; |
| 6600 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6601 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6602 | ptr = txn->req.sol + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6603 | |
| 6604 | while ((len-- > 0) && (*ptr++ != '/')); |
| 6605 | if (len <= 0) |
| 6606 | return 0; |
| 6607 | |
| 6608 | test->ptr = ptr; |
| 6609 | test->len = len; |
| 6610 | |
| 6611 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 6612 | return 1; |
| 6613 | } |
| 6614 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6615 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6616 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6617 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6618 | { |
| 6619 | struct http_txn *txn = l7; |
| 6620 | char *ptr; |
| 6621 | int len; |
| 6622 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6623 | if (!txn) |
| 6624 | return 0; |
| 6625 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6626 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6627 | return 0; |
| 6628 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6629 | len = txn->rsp.sl.st.v_l; |
| 6630 | ptr = txn->rsp.sol; |
| 6631 | |
| 6632 | while ((len-- > 0) && (*ptr++ != '/')); |
| 6633 | if (len <= 0) |
| 6634 | return 0; |
| 6635 | |
| 6636 | test->ptr = ptr; |
| 6637 | test->len = len; |
| 6638 | |
| 6639 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 6640 | return 1; |
| 6641 | } |
| 6642 | |
| 6643 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6644 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6645 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6646 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6647 | { |
| 6648 | struct http_txn *txn = l7; |
| 6649 | char *ptr; |
| 6650 | int len; |
| 6651 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6652 | if (!txn) |
| 6653 | return 0; |
| 6654 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6655 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6656 | return 0; |
| 6657 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6658 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6659 | ptr = txn->rsp.sol + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6660 | |
| 6661 | test->i = __strl2ui(ptr, len); |
| 6662 | test->flags = ACL_TEST_F_VOL_1ST; |
| 6663 | return 1; |
| 6664 | } |
| 6665 | |
| 6666 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6667 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6668 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6669 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6670 | { |
| 6671 | struct http_txn *txn = l7; |
| 6672 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6673 | if (!txn) |
| 6674 | return 0; |
| 6675 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6676 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6677 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6678 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6679 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6680 | /* ensure the indexes are not affected */ |
| 6681 | return 0; |
| 6682 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6683 | test->len = txn->req.sl.rq.u_l; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6684 | test->ptr = txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6685 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 6686 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 6687 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6688 | return 1; |
| 6689 | } |
| 6690 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6691 | static int |
| 6692 | acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6693 | struct acl_expr *expr, struct acl_test *test) |
| 6694 | { |
| 6695 | struct http_txn *txn = l7; |
| 6696 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6697 | if (!txn) |
| 6698 | return 0; |
| 6699 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6700 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6701 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6702 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6703 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6704 | /* ensure the indexes are not affected */ |
| 6705 | return 0; |
| 6706 | |
| 6707 | /* Parse HTTP request */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6708 | 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] | 6709 | test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr; |
| 6710 | test->i = AF_INET; |
| 6711 | |
| 6712 | /* |
| 6713 | * If we are parsing url in frontend space, we prepare backend stage |
| 6714 | * to not parse again the same url ! optimization lazyness... |
| 6715 | */ |
| 6716 | if (px->options & PR_O_HTTP_PROXY) |
| 6717 | l4->flags |= SN_ADDR_SET; |
| 6718 | |
| 6719 | test->flags = ACL_TEST_F_READ_ONLY; |
| 6720 | return 1; |
| 6721 | } |
| 6722 | |
| 6723 | static int |
| 6724 | acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6725 | struct acl_expr *expr, struct acl_test *test) |
| 6726 | { |
| 6727 | struct http_txn *txn = l7; |
| 6728 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6729 | if (!txn) |
| 6730 | return 0; |
| 6731 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6732 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6733 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6734 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6735 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6736 | /* ensure the indexes are not affected */ |
| 6737 | return 0; |
| 6738 | |
| 6739 | /* Same optimization as url_ip */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6740 | 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] | 6741 | test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port); |
| 6742 | |
| 6743 | if (px->options & PR_O_HTTP_PROXY) |
| 6744 | l4->flags |= SN_ADDR_SET; |
| 6745 | |
| 6746 | test->flags = ACL_TEST_F_READ_ONLY; |
| 6747 | return 1; |
| 6748 | } |
| 6749 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6750 | /* 5. Check on HTTP header. A pointer to the beginning of the value is returned. |
| 6751 | * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr(). |
| 6752 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6753 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6754 | 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] | 6755 | struct acl_expr *expr, struct acl_test *test) |
| 6756 | { |
| 6757 | struct http_txn *txn = l7; |
| 6758 | struct hdr_idx *idx = &txn->hdr_idx; |
| 6759 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6760 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6761 | if (!txn) |
| 6762 | return 0; |
| 6763 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6764 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 6765 | /* search for header from the beginning */ |
| 6766 | ctx->idx = 0; |
| 6767 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6768 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 6769 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 6770 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6771 | test->len = ctx->vlen; |
| 6772 | test->ptr = (char *)ctx->line + ctx->val; |
| 6773 | return 1; |
| 6774 | } |
| 6775 | |
| 6776 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 6777 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6778 | return 0; |
| 6779 | } |
| 6780 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6781 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6782 | acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6783 | struct acl_expr *expr, struct acl_test *test) |
| 6784 | { |
| 6785 | struct http_txn *txn = l7; |
| 6786 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6787 | if (!txn) |
| 6788 | return 0; |
| 6789 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6790 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6791 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6792 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6793 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6794 | /* ensure the indexes are not affected */ |
| 6795 | return 0; |
| 6796 | |
| 6797 | return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test); |
| 6798 | } |
| 6799 | |
| 6800 | static int |
| 6801 | acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6802 | struct acl_expr *expr, struct acl_test *test) |
| 6803 | { |
| 6804 | struct http_txn *txn = l7; |
| 6805 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6806 | if (!txn) |
| 6807 | return 0; |
| 6808 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6809 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6810 | return 0; |
| 6811 | |
| 6812 | return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test); |
| 6813 | } |
| 6814 | |
| 6815 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 6816 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 6817 | */ |
| 6818 | static int |
| 6819 | 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] | 6820 | struct acl_expr *expr, struct acl_test *test) |
| 6821 | { |
| 6822 | struct http_txn *txn = l7; |
| 6823 | struct hdr_idx *idx = &txn->hdr_idx; |
| 6824 | struct hdr_ctx ctx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6825 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6826 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6827 | if (!txn) |
| 6828 | return 0; |
| 6829 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6830 | ctx.idx = 0; |
| 6831 | cnt = 0; |
| 6832 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx)) |
| 6833 | cnt++; |
| 6834 | |
| 6835 | test->i = cnt; |
| 6836 | test->flags = ACL_TEST_F_VOL_HDR; |
| 6837 | return 1; |
| 6838 | } |
| 6839 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6840 | static int |
| 6841 | acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6842 | struct acl_expr *expr, struct acl_test *test) |
| 6843 | { |
| 6844 | struct http_txn *txn = l7; |
| 6845 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6846 | if (!txn) |
| 6847 | return 0; |
| 6848 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6849 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6850 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6851 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6852 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6853 | /* ensure the indexes are not affected */ |
| 6854 | return 0; |
| 6855 | |
| 6856 | return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test); |
| 6857 | } |
| 6858 | |
| 6859 | static int |
| 6860 | acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6861 | struct acl_expr *expr, struct acl_test *test) |
| 6862 | { |
| 6863 | struct http_txn *txn = l7; |
| 6864 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6865 | if (!txn) |
| 6866 | return 0; |
| 6867 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6868 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6869 | return 0; |
| 6870 | |
| 6871 | return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test); |
| 6872 | } |
| 6873 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6874 | /* 7. Check on HTTP header's integer value. The integer value is returned. |
| 6875 | * FIXME: the type is 'int', it may not be appropriate for everything. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6876 | * 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] | 6877 | */ |
| 6878 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6879 | 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] | 6880 | struct acl_expr *expr, struct acl_test *test) |
| 6881 | { |
| 6882 | struct http_txn *txn = l7; |
| 6883 | struct hdr_idx *idx = &txn->hdr_idx; |
| 6884 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6885 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6886 | if (!txn) |
| 6887 | return 0; |
| 6888 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6889 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 6890 | /* search for header from the beginning */ |
| 6891 | ctx->idx = 0; |
| 6892 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6893 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 6894 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 6895 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6896 | test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen); |
| 6897 | return 1; |
| 6898 | } |
| 6899 | |
| 6900 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 6901 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6902 | return 0; |
| 6903 | } |
| 6904 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6905 | static int |
| 6906 | acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6907 | struct acl_expr *expr, struct acl_test *test) |
| 6908 | { |
| 6909 | struct http_txn *txn = l7; |
| 6910 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6911 | if (!txn) |
| 6912 | return 0; |
| 6913 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6914 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6915 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6916 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6917 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6918 | /* ensure the indexes are not affected */ |
| 6919 | return 0; |
| 6920 | |
| 6921 | return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test); |
| 6922 | } |
| 6923 | |
| 6924 | static int |
| 6925 | acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6926 | struct acl_expr *expr, struct acl_test *test) |
| 6927 | { |
| 6928 | struct http_txn *txn = l7; |
| 6929 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6930 | if (!txn) |
| 6931 | return 0; |
| 6932 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6933 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6934 | return 0; |
| 6935 | |
| 6936 | return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test); |
| 6937 | } |
| 6938 | |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 6939 | /* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned. |
| 6940 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 6941 | */ |
| 6942 | static int |
| 6943 | acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol, |
| 6944 | struct acl_expr *expr, struct acl_test *test) |
| 6945 | { |
| 6946 | struct http_txn *txn = l7; |
| 6947 | struct hdr_idx *idx = &txn->hdr_idx; |
| 6948 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
| 6949 | |
| 6950 | if (!txn) |
| 6951 | return 0; |
| 6952 | |
| 6953 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 6954 | /* search for header from the beginning */ |
| 6955 | ctx->idx = 0; |
| 6956 | |
| 6957 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 6958 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 6959 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6960 | /* Same optimization as url_ip */ |
| 6961 | memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr)); |
| 6962 | url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr); |
| 6963 | test->ptr = (void *)&l4->srv_addr.sin_addr; |
| 6964 | test->i = AF_INET; |
| 6965 | return 1; |
| 6966 | } |
| 6967 | |
| 6968 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 6969 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6970 | return 0; |
| 6971 | } |
| 6972 | |
| 6973 | static int |
| 6974 | acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6975 | struct acl_expr *expr, struct acl_test *test) |
| 6976 | { |
| 6977 | struct http_txn *txn = l7; |
| 6978 | |
| 6979 | if (!txn) |
| 6980 | return 0; |
| 6981 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6982 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 6983 | return 0; |
| 6984 | |
| 6985 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6986 | /* ensure the indexes are not affected */ |
| 6987 | return 0; |
| 6988 | |
| 6989 | return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test); |
| 6990 | } |
| 6991 | |
| 6992 | static int |
| 6993 | acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6994 | struct acl_expr *expr, struct acl_test *test) |
| 6995 | { |
| 6996 | struct http_txn *txn = l7; |
| 6997 | |
| 6998 | if (!txn) |
| 6999 | return 0; |
| 7000 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7001 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 7002 | return 0; |
| 7003 | |
| 7004 | return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test); |
| 7005 | } |
| 7006 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 7007 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 7008 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 7009 | */ |
| 7010 | static int |
| 7011 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7012 | struct acl_expr *expr, struct acl_test *test) |
| 7013 | { |
| 7014 | struct http_txn *txn = l7; |
| 7015 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7016 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7017 | if (!txn) |
| 7018 | return 0; |
| 7019 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7020 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7021 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7022 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7023 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7024 | /* ensure the indexes are not affected */ |
| 7025 | return 0; |
| 7026 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7027 | 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] | 7028 | ptr = http_get_path(txn); |
| 7029 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 7030 | return 0; |
| 7031 | |
| 7032 | /* OK, we got the '/' ! */ |
| 7033 | test->ptr = ptr; |
| 7034 | |
| 7035 | while (ptr < end && *ptr != '?') |
| 7036 | ptr++; |
| 7037 | |
| 7038 | test->len = ptr - test->ptr; |
| 7039 | |
| 7040 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 7041 | test->flags = ACL_TEST_F_VOL_1ST; |
| 7042 | return 1; |
| 7043 | } |
| 7044 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 7045 | static int |
| 7046 | acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir, |
| 7047 | struct acl_expr *expr, struct acl_test *test) |
| 7048 | { |
| 7049 | struct buffer *req = s->req; |
| 7050 | struct http_txn *txn = &s->txn; |
| 7051 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 7052 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 7053 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 7054 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 7055 | */ |
| 7056 | |
| 7057 | if (!s || !req) |
| 7058 | return 0; |
| 7059 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7060 | if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 7061 | /* Already decoded as OK */ |
| 7062 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 7063 | return 1; |
| 7064 | } |
| 7065 | |
| 7066 | /* Try to decode HTTP request */ |
| 7067 | if (likely(req->lr < req->r)) |
| 7068 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
| 7069 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7070 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 7071 | if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) { |
| 7072 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 7073 | return 1; |
| 7074 | } |
| 7075 | /* wait for final state */ |
| 7076 | test->flags |= ACL_TEST_F_MAY_CHANGE; |
| 7077 | return 0; |
| 7078 | } |
| 7079 | |
| 7080 | /* OK we got a valid HTTP request. We have some minor preparation to |
| 7081 | * perform so that further checks can rely on HTTP tests. |
| 7082 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7083 | txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 7084 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 7085 | s->flags |= SN_REDIRECTABLE; |
| 7086 | |
| 7087 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) { |
| 7088 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 7089 | return 1; |
| 7090 | } |
| 7091 | |
| 7092 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 7093 | return 1; |
| 7094 | } |
| 7095 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7096 | |
| 7097 | /************************************************************************/ |
| 7098 | /* All supported keywords must be declared here. */ |
| 7099 | /************************************************************************/ |
| 7100 | |
| 7101 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 7102 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 7103 | { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT }, |
| 7104 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 7105 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT }, |
| 7106 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 7107 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 7108 | { "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] | 7109 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 7110 | { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 7111 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 7112 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 7113 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 7114 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 7115 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 7116 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 7117 | { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE }, |
| 7118 | { "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] | 7119 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 7120 | /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */ |
| 7121 | { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 7122 | { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 7123 | { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 7124 | { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 7125 | { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 7126 | { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 7127 | { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 7128 | { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
| 7129 | { "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] | 7130 | { "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] | 7131 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 7132 | { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 7133 | { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE }, |
| 7134 | { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE }, |
| 7135 | { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE }, |
| 7136 | { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE }, |
| 7137 | { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE }, |
| 7138 | { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE }, |
| 7139 | { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
| 7140 | { "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] | 7141 | { "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] | 7142 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 7143 | { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 7144 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 7145 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 7146 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 7147 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 7148 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 7149 | { "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] | 7150 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 7151 | { NULL, NULL, NULL, NULL }, |
| 7152 | |
| 7153 | #if 0 |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7154 | { "line", acl_parse_str, acl_fetch_line, acl_match_str }, |
| 7155 | { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg }, |
| 7156 | { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg }, |
| 7157 | { "line_end", acl_parse_str, acl_fetch_line, acl_match_end }, |
| 7158 | { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub }, |
| 7159 | { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir }, |
| 7160 | { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom }, |
| 7161 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7162 | { "cook", acl_parse_str, acl_fetch_cook, acl_match_str }, |
| 7163 | { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg }, |
| 7164 | { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg }, |
| 7165 | { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end }, |
| 7166 | { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub }, |
| 7167 | { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir }, |
| 7168 | { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom }, |
| 7169 | { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst }, |
| 7170 | |
| 7171 | { "auth_user", acl_parse_str, acl_fetch_user, acl_match_str }, |
| 7172 | { "auth_regex", acl_parse_reg, acl_fetch_user, acl_match_reg }, |
| 7173 | { "auth_clear", acl_parse_str, acl_fetch_auth, acl_match_str }, |
| 7174 | { "auth_md5", acl_parse_str, acl_fetch_auth, acl_match_md5 }, |
| 7175 | { NULL, NULL, NULL, NULL }, |
| 7176 | #endif |
| 7177 | }}; |
| 7178 | |
| 7179 | |
| 7180 | __attribute__((constructor)) |
| 7181 | static void __http_protocol_init(void) |
| 7182 | { |
| 7183 | acl_register_keywords(&acl_kws); |
| 7184 | } |
| 7185 | |
| 7186 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7187 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 7188 | * Local variables: |
| 7189 | * c-indent-level: 8 |
| 7190 | * c-basic-offset: 8 |
| 7191 | * End: |
| 7192 | */ |