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> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 27 | #include <common/base64.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 28 | #include <common/compat.h> |
| 29 | #include <common/config.h> |
Willy Tarreau | a4cd1f5 | 2006-12-16 19:57:26 +0100 | [diff] [blame] | 30 | #include <common/debug.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 31 | #include <common/memory.h> |
| 32 | #include <common/mini-clist.h> |
| 33 | #include <common/standard.h> |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 34 | #include <common/ticks.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 35 | #include <common/time.h> |
| 36 | #include <common/uri_auth.h> |
| 37 | #include <common/version.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 38 | |
| 39 | #include <types/capture.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 40 | #include <types/global.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 41 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 42 | #include <proto/acl.h> |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 43 | #include <proto/auth.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 44 | #include <proto/backend.h> |
| 45 | #include <proto/buffers.h> |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 46 | #include <proto/checks.h> |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 47 | #include <proto/client.h> |
Willy Tarreau | 9186126 | 2007-10-17 17:06:05 +0200 | [diff] [blame] | 48 | #include <proto/dumpstats.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 49 | #include <proto/fd.h> |
| 50 | #include <proto/log.h> |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 51 | #include <proto/hdr_idx.h> |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 52 | #include <proto/proto_tcp.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 53 | #include <proto/proto_http.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 54 | #include <proto/proxy.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 55 | #include <proto/queue.h> |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 56 | #include <proto/server.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 57 | #include <proto/session.h> |
Willy Tarreau | cff6411 | 2008-11-03 06:26:53 +0100 | [diff] [blame] | 58 | #include <proto/stream_interface.h> |
Willy Tarreau | 2d21279 | 2008-08-27 21:41:35 +0200 | [diff] [blame] | 59 | #include <proto/stream_sock.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 60 | #include <proto/task.h> |
| 61 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 62 | const char HTTP_100[] = |
| 63 | "HTTP/1.1 100 Continue\r\n\r\n"; |
| 64 | |
| 65 | const struct chunk http_100_chunk = { |
| 66 | .str = (char *)&HTTP_100, |
| 67 | .len = sizeof(HTTP_100)-1 |
| 68 | }; |
| 69 | |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 70 | /* This is used by remote monitoring */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 71 | const char HTTP_200[] = |
Willy Tarreau | 1c47f85 | 2006-07-09 08:22:27 +0200 | [diff] [blame] | 72 | "HTTP/1.0 200 OK\r\n" |
| 73 | "Cache-Control: no-cache\r\n" |
| 74 | "Connection: close\r\n" |
| 75 | "Content-Type: text/html\r\n" |
| 76 | "\r\n" |
| 77 | "<html><body><h1>200 OK</h1>\nHAProxy: service ready.\n</body></html>\n"; |
| 78 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 79 | const struct chunk http_200_chunk = { |
| 80 | .str = (char *)&HTTP_200, |
| 81 | .len = sizeof(HTTP_200)-1 |
| 82 | }; |
| 83 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 84 | /* Warning: no "connection" header is provided with the 3xx messages below */ |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 85 | const char *HTTP_301 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 86 | "HTTP/1.1 301 Moved Permanently\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 87 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 88 | "Content-length: 0\r\n" |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 89 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 90 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 91 | const char *HTTP_302 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 92 | "HTTP/1.1 302 Found\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 93 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 94 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 95 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 96 | |
| 97 | /* same as 302 except that the browser MUST retry with the GET method */ |
| 98 | const char *HTTP_303 = |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 99 | "HTTP/1.1 303 See Other\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 100 | "Cache-Control: no-cache\r\n" |
Willy Tarreau | bc5aa19 | 2010-01-03 15:09:36 +0100 | [diff] [blame] | 101 | "Content-length: 0\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 102 | "Location: "; /* not terminated since it will be concatenated with the URL */ |
| 103 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 104 | /* Warning: this one is an sprintf() fmt string, with <realm> as its only argument */ |
| 105 | const char *HTTP_401_fmt = |
| 106 | "HTTP/1.0 401 Unauthorized\r\n" |
| 107 | "Cache-Control: no-cache\r\n" |
| 108 | "Connection: close\r\n" |
Willy Tarreau | 791d66d | 2006-07-08 16:53:38 +0200 | [diff] [blame] | 109 | "Content-Type: text/html\r\n" |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 110 | "WWW-Authenticate: Basic realm=\"%s\"\r\n" |
| 111 | "\r\n" |
| 112 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 113 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 114 | const char *HTTP_407_fmt = |
| 115 | "HTTP/1.0 407 Unauthorized\r\n" |
| 116 | "Cache-Control: no-cache\r\n" |
| 117 | "Connection: close\r\n" |
| 118 | "Content-Type: text/html\r\n" |
| 119 | "Proxy-Authenticate: Basic realm=\"%s\"\r\n" |
| 120 | "\r\n" |
| 121 | "<html><body><h1>401 Unauthorized</h1>\nYou need a valid user and password to access this content.\n</body></html>\n"; |
| 122 | |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 123 | |
| 124 | const int http_err_codes[HTTP_ERR_SIZE] = { |
| 125 | [HTTP_ERR_400] = 400, |
| 126 | [HTTP_ERR_403] = 403, |
| 127 | [HTTP_ERR_408] = 408, |
| 128 | [HTTP_ERR_500] = 500, |
| 129 | [HTTP_ERR_502] = 502, |
| 130 | [HTTP_ERR_503] = 503, |
| 131 | [HTTP_ERR_504] = 504, |
| 132 | }; |
| 133 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 134 | static const char *http_err_msgs[HTTP_ERR_SIZE] = { |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 135 | [HTTP_ERR_400] = |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 136 | "HTTP/1.0 400 Bad request\r\n" |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 137 | "Cache-Control: no-cache\r\n" |
| 138 | "Connection: close\r\n" |
| 139 | "Content-Type: text/html\r\n" |
| 140 | "\r\n" |
| 141 | "<html><body><h1>400 Bad request</h1>\nYour browser sent an invalid request.\n</body></html>\n", |
| 142 | |
| 143 | [HTTP_ERR_403] = |
| 144 | "HTTP/1.0 403 Forbidden\r\n" |
| 145 | "Cache-Control: no-cache\r\n" |
| 146 | "Connection: close\r\n" |
| 147 | "Content-Type: text/html\r\n" |
| 148 | "\r\n" |
| 149 | "<html><body><h1>403 Forbidden</h1>\nRequest forbidden by administrative rules.\n</body></html>\n", |
| 150 | |
| 151 | [HTTP_ERR_408] = |
| 152 | "HTTP/1.0 408 Request Time-out\r\n" |
| 153 | "Cache-Control: no-cache\r\n" |
| 154 | "Connection: close\r\n" |
| 155 | "Content-Type: text/html\r\n" |
| 156 | "\r\n" |
| 157 | "<html><body><h1>408 Request Time-out</h1>\nYour browser didn't send a complete request in time.\n</body></html>\n", |
| 158 | |
| 159 | [HTTP_ERR_500] = |
| 160 | "HTTP/1.0 500 Server Error\r\n" |
| 161 | "Cache-Control: no-cache\r\n" |
| 162 | "Connection: close\r\n" |
| 163 | "Content-Type: text/html\r\n" |
| 164 | "\r\n" |
| 165 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n", |
| 166 | |
| 167 | [HTTP_ERR_502] = |
| 168 | "HTTP/1.0 502 Bad Gateway\r\n" |
| 169 | "Cache-Control: no-cache\r\n" |
| 170 | "Connection: close\r\n" |
| 171 | "Content-Type: text/html\r\n" |
| 172 | "\r\n" |
| 173 | "<html><body><h1>502 Bad Gateway</h1>\nThe server returned an invalid or incomplete response.\n</body></html>\n", |
| 174 | |
| 175 | [HTTP_ERR_503] = |
| 176 | "HTTP/1.0 503 Service Unavailable\r\n" |
| 177 | "Cache-Control: no-cache\r\n" |
| 178 | "Connection: close\r\n" |
| 179 | "Content-Type: text/html\r\n" |
| 180 | "\r\n" |
| 181 | "<html><body><h1>503 Service Unavailable</h1>\nNo server is available to handle this request.\n</body></html>\n", |
| 182 | |
| 183 | [HTTP_ERR_504] = |
| 184 | "HTTP/1.0 504 Gateway Time-out\r\n" |
| 185 | "Cache-Control: no-cache\r\n" |
| 186 | "Connection: close\r\n" |
| 187 | "Content-Type: text/html\r\n" |
| 188 | "\r\n" |
| 189 | "<html><body><h1>504 Gateway Time-out</h1>\nThe server didn't respond in time.\n</body></html>\n", |
| 190 | |
| 191 | }; |
| 192 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 193 | /* We must put the messages here since GCC cannot initialize consts depending |
| 194 | * on strlen(). |
| 195 | */ |
| 196 | struct chunk http_err_chunks[HTTP_ERR_SIZE]; |
| 197 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 198 | #define FD_SETS_ARE_BITFIELDS |
| 199 | #ifdef FD_SETS_ARE_BITFIELDS |
| 200 | /* |
| 201 | * This map is used with all the FD_* macros to check whether a particular bit |
| 202 | * is set or not. Each bit represents an ACSII code. FD_SET() sets those bytes |
| 203 | * which should be encoded. When FD_ISSET() returns non-zero, it means that the |
| 204 | * byte should be encoded. Be careful to always pass bytes from 0 to 255 |
| 205 | * exclusively to the macros. |
| 206 | */ |
| 207 | fd_set hdr_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 208 | fd_set url_encode_map[(sizeof(fd_set) > (256/8)) ? 1 : ((256/8) / sizeof(fd_set))]; |
| 209 | |
| 210 | #else |
| 211 | #error "Check if your OS uses bitfields for fd_sets" |
| 212 | #endif |
| 213 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 214 | void init_proto_http() |
| 215 | { |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 216 | int i; |
| 217 | char *tmp; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 218 | int msg; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 219 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 220 | for (msg = 0; msg < HTTP_ERR_SIZE; msg++) { |
| 221 | if (!http_err_msgs[msg]) { |
| 222 | Alert("Internal error: no message defined for HTTP return code %d. Aborting.\n", msg); |
| 223 | abort(); |
| 224 | } |
| 225 | |
| 226 | http_err_chunks[msg].str = (char *)http_err_msgs[msg]; |
| 227 | http_err_chunks[msg].len = strlen(http_err_msgs[msg]); |
| 228 | } |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 229 | |
| 230 | /* initialize the log header encoding map : '{|}"#' should be encoded with |
| 231 | * '#' as prefix, as well as non-printable characters ( <32 or >= 127 ). |
| 232 | * URL encoding only requires '"', '#' to be encoded as well as non- |
| 233 | * printable characters above. |
| 234 | */ |
| 235 | memset(hdr_encode_map, 0, sizeof(hdr_encode_map)); |
| 236 | memset(url_encode_map, 0, sizeof(url_encode_map)); |
| 237 | for (i = 0; i < 32; i++) { |
| 238 | FD_SET(i, hdr_encode_map); |
| 239 | FD_SET(i, url_encode_map); |
| 240 | } |
| 241 | for (i = 127; i < 256; i++) { |
| 242 | FD_SET(i, hdr_encode_map); |
| 243 | FD_SET(i, url_encode_map); |
| 244 | } |
| 245 | |
| 246 | tmp = "\"#{|}"; |
| 247 | while (*tmp) { |
| 248 | FD_SET(*tmp, hdr_encode_map); |
| 249 | tmp++; |
| 250 | } |
| 251 | |
| 252 | tmp = "\"#"; |
| 253 | while (*tmp) { |
| 254 | FD_SET(*tmp, url_encode_map); |
| 255 | tmp++; |
| 256 | } |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 257 | |
| 258 | /* memory allocations */ |
| 259 | pool2_requri = create_pool("requri", REQURI_LEN, MEM_F_SHARED); |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 260 | pool2_capture = create_pool("capture", CAPTURE_LEN, MEM_F_SHARED); |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 261 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 262 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 263 | /* |
| 264 | * We have 26 list of methods (1 per first letter), each of which can have |
| 265 | * up to 3 entries (2 valid, 1 null). |
| 266 | */ |
| 267 | struct http_method_desc { |
| 268 | http_meth_t meth; |
| 269 | int len; |
| 270 | const char text[8]; |
| 271 | }; |
| 272 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 273 | const struct http_method_desc http_methods[26][3] = { |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 274 | ['C' - 'A'] = { |
| 275 | [0] = { .meth = HTTP_METH_CONNECT , .len=7, .text="CONNECT" }, |
| 276 | }, |
| 277 | ['D' - 'A'] = { |
| 278 | [0] = { .meth = HTTP_METH_DELETE , .len=6, .text="DELETE" }, |
| 279 | }, |
| 280 | ['G' - 'A'] = { |
| 281 | [0] = { .meth = HTTP_METH_GET , .len=3, .text="GET" }, |
| 282 | }, |
| 283 | ['H' - 'A'] = { |
| 284 | [0] = { .meth = HTTP_METH_HEAD , .len=4, .text="HEAD" }, |
| 285 | }, |
| 286 | ['P' - 'A'] = { |
| 287 | [0] = { .meth = HTTP_METH_POST , .len=4, .text="POST" }, |
| 288 | [1] = { .meth = HTTP_METH_PUT , .len=3, .text="PUT" }, |
| 289 | }, |
| 290 | ['T' - 'A'] = { |
| 291 | [0] = { .meth = HTTP_METH_TRACE , .len=5, .text="TRACE" }, |
| 292 | }, |
| 293 | /* rest is empty like this : |
| 294 | * [1] = { .meth = HTTP_METH_NONE , .len=0, .text="" }, |
| 295 | */ |
| 296 | }; |
| 297 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 298 | /* 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] | 299 | * 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] | 300 | * RFC2616 for those chars. |
| 301 | */ |
| 302 | |
| 303 | const char http_is_spht[256] = { |
| 304 | [' '] = 1, ['\t'] = 1, |
| 305 | }; |
| 306 | |
| 307 | const char http_is_crlf[256] = { |
| 308 | ['\r'] = 1, ['\n'] = 1, |
| 309 | }; |
| 310 | |
| 311 | const char http_is_lws[256] = { |
| 312 | [' '] = 1, ['\t'] = 1, |
| 313 | ['\r'] = 1, ['\n'] = 1, |
| 314 | }; |
| 315 | |
| 316 | const char http_is_sep[256] = { |
| 317 | ['('] = 1, [')'] = 1, ['<'] = 1, ['>'] = 1, |
| 318 | ['@'] = 1, [','] = 1, [';'] = 1, [':'] = 1, |
| 319 | ['"'] = 1, ['/'] = 1, ['['] = 1, [']'] = 1, |
| 320 | ['{'] = 1, ['}'] = 1, ['?'] = 1, ['='] = 1, |
| 321 | [' '] = 1, ['\t'] = 1, ['\\'] = 1, |
| 322 | }; |
| 323 | |
| 324 | const char http_is_ctl[256] = { |
| 325 | [0 ... 31] = 1, |
| 326 | [127] = 1, |
| 327 | }; |
| 328 | |
| 329 | /* |
| 330 | * A token is any ASCII char that is neither a separator nor a CTL char. |
| 331 | * Do not overwrite values in assignment since gcc-2.95 will not handle |
| 332 | * them correctly. Instead, define every non-CTL char's status. |
| 333 | */ |
| 334 | const char http_is_token[256] = { |
| 335 | [' '] = 0, ['!'] = 1, ['"'] = 0, ['#'] = 1, |
| 336 | ['$'] = 1, ['%'] = 1, ['&'] = 1, ['\''] = 1, |
| 337 | ['('] = 0, [')'] = 0, ['*'] = 1, ['+'] = 1, |
| 338 | [','] = 0, ['-'] = 1, ['.'] = 1, ['/'] = 0, |
| 339 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, |
| 340 | ['4'] = 1, ['5'] = 1, ['6'] = 1, ['7'] = 1, |
| 341 | ['8'] = 1, ['9'] = 1, [':'] = 0, [';'] = 0, |
| 342 | ['<'] = 0, ['='] = 0, ['>'] = 0, ['?'] = 0, |
| 343 | ['@'] = 0, ['A'] = 1, ['B'] = 1, ['C'] = 1, |
| 344 | ['D'] = 1, ['E'] = 1, ['F'] = 1, ['G'] = 1, |
| 345 | ['H'] = 1, ['I'] = 1, ['J'] = 1, ['K'] = 1, |
| 346 | ['L'] = 1, ['M'] = 1, ['N'] = 1, ['O'] = 1, |
| 347 | ['P'] = 1, ['Q'] = 1, ['R'] = 1, ['S'] = 1, |
| 348 | ['T'] = 1, ['U'] = 1, ['V'] = 1, ['W'] = 1, |
| 349 | ['X'] = 1, ['Y'] = 1, ['Z'] = 1, ['['] = 0, |
| 350 | ['\\'] = 0, [']'] = 0, ['^'] = 1, ['_'] = 1, |
| 351 | ['`'] = 1, ['a'] = 1, ['b'] = 1, ['c'] = 1, |
| 352 | ['d'] = 1, ['e'] = 1, ['f'] = 1, ['g'] = 1, |
| 353 | ['h'] = 1, ['i'] = 1, ['j'] = 1, ['k'] = 1, |
| 354 | ['l'] = 1, ['m'] = 1, ['n'] = 1, ['o'] = 1, |
| 355 | ['p'] = 1, ['q'] = 1, ['r'] = 1, ['s'] = 1, |
| 356 | ['t'] = 1, ['u'] = 1, ['v'] = 1, ['w'] = 1, |
| 357 | ['x'] = 1, ['y'] = 1, ['z'] = 1, ['{'] = 0, |
| 358 | ['|'] = 1, ['}'] = 0, ['~'] = 1, |
| 359 | }; |
| 360 | |
| 361 | |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 362 | /* |
| 363 | * An http ver_token is any ASCII which can be found in an HTTP version, |
| 364 | * which includes 'H', 'T', 'P', '/', '.' and any digit. |
| 365 | */ |
| 366 | const char http_is_ver_token[256] = { |
| 367 | ['.'] = 1, ['/'] = 1, |
| 368 | ['0'] = 1, ['1'] = 1, ['2'] = 1, ['3'] = 1, ['4'] = 1, |
| 369 | ['5'] = 1, ['6'] = 1, ['7'] = 1, ['8'] = 1, ['9'] = 1, |
| 370 | ['H'] = 1, ['P'] = 1, ['T'] = 1, |
| 371 | }; |
| 372 | |
| 373 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 374 | /* |
Willy Tarreau | e988a79 | 2010-01-04 21:13:14 +0100 | [diff] [blame] | 375 | * Silent debug that outputs only in strace, using fd #-1. Trash is modified. |
| 376 | */ |
| 377 | #if defined(DEBUG_FSM) |
| 378 | static void http_silent_debug(int line, struct session *s) |
| 379 | { |
| 380 | int size = 0; |
| 381 | size += snprintf(trash + size, sizeof(trash) - size, |
| 382 | "[%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", |
| 383 | line, |
| 384 | s->si[0].state, s->si[0].fd, s->txn.req.msg_state, s->req->flags, s->req->analysers, |
| 385 | 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); |
| 386 | write(-1, trash, size); |
| 387 | size = 0; |
| 388 | size += snprintf(trash + size, sizeof(trash) - size, |
| 389 | " %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", |
| 390 | line, |
| 391 | s->si[1].state, s->si[1].fd, s->txn.rsp.msg_state, s->rep->flags, s->rep->analysers, |
| 392 | 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); |
| 393 | |
| 394 | write(-1, trash, size); |
| 395 | } |
| 396 | #else |
| 397 | #define http_silent_debug(l,s) do { } while (0) |
| 398 | #endif |
| 399 | |
| 400 | /* |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 401 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 402 | * CRLF. Text length is measured first, so it cannot be NULL. |
| 403 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 404 | * of headers is automatically adjusted. The number of bytes added is returned |
| 405 | * on success, otherwise <0 is returned indicating an error. |
| 406 | */ |
| 407 | int http_header_add_tail(struct buffer *b, struct http_msg *msg, |
| 408 | struct hdr_idx *hdr_idx, const char *text) |
| 409 | { |
| 410 | int bytes, len; |
| 411 | |
| 412 | len = strlen(text); |
| 413 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 414 | if (!bytes) |
| 415 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 416 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 417 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 418 | } |
| 419 | |
| 420 | /* |
| 421 | * Adds a header and its CRLF at the tail of buffer <b>, just before the last |
| 422 | * CRLF. <len> bytes are copied, not counting the CRLF. If <text> is NULL, then |
| 423 | * the buffer is only opened and the space reserved, but nothing is copied. |
| 424 | * The header is also automatically added to the index <hdr_idx>, and the end |
| 425 | * of headers is automatically adjusted. The number of bytes added is returned |
| 426 | * on success, otherwise <0 is returned indicating an error. |
| 427 | */ |
| 428 | int http_header_add_tail2(struct buffer *b, struct http_msg *msg, |
| 429 | struct hdr_idx *hdr_idx, const char *text, int len) |
| 430 | { |
| 431 | int bytes; |
| 432 | |
| 433 | bytes = buffer_insert_line2(b, b->data + msg->eoh, text, len); |
| 434 | if (!bytes) |
| 435 | return -1; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 436 | http_msg_move_end(msg, bytes); |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 437 | return hdr_idx_add(len, 1, hdr_idx, hdr_idx->tail); |
| 438 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 439 | |
| 440 | /* |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 441 | * Checks if <hdr> is exactly <name> for <len> chars, and ends with a colon. |
| 442 | * If so, returns the position of the first non-space character relative to |
| 443 | * <hdr>, or <end>-<hdr> if not found before. If no value is found, it tries |
| 444 | * to return a pointer to the place after the first space. Returns 0 if the |
| 445 | * header name does not match. Checks are case-insensitive. |
| 446 | */ |
| 447 | int http_header_match2(const char *hdr, const char *end, |
| 448 | const char *name, int len) |
| 449 | { |
| 450 | const char *val; |
| 451 | |
| 452 | if (hdr + len >= end) |
| 453 | return 0; |
| 454 | if (hdr[len] != ':') |
| 455 | return 0; |
| 456 | if (strncasecmp(hdr, name, len) != 0) |
| 457 | return 0; |
| 458 | val = hdr + len + 1; |
| 459 | while (val < end && HTTP_IS_SPHT(*val)) |
| 460 | val++; |
| 461 | if ((val >= end) && (len + 2 <= end - hdr)) |
| 462 | return len + 2; /* we may replace starting from second space */ |
| 463 | return val - hdr; |
| 464 | } |
| 465 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 466 | /* Find the end of the header value contained between <s> and <e>. See RFC2616, |
| 467 | * par 2.2 for more information. Note that it requires a valid header to return |
| 468 | * a valid result. This works for headers defined as comma-separated lists. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 469 | */ |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 470 | char *find_hdr_value_end(char *s, const char *e) |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 471 | { |
| 472 | int quoted, qdpair; |
| 473 | |
| 474 | quoted = qdpair = 0; |
| 475 | for (; s < e; s++) { |
| 476 | if (qdpair) qdpair = 0; |
| 477 | else if (quoted && *s == '\\') qdpair = 1; |
| 478 | else if (quoted && *s == '"') quoted = 0; |
| 479 | else if (*s == '"') quoted = 1; |
| 480 | else if (*s == ',') return s; |
| 481 | } |
| 482 | return s; |
| 483 | } |
| 484 | |
| 485 | /* Find the first or next occurrence of header <name> in message buffer <sol> |
| 486 | * using headers index <idx>, and return it in the <ctx> structure. This |
| 487 | * structure holds everything necessary to use the header and find next |
| 488 | * occurrence. If its <idx> member is 0, the header is searched from the |
| 489 | * beginning. Otherwise, the next occurrence is returned. The function returns |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 490 | * 1 when it finds a value, and 0 when there is no more. It is designed to work |
| 491 | * with headers defined as comma-separated lists. As a special case, if ctx->val |
| 492 | * is NULL when searching for a new values of a header, the current header is |
| 493 | * rescanned. This allows rescanning after a header deletion. |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 494 | */ |
| 495 | int http_find_header2(const char *name, int len, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 496 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 497 | struct hdr_ctx *ctx) |
| 498 | { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 499 | char *eol, *sov; |
| 500 | int cur_idx, old_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 501 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 502 | cur_idx = ctx->idx; |
| 503 | if (cur_idx) { |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 504 | /* We have previously returned a value, let's search |
| 505 | * another one on the same line. |
| 506 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 507 | sol = ctx->line; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 508 | ctx->del = ctx->val + ctx->vlen; |
| 509 | sov = sol + ctx->del; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 510 | eol = sol + idx->v[cur_idx].len; |
| 511 | |
| 512 | if (sov >= eol) |
| 513 | /* no more values in this header */ |
| 514 | goto next_hdr; |
| 515 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 516 | /* values remaining for this header, skip the comma but save it |
| 517 | * for later use (eg: for header deletion). |
| 518 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 519 | sov++; |
| 520 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 521 | sov++; |
| 522 | |
| 523 | goto return_hdr; |
| 524 | } |
| 525 | |
| 526 | /* first request for this header */ |
| 527 | sol += hdr_idx_first_pos(idx); |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 528 | old_idx = 0; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 529 | cur_idx = hdr_idx_first_idx(idx); |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 530 | while (cur_idx) { |
| 531 | eol = sol + idx->v[cur_idx].len; |
| 532 | |
Willy Tarreau | 1ad7c6d | 2007-06-10 21:42:55 +0200 | [diff] [blame] | 533 | if (len == 0) { |
| 534 | /* No argument was passed, we want any header. |
| 535 | * To achieve this, we simply build a fake request. */ |
| 536 | while (sol + len < eol && sol[len] != ':') |
| 537 | len++; |
| 538 | name = sol; |
| 539 | } |
| 540 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 541 | if ((len < eol - sol) && |
| 542 | (sol[len] == ':') && |
| 543 | (strncasecmp(sol, name, len) == 0)) { |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 544 | ctx->del = len; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 545 | sov = sol + len + 1; |
| 546 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 547 | sov++; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 548 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 549 | ctx->line = sol; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 550 | ctx->prev = old_idx; |
| 551 | return_hdr: |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 552 | ctx->idx = cur_idx; |
| 553 | ctx->val = sov - sol; |
| 554 | |
| 555 | eol = find_hdr_value_end(sov, eol); |
| 556 | ctx->vlen = eol - sov; |
| 557 | return 1; |
| 558 | } |
| 559 | next_hdr: |
| 560 | sol = eol + idx->v[cur_idx].cr + 1; |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 561 | old_idx = cur_idx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 562 | cur_idx = idx->v[cur_idx].next; |
| 563 | } |
| 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | int http_find_header(const char *name, |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 568 | char *sol, struct hdr_idx *idx, |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 569 | struct hdr_ctx *ctx) |
| 570 | { |
| 571 | return http_find_header2(name, strlen(name), sol, idx, ctx); |
| 572 | } |
| 573 | |
Willy Tarreau | 68085d8 | 2010-01-18 14:54:04 +0100 | [diff] [blame] | 574 | /* Remove one value of a header. This only works on a <ctx> returned by one of |
| 575 | * the http_find_header functions. The value is removed, as well as surrounding |
| 576 | * commas if any. If the removed value was alone, the whole header is removed. |
| 577 | * The ctx is always updated accordingly, as well as buffer <buf> and HTTP |
| 578 | * message <msg>. The new index is returned. If it is zero, it means there is |
| 579 | * no more header, so any processing may stop. The ctx is always left in a form |
| 580 | * that can be handled by http_find_header2() to find next occurrence. |
| 581 | */ |
| 582 | int http_remove_header2(struct http_msg *msg, struct buffer *buf, |
| 583 | struct hdr_idx *idx, struct hdr_ctx *ctx) |
| 584 | { |
| 585 | int cur_idx = ctx->idx; |
| 586 | char *sol = ctx->line; |
| 587 | struct hdr_idx_elem *hdr; |
| 588 | int delta, skip_comma; |
| 589 | |
| 590 | if (!cur_idx) |
| 591 | return 0; |
| 592 | |
| 593 | hdr = &idx->v[cur_idx]; |
| 594 | if (sol[ctx->del] == ':' && ctx->val + ctx->vlen == hdr->len) { |
| 595 | /* This was the only value of the header, we must now remove it entirely. */ |
| 596 | delta = buffer_replace2(buf, sol, sol + hdr->len + hdr->cr + 1, NULL, 0); |
| 597 | http_msg_move_end(msg, delta); |
| 598 | idx->used--; |
| 599 | hdr->len = 0; /* unused entry */ |
| 600 | idx->v[ctx->prev].next = idx->v[ctx->idx].next; |
| 601 | ctx->idx = ctx->prev; /* walk back to the end of previous header */ |
| 602 | ctx->line -= idx->v[ctx->idx].len + idx->v[cur_idx].cr + 1; |
| 603 | ctx->val = idx->v[ctx->idx].len; /* point to end of previous header */ |
| 604 | ctx->vlen = 0; |
| 605 | return ctx->idx; |
| 606 | } |
| 607 | |
| 608 | /* This was not the only value of this header. We have to remove between |
| 609 | * ctx->del+1 and ctx->val+ctx->vlen+1 included. If it is the last entry |
| 610 | * of the list, we remove the last separator. |
| 611 | */ |
| 612 | |
| 613 | skip_comma = (ctx->val + ctx->vlen == hdr->len) ? 0 : 1; |
| 614 | delta = buffer_replace2(buf, sol + ctx->del + skip_comma, |
| 615 | sol + ctx->val + ctx->vlen + skip_comma, |
| 616 | NULL, 0); |
| 617 | hdr->len += delta; |
| 618 | http_msg_move_end(msg, delta); |
| 619 | ctx->val = ctx->del; |
| 620 | ctx->vlen = 0; |
| 621 | return ctx->idx; |
| 622 | } |
| 623 | |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 624 | /* This function handles a server error at the stream interface level. The |
| 625 | * stream interface is assumed to be already in a closed state. An optional |
| 626 | * message is copied into the input buffer, and an HTTP status code stored. |
| 627 | * 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] | 628 | * in this buffer will be lost. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 629 | */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 630 | static void http_server_error(struct session *t, struct stream_interface *si, |
| 631 | int err, int finst, int status, const struct chunk *msg) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 632 | { |
Willy Tarreau | d5fd51c | 2010-01-22 14:17:47 +0100 | [diff] [blame] | 633 | buffer_auto_read(si->ob); |
| 634 | buffer_abort(si->ob); |
| 635 | buffer_auto_close(si->ob); |
| 636 | buffer_erase(si->ob); |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 637 | buffer_auto_close(si->ib); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 638 | buffer_auto_read(si->ib); |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 639 | if (status > 0 && msg) { |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 640 | t->txn.status = status; |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 641 | buffer_write(si->ib, msg->str, msg->len); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 642 | } |
| 643 | if (!(t->flags & SN_ERR_MASK)) |
| 644 | t->flags |= err; |
| 645 | if (!(t->flags & SN_FINST_MASK)) |
| 646 | t->flags |= finst; |
| 647 | } |
| 648 | |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 649 | /* This function returns the appropriate error location for the given session |
| 650 | * and message. |
| 651 | */ |
| 652 | |
| 653 | struct chunk *error_message(struct session *s, int msgnum) |
| 654 | { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 655 | if (s->be->errmsg[msgnum].str) |
| 656 | return &s->be->errmsg[msgnum]; |
Willy Tarreau | 8058743 | 2006-12-24 17:47:20 +0100 | [diff] [blame] | 657 | else if (s->fe->errmsg[msgnum].str) |
| 658 | return &s->fe->errmsg[msgnum]; |
| 659 | else |
| 660 | return &http_err_chunks[msgnum]; |
| 661 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 662 | |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 663 | /* |
| 664 | * returns HTTP_METH_NONE if there is nothing valid to read (empty or non-text |
| 665 | * string), HTTP_METH_OTHER for unknown methods, or the identified method. |
| 666 | */ |
| 667 | static http_meth_t find_http_meth(const char *str, const int len) |
| 668 | { |
| 669 | unsigned char m; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 670 | const struct http_method_desc *h; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 671 | |
| 672 | m = ((unsigned)*str - 'A'); |
| 673 | |
| 674 | if (m < 26) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 675 | for (h = http_methods[m]; h->len > 0; h++) { |
| 676 | if (unlikely(h->len != len)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 677 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 678 | if (likely(memcmp(str, h->text, h->len) == 0)) |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 679 | return h->meth; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 680 | }; |
| 681 | return HTTP_METH_OTHER; |
| 682 | } |
| 683 | return HTTP_METH_NONE; |
| 684 | |
| 685 | } |
| 686 | |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 687 | /* Parse the URI from the given transaction (which is assumed to be in request |
| 688 | * phase) and look for the "/" beginning the PATH. If not found, return NULL. |
| 689 | * It is returned otherwise. |
| 690 | */ |
| 691 | static char * |
| 692 | http_get_path(struct http_txn *txn) |
| 693 | { |
| 694 | char *ptr, *end; |
| 695 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 696 | ptr = txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 697 | end = ptr + txn->req.sl.rq.u_l; |
| 698 | |
| 699 | if (ptr >= end) |
| 700 | return NULL; |
| 701 | |
| 702 | /* RFC2616, par. 5.1.2 : |
| 703 | * Request-URI = "*" | absuri | abspath | authority |
| 704 | */ |
| 705 | |
| 706 | if (*ptr == '*') |
| 707 | return NULL; |
| 708 | |
| 709 | if (isalpha((unsigned char)*ptr)) { |
| 710 | /* this is a scheme as described by RFC3986, par. 3.1 */ |
| 711 | ptr++; |
| 712 | while (ptr < end && |
| 713 | (isalnum((unsigned char)*ptr) || *ptr == '+' || *ptr == '-' || *ptr == '.')) |
| 714 | ptr++; |
| 715 | /* skip '://' */ |
| 716 | if (ptr == end || *ptr++ != ':') |
| 717 | return NULL; |
| 718 | if (ptr == end || *ptr++ != '/') |
| 719 | return NULL; |
| 720 | if (ptr == end || *ptr++ != '/') |
| 721 | return NULL; |
| 722 | } |
| 723 | /* skip [user[:passwd]@]host[:[port]] */ |
| 724 | |
| 725 | while (ptr < end && *ptr != '/') |
| 726 | ptr++; |
| 727 | |
| 728 | if (ptr == end) |
| 729 | return NULL; |
| 730 | |
| 731 | /* OK, we got the '/' ! */ |
| 732 | return ptr; |
| 733 | } |
| 734 | |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 735 | /* Returns a 302 for a redirectable request. This may only be called just after |
| 736 | * the stream interface has moved to SI_ST_ASS. Unprocessable requests are |
| 737 | * left unchanged and will follow normal proxy processing. |
| 738 | */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 739 | void perform_http_redirect(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 740 | { |
| 741 | struct http_txn *txn; |
| 742 | struct chunk rdr; |
| 743 | char *path; |
| 744 | int len; |
| 745 | |
| 746 | /* 1: create the response header */ |
| 747 | rdr.len = strlen(HTTP_302); |
| 748 | rdr.str = trash; |
Willy Tarreau | 59e0b0f | 2010-01-09 21:29:23 +0100 | [diff] [blame] | 749 | rdr.size = sizeof(trash); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 750 | memcpy(rdr.str, HTTP_302, rdr.len); |
| 751 | |
| 752 | /* 2: add the server's prefix */ |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 753 | if (rdr.len + s->srv->rdr_len > rdr.size) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 754 | return; |
| 755 | |
Willy Tarreau | dcb75c4 | 2010-01-10 00:24:22 +0100 | [diff] [blame] | 756 | /* special prefix "/" means don't change URL */ |
| 757 | if (s->srv->rdr_len != 1 || *s->srv->rdr_pfx != '/') { |
| 758 | memcpy(rdr.str + rdr.len, s->srv->rdr_pfx, s->srv->rdr_len); |
| 759 | rdr.len += s->srv->rdr_len; |
| 760 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 761 | |
| 762 | /* 3: add the request URI */ |
| 763 | txn = &s->txn; |
| 764 | path = http_get_path(txn); |
| 765 | if (!path) |
| 766 | return; |
| 767 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 768 | 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] | 769 | if (rdr.len + len > rdr.size - 4) /* 4 for CRLF-CRLF */ |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 770 | return; |
| 771 | |
| 772 | memcpy(rdr.str + rdr.len, path, len); |
| 773 | rdr.len += len; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 774 | |
| 775 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 776 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 777 | rdr.len += 29; |
| 778 | } else { |
| 779 | memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23); |
| 780 | rdr.len += 23; |
| 781 | } |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 782 | |
| 783 | /* prepare to return without error. */ |
Willy Tarreau | 99126c3 | 2008-11-27 10:30:51 +0100 | [diff] [blame] | 784 | si->shutr(si); |
| 785 | si->shutw(si); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 786 | si->err_type = SI_ET_NONE; |
| 787 | si->err_loc = NULL; |
| 788 | si->state = SI_ST_CLO; |
| 789 | |
| 790 | /* send the message */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 791 | 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] | 792 | |
| 793 | /* FIXME: we should increase a counter of redirects per server and per backend. */ |
| 794 | if (s->srv) |
Willy Tarreau | 7f062c4 | 2009-03-05 18:43:00 +0100 | [diff] [blame] | 795 | srv_inc_sess_ctr(s->srv); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 796 | } |
| 797 | |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 798 | /* Return the error message corresponding to si->err_type. It is assumed |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 799 | * that the server side is closed. Note that err_type is actually a |
| 800 | * bitmask, where almost only aborts may be cumulated with other |
| 801 | * values. We consider that aborted operations are more important |
| 802 | * than timeouts or errors due to the fact that nobody else in the |
| 803 | * logs might explain incomplete retries. All others should avoid |
| 804 | * being cumulated. It should normally not be possible to have multiple |
| 805 | * aborts at once, but just in case, the first one in sequence is reported. |
| 806 | */ |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 807 | void http_return_srv_error(struct session *s, struct stream_interface *si) |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 808 | { |
Willy Tarreau | 0cac36f | 2008-11-30 20:44:17 +0100 | [diff] [blame] | 809 | int err_type = si->err_type; |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 810 | |
| 811 | if (err_type & SI_ET_QUEUE_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 812 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_Q, |
| 813 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 814 | else if (err_type & SI_ET_CONN_ABRT) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 815 | http_server_error(s, si, SN_ERR_CLICL, SN_FINST_C, |
| 816 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 817 | else if (err_type & SI_ET_QUEUE_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 818 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_Q, |
| 819 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 820 | else if (err_type & SI_ET_QUEUE_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 821 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_Q, |
| 822 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 823 | else if (err_type & SI_ET_CONN_TO) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 824 | http_server_error(s, si, SN_ERR_SRVTO, SN_FINST_C, |
| 825 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 826 | else if (err_type & SI_ET_CONN_ERR) |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 827 | http_server_error(s, si, SN_ERR_SRVCL, SN_FINST_C, |
| 828 | 503, error_message(s, HTTP_ERR_503)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 829 | else /* SI_ET_CONN_OTHER and others */ |
Willy Tarreau | 2d3d94c | 2008-11-30 20:20:08 +0100 | [diff] [blame] | 830 | http_server_error(s, si, SN_ERR_INTERNAL, SN_FINST_C, |
| 831 | 500, error_message(s, HTTP_ERR_500)); |
Willy Tarreau | efb453c | 2008-10-26 20:49:47 +0100 | [diff] [blame] | 832 | } |
| 833 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 834 | extern const char sess_term_cond[8]; |
| 835 | extern const char sess_fin_state[8]; |
| 836 | extern const char *monthname[12]; |
| 837 | const char sess_cookie[4] = "NIDV"; /* No cookie, Invalid cookie, cookie for a Down server, Valid cookie */ |
| 838 | const char sess_set_cookie[8] = "N1I3PD5R"; /* No set-cookie, unknown, Set-Cookie Inserted, unknown, |
| 839 | Set-cookie seen and left unchanged (passive), Set-cookie Deleted, |
| 840 | unknown, Set-cookie Rewritten */ |
Willy Tarreau | 332f8bf | 2007-05-13 21:36:56 +0200 | [diff] [blame] | 841 | struct pool_head *pool2_requri; |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 842 | struct pool_head *pool2_capture; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 843 | |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 844 | void http_sess_clflog(struct session *s) |
| 845 | { |
| 846 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 847 | struct proxy *fe = s->fe; |
| 848 | struct proxy *be = s->be; |
| 849 | struct proxy *prx_log; |
| 850 | struct http_txn *txn = &s->txn; |
| 851 | int tolog, level, err; |
| 852 | char *uri, *h; |
| 853 | char *svid; |
| 854 | struct tm tm; |
| 855 | static char tmpline[MAX_SYSLOG_LEN]; |
| 856 | int hdr; |
| 857 | size_t w; |
| 858 | int t_request; |
| 859 | |
| 860 | prx_log = fe; |
| 861 | err = (s->flags & (SN_ERR_MASK | SN_REDISP)) || |
| 862 | (s->conn_retries != be->conn_retries) || |
| 863 | txn->status >= 500; |
| 864 | |
| 865 | if (s->cli_addr.ss_family == AF_INET) |
| 866 | inet_ntop(AF_INET, |
| 867 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 868 | pn, sizeof(pn)); |
| 869 | else |
| 870 | inet_ntop(AF_INET6, |
| 871 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 872 | pn, sizeof(pn)); |
| 873 | |
| 874 | get_gmtime(s->logs.accept_date.tv_sec, &tm); |
| 875 | |
| 876 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 877 | tolog = fe->to_log; |
| 878 | |
| 879 | h = tmpline; |
| 880 | |
| 881 | w = snprintf(h, sizeof(tmpline), |
| 882 | "%s - - [%02d/%s/%04d:%02d:%02d:%02d +0000]", |
| 883 | pn, |
| 884 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 885 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
| 886 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 887 | goto trunc; |
| 888 | h += w; |
| 889 | |
| 890 | if (h >= tmpline + sizeof(tmpline) - 4) |
| 891 | goto trunc; |
| 892 | |
| 893 | *(h++) = ' '; |
| 894 | *(h++) = '\"'; |
| 895 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 896 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 897 | '#', url_encode_map, uri); |
| 898 | *(h++) = '\"'; |
| 899 | |
| 900 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), " %d %lld", txn->status, s->logs.bytes_out); |
| 901 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 902 | goto trunc; |
| 903 | h += w; |
| 904 | |
| 905 | if (h >= tmpline + sizeof(tmpline) - 9) |
| 906 | goto trunc; |
| 907 | memcpy(h, " \"-\" \"-\"", 8); |
| 908 | h += 8; |
| 909 | |
| 910 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 911 | " %d %03d", |
| 912 | (s->cli_addr.ss_family == AF_INET) ? |
| 913 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 914 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
| 915 | (int)s->logs.accept_date.tv_usec/1000); |
| 916 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 917 | goto trunc; |
| 918 | h += w; |
| 919 | |
| 920 | w = strlen(fe->id); |
| 921 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 922 | goto trunc; |
| 923 | *(h++) = ' '; |
| 924 | *(h++) = '\"'; |
| 925 | memcpy(h, fe->id, w); |
| 926 | h += w; |
| 927 | *(h++) = '\"'; |
| 928 | |
| 929 | w = strlen(be->id); |
| 930 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 931 | goto trunc; |
| 932 | *(h++) = ' '; |
| 933 | *(h++) = '\"'; |
| 934 | memcpy(h, be->id, w); |
| 935 | h += w; |
| 936 | *(h++) = '\"'; |
| 937 | |
| 938 | svid = (tolog & LW_SVID) ? |
| 939 | (s->data_source != DATA_SRC_STATS) ? |
| 940 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 941 | |
| 942 | w = strlen(svid); |
| 943 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 944 | goto trunc; |
| 945 | *(h++) = ' '; |
| 946 | *(h++) = '\"'; |
| 947 | memcpy(h, svid, w); |
| 948 | h += w; |
| 949 | *(h++) = '\"'; |
| 950 | |
| 951 | t_request = -1; |
| 952 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 953 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 954 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 955 | " %d %ld %ld %ld %ld", |
| 956 | t_request, |
| 957 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
| 958 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 959 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 960 | s->logs.t_close); |
| 961 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 962 | goto trunc; |
| 963 | h += w; |
| 964 | |
| 965 | if (h >= tmpline + sizeof(tmpline) - 8) |
| 966 | goto trunc; |
| 967 | *(h++) = ' '; |
| 968 | *(h++) = '\"'; |
| 969 | *(h++) = sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT]; |
| 970 | *(h++) = sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT]; |
| 971 | *(h++) = (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 972 | *(h++) = (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-'; |
| 973 | *(h++) = '\"'; |
| 974 | |
| 975 | w = snprintf(h, sizeof(tmpline) - (h - tmpline), |
| 976 | " %d %d %d %d %d %ld %ld", |
| 977 | actconn, fe->feconn, be->beconn, s->srv ? s->srv->cur_sess : 0, |
| 978 | (s->conn_retries > 0) ? (be->conn_retries - s->conn_retries) : be->conn_retries, |
| 979 | s->logs.srv_queue_size, s->logs.prx_queue_size); |
| 980 | |
| 981 | if (w < 0 || w >= sizeof(tmpline) - (h - tmpline)) |
| 982 | goto trunc; |
| 983 | h += w; |
| 984 | |
| 985 | if (txn->cli_cookie) { |
| 986 | w = strlen(txn->cli_cookie); |
| 987 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 988 | goto trunc; |
| 989 | *(h++) = ' '; |
| 990 | *(h++) = '\"'; |
| 991 | memcpy(h, txn->cli_cookie, w); |
| 992 | h += w; |
| 993 | *(h++) = '\"'; |
| 994 | } else { |
| 995 | if (h >= tmpline + sizeof(tmpline) - 5) |
| 996 | goto trunc; |
| 997 | memcpy(h, " \"-\"", 4); |
| 998 | h += 4; |
| 999 | } |
| 1000 | |
| 1001 | if (txn->srv_cookie) { |
| 1002 | w = strlen(txn->srv_cookie); |
| 1003 | if (h >= tmpline + sizeof(tmpline) - 4 - w) |
| 1004 | goto trunc; |
| 1005 | *(h++) = ' '; |
| 1006 | *(h++) = '\"'; |
| 1007 | memcpy(h, txn->srv_cookie, w); |
| 1008 | h += w; |
| 1009 | *(h++) = '\"'; |
| 1010 | } else { |
| 1011 | if (h >= tmpline + sizeof(tmpline) - 5) |
| 1012 | goto trunc; |
| 1013 | memcpy(h, " \"-\"", 4); |
| 1014 | h += 4; |
| 1015 | } |
| 1016 | |
| 1017 | if ((fe->to_log & LW_REQHDR) && txn->req.cap) { |
| 1018 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 1019 | if (h >= sizeof (tmpline) + tmpline - 4) |
| 1020 | goto trunc; |
| 1021 | *(h++) = ' '; |
| 1022 | *(h++) = '\"'; |
| 1023 | h = encode_string(h, tmpline + sizeof(tmpline) - 2, |
| 1024 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 1025 | *(h++) = '\"'; |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | if ((fe->to_log & LW_RSPHDR) && txn->rsp.cap) { |
| 1030 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 1031 | if (h >= sizeof (tmpline) + tmpline - 4) |
| 1032 | goto trunc; |
| 1033 | *(h++) = ' '; |
| 1034 | *(h++) = '\"'; |
| 1035 | h = encode_string(h, tmpline + sizeof(tmpline) - 2, |
| 1036 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 1037 | *(h++) = '\"'; |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | trunc: |
| 1042 | *h = '\0'; |
| 1043 | |
| 1044 | level = LOG_INFO; |
| 1045 | if (err && (fe->options2 & PR_O2_LOGERRORS)) |
| 1046 | level = LOG_ERR; |
| 1047 | |
| 1048 | send_log(prx_log, level, "%s\n", tmpline); |
| 1049 | |
| 1050 | s->logs.logwait = 0; |
| 1051 | } |
| 1052 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1053 | /* |
| 1054 | * send a log for the session when we have enough info about it. |
| 1055 | * Will not log if the frontend has no log defined. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1056 | */ |
Willy Tarreau | 55a8d0e | 2008-11-30 18:47:21 +0100 | [diff] [blame] | 1057 | void http_sess_log(struct session *s) |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1058 | { |
| 1059 | char pn[INET6_ADDRSTRLEN + strlen(":65535")]; |
| 1060 | struct proxy *fe = s->fe; |
| 1061 | struct proxy *be = s->be; |
| 1062 | struct proxy *prx_log; |
| 1063 | struct http_txn *txn = &s->txn; |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1064 | int tolog, level, err; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1065 | char *uri, *h; |
| 1066 | char *svid; |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1067 | struct tm tm; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1068 | static char tmpline[MAX_SYSLOG_LEN]; |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1069 | int t_request; |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1070 | int hdr; |
| 1071 | |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1072 | /* if we don't want to log normal traffic, return now */ |
| 1073 | err = (s->flags & (SN_ERR_MASK | SN_REDISP)) || |
| 1074 | (s->conn_retries != be->conn_retries) || |
| 1075 | txn->status >= 500; |
| 1076 | if (!err && (fe->options2 & PR_O2_NOLOGNORM)) |
| 1077 | return; |
| 1078 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1079 | if (fe->logfac1 < 0 && fe->logfac2 < 0) |
| 1080 | return; |
| 1081 | prx_log = fe; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1082 | |
Emeric Brun | 3a058f3 | 2009-06-30 18:26:00 +0200 | [diff] [blame] | 1083 | if (prx_log->options2 & PR_O2_CLFLOG) |
| 1084 | return http_sess_clflog(s); |
| 1085 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1086 | if (s->cli_addr.ss_family == AF_INET) |
| 1087 | inet_ntop(AF_INET, |
| 1088 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 1089 | pn, sizeof(pn)); |
| 1090 | else |
| 1091 | inet_ntop(AF_INET6, |
| 1092 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 1093 | pn, sizeof(pn)); |
| 1094 | |
Willy Tarreau | b7f694f | 2008-06-22 17:18:02 +0200 | [diff] [blame] | 1095 | get_localtime(s->logs.accept_date.tv_sec, &tm); |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1096 | |
| 1097 | /* FIXME: let's limit ourselves to frontend logging for now. */ |
| 1098 | tolog = fe->to_log; |
| 1099 | |
| 1100 | h = tmpline; |
| 1101 | if (fe->to_log & LW_REQHDR && |
| 1102 | txn->req.cap && |
| 1103 | (h < tmpline + sizeof(tmpline) - 10)) { |
| 1104 | *(h++) = ' '; |
| 1105 | *(h++) = '{'; |
| 1106 | for (hdr = 0; hdr < fe->nb_req_cap; hdr++) { |
| 1107 | if (hdr) |
| 1108 | *(h++) = '|'; |
| 1109 | if (txn->req.cap[hdr] != NULL) |
| 1110 | h = encode_string(h, tmpline + sizeof(tmpline) - 7, |
| 1111 | '#', hdr_encode_map, txn->req.cap[hdr]); |
| 1112 | } |
| 1113 | *(h++) = '}'; |
| 1114 | } |
| 1115 | |
| 1116 | if (fe->to_log & LW_RSPHDR && |
| 1117 | txn->rsp.cap && |
| 1118 | (h < tmpline + sizeof(tmpline) - 7)) { |
| 1119 | *(h++) = ' '; |
| 1120 | *(h++) = '{'; |
| 1121 | for (hdr = 0; hdr < fe->nb_rsp_cap; hdr++) { |
| 1122 | if (hdr) |
| 1123 | *(h++) = '|'; |
| 1124 | if (txn->rsp.cap[hdr] != NULL) |
| 1125 | h = encode_string(h, tmpline + sizeof(tmpline) - 4, |
| 1126 | '#', hdr_encode_map, txn->rsp.cap[hdr]); |
| 1127 | } |
| 1128 | *(h++) = '}'; |
| 1129 | } |
| 1130 | |
| 1131 | if (h < tmpline + sizeof(tmpline) - 4) { |
| 1132 | *(h++) = ' '; |
| 1133 | *(h++) = '"'; |
| 1134 | uri = txn->uri ? txn->uri : "<BADREQ>"; |
| 1135 | h = encode_string(h, tmpline + sizeof(tmpline) - 1, |
| 1136 | '#', url_encode_map, uri); |
| 1137 | *(h++) = '"'; |
| 1138 | } |
| 1139 | *h = '\0'; |
| 1140 | |
| 1141 | svid = (tolog & LW_SVID) ? |
| 1142 | (s->data_source != DATA_SRC_STATS) ? |
| 1143 | (s->srv != NULL) ? s->srv->id : "<NOSRV>" : "<STATS>" : "-"; |
| 1144 | |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1145 | t_request = -1; |
| 1146 | if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept)) |
| 1147 | t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request); |
| 1148 | |
Willy Tarreau | c9bd0cc | 2009-05-10 11:57:02 +0200 | [diff] [blame] | 1149 | level = LOG_INFO; |
| 1150 | if (err && (fe->options2 & PR_O2_LOGERRORS)) |
| 1151 | level = LOG_ERR; |
| 1152 | |
| 1153 | send_log(prx_log, level, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1154 | "%s:%d [%02d/%s/%04d:%02d:%02d:%02d.%03d]" |
Willy Tarreau | 1772ece | 2009-04-03 14:49:12 +0200 | [diff] [blame] | 1155 | " %s %s/%s %d/%ld/%ld/%ld/%s%ld %d %s%lld" |
| 1156 | " %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] | 1157 | pn, |
| 1158 | (s->cli_addr.ss_family == AF_INET) ? |
| 1159 | ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port) : |
| 1160 | ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
Willy Tarreau | fe94460 | 2007-10-25 10:34:16 +0200 | [diff] [blame] | 1161 | tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
Willy Tarreau | 1772ece | 2009-04-03 14:49:12 +0200 | [diff] [blame] | 1162 | 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] | 1163 | fe->id, be->id, svid, |
Willy Tarreau | 7008987 | 2008-06-13 21:12:51 +0200 | [diff] [blame] | 1164 | t_request, |
| 1165 | (s->logs.t_queue >= 0) ? s->logs.t_queue - t_request : -1, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1166 | (s->logs.t_connect >= 0) ? s->logs.t_connect - s->logs.t_queue : -1, |
| 1167 | (s->logs.t_data >= 0) ? s->logs.t_data - s->logs.t_connect : -1, |
| 1168 | (tolog & LW_BYTES) ? "" : "+", s->logs.t_close, |
| 1169 | txn->status, |
Willy Tarreau | 8b3977f | 2008-01-18 11:16:32 +0100 | [diff] [blame] | 1170 | (tolog & LW_BYTES) ? "" : "+", s->logs.bytes_out, |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1171 | txn->cli_cookie ? txn->cli_cookie : "-", |
| 1172 | txn->srv_cookie ? txn->srv_cookie : "-", |
| 1173 | sess_term_cond[(s->flags & SN_ERR_MASK) >> SN_ERR_SHIFT], |
| 1174 | sess_fin_state[(s->flags & SN_FINST_MASK) >> SN_FINST_SHIFT], |
| 1175 | (be->options & PR_O_COOK_ANY) ? sess_cookie[(txn->flags & TX_CK_MASK) >> TX_CK_SHIFT] : '-', |
| 1176 | (be->options & PR_O_COOK_ANY) ? sess_set_cookie[(txn->flags & TX_SCK_MASK) >> TX_SCK_SHIFT] : '-', |
| 1177 | 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] | 1178 | (s->flags & SN_REDISP)?"+":"", |
| 1179 | (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] | 1180 | s->logs.srv_queue_size, s->logs.prx_queue_size, tmpline); |
| 1181 | |
| 1182 | s->logs.logwait = 0; |
| 1183 | } |
| 1184 | |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1185 | |
| 1186 | /* |
| 1187 | * Capture headers from message starting at <som> according to header list |
| 1188 | * <cap_hdr>, and fill the <idx> structure appropriately. |
| 1189 | */ |
| 1190 | void capture_headers(char *som, struct hdr_idx *idx, |
| 1191 | char **cap, struct cap_hdr *cap_hdr) |
| 1192 | { |
| 1193 | char *eol, *sol, *col, *sov; |
| 1194 | int cur_idx; |
| 1195 | struct cap_hdr *h; |
| 1196 | int len; |
| 1197 | |
| 1198 | sol = som + hdr_idx_first_pos(idx); |
| 1199 | cur_idx = hdr_idx_first_idx(idx); |
| 1200 | |
| 1201 | while (cur_idx) { |
| 1202 | eol = sol + idx->v[cur_idx].len; |
| 1203 | |
| 1204 | col = sol; |
| 1205 | while (col < eol && *col != ':') |
| 1206 | col++; |
| 1207 | |
| 1208 | sov = col + 1; |
| 1209 | while (sov < eol && http_is_lws[(unsigned char)*sov]) |
| 1210 | sov++; |
| 1211 | |
| 1212 | for (h = cap_hdr; h; h = h->next) { |
| 1213 | if ((h->namelen == col - sol) && |
| 1214 | (strncasecmp(sol, h->name, h->namelen) == 0)) { |
| 1215 | if (cap[h->index] == NULL) |
| 1216 | cap[h->index] = |
Willy Tarreau | cf7f320 | 2007-05-13 22:46:04 +0200 | [diff] [blame] | 1217 | pool_alloc2(h->pool); |
Willy Tarreau | 117f59e | 2007-03-04 18:17:17 +0100 | [diff] [blame] | 1218 | |
| 1219 | if (cap[h->index] == NULL) { |
| 1220 | Alert("HTTP capture : out of memory.\n"); |
| 1221 | continue; |
| 1222 | } |
| 1223 | |
| 1224 | len = eol - sov; |
| 1225 | if (len > h->len) |
| 1226 | len = h->len; |
| 1227 | |
| 1228 | memcpy(cap[h->index], sov, len); |
| 1229 | cap[h->index][len]=0; |
| 1230 | } |
| 1231 | } |
| 1232 | sol = eol + idx->v[cur_idx].cr + 1; |
| 1233 | cur_idx = idx->v[cur_idx].next; |
| 1234 | } |
| 1235 | } |
| 1236 | |
| 1237 | |
Willy Tarreau | 4225058 | 2007-04-01 01:30:43 +0200 | [diff] [blame] | 1238 | /* either we find an LF at <ptr> or we jump to <bad>. |
| 1239 | */ |
| 1240 | #define EXPECT_LF_HERE(ptr, bad) do { if (unlikely(*(ptr) != '\n')) goto bad; } while (0) |
| 1241 | |
| 1242 | /* plays with variables <ptr>, <end> and <state>. Jumps to <good> if OK, |
| 1243 | * otherwise to <http_msg_ood> with <state> set to <st>. |
| 1244 | */ |
| 1245 | #define EAT_AND_JUMP_OR_RETURN(good, st) do { \ |
| 1246 | ptr++; \ |
| 1247 | if (likely(ptr < end)) \ |
| 1248 | goto good; \ |
| 1249 | else { \ |
| 1250 | state = (st); \ |
| 1251 | goto http_msg_ood; \ |
| 1252 | } \ |
| 1253 | } while (0) |
| 1254 | |
| 1255 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1256 | /* |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1257 | * This function parses a status line between <ptr> and <end>, starting with |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1258 | * parser state <state>. Only states HTTP_MSG_RPVER, HTTP_MSG_RPVER_SP, |
| 1259 | * HTTP_MSG_RPCODE, HTTP_MSG_RPCODE_SP and HTTP_MSG_RPREASON are handled. Others |
| 1260 | * will give undefined results. |
| 1261 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1262 | * and that msg->sol points to the beginning of the response. |
| 1263 | * If a complete line is found (which implies that at least one CR or LF is |
| 1264 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1265 | * returned indicating an incomplete line (which does not mean that parts have |
| 1266 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1267 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1268 | * upon next call. |
| 1269 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1270 | * This function was intentionally designed to be called from |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1271 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1272 | * 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] | 1273 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1274 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1275 | const char *http_parse_stsline(struct http_msg *msg, const char *msg_buf, |
| 1276 | unsigned int state, const char *ptr, const char *end, |
| 1277 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1278 | { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1279 | switch (state) { |
| 1280 | http_msg_rpver: |
| 1281 | case HTTP_MSG_RPVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1282 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1283 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver, HTTP_MSG_RPVER); |
| 1284 | |
| 1285 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1286 | msg->sl.st.v_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1287 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1288 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1289 | state = HTTP_MSG_ERROR; |
| 1290 | break; |
| 1291 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1292 | http_msg_rpver_sp: |
| 1293 | case HTTP_MSG_RPVER_SP: |
| 1294 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1295 | msg->sl.st.c = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1296 | goto http_msg_rpcode; |
| 1297 | } |
| 1298 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1299 | EAT_AND_JUMP_OR_RETURN(http_msg_rpver_sp, HTTP_MSG_RPVER_SP); |
| 1300 | /* so it's a CR/LF, this is invalid */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1301 | state = HTTP_MSG_ERROR; |
| 1302 | break; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1303 | |
| 1304 | http_msg_rpcode: |
| 1305 | case HTTP_MSG_RPCODE: |
| 1306 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1307 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode, HTTP_MSG_RPCODE); |
| 1308 | |
| 1309 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1310 | 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] | 1311 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1312 | } |
| 1313 | |
| 1314 | /* so it's a CR/LF, so there is no reason phrase */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1315 | 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] | 1316 | http_msg_rsp_reason: |
| 1317 | /* FIXME: should we support HTTP responses without any reason phrase ? */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1318 | msg->sl.st.r = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1319 | msg->sl.st.r_l = 0; |
| 1320 | goto http_msg_rpline_eol; |
| 1321 | |
| 1322 | http_msg_rpcode_sp: |
| 1323 | case HTTP_MSG_RPCODE_SP: |
| 1324 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1325 | msg->sl.st.r = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1326 | goto http_msg_rpreason; |
| 1327 | } |
| 1328 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1329 | EAT_AND_JUMP_OR_RETURN(http_msg_rpcode_sp, HTTP_MSG_RPCODE_SP); |
| 1330 | /* so it's a CR/LF, so there is no reason phrase */ |
| 1331 | goto http_msg_rsp_reason; |
| 1332 | |
| 1333 | http_msg_rpreason: |
| 1334 | case HTTP_MSG_RPREASON: |
| 1335 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1336 | EAT_AND_JUMP_OR_RETURN(http_msg_rpreason, HTTP_MSG_RPREASON); |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1337 | 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] | 1338 | http_msg_rpline_eol: |
| 1339 | /* We have seen the end of line. Note that we do not |
| 1340 | * necessarily have the \n yet, but at least we know that we |
| 1341 | * have EITHER \r OR \n, otherwise the response would not be |
| 1342 | * complete. We can then record the response length and return |
| 1343 | * to the caller which will be able to register it. |
| 1344 | */ |
| 1345 | msg->sl.st.l = ptr - msg->sol; |
| 1346 | return ptr; |
| 1347 | |
| 1348 | #ifdef DEBUG_FULL |
| 1349 | default: |
| 1350 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1351 | exit(1); |
| 1352 | #endif |
| 1353 | } |
| 1354 | |
| 1355 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1356 | /* out of valid data */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1357 | if (ret_state) |
| 1358 | *ret_state = state; |
| 1359 | if (ret_ptr) |
| 1360 | *ret_ptr = (char *)ptr; |
| 1361 | return NULL; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1362 | } |
| 1363 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1364 | /* |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1365 | * This function parses a request line between <ptr> and <end>, starting with |
| 1366 | * parser state <state>. Only states HTTP_MSG_RQMETH, HTTP_MSG_RQMETH_SP, |
| 1367 | * HTTP_MSG_RQURI, HTTP_MSG_RQURI_SP and HTTP_MSG_RQVER are handled. Others |
| 1368 | * will give undefined results. |
| 1369 | * Note that it is upon the caller's responsibility to ensure that ptr < end, |
| 1370 | * and that msg->sol points to the beginning of the request. |
| 1371 | * If a complete line is found (which implies that at least one CR or LF is |
| 1372 | * found before <end>, the updated <ptr> is returned, otherwise NULL is |
| 1373 | * returned indicating an incomplete line (which does not mean that parts have |
| 1374 | * not been updated). In the incomplete case, if <ret_ptr> or <ret_state> are |
| 1375 | * non-NULL, they are fed with the new <ptr> and <state> values to be passed |
| 1376 | * upon next call. |
| 1377 | * |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1378 | * This function was intentionally designed to be called from |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1379 | * http_msg_analyzer() with the lowest overhead. It should integrate perfectly |
| 1380 | * 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] | 1381 | * labels and variable names. Note that msg->sol is left unchanged. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1382 | */ |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1383 | const char *http_parse_reqline(struct http_msg *msg, const char *msg_buf, |
| 1384 | unsigned int state, const char *ptr, const char *end, |
| 1385 | char **ret_ptr, unsigned int *ret_state) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1386 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1387 | switch (state) { |
| 1388 | http_msg_rqmeth: |
| 1389 | case HTTP_MSG_RQMETH: |
| 1390 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1391 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth, HTTP_MSG_RQMETH); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1392 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1393 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1394 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1395 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1396 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1397 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1398 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 1399 | /* HTTP 0.9 request */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1400 | msg->sl.rq.m_l = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1401 | http_msg_req09_uri: |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1402 | msg->sl.rq.u = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1403 | http_msg_req09_uri_e: |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1404 | 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] | 1405 | http_msg_req09_ver: |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1406 | msg->sl.rq.v = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1407 | msg->sl.rq.v_l = 0; |
| 1408 | goto http_msg_rqline_eol; |
| 1409 | } |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1410 | state = HTTP_MSG_ERROR; |
| 1411 | break; |
| 1412 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1413 | http_msg_rqmeth_sp: |
| 1414 | case HTTP_MSG_RQMETH_SP: |
| 1415 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1416 | msg->sl.rq.u = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1417 | goto http_msg_rquri; |
| 1418 | } |
| 1419 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1420 | EAT_AND_JUMP_OR_RETURN(http_msg_rqmeth_sp, HTTP_MSG_RQMETH_SP); |
| 1421 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1422 | goto http_msg_req09_uri; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1423 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1424 | http_msg_rquri: |
| 1425 | case HTTP_MSG_RQURI: |
| 1426 | if (likely(!HTTP_IS_LWS(*ptr))) |
| 1427 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri, HTTP_MSG_RQURI); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1428 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1429 | if (likely(HTTP_IS_SPHT(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1430 | 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] | 1431 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1432 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1433 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1434 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1435 | goto http_msg_req09_uri_e; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1436 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1437 | http_msg_rquri_sp: |
| 1438 | case HTTP_MSG_RQURI_SP: |
| 1439 | if (likely(!HTTP_IS_LWS(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1440 | msg->sl.rq.v = (ptr - msg_buf) - msg->som; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1441 | goto http_msg_rqver; |
| 1442 | } |
| 1443 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1444 | EAT_AND_JUMP_OR_RETURN(http_msg_rquri_sp, HTTP_MSG_RQURI_SP); |
| 1445 | /* so it's a CR/LF, meaning an HTTP 0.9 request */ |
| 1446 | goto http_msg_req09_ver; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1447 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1448 | http_msg_rqver: |
| 1449 | case HTTP_MSG_RQVER: |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1450 | if (likely(HTTP_IS_VER_TOKEN(*ptr))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1451 | EAT_AND_JUMP_OR_RETURN(http_msg_rqver, HTTP_MSG_RQVER); |
Willy Tarreau | 4b89ad4 | 2007-03-04 18:13:58 +0100 | [diff] [blame] | 1452 | |
| 1453 | if (likely(HTTP_IS_CRLF(*ptr))) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1454 | 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] | 1455 | http_msg_rqline_eol: |
| 1456 | /* We have seen the end of line. Note that we do not |
| 1457 | * necessarily have the \n yet, but at least we know that we |
| 1458 | * have EITHER \r OR \n, otherwise the request would not be |
| 1459 | * complete. We can then record the request length and return |
| 1460 | * to the caller which will be able to register it. |
| 1461 | */ |
| 1462 | msg->sl.rq.l = ptr - msg->sol; |
| 1463 | return ptr; |
| 1464 | } |
| 1465 | |
| 1466 | /* neither an HTTP_VER token nor a CRLF */ |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1467 | state = HTTP_MSG_ERROR; |
| 1468 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1469 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1470 | #ifdef DEBUG_FULL |
| 1471 | default: |
| 1472 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1473 | exit(1); |
| 1474 | #endif |
| 1475 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1476 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1477 | http_msg_ood: |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1478 | /* out of valid data */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1479 | if (ret_state) |
| 1480 | *ret_state = state; |
| 1481 | if (ret_ptr) |
| 1482 | *ret_ptr = (char *)ptr; |
| 1483 | return NULL; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1484 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1485 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1486 | /* |
| 1487 | * Returns the data from Authorization header. Function may be called more |
| 1488 | * than once so data is stored in txn->auth_data. When no header is found |
| 1489 | * or auth method is unknown auth_method is set to HTTP_AUTH_WRONG to avoid |
| 1490 | * searching again for something we are unable to find anyway. |
| 1491 | */ |
| 1492 | |
| 1493 | char get_http_auth_buff[BUFSIZE]; |
| 1494 | |
| 1495 | int |
| 1496 | get_http_auth(struct session *s) |
| 1497 | { |
| 1498 | |
| 1499 | struct http_txn *txn = &s->txn; |
| 1500 | struct chunk auth_method; |
| 1501 | struct hdr_ctx ctx; |
| 1502 | char *h, *p; |
| 1503 | int len; |
| 1504 | |
| 1505 | #ifdef DEBUG_AUTH |
| 1506 | printf("Auth for session %p: %d\n", s, txn->auth.method); |
| 1507 | #endif |
| 1508 | |
| 1509 | if (txn->auth.method == HTTP_AUTH_WRONG) |
| 1510 | return 0; |
| 1511 | |
| 1512 | if (txn->auth.method) |
| 1513 | return 1; |
| 1514 | |
| 1515 | txn->auth.method = HTTP_AUTH_WRONG; |
| 1516 | |
| 1517 | ctx.idx = 0; |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 1518 | |
| 1519 | if (txn->flags & TX_USE_PX_CONN) { |
| 1520 | h = "Proxy-Authorization"; |
| 1521 | len = strlen(h); |
| 1522 | } else { |
| 1523 | h = "Authorization"; |
| 1524 | len = strlen(h); |
| 1525 | } |
| 1526 | |
| 1527 | if (!http_find_header2(h, len, txn->req.sol, &txn->hdr_idx, &ctx)) |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 1528 | return 0; |
| 1529 | |
| 1530 | h = ctx.line + ctx.val; |
| 1531 | |
| 1532 | p = memchr(h, ' ', ctx.vlen); |
| 1533 | if (!p || p == h) |
| 1534 | return 0; |
| 1535 | |
| 1536 | chunk_initlen(&auth_method, h, 0, p-h); |
| 1537 | chunk_initlen(&txn->auth.method_data, p+1, 0, ctx.vlen-(p-h)-1); |
| 1538 | |
| 1539 | if (!strncasecmp("Basic", auth_method.str, auth_method.len)) { |
| 1540 | |
| 1541 | len = base64dec(txn->auth.method_data.str, txn->auth.method_data.len, |
| 1542 | get_http_auth_buff, BUFSIZE - 1); |
| 1543 | |
| 1544 | if (len < 0) |
| 1545 | return 0; |
| 1546 | |
| 1547 | |
| 1548 | get_http_auth_buff[len] = '\0'; |
| 1549 | |
| 1550 | p = strchr(get_http_auth_buff, ':'); |
| 1551 | |
| 1552 | if (!p) |
| 1553 | return 0; |
| 1554 | |
| 1555 | txn->auth.user = get_http_auth_buff; |
| 1556 | *p = '\0'; |
| 1557 | txn->auth.pass = p+1; |
| 1558 | |
| 1559 | txn->auth.method = HTTP_AUTH_BASIC; |
| 1560 | return 1; |
| 1561 | } |
| 1562 | |
| 1563 | return 0; |
| 1564 | } |
| 1565 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1566 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1567 | /* |
| 1568 | * This function parses an HTTP message, either a request or a response, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1569 | * depending on the initial msg->msg_state. It can be preempted everywhere |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1570 | * when data are missing and recalled at the exact same location with no |
| 1571 | * information loss. The header index is re-initialized when switching from |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 1572 | * 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] | 1573 | * fields. Note that msg->som and msg->sol will be initialized after completing |
| 1574 | * the first state, so that none of the msg pointers has to be initialized |
| 1575 | * prior to the first call. |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1576 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1577 | void http_msg_analyzer(struct buffer *buf, struct http_msg *msg, struct hdr_idx *idx) |
| 1578 | { |
Willy Tarreau | e69eada | 2008-01-27 00:34:10 +0100 | [diff] [blame] | 1579 | unsigned int state; /* updated only when leaving the FSM */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1580 | register char *ptr, *end; /* request pointers, to avoid dereferences */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1581 | |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1582 | state = msg->msg_state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1583 | ptr = buf->lr; |
| 1584 | end = buf->r; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1585 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1586 | if (unlikely(ptr >= end)) |
| 1587 | goto http_msg_ood; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1588 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1589 | switch (state) { |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1590 | /* |
| 1591 | * First, states that are specific to the response only. |
| 1592 | * We check them first so that request and headers are |
| 1593 | * closer to each other (accessed more often). |
| 1594 | */ |
| 1595 | http_msg_rpbefore: |
| 1596 | case HTTP_MSG_RPBEFORE: |
| 1597 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1598 | /* we have a start of message, but we have to check |
| 1599 | * first if we need to remove some CRLF. We can only |
| 1600 | * do this when send_max=0. |
| 1601 | */ |
| 1602 | char *beg = buf->w + buf->send_max; |
| 1603 | if (beg >= buf->data + buf->size) |
| 1604 | beg -= buf->size; |
| 1605 | if (unlikely(ptr != beg)) { |
| 1606 | if (buf->send_max) |
| 1607 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1608 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1609 | buffer_ignore(buf, ptr - beg); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1610 | } |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1611 | msg->som = ptr - buf->data; |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 1612 | msg->sol = ptr; |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1613 | hdr_idx_init(idx); |
| 1614 | state = HTTP_MSG_RPVER; |
| 1615 | goto http_msg_rpver; |
| 1616 | } |
| 1617 | |
| 1618 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1619 | goto http_msg_invalid; |
| 1620 | |
| 1621 | if (unlikely(*ptr == '\n')) |
| 1622 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1623 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore_cr, HTTP_MSG_RPBEFORE_CR); |
| 1624 | /* stop here */ |
| 1625 | |
| 1626 | http_msg_rpbefore_cr: |
| 1627 | case HTTP_MSG_RPBEFORE_CR: |
| 1628 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1629 | EAT_AND_JUMP_OR_RETURN(http_msg_rpbefore, HTTP_MSG_RPBEFORE); |
| 1630 | /* stop here */ |
| 1631 | |
| 1632 | http_msg_rpver: |
| 1633 | case HTTP_MSG_RPVER: |
| 1634 | case HTTP_MSG_RPVER_SP: |
| 1635 | case HTTP_MSG_RPCODE: |
| 1636 | case HTTP_MSG_RPCODE_SP: |
| 1637 | case HTTP_MSG_RPREASON: |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 1638 | ptr = (char *)http_parse_stsline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1639 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1640 | if (unlikely(!ptr)) |
| 1641 | return; |
| 1642 | |
| 1643 | /* we have a full response and we know that we have either a CR |
| 1644 | * or an LF at <ptr>. |
| 1645 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1646 | //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] | 1647 | hdr_idx_set_start(idx, msg->sl.st.l, *ptr == '\r'); |
| 1648 | |
| 1649 | msg->sol = ptr; |
| 1650 | if (likely(*ptr == '\r')) |
| 1651 | EAT_AND_JUMP_OR_RETURN(http_msg_rpline_end, HTTP_MSG_RPLINE_END); |
| 1652 | goto http_msg_rpline_end; |
| 1653 | |
| 1654 | http_msg_rpline_end: |
| 1655 | case HTTP_MSG_RPLINE_END: |
| 1656 | /* msg->sol must point to the first of CR or LF. */ |
| 1657 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1658 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_first, HTTP_MSG_HDR_FIRST); |
| 1659 | /* stop here */ |
| 1660 | |
| 1661 | /* |
| 1662 | * Second, states that are specific to the request only |
| 1663 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1664 | http_msg_rqbefore: |
| 1665 | case HTTP_MSG_RQBEFORE: |
| 1666 | if (likely(HTTP_IS_TOKEN(*ptr))) { |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1667 | /* we have a start of message, but we have to check |
| 1668 | * first if we need to remove some CRLF. We can only |
| 1669 | * do this when send_max=0. |
| 1670 | */ |
| 1671 | char *beg = buf->w + buf->send_max; |
| 1672 | if (beg >= buf->data + buf->size) |
| 1673 | beg -= buf->size; |
| 1674 | if (likely(ptr != beg)) { |
| 1675 | if (buf->send_max) |
| 1676 | goto http_msg_ood; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1677 | /* Remove empty leading lines, as recommended by RFC2616. */ |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1678 | buffer_ignore(buf, ptr - beg); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1679 | } |
Willy Tarreau | 15de77e | 2010-01-02 21:59:16 +0100 | [diff] [blame] | 1680 | msg->som = ptr - buf->data; |
Willy Tarreau | 1d3bcce | 2009-12-27 15:50:06 +0100 | [diff] [blame] | 1681 | msg->sol = ptr; |
Willy Tarreau | f0d058e | 2007-01-25 12:03:42 +0100 | [diff] [blame] | 1682 | /* we will need this when keep-alive will be supported |
| 1683 | hdr_idx_init(idx); |
| 1684 | */ |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1685 | state = HTTP_MSG_RQMETH; |
| 1686 | goto http_msg_rqmeth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1687 | } |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1688 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1689 | if (unlikely(!HTTP_IS_CRLF(*ptr))) |
| 1690 | goto http_msg_invalid; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1691 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1692 | if (unlikely(*ptr == '\n')) |
| 1693 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
| 1694 | 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] | 1695 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1696 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1697 | http_msg_rqbefore_cr: |
| 1698 | case HTTP_MSG_RQBEFORE_CR: |
| 1699 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1700 | EAT_AND_JUMP_OR_RETURN(http_msg_rqbefore, HTTP_MSG_RQBEFORE); |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1701 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1702 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1703 | http_msg_rqmeth: |
| 1704 | case HTTP_MSG_RQMETH: |
| 1705 | case HTTP_MSG_RQMETH_SP: |
| 1706 | case HTTP_MSG_RQURI: |
| 1707 | case HTTP_MSG_RQURI_SP: |
| 1708 | case HTTP_MSG_RQVER: |
| 1709 | ptr = (char *)http_parse_reqline(msg, buf->data, state, ptr, end, |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1710 | &buf->lr, &msg->msg_state); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1711 | if (unlikely(!ptr)) |
| 1712 | return; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1713 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1714 | /* we have a full request and we know that we have either a CR |
| 1715 | * or an LF at <ptr>. |
| 1716 | */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1717 | //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] | 1718 | hdr_idx_set_start(idx, msg->sl.rq.l, *ptr == '\r'); |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1719 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1720 | msg->sol = ptr; |
| 1721 | if (likely(*ptr == '\r')) |
| 1722 | 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] | 1723 | goto http_msg_rqline_end; |
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 | http_msg_rqline_end: |
| 1726 | case HTTP_MSG_RQLINE_END: |
| 1727 | /* check for HTTP/0.9 request : no version information available. |
| 1728 | * msg->sol must point to the first of CR or LF. |
| 1729 | */ |
| 1730 | if (unlikely(msg->sl.rq.v_l == 0)) |
| 1731 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1732 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1733 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1734 | 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] | 1735 | /* stop here */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1736 | |
Willy Tarreau | 8973c70 | 2007-01-21 23:58:29 +0100 | [diff] [blame] | 1737 | /* |
| 1738 | * Common states below |
| 1739 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1740 | http_msg_hdr_first: |
| 1741 | case HTTP_MSG_HDR_FIRST: |
| 1742 | msg->sol = ptr; |
| 1743 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1744 | goto http_msg_hdr_name; |
| 1745 | } |
| 1746 | |
| 1747 | if (likely(*ptr == '\r')) |
| 1748 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1749 | goto http_msg_last_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1750 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1751 | http_msg_hdr_name: |
| 1752 | case HTTP_MSG_HDR_NAME: |
| 1753 | /* assumes msg->sol points to the first char */ |
| 1754 | if (likely(HTTP_IS_TOKEN(*ptr))) |
| 1755 | 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] | 1756 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1757 | if (likely(*ptr == ':')) { |
| 1758 | msg->col = ptr - buf->data; |
| 1759 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_sp, HTTP_MSG_HDR_L1_SP); |
| 1760 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1761 | |
Willy Tarreau | 32a4ec0 | 2009-04-02 11:35:18 +0200 | [diff] [blame] | 1762 | if (likely(msg->err_pos < -1) || *ptr == '\n') |
| 1763 | goto http_msg_invalid; |
| 1764 | |
| 1765 | if (msg->err_pos == -1) /* capture error pointer */ |
| 1766 | msg->err_pos = ptr - buf->data; /* >= 0 now */ |
| 1767 | |
| 1768 | /* and we still accept this non-token character */ |
| 1769 | 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] | 1770 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1771 | http_msg_hdr_l1_sp: |
| 1772 | case HTTP_MSG_HDR_L1_SP: |
| 1773 | /* assumes msg->sol points to the first char and msg->col to the colon */ |
| 1774 | if (likely(HTTP_IS_SPHT(*ptr))) |
| 1775 | 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] | 1776 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1777 | /* header value can be basically anything except CR/LF */ |
| 1778 | msg->sov = ptr - buf->data; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1779 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1780 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1781 | goto http_msg_hdr_val; |
| 1782 | } |
| 1783 | |
| 1784 | if (likely(*ptr == '\r')) |
| 1785 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l1_lf, HTTP_MSG_HDR_L1_LF); |
| 1786 | goto http_msg_hdr_l1_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1787 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1788 | http_msg_hdr_l1_lf: |
| 1789 | case HTTP_MSG_HDR_L1_LF: |
| 1790 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1791 | 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] | 1792 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1793 | http_msg_hdr_l1_lws: |
| 1794 | case HTTP_MSG_HDR_L1_LWS: |
| 1795 | if (likely(HTTP_IS_SPHT(*ptr))) { |
| 1796 | /* replace HT,CR,LF with spaces */ |
| 1797 | for (; buf->data+msg->sov < ptr; msg->sov++) |
| 1798 | buf->data[msg->sov] = ' '; |
| 1799 | goto http_msg_hdr_l1_sp; |
| 1800 | } |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 1801 | /* we had a header consisting only in spaces ! */ |
| 1802 | msg->eol = buf->data + msg->sov; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1803 | goto http_msg_complete_header; |
| 1804 | |
| 1805 | http_msg_hdr_val: |
| 1806 | case HTTP_MSG_HDR_VAL: |
| 1807 | /* assumes msg->sol points to the first char, msg->col to the |
| 1808 | * colon, and msg->sov points to the first character of the |
| 1809 | * value. |
| 1810 | */ |
| 1811 | if (likely(!HTTP_IS_CRLF(*ptr))) |
| 1812 | 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] | 1813 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1814 | msg->eol = ptr; |
| 1815 | /* Note: we could also copy eol into ->eoh so that we have the |
| 1816 | * real header end in case it ends with lots of LWS, but is this |
| 1817 | * really needed ? |
| 1818 | */ |
| 1819 | if (likely(*ptr == '\r')) |
| 1820 | EAT_AND_JUMP_OR_RETURN(http_msg_hdr_l2_lf, HTTP_MSG_HDR_L2_LF); |
| 1821 | goto http_msg_hdr_l2_lf; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1822 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1823 | http_msg_hdr_l2_lf: |
| 1824 | case HTTP_MSG_HDR_L2_LF: |
| 1825 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1826 | 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] | 1827 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1828 | http_msg_hdr_l2_lws: |
| 1829 | case HTTP_MSG_HDR_L2_LWS: |
| 1830 | if (unlikely(HTTP_IS_SPHT(*ptr))) { |
| 1831 | /* LWS: replace HT,CR,LF with spaces */ |
| 1832 | for (; msg->eol < ptr; msg->eol++) |
| 1833 | *msg->eol = ' '; |
| 1834 | goto http_msg_hdr_val; |
| 1835 | } |
| 1836 | http_msg_complete_header: |
| 1837 | /* |
| 1838 | * It was a new header, so the last one is finished. |
| 1839 | * Assumes msg->sol points to the first char, msg->col to the |
| 1840 | * colon, msg->sov points to the first character of the value |
| 1841 | * and msg->eol to the first CR or LF so we know how the line |
| 1842 | * ends. We insert last header into the index. |
| 1843 | */ |
| 1844 | /* |
| 1845 | fprintf(stderr,"registering %-2d bytes : ", msg->eol - msg->sol); |
| 1846 | write(2, msg->sol, msg->eol-msg->sol); |
| 1847 | fprintf(stderr,"\n"); |
| 1848 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 1849 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1850 | if (unlikely(hdr_idx_add(msg->eol - msg->sol, *msg->eol == '\r', |
| 1851 | idx, idx->tail) < 0)) |
| 1852 | goto http_msg_invalid; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1853 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1854 | msg->sol = ptr; |
| 1855 | if (likely(!HTTP_IS_CRLF(*ptr))) { |
| 1856 | goto http_msg_hdr_name; |
| 1857 | } |
| 1858 | |
| 1859 | if (likely(*ptr == '\r')) |
| 1860 | EAT_AND_JUMP_OR_RETURN(http_msg_last_lf, HTTP_MSG_LAST_LF); |
| 1861 | goto http_msg_last_lf; |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1862 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1863 | http_msg_last_lf: |
| 1864 | case HTTP_MSG_LAST_LF: |
| 1865 | /* Assumes msg->sol points to the first of either CR or LF */ |
| 1866 | EXPECT_LF_HERE(ptr, http_msg_invalid); |
| 1867 | ptr++; |
| 1868 | buf->lr = ptr; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1869 | msg->col = msg->sov = buf->lr - buf->data; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1870 | msg->eoh = msg->sol - buf->data; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 1871 | msg->sol = buf->data + msg->som; |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1872 | msg->msg_state = HTTP_MSG_BODY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1873 | return; |
| 1874 | #ifdef DEBUG_FULL |
| 1875 | default: |
| 1876 | fprintf(stderr, "FIXME !!!! impossible state at %s:%d = %d\n", __FILE__, __LINE__, state); |
| 1877 | exit(1); |
Willy Tarreau | 230fd0b | 2006-12-17 12:05:00 +0100 | [diff] [blame] | 1878 | #endif |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1879 | } |
| 1880 | http_msg_ood: |
| 1881 | /* out of data */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1882 | msg->msg_state = state; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1883 | buf->lr = ptr; |
| 1884 | return; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 1885 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1886 | http_msg_invalid: |
| 1887 | /* invalid message */ |
Willy Tarreau | b326fcc | 2007-03-03 13:54:32 +0100 | [diff] [blame] | 1888 | msg->msg_state = HTTP_MSG_ERROR; |
Willy Tarreau | 7552c03 | 2009-03-01 11:10:40 +0100 | [diff] [blame] | 1889 | buf->lr = ptr; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 1890 | return; |
| 1891 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1892 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1893 | /* convert an HTTP/0.9 request into an HTTP/1.0 request. Returns 1 if the |
| 1894 | * conversion succeeded, 0 in case of error. If the request was already 1.X, |
| 1895 | * nothing is done and 1 is returned. |
| 1896 | */ |
| 1897 | static int http_upgrade_v09_to_v10(struct buffer *req, struct http_msg *msg, struct http_txn *txn) |
| 1898 | { |
| 1899 | int delta; |
| 1900 | char *cur_end; |
| 1901 | |
| 1902 | if (msg->sl.rq.v_l != 0) |
| 1903 | return 1; |
| 1904 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1905 | cur_end = msg->sol + msg->sl.rq.l; |
| 1906 | delta = 0; |
| 1907 | |
| 1908 | if (msg->sl.rq.u_l == 0) { |
| 1909 | /* if no URI was set, add "/" */ |
| 1910 | delta = buffer_replace2(req, cur_end, cur_end, " /", 2); |
| 1911 | cur_end += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 1912 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1913 | } |
| 1914 | /* add HTTP version */ |
| 1915 | 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] | 1916 | http_msg_move_end(msg, delta); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 1917 | cur_end += delta; |
| 1918 | cur_end = (char *)http_parse_reqline(msg, req->data, |
| 1919 | HTTP_MSG_RQMETH, |
| 1920 | msg->sol, cur_end + 1, |
| 1921 | NULL, NULL); |
| 1922 | if (unlikely(!cur_end)) |
| 1923 | return 0; |
| 1924 | |
| 1925 | /* we have a full HTTP/1.0 request now and we know that |
| 1926 | * we have either a CR or an LF at <ptr>. |
| 1927 | */ |
| 1928 | hdr_idx_set_start(&txn->hdr_idx, msg->sl.rq.l, *cur_end == '\r'); |
| 1929 | return 1; |
| 1930 | } |
| 1931 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1932 | /* Parse the Connection: header of an HTTP request, looking for both "close" |
| 1933 | * and "keep-alive" values. If a buffer is provided and we already know that |
| 1934 | * some headers may safely be removed, we remove them now. The <to_del> flags |
| 1935 | * are used for that : |
| 1936 | * - bit 0 means remove "close" headers (in HTTP/1.0 requests/responses) |
| 1937 | * - bit 1 means remove "keep-alive" headers (in HTTP/1.1 reqs/resp to 1.1). |
| 1938 | * The TX_HDR_CONN_* flags are adjusted in txn->flags depending on what was |
| 1939 | * found, and TX_CON_*_SET is adjusted depending on what is left so only |
| 1940 | * harmless combinations may be removed. Do not call that after changes have |
| 1941 | * been processed. If unused, the buffer can be NULL, and no data will be |
| 1942 | * changed. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1943 | */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1944 | 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] | 1945 | { |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1946 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1947 | const char *hdr_val = "Connection"; |
| 1948 | int hdr_len = 10; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1949 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1950 | if (txn->flags & TX_HDR_CONN_PRS) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1951 | return; |
| 1952 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1953 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1954 | hdr_val = "Proxy-Connection"; |
| 1955 | hdr_len = 16; |
| 1956 | } |
| 1957 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1958 | ctx.idx = 0; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1959 | txn->flags &= ~(TX_CON_KAL_SET|TX_CON_CLO_SET); |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1960 | 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] | 1961 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 1962 | txn->flags |= TX_HDR_CONN_KAL; |
| 1963 | if ((to_del & 2) && buf) |
| 1964 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
| 1965 | else |
| 1966 | txn->flags |= TX_CON_KAL_SET; |
| 1967 | } |
| 1968 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 1969 | txn->flags |= TX_HDR_CONN_CLO; |
| 1970 | if ((to_del & 1) && buf) |
| 1971 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
| 1972 | else |
| 1973 | txn->flags |= TX_CON_CLO_SET; |
| 1974 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1975 | } |
| 1976 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1977 | txn->flags |= TX_HDR_CONN_PRS; |
| 1978 | return; |
| 1979 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 1980 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1981 | /* Apply desired changes on the Connection: header. Values may be removed and/or |
| 1982 | * added depending on the <wanted> flags, which are exclusively composed of |
| 1983 | * TX_CON_CLO_SET and TX_CON_KAL_SET, depending on what flags are desired. The |
| 1984 | * TX_CON_*_SET flags are adjusted in txn->flags depending on what is left. |
| 1985 | */ |
| 1986 | void http_change_connection_header(struct http_txn *txn, struct http_msg *msg, struct buffer *buf, int wanted) |
| 1987 | { |
| 1988 | struct hdr_ctx ctx; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1989 | const char *hdr_val = "Connection"; |
| 1990 | int hdr_len = 10; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 1991 | |
| 1992 | ctx.idx = 0; |
| 1993 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 1994 | |
| 1995 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 1996 | hdr_val = "Proxy-Connection"; |
| 1997 | hdr_len = 16; |
| 1998 | } |
| 1999 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2000 | txn->flags &= ~(TX_CON_CLO_SET | TX_CON_KAL_SET); |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2001 | 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] | 2002 | if (ctx.vlen >= 10 && word_match(ctx.line + ctx.val, ctx.vlen, "keep-alive", 10)) { |
| 2003 | if (wanted & TX_CON_KAL_SET) |
| 2004 | txn->flags |= TX_CON_KAL_SET; |
| 2005 | else |
| 2006 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2007 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2008 | else if (ctx.vlen >= 5 && word_match(ctx.line + ctx.val, ctx.vlen, "close", 5)) { |
| 2009 | if (wanted & TX_CON_CLO_SET) |
| 2010 | txn->flags |= TX_CON_CLO_SET; |
| 2011 | else |
| 2012 | http_remove_header2(msg, buf, &txn->hdr_idx, &ctx); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2013 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2014 | } |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2015 | |
| 2016 | if (wanted == (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 2017 | return; |
| 2018 | |
| 2019 | if ((wanted & TX_CON_CLO_SET) && !(txn->flags & TX_CON_CLO_SET)) { |
| 2020 | txn->flags |= TX_CON_CLO_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2021 | hdr_val = "Connection: close"; |
| 2022 | hdr_len = 17; |
| 2023 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2024 | hdr_val = "Proxy-Connection: close"; |
| 2025 | hdr_len = 23; |
| 2026 | } |
| 2027 | 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] | 2028 | } |
| 2029 | |
| 2030 | if ((wanted & TX_CON_KAL_SET) && !(txn->flags & TX_CON_KAL_SET)) { |
| 2031 | txn->flags |= TX_CON_KAL_SET; |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2032 | hdr_val = "Connection: keep-alive"; |
| 2033 | hdr_len = 22; |
| 2034 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 2035 | hdr_val = "Proxy-Connection: keep-alive"; |
| 2036 | hdr_len = 28; |
| 2037 | } |
| 2038 | 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] | 2039 | } |
| 2040 | return; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2041 | } |
| 2042 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2043 | /* Parse the chunk size at buf->lr. Once done, it adjusts ->lr to point to the |
| 2044 | * first byte of body, and increments msg->sov by the number of bytes parsed, |
| 2045 | * so that we know we can forward between ->som and ->sov. Note that due to |
| 2046 | * possible wrapping at the end of the buffer, it is possible that msg->sov is |
| 2047 | * lower than msg->som. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2048 | * 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] | 2049 | * 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] | 2050 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2051 | int http_parse_chunk_size(struct buffer *buf, struct http_msg *msg) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2052 | { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2053 | char *ptr = buf->lr; |
| 2054 | char *end = buf->data + buf->size; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2055 | unsigned int chunk = 0; |
| 2056 | |
| 2057 | /* The chunk size is in the following form, though we are only |
| 2058 | * interested in the size and CRLF : |
| 2059 | * 1*HEXDIGIT *WSP *[ ';' extensions ] CRLF |
| 2060 | */ |
| 2061 | while (1) { |
| 2062 | int c; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2063 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2064 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2065 | c = hex2i(*ptr); |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2066 | if (c < 0) /* not a hex digit anymore */ |
| 2067 | break; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2068 | if (++ptr >= end) |
| 2069 | ptr = buf->data; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2070 | if (chunk & 0xF000000) /* overflow will occur */ |
| 2071 | return -1; |
| 2072 | chunk = (chunk << 4) + c; |
| 2073 | } |
| 2074 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2075 | /* empty size not allowed */ |
| 2076 | if (ptr == buf->lr) |
| 2077 | return -1; |
| 2078 | |
| 2079 | while (http_is_spht[(unsigned char)*ptr]) { |
| 2080 | if (++ptr >= end) |
| 2081 | ptr = buf->data; |
| 2082 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2083 | return 0; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2084 | } |
| 2085 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2086 | /* Up to there, we know that at least one byte is present at *ptr. Check |
| 2087 | * for the end of chunk size. |
| 2088 | */ |
| 2089 | while (1) { |
| 2090 | if (likely(HTTP_IS_CRLF(*ptr))) { |
| 2091 | /* we now have a CR or an LF at ptr */ |
| 2092 | if (likely(*ptr == '\r')) { |
| 2093 | if (++ptr >= end) |
| 2094 | ptr = buf->data; |
| 2095 | if (ptr == buf->r) |
| 2096 | return 0; |
| 2097 | } |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2098 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2099 | if (*ptr != '\n') |
| 2100 | return -1; |
| 2101 | if (++ptr >= end) |
| 2102 | ptr = buf->data; |
| 2103 | /* done */ |
| 2104 | break; |
| 2105 | } |
| 2106 | else if (*ptr == ';') { |
| 2107 | /* chunk extension, ends at next CRLF */ |
| 2108 | if (++ptr >= end) |
| 2109 | ptr = buf->data; |
| 2110 | if (ptr == buf->r) |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2111 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2112 | |
| 2113 | while (!HTTP_IS_CRLF(*ptr)) { |
| 2114 | if (++ptr >= end) |
| 2115 | ptr = buf->data; |
| 2116 | if (ptr == buf->r) |
| 2117 | return 0; |
| 2118 | } |
| 2119 | /* we have a CRLF now, loop above */ |
| 2120 | continue; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2121 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2122 | else |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2123 | return -1; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2124 | } |
| 2125 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2126 | /* OK we found our CRLF and now <ptr> points to the next byte, |
| 2127 | * which may or may not be present. We save that into ->lr and |
| 2128 | * ->sov. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2129 | */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2130 | msg->sov += ptr - buf->lr; |
| 2131 | buf->lr = ptr; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2132 | msg->hdr_content_len = chunk; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2133 | msg->msg_state = chunk ? HTTP_MSG_DATA : HTTP_MSG_TRAILERS; |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 2134 | return 1; |
| 2135 | } |
| 2136 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2137 | /* This function skips trailers in the buffer <buf> associated with HTTP |
| 2138 | * message <msg>. The first visited position is buf->lr. If the end of |
| 2139 | * the trailers is found, it is automatically scheduled to be forwarded, |
| 2140 | * msg->msg_state switches to HTTP_MSG_DONE, and the function returns >0. |
| 2141 | * If not enough data are available, the function does not change anything |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2142 | * except maybe buf->lr and msg->sov if it could parse some lines, and returns |
| 2143 | * zero. If a parse error is encountered, the function returns < 0 and does not |
| 2144 | * change anything except maybe buf->lr and msg->sov. Note that the message |
| 2145 | * must already be in HTTP_MSG_TRAILERS state before calling this function, |
| 2146 | * which implies that all non-trailers data have already been scheduled for |
| 2147 | * forwarding, and that the difference between msg->som and msg->sov exactly |
| 2148 | * matches the length of trailers already parsed and not forwarded. It is also |
| 2149 | * important to note that this function is designed to be able to parse wrapped |
| 2150 | * headers at end of buffer. |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2151 | */ |
| 2152 | int http_forward_trailers(struct buffer *buf, struct http_msg *msg) |
| 2153 | { |
| 2154 | /* we have buf->lr which points to next line. Look for CRLF. */ |
| 2155 | while (1) { |
| 2156 | char *p1 = NULL, *p2 = NULL; |
| 2157 | char *ptr = buf->lr; |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2158 | int bytes; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2159 | |
| 2160 | /* scan current line and stop at LF or CRLF */ |
| 2161 | while (1) { |
| 2162 | if (ptr == buf->r) |
| 2163 | return 0; |
| 2164 | |
| 2165 | if (*ptr == '\n') { |
| 2166 | if (!p1) |
| 2167 | p1 = ptr; |
| 2168 | p2 = ptr; |
| 2169 | break; |
| 2170 | } |
| 2171 | |
| 2172 | if (*ptr == '\r') { |
| 2173 | if (p1) |
| 2174 | return -1; |
| 2175 | p1 = ptr; |
| 2176 | } |
| 2177 | |
| 2178 | ptr++; |
| 2179 | if (ptr >= buf->data + buf->size) |
| 2180 | ptr = buf->data; |
| 2181 | } |
| 2182 | |
| 2183 | /* after LF; point to beginning of next line */ |
| 2184 | p2++; |
| 2185 | if (p2 >= buf->data + buf->size) |
| 2186 | p2 = buf->data; |
| 2187 | |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2188 | bytes = p2 - buf->lr; |
| 2189 | if (bytes < 0) |
| 2190 | bytes += buf->size; |
| 2191 | |
| 2192 | /* schedule this line for forwarding */ |
| 2193 | msg->sov += bytes; |
| 2194 | if (msg->sov >= buf->size) |
| 2195 | msg->sov -= buf->size; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2196 | |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2197 | if (p1 == buf->lr) { |
| 2198 | /* LF/CRLF at beginning of line => end of trailers at p2. |
| 2199 | * Everything was scheduled for forwarding, there's nothing |
| 2200 | * left from this message. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 2201 | */ |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 2202 | buf->lr = p2; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 2203 | msg->msg_state = HTTP_MSG_DONE; |
| 2204 | return 1; |
| 2205 | } |
| 2206 | /* OK, next line then */ |
| 2207 | buf->lr = p2; |
| 2208 | } |
| 2209 | } |
| 2210 | |
| 2211 | /* This function may be called only in HTTP_MSG_DATA_CRLF. It reads the CRLF or |
| 2212 | * a possible LF alone at the end of a chunk. It automatically adjusts msg->sov, |
| 2213 | * ->som, buf->lr in order to include this part into the next forwarding phase. |
| 2214 | * It also sets msg_state to HTTP_MSG_CHUNK_SIZE and returns >0 on success. If |
| 2215 | * not enough data are available, the function does not change anything and |
| 2216 | * returns zero. If a parse error is encountered, the function returns < 0 and |
| 2217 | * does not change anything. Note: this function is designed to parse wrapped |
| 2218 | * CRLF at the end of the buffer. |
| 2219 | */ |
| 2220 | int http_skip_chunk_crlf(struct buffer *buf, struct http_msg *msg) |
| 2221 | { |
| 2222 | char *ptr; |
| 2223 | int bytes; |
| 2224 | |
| 2225 | /* NB: we'll check data availabilty at the end. It's not a |
| 2226 | * problem because whatever we match first will be checked |
| 2227 | * against the correct length. |
| 2228 | */ |
| 2229 | bytes = 1; |
| 2230 | ptr = buf->lr; |
| 2231 | if (*ptr == '\r') { |
| 2232 | bytes++; |
| 2233 | ptr++; |
| 2234 | if (ptr >= buf->data + buf->size) |
| 2235 | ptr = buf->data; |
| 2236 | } |
| 2237 | |
| 2238 | if (buf->l < bytes) |
| 2239 | return 0; |
| 2240 | |
| 2241 | if (*ptr != '\n') |
| 2242 | return -1; |
| 2243 | |
| 2244 | ptr++; |
| 2245 | if (ptr >= buf->data + buf->size) |
| 2246 | ptr = buf->data; |
| 2247 | buf->lr = ptr; |
| 2248 | /* prepare the CRLF to be forwarded. msg->som may be before data but we don't care */ |
| 2249 | msg->sov = ptr - buf->data; |
| 2250 | msg->som = msg->sov - bytes; |
| 2251 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 2252 | return 1; |
| 2253 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2254 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2255 | void http_buffer_heavy_realign(struct buffer *buf, struct http_msg *msg) |
| 2256 | { |
| 2257 | char *end = buf->data + buf->size; |
| 2258 | int off = buf->data + buf->size - buf->w; |
| 2259 | |
| 2260 | /* two possible cases : |
| 2261 | * - the buffer is in one contiguous block, we move it in-place |
| 2262 | * - the buffer is in two blocks, we move it via the trash |
| 2263 | */ |
| 2264 | if (buf->l) { |
Willy Tarreau | b97f199 | 2010-02-25 23:54:31 +0100 | [diff] [blame] | 2265 | if (buf->r <= buf->w) |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2266 | /* non-contiguous block */ |
Willy Tarreau | b97f199 | 2010-02-25 23:54:31 +0100 | [diff] [blame] | 2267 | buffer_bounce_realign(buf); |
| 2268 | else |
| 2269 | memmove(buf->data, buf->w, buf->l); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2270 | } |
| 2271 | |
| 2272 | /* adjust all known pointers */ |
| 2273 | buf->w = buf->data; |
| 2274 | buf->lr += off; if (buf->lr >= end) buf->lr -= buf->size; |
| 2275 | buf->r += off; if (buf->r >= end) buf->r -= buf->size; |
| 2276 | msg->sol += off; if (msg->sol >= end) msg->sol -= buf->size; |
| 2277 | msg->eol += off; if (msg->eol >= end) msg->eol -= buf->size; |
| 2278 | |
| 2279 | /* adjust relative pointers */ |
| 2280 | msg->som = 0; |
| 2281 | msg->eoh += off; if (msg->eoh >= buf->size) msg->eoh -= buf->size; |
| 2282 | msg->col += off; if (msg->col >= buf->size) msg->col -= buf->size; |
| 2283 | msg->sov += off; if (msg->sov >= buf->size) msg->sov -= buf->size; |
| 2284 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2285 | if (msg->err_pos >= 0) { |
| 2286 | msg->err_pos += off; |
| 2287 | if (msg->err_pos >= buf->size) |
| 2288 | msg->err_pos -= buf->size; |
| 2289 | } |
| 2290 | |
| 2291 | buf->flags &= ~BF_FULL; |
| 2292 | if (buf->l >= buffer_max_len(buf)) |
| 2293 | buf->flags |= BF_FULL; |
| 2294 | } |
| 2295 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2296 | /* This stream analyser waits for a complete HTTP request. It returns 1 if the |
| 2297 | * processing can continue on next analysers, or zero if it either needs more |
| 2298 | * data or wants to immediately abort the request (eg: timeout, error, ...). It |
| 2299 | * is tied to AN_REQ_WAIT_HTTP and may may remove itself from s->req->analysers |
| 2300 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 2301 | * abort. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2302 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2303 | 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] | 2304 | { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2305 | /* |
| 2306 | * We will parse the partial (or complete) lines. |
| 2307 | * We will check the request syntax, and also join multi-line |
| 2308 | * headers. An index of all the lines will be elaborated while |
| 2309 | * parsing. |
| 2310 | * |
| 2311 | * For the parsing, we use a 28 states FSM. |
| 2312 | * |
| 2313 | * Here is the information we currently have : |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 2314 | * req->data + msg->som = beginning of request |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2315 | * req->data + msg->eoh = end of processed headers / start of current one |
| 2316 | * msg->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2317 | * req->lr = first non-visited byte |
| 2318 | * req->r = end of data |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2319 | * |
| 2320 | * At end of parsing, we may perform a capture of the error (if any), and |
| 2321 | * we will set a few fields (msg->sol, txn->meth, sn->flags/SN_REDIRECTABLE). |
| 2322 | * We also check for monitor-uri, logging, HTTP/0.9 to 1.0 conversion, and |
| 2323 | * finally headers capture. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2324 | */ |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2325 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2326 | int cur_idx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2327 | int use_close_only; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2328 | struct http_txn *txn = &s->txn; |
| 2329 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2330 | struct hdr_ctx ctx; |
Willy Tarreau | 976f1ee | 2006-12-17 10:06:03 +0100 | [diff] [blame] | 2331 | |
Willy Tarreau | 6bf1736 | 2009-02-24 10:48:35 +0100 | [diff] [blame] | 2332 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 2333 | now_ms, __FUNCTION__, |
| 2334 | s, |
| 2335 | req, |
| 2336 | req->rex, req->wex, |
| 2337 | req->flags, |
| 2338 | req->l, |
| 2339 | req->analysers); |
| 2340 | |
Willy Tarreau | 52a0c60 | 2009-08-16 22:45:38 +0200 | [diff] [blame] | 2341 | /* we're speaking HTTP here, so let's speak HTTP to the client */ |
| 2342 | s->srv_error = http_return_srv_error; |
| 2343 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2344 | /* There's a protected area at the end of the buffer for rewriting |
| 2345 | * purposes. We don't want to start to parse the request if the |
| 2346 | * protected area is affected, because we may have to move processed |
| 2347 | * data later, which is much more complicated. |
| 2348 | */ |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2349 | if (req->l && msg->msg_state < HTTP_MSG_ERROR) { |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2350 | if ((txn->flags & TX_NOT_FIRST) && |
| 2351 | unlikely((req->flags & BF_FULL) || |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2352 | req->r < req->lr || |
| 2353 | req->r > req->data + req->size - global.tune.maxrewrite)) { |
| 2354 | if (req->send_max) { |
| 2355 | /* some data has still not left the buffer, wake us once that's done */ |
| 2356 | buffer_dont_connect(req); |
| 2357 | req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 2358 | return 0; |
| 2359 | } |
| 2360 | if (req->l <= req->size - global.tune.maxrewrite) |
| 2361 | http_buffer_heavy_realign(req, msg); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2362 | } |
| 2363 | |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2364 | /* Note that we have the same problem with the response ; we |
| 2365 | * may want to send a redirect, error or anything which requires |
| 2366 | * some spare space. So we'll ensure that we have at least |
| 2367 | * maxrewrite bytes available in the response buffer before |
| 2368 | * processing that one. This will only affect pipelined |
| 2369 | * keep-alive requests. |
| 2370 | */ |
| 2371 | if ((txn->flags & TX_NOT_FIRST) && |
| 2372 | unlikely((s->rep->flags & BF_FULL) || |
| 2373 | s->rep->r < s->rep->lr || |
| 2374 | s->rep->r > s->rep->data + s->rep->size - global.tune.maxrewrite)) { |
| 2375 | if (s->rep->send_max) { |
| 2376 | /* don't let a connection request be initiated */ |
| 2377 | buffer_dont_connect(req); |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2378 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 2379 | return 0; |
| 2380 | } |
| 2381 | } |
| 2382 | |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 2383 | if (likely(req->lr < req->r)) |
| 2384 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 2385 | } |
| 2386 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2387 | /* 1: we might have to print this header in debug mode */ |
| 2388 | if (unlikely((global.mode & MODE_DEBUG) && |
| 2389 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2390 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2391 | char *eol, *sol; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2392 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2393 | sol = msg->sol; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2394 | eol = sol + msg->sl.rq.l; |
| 2395 | debug_hdr("clireq", s, sol, eol); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 2396 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2397 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 2398 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2399 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2400 | while (cur_idx) { |
| 2401 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 2402 | debug_hdr("clihdr", s, sol, eol); |
| 2403 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 2404 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2405 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2406 | } |
| 2407 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2408 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2409 | /* |
| 2410 | * Now we quickly check if we have found a full valid request. |
| 2411 | * If not so, we check the FD and buffer states before leaving. |
| 2412 | * A full request is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2413 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2414 | * requests are checked first. When waiting for a second request |
| 2415 | * on a keep-alive session, if we encounter and error, close, t/o, |
| 2416 | * we note the error in the session flags but don't set any state. |
| 2417 | * Since the error will be noted there, it will not be counted by |
| 2418 | * process_session() as a frontend error. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2419 | */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2420 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2421 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2422 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2423 | * First, let's catch bad requests. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2424 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2425 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
| 2426 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2427 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2428 | /* 1: Since we are in header mode, if there's no space |
| 2429 | * left for headers, we won't be able to free more |
| 2430 | * later, so the session will never terminate. We |
| 2431 | * must terminate it now. |
| 2432 | */ |
| 2433 | if (unlikely(req->flags & BF_FULL)) { |
| 2434 | /* FIXME: check if URI is set and return Status |
| 2435 | * 414 Request URI too long instead. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2436 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2437 | goto return_bad_req; |
| 2438 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2439 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2440 | /* 2: have we encountered a read error ? */ |
| 2441 | else if (req->flags & BF_READ_ERROR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2442 | if (!(s->flags & SN_ERR_MASK)) |
| 2443 | s->flags |= SN_ERR_CLICL; |
| 2444 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2445 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2446 | goto failed_keep_alive; |
| 2447 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2448 | /* we cannot return any message on error */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2449 | if (msg->err_pos >= 0) |
| 2450 | 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] | 2451 | msg->msg_state = HTTP_MSG_ERROR; |
| 2452 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2453 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2454 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2455 | if (s->listener->counters) |
| 2456 | s->listener->counters->failed_req++; |
| 2457 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2458 | if (!(s->flags & SN_FINST_MASK)) |
| 2459 | s->flags |= SN_FINST_R; |
| 2460 | return 0; |
| 2461 | } |
Willy Tarreau | f9839bd | 2008-08-27 23:57:16 +0200 | [diff] [blame] | 2462 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2463 | /* 3: has the read timeout expired ? */ |
| 2464 | 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] | 2465 | if (!(s->flags & SN_ERR_MASK)) |
| 2466 | s->flags |= SN_ERR_CLITO; |
| 2467 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2468 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2469 | goto failed_keep_alive; |
| 2470 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2471 | /* read timeout : give up with an error message. */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2472 | if (msg->err_pos >= 0) |
| 2473 | 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] | 2474 | txn->status = 408; |
| 2475 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
| 2476 | msg->msg_state = HTTP_MSG_ERROR; |
| 2477 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2478 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2479 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2480 | if (s->listener->counters) |
| 2481 | s->listener->counters->failed_req++; |
| 2482 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2483 | if (!(s->flags & SN_FINST_MASK)) |
| 2484 | s->flags |= SN_FINST_R; |
| 2485 | return 0; |
| 2486 | } |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 2487 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2488 | /* 4: have we encountered a close ? */ |
| 2489 | else if (req->flags & BF_SHUTR) { |
Willy Tarreau | d3c343f | 2010-01-16 10:26:19 +0100 | [diff] [blame] | 2490 | if (!(s->flags & SN_ERR_MASK)) |
| 2491 | s->flags |= SN_ERR_CLICL; |
| 2492 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2493 | if (txn->flags & TX_WAIT_NEXT_RQ) |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2494 | goto failed_keep_alive; |
| 2495 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2496 | if (msg->err_pos >= 0) |
| 2497 | 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] | 2498 | txn->status = 400; |
| 2499 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 2500 | msg->msg_state = HTTP_MSG_ERROR; |
| 2501 | req->analysers = 0; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2502 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2503 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2504 | if (s->listener->counters) |
| 2505 | s->listener->counters->failed_req++; |
| 2506 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2507 | if (!(s->flags & SN_FINST_MASK)) |
| 2508 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 2509 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2510 | } |
| 2511 | |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2512 | buffer_dont_connect(req); |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2513 | req->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2514 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 1b194fe | 2009-03-21 21:10:04 +0100 | [diff] [blame] | 2515 | |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2516 | if ((msg->msg_state != HTTP_MSG_RQBEFORE) && (txn->flags & TX_WAIT_NEXT_RQ)) { |
| 2517 | /* If the client starts to talk, let's fall back to |
| 2518 | * request timeout processing. |
| 2519 | */ |
| 2520 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2521 | req->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 2522 | } |
| 2523 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2524 | /* just set the request timeout once at the beginning of the request */ |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2525 | if (!tick_isset(req->analyse_exp)) { |
| 2526 | if ((msg->msg_state == HTTP_MSG_RQBEFORE) && |
| 2527 | (txn->flags & TX_WAIT_NEXT_RQ) && |
| 2528 | tick_isset(s->be->timeout.httpka)) |
| 2529 | req->analyse_exp = tick_add(now_ms, s->be->timeout.httpka); |
| 2530 | else |
| 2531 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
| 2532 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2533 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2534 | /* we're not ready yet */ |
| 2535 | return 0; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2536 | |
| 2537 | failed_keep_alive: |
| 2538 | /* Here we process low-level errors for keep-alive requests. In |
| 2539 | * short, if the request is not the first one and it experiences |
| 2540 | * a timeout, read error or shutdown, we just silently close so |
| 2541 | * that the client can try again. |
| 2542 | */ |
| 2543 | txn->status = 0; |
| 2544 | msg->msg_state = HTTP_MSG_RQBEFORE; |
| 2545 | req->analysers = 0; |
| 2546 | s->logs.logwait = 0; |
Willy Tarreau | ff7b588 | 2010-01-22 14:41:29 +0100 | [diff] [blame] | 2547 | s->rep->flags &= ~BF_EXPECT_MORE; /* speed up sending a previous response */ |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 2548 | stream_int_retnclose(req->prod, NULL); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2549 | return 0; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2550 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2551 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2552 | /* OK now we have a complete HTTP request with indexed headers. Let's |
| 2553 | * complete the request parsing by setting a few fields we will need |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 2554 | * later. At this point, we have the last CRLF at req->data + msg->eoh. |
| 2555 | * If the request is in HTTP/0.9 form, the rule is still true, and eoh |
| 2556 | * points to the CRLF of the request line. req->lr points to the first |
| 2557 | * byte after the last LF. msg->col and msg->sov point to the first |
| 2558 | * byte of data. msg->eol cannot be trusted because it may have been |
| 2559 | * left uninitialized (for instance in the absence of headers). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2560 | */ |
Willy Tarreau | 9cdde23 | 2007-05-02 20:58:19 +0200 | [diff] [blame] | 2561 | |
Willy Tarreau | d9b587f | 2010-02-26 10:05:55 +0100 | [diff] [blame^] | 2562 | proxy_inc_fe_req_ctr(s->fe); /* one more valid request for this FE */ |
| 2563 | |
Willy Tarreau | b16a574 | 2010-01-10 14:46:16 +0100 | [diff] [blame] | 2564 | if (txn->flags & TX_WAIT_NEXT_RQ) { |
| 2565 | /* kill the pending keep-alive timeout */ |
| 2566 | txn->flags &= ~TX_WAIT_NEXT_RQ; |
| 2567 | req->analyse_exp = TICK_ETERNITY; |
| 2568 | } |
| 2569 | |
| 2570 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2571 | /* Maybe we found in invalid header name while we were configured not |
| 2572 | * to block on that, so we have to capture it now. |
| 2573 | */ |
| 2574 | if (unlikely(msg->err_pos >= 0)) |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 2575 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
| 2576 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2577 | /* |
| 2578 | * 1: identify the method |
| 2579 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2580 | txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2581 | |
| 2582 | /* we can make use of server redirect on GET and HEAD */ |
| 2583 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 2584 | s->flags |= SN_REDIRECTABLE; |
Willy Tarreau | fa7e102 | 2008-10-19 07:30:41 +0200 | [diff] [blame] | 2585 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2586 | /* |
| 2587 | * 2: check if the URI matches the monitor_uri. |
| 2588 | * We have to do this for every request which gets in, because |
| 2589 | * the monitor-uri is defined by the frontend. |
| 2590 | */ |
| 2591 | if (unlikely((s->fe->monitor_uri_len != 0) && |
| 2592 | (s->fe->monitor_uri_len == msg->sl.rq.u_l) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2593 | !memcmp(msg->sol + msg->sl.rq.u, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2594 | s->fe->monitor_uri, |
| 2595 | s->fe->monitor_uri_len))) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2596 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2597 | * We have found the monitor URI |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2598 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2599 | struct acl_cond *cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2600 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2601 | s->flags |= SN_MONITOR; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2602 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2603 | /* Check if we want to fail this monitor request or not */ |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2604 | list_for_each_entry(cond, &s->fe->mon_fail_cond, list) { |
| 2605 | 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] | 2606 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2607 | ret = acl_pass(ret); |
| 2608 | if (cond->pol == ACL_COND_UNLESS) |
| 2609 | ret = !ret; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2610 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2611 | if (ret) { |
| 2612 | /* we fail this request, let's return 503 service unavail */ |
| 2613 | txn->status = 503; |
| 2614 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_503)); |
| 2615 | goto return_prx_cond; |
Willy Tarreau | b80c230 | 2007-11-30 20:51:32 +0100 | [diff] [blame] | 2616 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2617 | } |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 2618 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2619 | /* nothing to fail, let's reply normaly */ |
| 2620 | txn->status = 200; |
| 2621 | stream_int_retnclose(req->prod, &http_200_chunk); |
| 2622 | goto return_prx_cond; |
| 2623 | } |
| 2624 | |
| 2625 | /* |
| 2626 | * 3: Maybe we have to copy the original REQURI for the logs ? |
| 2627 | * Note: we cannot log anymore if the request has been |
| 2628 | * classified as invalid. |
| 2629 | */ |
| 2630 | if (unlikely(s->logs.logwait & LW_REQ)) { |
| 2631 | /* we have a complete HTTP request that we must log */ |
| 2632 | if ((txn->uri = pool_alloc2(pool2_requri)) != NULL) { |
| 2633 | int urilen = msg->sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2634 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2635 | if (urilen >= REQURI_LEN) |
| 2636 | urilen = REQURI_LEN - 1; |
| 2637 | memcpy(txn->uri, &req->data[msg->som], urilen); |
| 2638 | txn->uri[urilen] = 0; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2639 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2640 | if (!(s->logs.logwait &= ~LW_REQ)) |
| 2641 | s->do_log(s); |
| 2642 | } else { |
| 2643 | Alert("HTTP logging : out of memory.\n"); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 2644 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2645 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2646 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2647 | /* 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] | 2648 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) |
| 2649 | goto return_bad_req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 2650 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2651 | /* ... and check if the request is HTTP/1.1 or above */ |
| 2652 | if ((msg->sl.rq.v_l == 8) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2653 | ((msg->sol[msg->sl.rq.v + 5] > '1') || |
| 2654 | ((msg->sol[msg->sl.rq.v + 5] == '1') && |
| 2655 | (msg->sol[msg->sl.rq.v + 7] >= '1')))) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2656 | txn->flags |= TX_REQ_VER_11; |
| 2657 | |
| 2658 | /* "connection" has not been parsed yet */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2659 | 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] | 2660 | |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 2661 | /* if the frontend has "option http-use-proxy-header", we'll check if |
| 2662 | * we have what looks like a proxied connection instead of a connection, |
| 2663 | * and in this case set the TX_USE_PX_CONN flag to use Proxy-connection. |
| 2664 | * Note that this is *not* RFC-compliant, however browsers and proxies |
| 2665 | * happen to do that despite being non-standard :-( |
| 2666 | * We consider that a request not beginning with either '/' or '*' is |
| 2667 | * a proxied connection, which covers both "scheme://location" and |
| 2668 | * CONNECT ip:port. |
| 2669 | */ |
| 2670 | if ((s->fe->options2 & PR_O2_USE_PXHDR) && |
| 2671 | msg->sol[msg->sl.rq.u] != '/' && msg->sol[msg->sl.rq.u] != '*') |
| 2672 | txn->flags |= TX_USE_PX_CONN; |
| 2673 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2674 | /* transfer length unknown*/ |
| 2675 | txn->flags &= ~TX_REQ_XFER_LEN; |
| 2676 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2677 | /* 5: we may need to capture headers */ |
| 2678 | if (unlikely((s->logs.logwait & LW_REQHDR) && s->fe->req_cap)) |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 2679 | capture_headers(msg->sol, &txn->hdr_idx, |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2680 | txn->req.cap, s->fe->req_cap); |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 2681 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2682 | /* 6: determine the transfer-length. |
| 2683 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 2684 | * the presence of a message-body in a REQUEST and its transfer length |
| 2685 | * must be determined that way (in order of precedence) : |
| 2686 | * 1. The presence of a message-body in a request is signaled by the |
| 2687 | * inclusion of a Content-Length or Transfer-Encoding header field |
| 2688 | * in the request's header fields. When a request message contains |
| 2689 | * both a message-body of non-zero length and a method that does |
| 2690 | * not define any semantics for that request message-body, then an |
| 2691 | * origin server SHOULD either ignore the message-body or respond |
| 2692 | * with an appropriate error message (e.g., 413). A proxy or |
| 2693 | * gateway, when presented the same request, SHOULD either forward |
| 2694 | * the request inbound with the message- body or ignore the |
| 2695 | * message-body when determining a response. |
| 2696 | * |
| 2697 | * 2. If a Transfer-Encoding header field (Section 9.7) is present |
| 2698 | * and the "chunked" transfer-coding (Section 6.2) is used, the |
| 2699 | * transfer-length is defined by the use of this transfer-coding. |
| 2700 | * If a Transfer-Encoding header field is present and the "chunked" |
| 2701 | * transfer-coding is not present, the transfer-length is defined |
| 2702 | * by the sender closing the connection. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2703 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2704 | * 3. If a Content-Length header field is present, its decimal value in |
| 2705 | * OCTETs represents both the entity-length and the transfer-length. |
| 2706 | * If a message is received with both a Transfer-Encoding header |
| 2707 | * field and a Content-Length header field, the latter MUST be ignored. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2708 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2709 | * 4. By the server closing the connection. (Closing the connection |
| 2710 | * cannot be used to indicate the end of a request body, since that |
| 2711 | * would leave no possibility for the server to send back a response.) |
| 2712 | * |
| 2713 | * Whenever a transfer-coding is applied to a message-body, the set of |
| 2714 | * transfer-codings MUST include "chunked", unless the message indicates |
| 2715 | * it is terminated by closing the connection. When the "chunked" |
| 2716 | * transfer-coding is used, it MUST be the last transfer-coding applied |
| 2717 | * to the message-body. |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2718 | */ |
| 2719 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2720 | use_close_only = 0; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2721 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2722 | /* set TE_CHNK and XFER_LEN only if "chunked" is seen last */ |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 2723 | while ((txn->flags & TX_REQ_VER_11) && |
| 2724 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2725 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
| 2726 | txn->flags |= (TX_REQ_TE_CHNK | TX_REQ_XFER_LEN); |
| 2727 | else if (txn->flags & TX_REQ_TE_CHNK) { |
| 2728 | /* bad transfer-encoding (chunked followed by something else) */ |
| 2729 | use_close_only = 1; |
| 2730 | txn->flags &= ~(TX_REQ_TE_CHNK | TX_REQ_XFER_LEN); |
| 2731 | break; |
| 2732 | } |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2733 | } |
| 2734 | |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2735 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2736 | while (!(txn->flags & TX_REQ_TE_CHNK) && !use_close_only && |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2737 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) { |
| 2738 | signed long long cl; |
| 2739 | |
| 2740 | if (!ctx.vlen) |
| 2741 | goto return_bad_req; |
| 2742 | |
| 2743 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) |
| 2744 | goto return_bad_req; /* parse failure */ |
| 2745 | |
| 2746 | if (cl < 0) |
| 2747 | goto return_bad_req; |
| 2748 | |
| 2749 | if ((txn->flags & TX_REQ_CNT_LEN) && (msg->hdr_content_len != cl)) |
| 2750 | goto return_bad_req; /* already specified, was different */ |
| 2751 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2752 | txn->flags |= TX_REQ_CNT_LEN | TX_REQ_XFER_LEN; |
Willy Tarreau | 32b47f4 | 2009-10-18 20:55:02 +0200 | [diff] [blame] | 2753 | msg->hdr_content_len = cl; |
| 2754 | } |
| 2755 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 2756 | /* bodyless requests have a known length */ |
| 2757 | if (!use_close_only) |
| 2758 | txn->flags |= TX_REQ_XFER_LEN; |
| 2759 | |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2760 | /* end of job, return OK */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2761 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2762 | req->analyse_exp = TICK_ETERNITY; |
| 2763 | return 1; |
| 2764 | |
| 2765 | return_bad_req: |
| 2766 | /* We centralize bad requests processing here */ |
| 2767 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 2768 | /* we detected a parsing error. We want to archive this request |
| 2769 | * in the dedicated proxy area for later troubleshooting. |
| 2770 | */ |
| 2771 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
| 2772 | } |
| 2773 | |
| 2774 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 2775 | txn->status = 400; |
| 2776 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2777 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 2778 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 2779 | if (s->listener->counters) |
| 2780 | s->listener->counters->failed_req++; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2781 | |
| 2782 | return_prx_cond: |
| 2783 | if (!(s->flags & SN_ERR_MASK)) |
| 2784 | s->flags |= SN_ERR_PRXCOND; |
| 2785 | if (!(s->flags & SN_FINST_MASK)) |
| 2786 | s->flags |= SN_FINST_R; |
| 2787 | |
| 2788 | req->analysers = 0; |
| 2789 | req->analyse_exp = TICK_ETERNITY; |
| 2790 | return 0; |
| 2791 | } |
| 2792 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2793 | /* This stream analyser runs all HTTP request processing which is common to |
| 2794 | * frontends and backends, which means blocking ACLs, filters, connection-close, |
| 2795 | * reqadd, stats and redirects. This is performed for the designated proxy. |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2796 | * 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] | 2797 | * either needs more data or wants to immediately abort the request (eg: deny, |
| 2798 | * error, ...). |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2799 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2800 | 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] | 2801 | { |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2802 | struct http_txn *txn = &s->txn; |
| 2803 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2804 | struct acl_cond *cond; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2805 | struct req_acl_rule *req_acl, *req_acl_final = NULL; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2806 | struct redirect_rule *rule; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 2807 | struct cond_wordlist *wl; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 2808 | int del_ka, del_cl, do_stats; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2809 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 2810 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2811 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2812 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 2813 | return 0; |
| 2814 | } |
| 2815 | |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 2816 | req->analysers &= ~an_bit; |
Willy Tarreau | d787e66 | 2009-07-07 10:14:51 +0200 | [diff] [blame] | 2817 | req->analyse_exp = TICK_ETERNITY; |
| 2818 | |
| 2819 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 2820 | now_ms, __FUNCTION__, |
| 2821 | s, |
| 2822 | req, |
| 2823 | req->rex, req->wex, |
| 2824 | req->flags, |
| 2825 | req->l, |
| 2826 | req->analysers); |
| 2827 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2828 | /* first check whether we have some ACLs set to block this request */ |
| 2829 | list_for_each_entry(cond, &px->block_cond, list) { |
| 2830 | int ret = acl_exec_cond(cond, px, s, txn, ACL_DIR_REQ); |
Willy Tarreau | b463dfb | 2008-06-07 23:08:56 +0200 | [diff] [blame] | 2831 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2832 | ret = acl_pass(ret); |
| 2833 | if (cond->pol == ACL_COND_UNLESS) |
| 2834 | ret = !ret; |
Willy Tarreau | 53b6c74 | 2006-12-17 13:37:46 +0100 | [diff] [blame] | 2835 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2836 | if (ret) { |
| 2837 | txn->status = 403; |
| 2838 | /* let's log the request time */ |
| 2839 | s->logs.tv_request = now; |
| 2840 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
| 2841 | goto return_prx_cond; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2842 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2843 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2844 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 2845 | do_stats = stats_check_uri(s, px); |
| 2846 | |
| 2847 | list_for_each_entry(req_acl, (do_stats?&px->uri_auth->req_acl:&px->req_acl), list) { |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2848 | int ret = 1; |
| 2849 | |
| 2850 | if (req_acl->action >= PR_REQ_ACL_ACT_MAX) |
| 2851 | continue; |
| 2852 | |
| 2853 | /* check condition, but only if attached */ |
Krzysztof Olędzki | 711ad9e | 2010-02-01 12:36:53 +0100 | [diff] [blame] | 2854 | if (req_acl->cond) { |
| 2855 | ret = acl_exec_cond(req_acl->cond, px, s, txn, ACL_DIR_REQ); |
| 2856 | ret = acl_pass(ret); |
Willy Tarreau | 5142594 | 2010-02-01 10:40:19 +0100 | [diff] [blame] | 2857 | |
Krzysztof Olędzki | 711ad9e | 2010-02-01 12:36:53 +0100 | [diff] [blame] | 2858 | if (req_acl->cond->pol == ACL_COND_UNLESS) |
| 2859 | ret = !ret; |
| 2860 | } |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2861 | |
| 2862 | if (ret) { |
| 2863 | req_acl_final = req_acl; |
| 2864 | break; |
| 2865 | } |
| 2866 | } |
| 2867 | |
| 2868 | if (req_acl_final && req_acl_final->action == PR_REQ_ACL_ACT_DENY) { |
| 2869 | txn->status = 403; |
| 2870 | s->logs.tv_request = now; |
| 2871 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
| 2872 | goto return_prx_cond; |
| 2873 | } |
| 2874 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2875 | /* try headers filters */ |
| 2876 | if (px->req_exp != NULL) { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 2877 | if (apply_filters_to_request(s, req, px) < 0) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2878 | goto return_bad_req; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2879 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 2880 | /* has the request been denied ? */ |
| 2881 | if (txn->flags & TX_CLDENY) { |
| 2882 | /* no need to go further */ |
| 2883 | txn->status = 403; |
| 2884 | /* let's log the request time */ |
| 2885 | s->logs.tv_request = now; |
| 2886 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_403)); |
| 2887 | goto return_prx_cond; |
| 2888 | } |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2889 | |
| 2890 | /* When a connection is tarpitted, we use the tarpit timeout, |
| 2891 | * which may be the same as the connect timeout if unspecified. |
| 2892 | * If unset, then set it to zero because we really want it to |
| 2893 | * eventually expire. We build the tarpit as an analyser. |
| 2894 | */ |
| 2895 | if (txn->flags & TX_CLTARPIT) { |
| 2896 | buffer_erase(s->req); |
| 2897 | /* wipe the request out so that we can drop the connection early |
| 2898 | * if the client closes first. |
| 2899 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 2900 | buffer_dont_connect(req); |
Willy Tarreau | c465fd7 | 2009-08-31 00:17:18 +0200 | [diff] [blame] | 2901 | req->analysers = 0; /* remove switching rules etc... */ |
| 2902 | req->analysers |= AN_REQ_HTTP_TARPIT; |
| 2903 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.tarpit); |
| 2904 | if (!req->analyse_exp) |
| 2905 | req->analyse_exp = tick_add(now_ms, 0); |
| 2906 | return 1; |
| 2907 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2908 | } |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 2909 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2910 | /* Until set to anything else, the connection mode is set as TUNNEL. It will |
| 2911 | * only change if both the request and the config reference something else. |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2912 | * Option httpclose by itself does not set a mode, it remains a tunnel mode |
| 2913 | * 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] | 2914 | * affect it (eg: server-close/keep-alive + httpclose = close). Note that we |
| 2915 | * avoid to redo the same work if FE and BE have the same settings (common). |
| 2916 | * The method consists in checking if options changed between the two calls |
| 2917 | * (implying that either one is non-null, or one of them is non-null and we |
| 2918 | * are there for the first time. |
Willy Tarreau | 4273664 | 2009-10-18 21:04:35 +0200 | [diff] [blame] | 2919 | */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2920 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2921 | del_cl = del_ka = 0; |
| 2922 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 2923 | if ((!(txn->flags & TX_HDR_CONN_PRS) && |
| 2924 | (s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO))) || |
| 2925 | ((s->fe->options & (PR_O_KEEPALIVE|PR_O_SERVER_CLO|PR_O_HTTP_CLOSE|PR_O_FORCE_CLO)) != |
| 2926 | (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] | 2927 | int tmp = TX_CON_WANT_TUN; |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2928 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2929 | if ((s->fe->options|s->be->options) & PR_O_KEEPALIVE) |
| 2930 | tmp = TX_CON_WANT_KAL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 2931 | if ((s->fe->options|s->be->options) & PR_O_SERVER_CLO) |
| 2932 | tmp = TX_CON_WANT_SCL; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2933 | if ((s->fe->options|s->be->options) & PR_O_FORCE_CLO) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2934 | tmp = TX_CON_WANT_CLO; |
| 2935 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2936 | if ((txn->flags & TX_CON_WANT_MSK) < tmp) |
| 2937 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | tmp; |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2938 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2939 | if (!(txn->flags & TX_HDR_CONN_PRS)) { |
| 2940 | /* parse the Connection header and possibly clean it */ |
| 2941 | int to_del = 0; |
| 2942 | if ((txn->flags & TX_REQ_VER_11) || |
| 2943 | (txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL) |
| 2944 | to_del |= 2; /* remove "keep-alive" */ |
| 2945 | if (!(txn->flags & TX_REQ_VER_11)) |
| 2946 | to_del |= 1; /* remove "close" */ |
| 2947 | http_parse_connection_header(txn, msg, req, to_del); |
Willy Tarreau | 0dfdf19 | 2010-01-05 11:33:11 +0100 | [diff] [blame] | 2948 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 2949 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2950 | /* check if client or config asks for explicit close in KAL/SCL */ |
| 2951 | if (((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 2952 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) && |
| 2953 | ((txn->flags & TX_HDR_CONN_CLO) || /* "connection: close" */ |
| 2954 | (txn->flags & (TX_REQ_VER_11|TX_HDR_CONN_KAL)) == 0 || /* no "connection: k-a" in 1.0 */ |
| 2955 | ((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] | 2956 | !(txn->flags & TX_REQ_XFER_LEN) || /* no length known => close */ |
| 2957 | s->fe->state == PR_STSTOPPED)) /* frontend is stopping */ |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 2958 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
| 2959 | } |
Willy Tarreau | 7859991 | 2009-10-17 20:12:21 +0200 | [diff] [blame] | 2960 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2961 | /* add request headers from the rule sets in the same order */ |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 2962 | list_for_each_entry(wl, &px->req_add, list) { |
Willy Tarreau | 8abd4cd | 2010-01-31 14:30:44 +0100 | [diff] [blame] | 2963 | if (wl->cond) { |
| 2964 | int ret = acl_exec_cond(wl->cond, px, s, txn, ACL_DIR_REQ); |
| 2965 | ret = acl_pass(ret); |
| 2966 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 2967 | ret = !ret; |
| 2968 | if (!ret) |
| 2969 | continue; |
| 2970 | } |
| 2971 | |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 2972 | 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] | 2973 | goto return_bad_req; |
| 2974 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 2975 | |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2976 | if (req_acl_final && req_acl_final->action == PR_REQ_ACL_ACT_HTTP_AUTH) { |
| 2977 | struct chunk msg; |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 2978 | char *realm = req_acl->http_auth.realm; |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2979 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 2980 | if (!realm) |
| 2981 | realm = do_stats?STATS_DEFAULT_REALM:px->id; |
| 2982 | |
Willy Tarreau | 844a7e7 | 2010-01-31 21:46:18 +0100 | [diff] [blame] | 2983 | sprintf(trash, (txn->flags & TX_USE_PX_CONN) ? HTTP_407_fmt : HTTP_401_fmt, realm); |
Krzysztof Piotr Oledzki | 59bb218 | 2010-01-29 17:58:21 +0100 | [diff] [blame] | 2984 | chunk_initlen(&msg, trash, sizeof(trash), strlen(trash)); |
| 2985 | txn->status = 401; |
| 2986 | stream_int_retnclose(req->prod, &msg); |
| 2987 | goto return_prx_cond; |
| 2988 | } |
| 2989 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 2990 | if (do_stats) { |
| 2991 | /* We need to provied stats for this request. |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 2992 | * FIXME!!! that one is rather dangerous, we want to |
| 2993 | * make it follow standard rules (eg: clear req->analysers). |
| 2994 | */ |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 2995 | |
| 2996 | s->logs.tv_request = now; |
| 2997 | s->data_source = DATA_SRC_STATS; |
| 2998 | s->data_state = DATA_ST_INIT; |
| 2999 | s->task->nice = -32; /* small boost for HTTP statistics */ |
| 3000 | stream_int_register_handler(s->rep->prod, http_stats_io_handler); |
| 3001 | s->rep->prod->private = s; |
| 3002 | s->rep->prod->st0 = s->rep->prod->st1 = 0; |
| 3003 | req->analysers = 0; |
| 3004 | |
| 3005 | return 0; |
| 3006 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3007 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 3008 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3009 | /* check whether we have some ACLs set to redirect this request */ |
| 3010 | list_for_each_entry(rule, &px->redirect_rules, list) { |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3011 | int ret = ACL_PAT_PASS; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3012 | |
Willy Tarreau | f285f54 | 2010-01-03 20:03:03 +0100 | [diff] [blame] | 3013 | if (rule->cond) { |
| 3014 | ret = acl_exec_cond(rule->cond, px, s, txn, ACL_DIR_REQ); |
| 3015 | ret = acl_pass(ret); |
| 3016 | if (rule->cond->pol == ACL_COND_UNLESS) |
| 3017 | ret = !ret; |
| 3018 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3019 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3020 | if (ret) { |
Willy Tarreau | 3bb9c23 | 2010-01-03 12:24:37 +0100 | [diff] [blame] | 3021 | struct chunk rdr = { .str = trash, .size = sizeof(trash), .len = 0 }; |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3022 | const char *msg_fmt; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3023 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3024 | /* build redirect message */ |
| 3025 | switch(rule->code) { |
| 3026 | case 303: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3027 | msg_fmt = HTTP_303; |
| 3028 | break; |
| 3029 | case 301: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3030 | msg_fmt = HTTP_301; |
| 3031 | break; |
| 3032 | case 302: |
| 3033 | default: |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3034 | msg_fmt = HTTP_302; |
| 3035 | break; |
| 3036 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3037 | |
Willy Tarreau | 3bb9c23 | 2010-01-03 12:24:37 +0100 | [diff] [blame] | 3038 | if (unlikely(!chunk_strcpy(&rdr, msg_fmt))) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3039 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3040 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3041 | switch(rule->type) { |
| 3042 | case REDIRECT_TYPE_PREFIX: { |
| 3043 | const char *path; |
| 3044 | int pathlen; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3045 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3046 | path = http_get_path(txn); |
| 3047 | /* build message using path */ |
| 3048 | if (path) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 3049 | 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] | 3050 | if (rule->flags & REDIRECT_FLAG_DROP_QS) { |
| 3051 | int qs = 0; |
| 3052 | while (qs < pathlen) { |
| 3053 | if (path[qs] == '?') { |
| 3054 | pathlen = qs; |
| 3055 | break; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3056 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3057 | qs++; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3058 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3059 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3060 | } else { |
| 3061 | path = "/"; |
| 3062 | pathlen = 1; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3063 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3064 | |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 3065 | if (rdr.len + rule->rdr_len + pathlen > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3066 | goto return_bad_req; |
| 3067 | |
| 3068 | /* add prefix. Note that if prefix == "/", we don't want to |
| 3069 | * add anything, otherwise it makes it hard for the user to |
| 3070 | * configure a self-redirection. |
| 3071 | */ |
| 3072 | if (rule->rdr_len != 1 || *rule->rdr_str != '/') { |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3073 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 3074 | rdr.len += rule->rdr_len; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3075 | } |
| 3076 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3077 | /* add path */ |
| 3078 | memcpy(rdr.str + rdr.len, path, pathlen); |
| 3079 | rdr.len += pathlen; |
Willy Tarreau | 81e3b4f | 2010-01-10 00:42:19 +0100 | [diff] [blame] | 3080 | |
| 3081 | /* append a slash at the end of the location is needed and missing */ |
| 3082 | if (rdr.len && rdr.str[rdr.len - 1] != '/' && |
| 3083 | (rule->flags & REDIRECT_FLAG_APPEND_SLASH)) { |
| 3084 | if (rdr.len > rdr.size - 5) |
| 3085 | goto return_bad_req; |
| 3086 | rdr.str[rdr.len] = '/'; |
| 3087 | rdr.len++; |
| 3088 | } |
| 3089 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3090 | break; |
| 3091 | } |
| 3092 | case REDIRECT_TYPE_LOCATION: |
| 3093 | default: |
Krzysztof Piotr Oledzki | 78abe61 | 2009-09-27 13:23:20 +0200 | [diff] [blame] | 3094 | if (rdr.len + rule->rdr_len > rdr.size - 4) |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3095 | goto return_bad_req; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3096 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3097 | /* add location */ |
| 3098 | memcpy(rdr.str + rdr.len, rule->rdr_str, rule->rdr_len); |
| 3099 | rdr.len += rule->rdr_len; |
| 3100 | break; |
| 3101 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3102 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3103 | if (rule->cookie_len) { |
| 3104 | memcpy(rdr.str + rdr.len, "\r\nSet-Cookie: ", 14); |
| 3105 | rdr.len += 14; |
| 3106 | memcpy(rdr.str + rdr.len, rule->cookie_str, rule->cookie_len); |
| 3107 | rdr.len += rule->cookie_len; |
| 3108 | memcpy(rdr.str + rdr.len, "\r\n", 2); |
| 3109 | rdr.len += 2; |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3110 | } |
Willy Tarreau | 06b917c | 2009-07-06 16:34:52 +0200 | [diff] [blame] | 3111 | |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3112 | /* add end of headers and the keep-alive/close status. |
| 3113 | * We may choose to set keep-alive if the Location begins |
| 3114 | * with a slash, because the client will come back to the |
| 3115 | * same server. |
| 3116 | */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3117 | txn->status = rule->code; |
| 3118 | /* let's log the request time */ |
| 3119 | s->logs.tv_request = now; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3120 | |
| 3121 | if (rule->rdr_len >= 1 && *rule->rdr_str == '/' && |
| 3122 | (txn->flags & TX_REQ_XFER_LEN) && |
| 3123 | !(txn->flags & TX_REQ_TE_CHNK) && !txn->req.hdr_content_len && |
| 3124 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL || |
| 3125 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL)) { |
| 3126 | /* keep-alive possible */ |
Willy Tarreau | 7566145 | 2010-01-10 10:35:01 +0100 | [diff] [blame] | 3127 | if (!(txn->flags & TX_REQ_VER_11)) { |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3128 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3129 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: keep-alive", 30); |
| 3130 | rdr.len += 30; |
| 3131 | } else { |
| 3132 | memcpy(rdr.str + rdr.len, "\r\nConnection: keep-alive", 24); |
| 3133 | rdr.len += 24; |
| 3134 | } |
Willy Tarreau | 7566145 | 2010-01-10 10:35:01 +0100 | [diff] [blame] | 3135 | } |
| 3136 | memcpy(rdr.str + rdr.len, "\r\n\r\n", 4); |
| 3137 | rdr.len += 4; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3138 | buffer_write(req->prod->ob, rdr.str, rdr.len); |
| 3139 | /* "eat" the request */ |
| 3140 | buffer_ignore(req, msg->sov - msg->som); |
| 3141 | msg->som = msg->sov; |
| 3142 | req->analysers = AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 9300fb2 | 2010-01-05 00:58:24 +0100 | [diff] [blame] | 3143 | s->rep->analysers = AN_RES_HTTP_XFER_BODY; |
| 3144 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3145 | txn->rsp.msg_state = HTTP_MSG_DONE; |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3146 | break; |
| 3147 | } else { |
| 3148 | /* keep-alive not possible */ |
Willy Tarreau | 88d349d | 2010-01-25 12:15:43 +0100 | [diff] [blame] | 3149 | if (unlikely(txn->flags & TX_USE_PX_CONN)) { |
| 3150 | memcpy(rdr.str + rdr.len, "\r\nProxy-Connection: close\r\n\r\n", 29); |
| 3151 | rdr.len += 29; |
| 3152 | } else { |
| 3153 | memcpy(rdr.str + rdr.len, "\r\nConnection: close\r\n\r\n", 23); |
| 3154 | rdr.len += 23; |
| 3155 | } |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 3156 | stream_int_retnclose(req->prod, &rdr); |
Willy Tarreau | a9679ac | 2010-01-03 17:32:57 +0100 | [diff] [blame] | 3157 | goto return_prx_cond; |
| 3158 | } |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3159 | } |
| 3160 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3161 | |
Willy Tarreau | 2be3939 | 2010-01-03 17:24:51 +0100 | [diff] [blame] | 3162 | /* POST requests may be accompanied with an "Expect: 100-Continue" header. |
| 3163 | * If this happens, then the data will not come immediately, so we must |
| 3164 | * send all what we have without waiting. Note that due to the small gain |
| 3165 | * in waiting for the body of the request, it's easier to simply put the |
| 3166 | * BF_SEND_DONTWAIT flag any time. It's a one-shot flag so it will remove |
| 3167 | * itself once used. |
| 3168 | */ |
| 3169 | req->flags |= BF_SEND_DONTWAIT; |
| 3170 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3171 | /* that's OK for us now, let's move on to next analysers */ |
| 3172 | return 1; |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 3173 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3174 | return_bad_req: |
| 3175 | /* We centralize bad requests processing here */ |
| 3176 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
| 3177 | /* we detected a parsing error. We want to archive this request |
| 3178 | * in the dedicated proxy area for later troubleshooting. |
| 3179 | */ |
| 3180 | http_capture_bad_message(&s->fe->invalid_req, s, req, msg, s->fe); |
| 3181 | } |
Willy Tarreau | 55ea757 | 2007-06-17 19:56:27 +0200 | [diff] [blame] | 3182 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3183 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3184 | txn->status = 400; |
| 3185 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3186 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3187 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3188 | if (s->listener->counters) |
| 3189 | s->listener->counters->failed_req++; |
Willy Tarreau | 6e4261e | 2007-09-18 18:36:05 +0200 | [diff] [blame] | 3190 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3191 | return_prx_cond: |
| 3192 | if (!(s->flags & SN_ERR_MASK)) |
| 3193 | s->flags |= SN_ERR_PRXCOND; |
| 3194 | if (!(s->flags & SN_FINST_MASK)) |
| 3195 | s->flags |= SN_FINST_R; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 3196 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3197 | req->analysers = 0; |
| 3198 | req->analyse_exp = TICK_ETERNITY; |
| 3199 | return 0; |
| 3200 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3201 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3202 | /* This function performs all the processing enabled for the current request. |
| 3203 | * It returns 1 if the processing can continue on next analysers, or zero if it |
| 3204 | * needs more data, encounters an error, or wants to immediately abort the |
| 3205 | * request. It relies on buffers flags, and updates s->req->analysers. |
| 3206 | */ |
| 3207 | int http_process_request(struct session *s, struct buffer *req, int an_bit) |
| 3208 | { |
| 3209 | struct http_txn *txn = &s->txn; |
| 3210 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3211 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 3212 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3213 | /* we need more data */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3214 | buffer_dont_connect(req); |
Willy Tarreau | 51aecc7 | 2009-07-12 09:47:04 +0200 | [diff] [blame] | 3215 | return 0; |
| 3216 | } |
| 3217 | |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 3218 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 3219 | now_ms, __FUNCTION__, |
| 3220 | s, |
| 3221 | req, |
| 3222 | req->rex, req->wex, |
| 3223 | req->flags, |
| 3224 | req->l, |
| 3225 | req->analysers); |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3226 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3227 | /* |
| 3228 | * Right now, we know that we have processed the entire headers |
| 3229 | * and that unwanted requests have been filtered out. We can do |
| 3230 | * whatever we want with the remaining request. Also, now we |
| 3231 | * may have separate values for ->fe, ->be. |
| 3232 | */ |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3233 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3234 | /* |
| 3235 | * If HTTP PROXY is set we simply get remote server address |
| 3236 | * parsing incoming request. |
| 3237 | */ |
| 3238 | 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] | 3239 | 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] | 3240 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 3241 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3242 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3243 | * 7: Now we can work with the cookies. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3244 | * Note that doing so might move headers in the request, but |
| 3245 | * the fields will stay coherent and the URI will not move. |
| 3246 | * This should only be performed in the backend. |
| 3247 | */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 3248 | 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] | 3249 | && !(txn->flags & (TX_CLDENY|TX_CLTARPIT))) |
| 3250 | manage_client_side_cookies(s, req); |
Willy Tarreau | 7ac51f6 | 2007-03-25 16:00:04 +0200 | [diff] [blame] | 3251 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3252 | /* |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 3253 | * 8: the appsession cookie was looked up very early in 1.2, |
| 3254 | * so let's do the same now. |
| 3255 | */ |
| 3256 | |
| 3257 | /* It needs to look into the URI */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 3258 | if ((txn->sessid == NULL) && s->be->appsession_name) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 3259 | 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] | 3260 | } |
| 3261 | |
| 3262 | /* |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3263 | * 9: add X-Forwarded-For if either the frontend or the backend |
| 3264 | * asks for it. |
| 3265 | */ |
| 3266 | if ((s->fe->options | s->be->options) & PR_O_FWDFOR) { |
| 3267 | if (s->cli_addr.ss_family == AF_INET) { |
| 3268 | /* Add an X-Forwarded-For header unless the source IP is |
| 3269 | * in the 'except' network range. |
| 3270 | */ |
| 3271 | if ((!s->fe->except_mask.s_addr || |
| 3272 | (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->fe->except_mask.s_addr) |
| 3273 | != s->fe->except_net.s_addr) && |
| 3274 | (!s->be->except_mask.s_addr || |
| 3275 | (((struct sockaddr_in *)&s->cli_addr)->sin_addr.s_addr & s->be->except_mask.s_addr) |
| 3276 | != s->be->except_net.s_addr)) { |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3277 | int len; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3278 | unsigned char *pn; |
| 3279 | pn = (unsigned char *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr; |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3280 | |
| 3281 | /* Note: we rely on the backend to get the header name to be used for |
| 3282 | * x-forwarded-for, because the header is really meant for the backends. |
| 3283 | * However, if the backend did not specify any option, we have to rely |
| 3284 | * on the frontend's header name. |
| 3285 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3286 | if (s->be->fwdfor_hdr_len) { |
| 3287 | len = s->be->fwdfor_hdr_len; |
| 3288 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
Ross West | af72a1d | 2008-08-03 10:51:45 +0200 | [diff] [blame] | 3289 | } else { |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3290 | len = s->fe->fwdfor_hdr_len; |
| 3291 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3292 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3293 | 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] | 3294 | |
Willy Tarreau | 4af6f3a | 2007-03-18 22:36:26 +0100 | [diff] [blame] | 3295 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3296 | &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3297 | goto return_bad_req; |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 3298 | } |
| 3299 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3300 | else if (s->cli_addr.ss_family == AF_INET6) { |
| 3301 | /* FIXME: for the sake of completeness, we should also support |
| 3302 | * 'except' here, although it is mostly useless in this case. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3303 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3304 | int len; |
| 3305 | char pn[INET6_ADDRSTRLEN]; |
| 3306 | inet_ntop(AF_INET6, |
| 3307 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 3308 | pn, sizeof(pn)); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3309 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3310 | /* Note: we rely on the backend to get the header name to be used for |
| 3311 | * x-forwarded-for, because the header is really meant for the backends. |
| 3312 | * However, if the backend did not specify any option, we have to rely |
| 3313 | * on the frontend's header name. |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3314 | */ |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3315 | if (s->be->fwdfor_hdr_len) { |
| 3316 | len = s->be->fwdfor_hdr_len; |
| 3317 | memcpy(trash, s->be->fwdfor_hdr_name, len); |
| 3318 | } else { |
| 3319 | len = s->fe->fwdfor_hdr_len; |
| 3320 | memcpy(trash, s->fe->fwdfor_hdr_name, len); |
matt.farnsworth@nokia.com | 1c2ab96 | 2008-04-14 20:47:37 +0200 | [diff] [blame] | 3321 | } |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3322 | len += sprintf(trash + len, ": %s", pn); |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3323 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3324 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3325 | &txn->hdr_idx, trash, len) < 0)) |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3326 | goto return_bad_req; |
| 3327 | } |
| 3328 | } |
| 3329 | |
| 3330 | /* |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3331 | * 10: add X-Original-To if either the frontend or the backend |
| 3332 | * asks for it. |
| 3333 | */ |
| 3334 | if ((s->fe->options | s->be->options) & PR_O_ORGTO) { |
| 3335 | |
| 3336 | /* FIXME: don't know if IPv6 can handle that case too. */ |
| 3337 | if (s->cli_addr.ss_family == AF_INET) { |
| 3338 | /* Add an X-Original-To header unless the destination IP is |
| 3339 | * in the 'except' network range. |
| 3340 | */ |
| 3341 | if (!(s->flags & SN_FRT_ADDR_SET)) |
| 3342 | get_frt_addr(s); |
| 3343 | |
| 3344 | if ((!s->fe->except_mask_to.s_addr || |
| 3345 | (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->fe->except_mask_to.s_addr) |
| 3346 | != s->fe->except_to.s_addr) && |
| 3347 | (!s->be->except_mask_to.s_addr || |
| 3348 | (((struct sockaddr_in *)&s->frt_addr)->sin_addr.s_addr & s->be->except_mask_to.s_addr) |
| 3349 | != s->be->except_to.s_addr)) { |
| 3350 | int len; |
| 3351 | unsigned char *pn; |
| 3352 | pn = (unsigned char *)&((struct sockaddr_in *)&s->frt_addr)->sin_addr; |
| 3353 | |
| 3354 | /* Note: we rely on the backend to get the header name to be used for |
| 3355 | * x-original-to, because the header is really meant for the backends. |
| 3356 | * However, if the backend did not specify any option, we have to rely |
| 3357 | * on the frontend's header name. |
| 3358 | */ |
| 3359 | if (s->be->orgto_hdr_len) { |
| 3360 | len = s->be->orgto_hdr_len; |
| 3361 | memcpy(trash, s->be->orgto_hdr_name, len); |
| 3362 | } else { |
| 3363 | len = s->fe->orgto_hdr_len; |
| 3364 | memcpy(trash, s->fe->orgto_hdr_name, len); |
Willy Tarreau | b86db34 | 2009-11-30 11:50:16 +0100 | [diff] [blame] | 3365 | } |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3366 | len += sprintf(trash + len, ": %d.%d.%d.%d", pn[0], pn[1], pn[2], pn[3]); |
| 3367 | |
| 3368 | if (unlikely(http_header_add_tail2(req, &txn->req, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 3369 | &txn->hdr_idx, trash, len) < 0)) |
Maik Broemme | 2850cb4 | 2009-04-17 18:53:21 +0200 | [diff] [blame] | 3370 | goto return_bad_req; |
| 3371 | } |
| 3372 | } |
| 3373 | } |
| 3374 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3375 | /* 11: add "Connection: close" or "Connection: keep-alive" if needed and not yet set. */ |
| 3376 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
| 3377 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) { |
| 3378 | unsigned int want_flags = 0; |
| 3379 | |
| 3380 | if (txn->flags & TX_REQ_VER_11) { |
| 3381 | if ((txn->flags & TX_CON_WANT_MSK) >= TX_CON_WANT_SCL || |
| 3382 | ((s->fe->options|s->be->options) & PR_O_HTTP_CLOSE)) |
| 3383 | want_flags |= TX_CON_CLO_SET; |
| 3384 | } else { |
| 3385 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) |
| 3386 | want_flags |= TX_CON_KAL_SET; |
| 3387 | } |
| 3388 | |
| 3389 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 3390 | http_change_connection_header(txn, msg, req, want_flags); |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3391 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3392 | |
Willy Tarreau | bbf0b37 | 2010-01-18 16:54:40 +0100 | [diff] [blame] | 3393 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3394 | /* If we have no server assigned yet and we're balancing on url_param |
| 3395 | * with a POST request, we may be interested in checking the body for |
| 3396 | * that parameter. This will be done in another analyser. |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3397 | */ |
| 3398 | if (!(s->flags & (SN_ASSIGNED|SN_DIRECT)) && |
| 3399 | s->txn.meth == HTTP_METH_POST && s->be->url_param_name != NULL && |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3400 | s->be->url_param_post_limit != 0 && |
| 3401 | (txn->flags & (TX_REQ_CNT_LEN|TX_REQ_TE_CHNK)) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 3402 | 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] | 3403 | buffer_dont_connect(req); |
| 3404 | req->analysers |= AN_REQ_HTTP_BODY; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3405 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3406 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3407 | if (txn->flags & TX_REQ_XFER_LEN) |
| 3408 | req->analysers |= AN_REQ_HTTP_XFER_BODY; |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 3409 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3410 | /************************************************************* |
| 3411 | * OK, that's finished for the headers. We have done what we * |
| 3412 | * could. Let's switch to the DATA state. * |
| 3413 | ************************************************************/ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3414 | req->analyse_exp = TICK_ETERNITY; |
| 3415 | req->analysers &= ~an_bit; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3416 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3417 | s->logs.tv_request = now; |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3418 | /* OK let's go on with the BODY now */ |
| 3419 | return 1; |
Willy Tarreau | 0661926 | 2006-12-17 08:37:22 +0100 | [diff] [blame] | 3420 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3421 | return_bad_req: /* let's centralize all bad requests */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3422 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR) || msg->err_pos >= 0) { |
Willy Tarreau | f073a83 | 2009-03-01 23:21:47 +0100 | [diff] [blame] | 3423 | /* we detected a parsing error. We want to archive this request |
| 3424 | * in the dedicated proxy area for later troubleshooting. |
| 3425 | */ |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3426 | 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] | 3427 | } |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 3428 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3429 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3430 | txn->status = 400; |
| 3431 | req->analysers = 0; |
| 3432 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3433 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3434 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3435 | if (s->listener->counters) |
| 3436 | s->listener->counters->failed_req++; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3437 | |
Willy Tarreau | 59234e9 | 2008-11-30 23:51:27 +0100 | [diff] [blame] | 3438 | if (!(s->flags & SN_ERR_MASK)) |
| 3439 | s->flags |= SN_ERR_PRXCOND; |
| 3440 | if (!(s->flags & SN_FINST_MASK)) |
| 3441 | s->flags |= SN_FINST_R; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 3442 | return 0; |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 3443 | } |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 3444 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3445 | /* This function is an analyser which processes the HTTP tarpit. It always |
| 3446 | * returns zero, at the beginning because it prevents any other processing |
| 3447 | * from occurring, and at the end because it terminates the request. |
| 3448 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3449 | 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] | 3450 | { |
| 3451 | struct http_txn *txn = &s->txn; |
| 3452 | |
| 3453 | /* This connection is being tarpitted. The CLIENT side has |
| 3454 | * already set the connect expiration date to the right |
| 3455 | * timeout. We just have to check that the client is still |
| 3456 | * there and that the timeout has not expired. |
| 3457 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3458 | buffer_dont_connect(req); |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3459 | if ((req->flags & (BF_SHUTR|BF_READ_ERROR)) == 0 && |
| 3460 | !tick_is_expired(req->analyse_exp, now_ms)) |
| 3461 | return 0; |
| 3462 | |
| 3463 | /* We will set the queue timer to the time spent, just for |
| 3464 | * logging purposes. We fake a 500 server error, so that the |
| 3465 | * attacker will not suspect his connection has been tarpitted. |
| 3466 | * It will not cause trouble to the logs because we can exclude |
| 3467 | * the tarpitted connections by filtering on the 'PT' status flags. |
| 3468 | */ |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3469 | s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3470 | |
| 3471 | txn->status = 500; |
| 3472 | if (req->flags != BF_READ_ERROR) |
| 3473 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_500)); |
| 3474 | |
| 3475 | req->analysers = 0; |
| 3476 | req->analyse_exp = TICK_ETERNITY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3477 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 3478 | s->fe->counters.failed_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 3479 | if (s->listener->counters) |
| 3480 | s->listener->counters->failed_req++; |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3481 | |
Willy Tarreau | 60b85b0 | 2008-11-30 23:28:40 +0100 | [diff] [blame] | 3482 | if (!(s->flags & SN_ERR_MASK)) |
| 3483 | s->flags |= SN_ERR_PRXCOND; |
| 3484 | if (!(s->flags & SN_FINST_MASK)) |
| 3485 | s->flags |= SN_FINST_T; |
| 3486 | return 0; |
| 3487 | } |
| 3488 | |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3489 | /* This function is an analyser which processes the HTTP request body. It looks |
| 3490 | * for parameters to be used for the load balancing algorithm (url_param). It |
| 3491 | * must only be called after the standard HTTP request processing has occurred, |
| 3492 | * because it expects the request to be parsed. It returns zero if it needs to |
| 3493 | * read more data, or 1 once it has completed its analysis. |
| 3494 | */ |
Willy Tarreau | 3a81629 | 2009-07-07 10:55:49 +0200 | [diff] [blame] | 3495 | 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] | 3496 | { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3497 | struct http_txn *txn = &s->txn; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3498 | struct http_msg *msg = &s->txn.req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3499 | long long limit = s->be->url_param_post_limit; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3500 | |
| 3501 | /* We have to parse the HTTP request body to find any required data. |
| 3502 | * "balance url_param check_post" should have been the only way to get |
| 3503 | * into this. We were brought here after HTTP header analysis, so all |
| 3504 | * related structures are ready. |
| 3505 | */ |
| 3506 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3507 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 3508 | goto missing_data; |
| 3509 | |
| 3510 | if (msg->msg_state < HTTP_MSG_100_SENT) { |
| 3511 | /* If we have HTTP/1.1 and Expect: 100-continue, then we must |
| 3512 | * send an HTTP/1.1 100 Continue intermediate response. |
| 3513 | */ |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 3514 | if (txn->flags & TX_REQ_VER_11) { |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3515 | struct hdr_ctx ctx; |
| 3516 | ctx.idx = 0; |
| 3517 | /* Expect is allowed in 1.1, look for it */ |
| 3518 | if (http_find_header2("Expect", 6, msg->sol, &txn->hdr_idx, &ctx) && |
| 3519 | unlikely(ctx.vlen == 12 && strncasecmp(ctx.line+ctx.val, "100-continue", 12) == 0)) { |
| 3520 | buffer_write(s->rep, http_100_chunk.str, http_100_chunk.len); |
| 3521 | } |
| 3522 | } |
| 3523 | msg->msg_state = HTTP_MSG_100_SENT; |
| 3524 | } |
| 3525 | |
| 3526 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3527 | /* we have msg->col and msg->sov which both point to the first |
| 3528 | * byte of message body. msg->som still points to the beginning |
| 3529 | * of the message. We must save the body in req->lr because it |
| 3530 | * survives buffer re-alignments. |
| 3531 | */ |
| 3532 | req->lr = req->data + msg->sov; |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3533 | if (txn->flags & TX_REQ_TE_CHNK) |
| 3534 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 3535 | else |
| 3536 | msg->msg_state = HTTP_MSG_DATA; |
| 3537 | } |
| 3538 | |
| 3539 | if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3540 | /* read the chunk size and assign it to ->hdr_content_len, then |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3541 | * set ->sov and ->lr to point to the body and switch to DATA or |
| 3542 | * TRAILERS state. |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3543 | */ |
| 3544 | int ret = http_parse_chunk_size(req, msg); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3545 | |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3546 | if (!ret) |
| 3547 | goto missing_data; |
| 3548 | else if (ret < 0) |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3549 | goto return_bad_req; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3550 | } |
| 3551 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 3552 | /* Now we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state. |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3553 | * We have the first non-header byte in msg->col, which is either the |
| 3554 | * beginning of the chunk size or of the data. The first data byte is in |
| 3555 | * msg->sov, which is equal to msg->col when not using transfer-encoding. |
| 3556 | * 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] | 3557 | */ |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3558 | |
| 3559 | if (msg->hdr_content_len < limit) |
| 3560 | limit = msg->hdr_content_len; |
| 3561 | |
Willy Tarreau | 7c96f67 | 2009-12-27 22:47:25 +0100 | [diff] [blame] | 3562 | 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] | 3563 | goto http_end; |
| 3564 | |
| 3565 | missing_data: |
| 3566 | /* we get here if we need to wait for more data */ |
Willy Tarreau | 115acb9 | 2009-12-26 13:56:06 +0100 | [diff] [blame] | 3567 | if (req->flags & BF_FULL) |
| 3568 | goto return_bad_req; |
| 3569 | |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3570 | if ((req->flags & BF_READ_TIMEOUT) || tick_is_expired(req->analyse_exp, now_ms)) { |
| 3571 | txn->status = 408; |
| 3572 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_408)); |
| 3573 | goto return_err_msg; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3574 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3575 | |
| 3576 | /* we get here if we need to wait for more data */ |
| 3577 | if (!(req->flags & (BF_FULL | BF_READ_ERROR | BF_SHUTR))) { |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3578 | /* Not enough data. We'll re-use the http-request |
| 3579 | * timeout here. Ideally, we should set the timeout |
| 3580 | * relative to the accept() date. We just set the |
| 3581 | * request timeout once at the beginning of the |
| 3582 | * request. |
| 3583 | */ |
Willy Tarreau | 520d95e | 2009-09-19 21:04:57 +0200 | [diff] [blame] | 3584 | buffer_dont_connect(req); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3585 | if (!tick_isset(req->analyse_exp)) |
Willy Tarreau | cd7afc0 | 2009-07-12 10:03:17 +0200 | [diff] [blame] | 3586 | req->analyse_exp = tick_add_ifset(now_ms, s->be->timeout.httpreq); |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3587 | return 0; |
| 3588 | } |
Willy Tarreau | 522d6c0 | 2009-12-06 18:49:18 +0100 | [diff] [blame] | 3589 | |
| 3590 | http_end: |
| 3591 | /* The situation will not evolve, so let's give up on the analysis. */ |
| 3592 | s->logs.tv_request = now; /* update the request timer to reflect full request */ |
| 3593 | req->analysers &= ~an_bit; |
| 3594 | req->analyse_exp = TICK_ETERNITY; |
| 3595 | return 1; |
| 3596 | |
| 3597 | return_bad_req: /* let's centralize all bad requests */ |
| 3598 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3599 | txn->status = 400; |
| 3600 | stream_int_retnclose(req->prod, error_message(s, HTTP_ERR_400)); |
| 3601 | |
| 3602 | return_err_msg: |
| 3603 | req->analysers = 0; |
| 3604 | s->fe->counters.failed_req++; |
| 3605 | if (s->listener->counters) |
| 3606 | s->listener->counters->failed_req++; |
| 3607 | |
| 3608 | if (!(s->flags & SN_ERR_MASK)) |
| 3609 | s->flags |= SN_ERR_PRXCOND; |
| 3610 | if (!(s->flags & SN_FINST_MASK)) |
| 3611 | s->flags |= SN_FINST_R; |
| 3612 | return 0; |
Willy Tarreau | d34af78 | 2008-11-30 23:36:37 +0100 | [diff] [blame] | 3613 | } |
| 3614 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3615 | /* Terminate current transaction and prepare a new one. This is very tricky |
| 3616 | * right now but it works. |
| 3617 | */ |
| 3618 | void http_end_txn_clean_session(struct session *s) |
| 3619 | { |
| 3620 | /* FIXME: We need a more portable way of releasing a backend's and a |
| 3621 | * server's connections. We need a safer way to reinitialize buffer |
| 3622 | * flags. We also need a more accurate method for computing per-request |
| 3623 | * data. |
| 3624 | */ |
| 3625 | http_silent_debug(__LINE__, s); |
| 3626 | |
| 3627 | s->req->cons->flags |= SI_FL_NOLINGER; |
| 3628 | s->req->cons->shutr(s->req->cons); |
| 3629 | s->req->cons->shutw(s->req->cons); |
| 3630 | |
| 3631 | http_silent_debug(__LINE__, s); |
| 3632 | |
| 3633 | if (s->flags & SN_BE_ASSIGNED) |
| 3634 | s->be->beconn--; |
| 3635 | |
| 3636 | s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now); |
| 3637 | session_process_counters(s); |
| 3638 | |
| 3639 | if (s->txn.status) { |
| 3640 | int n; |
| 3641 | |
| 3642 | n = s->txn.status / 100; |
| 3643 | if (n < 1 || n > 5) |
| 3644 | n = 0; |
| 3645 | |
| 3646 | if (s->fe->mode == PR_MODE_HTTP) |
| 3647 | s->fe->counters.p.http.rsp[n]++; |
| 3648 | |
| 3649 | if ((s->flags & SN_BE_ASSIGNED) && (s->fe != s->be) && |
| 3650 | (s->be->mode == PR_MODE_HTTP)) |
| 3651 | s->be->counters.p.http.rsp[n]++; |
| 3652 | } |
| 3653 | |
| 3654 | /* don't count other requests' data */ |
| 3655 | s->logs.bytes_in -= s->req->l - s->req->send_max; |
| 3656 | s->logs.bytes_out -= s->rep->l - s->rep->send_max; |
| 3657 | |
| 3658 | /* let's do a final log if we need it */ |
| 3659 | if (s->logs.logwait && |
| 3660 | !(s->flags & SN_MONITOR) && |
| 3661 | (!(s->fe->options & PR_O_NULLNOLOG) || s->req->total)) { |
| 3662 | s->do_log(s); |
| 3663 | } |
| 3664 | |
| 3665 | s->logs.accept_date = date; /* user-visible date for logging */ |
| 3666 | s->logs.tv_accept = now; /* corrected date for internal use */ |
| 3667 | tv_zero(&s->logs.tv_request); |
| 3668 | s->logs.t_queue = -1; |
| 3669 | s->logs.t_connect = -1; |
| 3670 | s->logs.t_data = -1; |
| 3671 | s->logs.t_close = 0; |
| 3672 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 3673 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 3674 | |
| 3675 | s->logs.bytes_in = s->req->total = s->req->l - s->req->send_max; |
| 3676 | s->logs.bytes_out = s->rep->total = s->rep->l - s->rep->send_max; |
| 3677 | |
| 3678 | if (s->pend_pos) |
| 3679 | pendconn_free(s->pend_pos); |
| 3680 | |
| 3681 | if (s->srv) { |
| 3682 | if (s->flags & SN_CURR_SESS) { |
| 3683 | s->flags &= ~SN_CURR_SESS; |
| 3684 | s->srv->cur_sess--; |
| 3685 | } |
| 3686 | if (may_dequeue_tasks(s->srv, s->be)) |
| 3687 | process_srv_queue(s->srv); |
| 3688 | } |
| 3689 | |
| 3690 | if (unlikely(s->srv_conn)) |
| 3691 | sess_change_server(s, NULL); |
| 3692 | s->srv = NULL; |
| 3693 | |
| 3694 | s->req->cons->state = s->req->cons->prev_state = SI_ST_INI; |
| 3695 | s->req->cons->fd = -1; /* just to help with debugging */ |
| 3696 | s->req->cons->err_type = SI_ET_NONE; |
| 3697 | s->req->cons->err_loc = NULL; |
| 3698 | s->req->cons->exp = TICK_ETERNITY; |
| 3699 | s->req->cons->flags = SI_FL_NONE; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3700 | s->req->flags &= ~(BF_SHUTW|BF_SHUTW_NOW|BF_AUTO_CONNECT|BF_WRITE_ERROR|BF_STREAMER|BF_STREAMER_FAST); |
| 3701 | 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] | 3702 | 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] | 3703 | s->flags &= ~(SN_CURR_SESS|SN_REDIRECTABLE); |
| 3704 | s->txn.meth = 0; |
| 3705 | http_reset_txn(s); |
Willy Tarreau | fcffa69 | 2010-01-10 14:21:19 +0100 | [diff] [blame] | 3706 | s->txn.flags |= TX_NOT_FIRST | TX_WAIT_NEXT_RQ; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3707 | if (s->be->options2 & PR_O2_INDEPSTR) |
| 3708 | s->req->cons->flags |= SI_FL_INDEP_STR; |
| 3709 | |
| 3710 | /* if the request buffer is not empty, it means we're |
| 3711 | * about to process another request, so send pending |
| 3712 | * data with MSG_MORE to merge TCP packets when possible. |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 3713 | * Just don't do this if the buffer is close to be full, |
| 3714 | * because the request will wait for it to flush a little |
| 3715 | * bit before proceeding. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3716 | */ |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 3717 | if (s->req->l > s->req->send_max) { |
| 3718 | if (s->rep->send_max && |
| 3719 | !(s->rep->flags & BF_FULL) && |
Willy Tarreau | 065e833 | 2010-01-08 00:30:20 +0100 | [diff] [blame] | 3720 | s->rep->r <= s->rep->data + s->rep->size - global.tune.maxrewrite) |
| 3721 | s->rep->flags |= BF_EXPECT_MORE; |
| 3722 | } |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3723 | |
| 3724 | /* we're removing the analysers, we MUST re-enable events detection */ |
| 3725 | buffer_auto_read(s->req); |
| 3726 | buffer_auto_close(s->req); |
| 3727 | buffer_auto_read(s->rep); |
| 3728 | buffer_auto_close(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3729 | |
| 3730 | /* make ->lr point to the first non-forwarded byte */ |
| 3731 | s->req->lr = s->req->w + s->req->send_max; |
| 3732 | if (s->req->lr >= s->req->data + s->req->size) |
| 3733 | s->req->lr -= s->req->size; |
| 3734 | s->rep->lr = s->rep->w + s->rep->send_max; |
| 3735 | if (s->rep->lr >= s->rep->data + s->rep->size) |
| 3736 | s->rep->lr -= s->req->size; |
| 3737 | |
| 3738 | s->req->analysers |= s->fe->fe_req_ana; |
| 3739 | s->rep->analysers = 0; |
| 3740 | |
| 3741 | http_silent_debug(__LINE__, s); |
| 3742 | } |
| 3743 | |
| 3744 | |
| 3745 | /* This function updates the request state machine according to the response |
| 3746 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 3747 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 3748 | * it is only used to find when a request/response couple is complete. Both |
| 3749 | * this function and its equivalent should loop until both return zero. It |
| 3750 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 3751 | */ |
| 3752 | int http_sync_req_state(struct session *s) |
| 3753 | { |
| 3754 | struct buffer *buf = s->req; |
| 3755 | struct http_txn *txn = &s->txn; |
| 3756 | unsigned int old_flags = buf->flags; |
| 3757 | unsigned int old_state = txn->req.msg_state; |
| 3758 | |
| 3759 | http_silent_debug(__LINE__, s); |
| 3760 | if (unlikely(txn->req.msg_state < HTTP_MSG_BODY)) |
| 3761 | return 0; |
| 3762 | |
| 3763 | if (txn->req.msg_state == HTTP_MSG_DONE) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3764 | /* No need to read anymore, the request was completely parsed. |
| 3765 | * We can shut the read side unless we want to abort_on_close. |
| 3766 | */ |
| 3767 | if (buf->cons->state == SI_ST_EST || !(s->be->options & PR_O_ABRT_CLOSE)) |
| 3768 | buffer_dont_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3769 | |
| 3770 | if (txn->rsp.msg_state == HTTP_MSG_ERROR) |
| 3771 | goto wait_other_side; |
| 3772 | |
| 3773 | if (txn->rsp.msg_state < HTTP_MSG_DONE) { |
| 3774 | /* The server has not finished to respond, so we |
| 3775 | * don't want to move in order not to upset it. |
| 3776 | */ |
| 3777 | goto wait_other_side; |
| 3778 | } |
| 3779 | |
| 3780 | if (txn->rsp.msg_state == HTTP_MSG_TUNNEL) { |
| 3781 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3782 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3783 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
| 3784 | goto wait_other_side; |
| 3785 | } |
| 3786 | |
| 3787 | /* When we get here, it means that both the request and the |
| 3788 | * response have finished receiving. Depending on the connection |
| 3789 | * mode, we'll have to wait for the last bytes to leave in either |
| 3790 | * direction, and sometimes for a close to be effective. |
| 3791 | */ |
| 3792 | |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3793 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 3794 | /* Server-close mode : queue a connection close to the server */ |
| 3795 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3796 | buffer_shutw_now(buf); |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3797 | } |
| 3798 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 3799 | /* Option forceclose is set, or either side wants to close, |
| 3800 | * let's enforce it now that we're not expecting any new |
| 3801 | * data to come. The caller knows the session is complete |
| 3802 | * once both states are CLOSED. |
| 3803 | */ |
| 3804 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3805 | buffer_shutr_now(buf); |
| 3806 | buffer_shutw_now(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3807 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3808 | } |
| 3809 | else { |
| 3810 | /* The last possible modes are keep-alive and tunnel. Since tunnel |
| 3811 | * mode does not set the body analyser, we can't reach this place |
| 3812 | * in tunnel mode, so we're left with keep-alive only. |
| 3813 | * This mode is currently not implemented, we switch to tunnel mode. |
| 3814 | */ |
| 3815 | buffer_auto_read(buf); |
| 3816 | txn->req.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3817 | } |
| 3818 | |
| 3819 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) { |
| 3820 | /* if we've just closed an output, let's switch */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3821 | buf->cons->flags |= SI_FL_NOLINGER; /* we want to close ASAP */ |
| 3822 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3823 | if (!(buf->flags & BF_OUT_EMPTY)) { |
| 3824 | txn->req.msg_state = HTTP_MSG_CLOSING; |
| 3825 | goto http_msg_closing; |
| 3826 | } |
| 3827 | else { |
| 3828 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3829 | goto http_msg_closed; |
| 3830 | } |
| 3831 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3832 | goto wait_other_side; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3833 | } |
| 3834 | |
| 3835 | if (txn->req.msg_state == HTTP_MSG_CLOSING) { |
| 3836 | http_msg_closing: |
| 3837 | /* nothing else to forward, just waiting for the output buffer |
| 3838 | * to be empty and for the shutw_now to take effect. |
| 3839 | */ |
| 3840 | if (buf->flags & BF_OUT_EMPTY) { |
| 3841 | txn->req.msg_state = HTTP_MSG_CLOSED; |
| 3842 | goto http_msg_closed; |
| 3843 | } |
| 3844 | else if (buf->flags & BF_SHUTW) { |
| 3845 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 3846 | goto wait_other_side; |
| 3847 | } |
| 3848 | } |
| 3849 | |
| 3850 | if (txn->req.msg_state == HTTP_MSG_CLOSED) { |
| 3851 | http_msg_closed: |
| 3852 | goto wait_other_side; |
| 3853 | } |
| 3854 | |
| 3855 | wait_other_side: |
| 3856 | http_silent_debug(__LINE__, s); |
| 3857 | return txn->req.msg_state != old_state || buf->flags != old_flags; |
| 3858 | } |
| 3859 | |
| 3860 | |
| 3861 | /* This function updates the response state machine according to the request |
| 3862 | * state machine and buffer flags. It returns 1 if it changes anything (flag |
| 3863 | * or state), otherwise zero. It ignores any state before HTTP_MSG_DONE, as |
| 3864 | * it is only used to find when a request/response couple is complete. Both |
| 3865 | * this function and its equivalent should loop until both return zero. It |
| 3866 | * can set its own state to DONE, CLOSING, CLOSED, TUNNEL, ERROR. |
| 3867 | */ |
| 3868 | int http_sync_res_state(struct session *s) |
| 3869 | { |
| 3870 | struct buffer *buf = s->rep; |
| 3871 | struct http_txn *txn = &s->txn; |
| 3872 | unsigned int old_flags = buf->flags; |
| 3873 | unsigned int old_state = txn->rsp.msg_state; |
| 3874 | |
| 3875 | http_silent_debug(__LINE__, s); |
| 3876 | if (unlikely(txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 3877 | return 0; |
| 3878 | |
| 3879 | if (txn->rsp.msg_state == HTTP_MSG_DONE) { |
| 3880 | /* In theory, we don't need to read anymore, but we must |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3881 | * still monitor the server connection for a possible close |
| 3882 | * while the request is being uploaded, so we don't disable |
| 3883 | * reading. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3884 | */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3885 | /* buffer_dont_read(buf); */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3886 | |
| 3887 | if (txn->req.msg_state == HTTP_MSG_ERROR) |
| 3888 | goto wait_other_side; |
| 3889 | |
| 3890 | if (txn->req.msg_state < HTTP_MSG_DONE) { |
| 3891 | /* The client seems to still be sending data, probably |
| 3892 | * because we got an error response during an upload. |
| 3893 | * We have the choice of either breaking the connection |
| 3894 | * or letting it pass through. Let's do the later. |
| 3895 | */ |
| 3896 | goto wait_other_side; |
| 3897 | } |
| 3898 | |
| 3899 | if (txn->req.msg_state == HTTP_MSG_TUNNEL) { |
| 3900 | /* if any side switches to tunnel mode, the other one does too */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3901 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3902 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
| 3903 | goto wait_other_side; |
| 3904 | } |
| 3905 | |
| 3906 | /* When we get here, it means that both the request and the |
| 3907 | * response have finished receiving. Depending on the connection |
| 3908 | * mode, we'll have to wait for the last bytes to leave in either |
| 3909 | * direction, and sometimes for a close to be effective. |
| 3910 | */ |
| 3911 | |
| 3912 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 3913 | /* Server-close mode : shut read and wait for the request |
| 3914 | * side to close its output buffer. The caller will detect |
| 3915 | * when we're in DONE and the other is in CLOSED and will |
| 3916 | * catch that for the final cleanup. |
| 3917 | */ |
| 3918 | if (!(buf->flags & (BF_SHUTR|BF_SHUTR_NOW))) |
| 3919 | buffer_shutr_now(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3920 | } |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3921 | else if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 3922 | /* Option forceclose is set, or either side wants to close, |
| 3923 | * let's enforce it now that we're not expecting any new |
| 3924 | * data to come. The caller knows the session is complete |
| 3925 | * once both states are CLOSED. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3926 | */ |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3927 | if (!(buf->flags & (BF_SHUTW|BF_SHUTW_NOW))) { |
| 3928 | buffer_shutr_now(buf); |
| 3929 | buffer_shutw_now(buf); |
| 3930 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3931 | } |
| 3932 | else { |
Willy Tarreau | cce7fa4 | 2010-01-16 23:19:39 +0100 | [diff] [blame] | 3933 | /* The last possible modes are keep-alive and tunnel. Since tunnel |
| 3934 | * mode does not set the body analyser, we can't reach this place |
| 3935 | * in tunnel mode, so we're left with keep-alive only. |
| 3936 | * This mode is currently not implemented, we switch to tunnel mode. |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3937 | */ |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3938 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3939 | txn->rsp.msg_state = HTTP_MSG_TUNNEL; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3940 | } |
| 3941 | |
| 3942 | if (buf->flags & (BF_SHUTW|BF_SHUTW_NOW)) { |
| 3943 | /* if we've just closed an output, let's switch */ |
| 3944 | if (!(buf->flags & BF_OUT_EMPTY)) { |
| 3945 | txn->rsp.msg_state = HTTP_MSG_CLOSING; |
| 3946 | goto http_msg_closing; |
| 3947 | } |
| 3948 | else { |
| 3949 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 3950 | goto http_msg_closed; |
| 3951 | } |
| 3952 | } |
| 3953 | goto wait_other_side; |
| 3954 | } |
| 3955 | |
| 3956 | if (txn->rsp.msg_state == HTTP_MSG_CLOSING) { |
| 3957 | http_msg_closing: |
| 3958 | /* nothing else to forward, just waiting for the output buffer |
| 3959 | * to be empty and for the shutw_now to take effect. |
| 3960 | */ |
| 3961 | if (buf->flags & BF_OUT_EMPTY) { |
| 3962 | txn->rsp.msg_state = HTTP_MSG_CLOSED; |
| 3963 | goto http_msg_closed; |
| 3964 | } |
| 3965 | else if (buf->flags & BF_SHUTW) { |
| 3966 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 3967 | goto wait_other_side; |
| 3968 | } |
| 3969 | } |
| 3970 | |
| 3971 | if (txn->rsp.msg_state == HTTP_MSG_CLOSED) { |
| 3972 | http_msg_closed: |
| 3973 | /* drop any pending data */ |
| 3974 | buffer_ignore(buf, buf->l - buf->send_max); |
| 3975 | buffer_auto_close(buf); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3976 | buffer_auto_read(buf); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3977 | goto wait_other_side; |
| 3978 | } |
| 3979 | |
| 3980 | wait_other_side: |
| 3981 | http_silent_debug(__LINE__, s); |
| 3982 | return txn->rsp.msg_state != old_state || buf->flags != old_flags; |
| 3983 | } |
| 3984 | |
| 3985 | |
| 3986 | /* Resync the request and response state machines. Return 1 if either state |
| 3987 | * changes. |
| 3988 | */ |
| 3989 | int http_resync_states(struct session *s) |
| 3990 | { |
| 3991 | struct http_txn *txn = &s->txn; |
| 3992 | int old_req_state = txn->req.msg_state; |
| 3993 | int old_res_state = txn->rsp.msg_state; |
| 3994 | |
| 3995 | http_silent_debug(__LINE__, s); |
| 3996 | http_sync_req_state(s); |
| 3997 | while (1) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 3998 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 3999 | if (!http_sync_res_state(s)) |
| 4000 | break; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4001 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4002 | if (!http_sync_req_state(s)) |
| 4003 | break; |
| 4004 | } |
| 4005 | http_silent_debug(__LINE__, s); |
| 4006 | /* OK, both state machines agree on a compatible state. |
| 4007 | * There are a few cases we're interested in : |
| 4008 | * - HTTP_MSG_TUNNEL on either means we have to disable both analysers |
| 4009 | * - HTTP_MSG_CLOSED on both sides means we've reached the end in both |
| 4010 | * directions, so let's simply disable both analysers. |
| 4011 | * - HTTP_MSG_CLOSED on the response only means we must abort the |
| 4012 | * request. |
| 4013 | * - HTTP_MSG_CLOSED on the request and HTTP_MSG_DONE on the response |
| 4014 | * with server-close mode means we've completed one request and we |
| 4015 | * must re-initialize the server connection. |
| 4016 | */ |
| 4017 | |
| 4018 | if (txn->req.msg_state == HTTP_MSG_TUNNEL || |
| 4019 | txn->rsp.msg_state == HTTP_MSG_TUNNEL || |
| 4020 | (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4021 | txn->rsp.msg_state == HTTP_MSG_CLOSED)) { |
| 4022 | s->req->analysers = 0; |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4023 | buffer_auto_close(s->req); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4024 | buffer_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4025 | s->rep->analysers = 0; |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4026 | buffer_auto_close(s->rep); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4027 | buffer_auto_read(s->rep); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4028 | } |
Willy Tarreau | 2fa144c | 2010-01-04 23:13:26 +0100 | [diff] [blame] | 4029 | else if (txn->rsp.msg_state == HTTP_MSG_CLOSED || |
| 4030 | txn->rsp.msg_state == HTTP_MSG_ERROR || |
| 4031 | (s->rep->flags & BF_SHUTW)) { |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4032 | s->rep->analysers = 0; |
| 4033 | buffer_auto_close(s->rep); |
| 4034 | buffer_auto_read(s->rep); |
| 4035 | s->req->analysers = 0; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4036 | buffer_abort(s->req); |
| 4037 | buffer_auto_close(s->req); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4038 | buffer_auto_read(s->req); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4039 | buffer_ignore(s->req, s->req->l - s->req->send_max); |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4040 | } |
| 4041 | else if (txn->req.msg_state == HTTP_MSG_CLOSED && |
| 4042 | txn->rsp.msg_state == HTTP_MSG_DONE && |
| 4043 | ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL)) { |
| 4044 | /* server-close: terminate this server connection and |
| 4045 | * reinitialize a fresh-new transaction. |
| 4046 | */ |
| 4047 | http_end_txn_clean_session(s); |
| 4048 | } |
| 4049 | |
| 4050 | http_silent_debug(__LINE__, s); |
| 4051 | return txn->req.msg_state != old_req_state || |
| 4052 | txn->rsp.msg_state != old_res_state; |
| 4053 | } |
| 4054 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4055 | /* This function is an analyser which forwards request body (including chunk |
| 4056 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 4057 | * zero byte. The only situation where it must not be called is when we're in |
| 4058 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 4059 | * remaining data and to resync after end of body. It expects the msg_state to |
| 4060 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 4061 | * read more data, or 1 once we can go on with next request or end the session. |
| 4062 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len |
| 4063 | * bytes of pending data + the headers if not already done (between som and sov). |
| 4064 | * It eventually adjusts som to match sov after the data in between have been sent. |
| 4065 | */ |
| 4066 | int http_request_forward_body(struct session *s, struct buffer *req, int an_bit) |
| 4067 | { |
| 4068 | struct http_txn *txn = &s->txn; |
| 4069 | struct http_msg *msg = &s->txn.req; |
| 4070 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4071 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4072 | return 0; |
| 4073 | |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 4074 | if ((req->flags & (BF_READ_ERROR|BF_READ_TIMEOUT|BF_WRITE_ERROR|BF_WRITE_TIMEOUT)) || |
| 4075 | ((req->flags & BF_SHUTW) && (req->to_forward || req->send_max))) { |
| 4076 | /* Output closed while we were sending data. We must abort. */ |
| 4077 | buffer_ignore(req, req->l - req->send_max); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4078 | buffer_auto_read(req); |
| 4079 | buffer_auto_close(req); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 4080 | req->analysers &= ~an_bit; |
| 4081 | return 1; |
| 4082 | } |
| 4083 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 4084 | buffer_dont_close(req); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4085 | |
| 4086 | /* Note that we don't have to send 100-continue back because we don't |
| 4087 | * need the data to complete our job, and it's up to the server to |
| 4088 | * decide whether to return 100, 417 or anything else in return of |
| 4089 | * an "Expect: 100-continue" header. |
| 4090 | */ |
| 4091 | |
| 4092 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4093 | /* we have msg->col and msg->sov which both point to the first |
| 4094 | * byte of message body. msg->som still points to the beginning |
| 4095 | * of the message. We must save the body in req->lr because it |
| 4096 | * survives buffer re-alignments. |
| 4097 | */ |
| 4098 | req->lr = req->data + msg->sov; |
| 4099 | if (txn->flags & TX_REQ_TE_CHNK) |
| 4100 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4101 | else { |
| 4102 | msg->msg_state = HTTP_MSG_DATA; |
| 4103 | } |
| 4104 | } |
| 4105 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4106 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4107 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4108 | /* we may have some data pending */ |
| 4109 | if (msg->hdr_content_len || msg->som != msg->sov) { |
| 4110 | int bytes = msg->sov - msg->som; |
| 4111 | if (bytes < 0) /* sov may have wrapped at the end */ |
| 4112 | bytes += req->size; |
| 4113 | buffer_forward(req, bytes + msg->hdr_content_len); |
| 4114 | msg->hdr_content_len = 0; /* don't forward that again */ |
| 4115 | msg->som = msg->sov; |
| 4116 | } |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 4117 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4118 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 4119 | /* must still forward */ |
| 4120 | if (req->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4121 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4122 | |
| 4123 | /* nothing left to forward */ |
| 4124 | if (txn->flags & TX_REQ_TE_CHNK) |
| 4125 | msg->msg_state = HTTP_MSG_DATA_CRLF; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4126 | else |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4127 | msg->msg_state = HTTP_MSG_DONE; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4128 | } |
| 4129 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4130 | /* read the chunk size and assign it to ->hdr_content_len, then |
| 4131 | * set ->sov and ->lr to point to the body and switch to DATA or |
| 4132 | * TRAILERS state. |
| 4133 | */ |
| 4134 | int ret = http_parse_chunk_size(req, msg); |
| 4135 | |
| 4136 | if (!ret) |
| 4137 | goto missing_data; |
| 4138 | else if (ret < 0) |
| 4139 | goto return_bad_req; |
| 4140 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4141 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4142 | else if (msg->msg_state == HTTP_MSG_DATA_CRLF) { |
| 4143 | /* we want the CRLF after the data */ |
| 4144 | int ret; |
| 4145 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 4146 | req->lr = req->w + req->send_max; |
| 4147 | if (req->lr >= req->data + req->size) |
| 4148 | req->lr -= req->size; |
| 4149 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4150 | ret = http_skip_chunk_crlf(req, msg); |
| 4151 | |
| 4152 | if (ret == 0) |
| 4153 | goto missing_data; |
| 4154 | else if (ret < 0) |
| 4155 | goto return_bad_req; |
| 4156 | /* we're in MSG_CHUNK_SIZE now */ |
| 4157 | } |
| 4158 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
| 4159 | int ret = http_forward_trailers(req, msg); |
| 4160 | |
| 4161 | if (ret == 0) |
| 4162 | goto missing_data; |
| 4163 | else if (ret < 0) |
| 4164 | goto return_bad_req; |
| 4165 | /* we're in HTTP_MSG_DONE now */ |
| 4166 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4167 | else { |
| 4168 | /* other states, DONE...TUNNEL */ |
| 4169 | if (http_resync_states(s)) { |
| 4170 | /* some state changes occurred, maybe the analyser |
| 4171 | * was disabled too. |
Willy Tarreau | face839 | 2010-01-03 11:37:54 +0100 | [diff] [blame] | 4172 | */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4173 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
| 4174 | goto return_bad_req; |
| 4175 | return 1; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4176 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4177 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4178 | } |
| 4179 | } |
| 4180 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4181 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4182 | /* stop waiting for data if the input is closed before the end */ |
| 4183 | if (req->flags & BF_SHUTR) |
| 4184 | goto return_bad_req; |
| 4185 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4186 | /* waiting for the last bits to leave the buffer */ |
| 4187 | if (req->flags & BF_SHUTW) |
| 4188 | goto return_bad_req; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4189 | |
| 4190 | http_silent_debug(__LINE__, s); |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4191 | return 0; |
| 4192 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4193 | return_bad_req: /* let's centralize all bad requests */ |
| 4194 | txn->req.msg_state = HTTP_MSG_ERROR; |
| 4195 | txn->status = 400; |
| 4196 | /* 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] | 4197 | 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] | 4198 | req->analysers = 0; |
| 4199 | s->fe->counters.failed_req++; |
| 4200 | if (s->listener->counters) |
| 4201 | s->listener->counters->failed_req++; |
| 4202 | |
| 4203 | if (!(s->flags & SN_ERR_MASK)) |
| 4204 | s->flags |= SN_ERR_PRXCOND; |
| 4205 | if (!(s->flags & SN_FINST_MASK)) |
| 4206 | s->flags |= SN_FINST_R; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4207 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4208 | return 0; |
| 4209 | } |
| 4210 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4211 | /* This stream analyser waits for a complete HTTP response. It returns 1 if the |
| 4212 | * processing can continue on next analysers, or zero if it either needs more |
| 4213 | * data or wants to immediately abort the response (eg: timeout, error, ...). It |
| 4214 | * is tied to AN_RES_WAIT_HTTP and may may remove itself from s->rep->analysers |
| 4215 | * when it has nothing left to do, and may remove any analyser when it wants to |
| 4216 | * abort. |
Willy Tarreau | c65a3ba | 2008-08-11 23:42:50 +0200 | [diff] [blame] | 4217 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4218 | 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] | 4219 | { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4220 | struct http_txn *txn = &s->txn; |
| 4221 | struct http_msg *msg = &txn->rsp; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4222 | struct hdr_ctx ctx; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4223 | int use_close_only; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4224 | int cur_idx; |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4225 | int n; |
Willy Tarreau | adfb856 | 2008-08-11 15:24:42 +0200 | [diff] [blame] | 4226 | |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4227 | 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] | 4228 | now_ms, __FUNCTION__, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4229 | s, |
Willy Tarreau | 3a16b2c | 2008-08-28 08:54:27 +0200 | [diff] [blame] | 4230 | rep, |
| 4231 | rep->rex, rep->wex, |
| 4232 | rep->flags, |
| 4233 | rep->l, |
| 4234 | rep->analysers); |
Willy Tarreau | 67f0eea | 2008-08-10 22:55:22 +0200 | [diff] [blame] | 4235 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4236 | /* |
| 4237 | * Now parse the partial (or complete) lines. |
| 4238 | * We will check the response syntax, and also join multi-line |
| 4239 | * headers. An index of all the lines will be elaborated while |
| 4240 | * parsing. |
| 4241 | * |
| 4242 | * For the parsing, we use a 28 states FSM. |
| 4243 | * |
| 4244 | * Here is the information we currently have : |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4245 | * rep->data + msg->som = beginning of response |
| 4246 | * rep->data + msg->eoh = end of processed headers / start of current one |
| 4247 | * msg->eol = end of current header or line (LF or CRLF) |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4248 | * rep->lr = first non-visited byte |
| 4249 | * rep->r = end of data |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4250 | * Once we reach MSG_BODY, rep->sol = rep->data + msg->som |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4251 | */ |
| 4252 | |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4253 | /* There's a protected area at the end of the buffer for rewriting |
| 4254 | * purposes. We don't want to start to parse the request if the |
| 4255 | * protected area is affected, because we may have to move processed |
| 4256 | * data later, which is much more complicated. |
| 4257 | */ |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4258 | if (rep->l && msg->msg_state < HTTP_MSG_ERROR) { |
| 4259 | if (unlikely((rep->flags & BF_FULL) || |
| 4260 | rep->r < rep->lr || |
| 4261 | rep->r > rep->data + rep->size - global.tune.maxrewrite)) { |
| 4262 | if (rep->send_max) { |
| 4263 | /* some data has still not left the buffer, wake us once that's done */ |
| 4264 | buffer_dont_close(rep); |
| 4265 | rep->flags |= BF_READ_DONTWAIT; /* try to get back here ASAP */ |
| 4266 | return 0; |
| 4267 | } |
| 4268 | if (rep->l <= rep->size - global.tune.maxrewrite) |
| 4269 | http_buffer_heavy_realign(rep, msg); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4270 | } |
| 4271 | |
Willy Tarreau | 2ab6eb1 | 2010-01-02 22:04:45 +0100 | [diff] [blame] | 4272 | if (likely(rep->lr < rep->r)) |
| 4273 | http_msg_analyzer(rep, msg, &txn->hdr_idx); |
Willy Tarreau | 83e3af0 | 2009-12-28 17:39:57 +0100 | [diff] [blame] | 4274 | } |
| 4275 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4276 | /* 1: we might have to print this header in debug mode */ |
| 4277 | if (unlikely((global.mode & MODE_DEBUG) && |
| 4278 | (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) && |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4279 | (msg->msg_state >= HTTP_MSG_BODY || msg->msg_state == HTTP_MSG_ERROR))) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4280 | char *eol, *sol; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4281 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4282 | sol = msg->sol; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4283 | eol = sol + msg->sl.rq.l; |
| 4284 | debug_hdr("srvrep", s, sol, eol); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4285 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4286 | sol += hdr_idx_first_pos(&txn->hdr_idx); |
| 4287 | cur_idx = hdr_idx_first_idx(&txn->hdr_idx); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4288 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4289 | while (cur_idx) { |
| 4290 | eol = sol + txn->hdr_idx.v[cur_idx].len; |
| 4291 | debug_hdr("srvhdr", s, sol, eol); |
| 4292 | sol = eol + txn->hdr_idx.v[cur_idx].cr + 1; |
| 4293 | cur_idx = txn->hdr_idx.v[cur_idx].next; |
| 4294 | } |
| 4295 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4296 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4297 | /* |
| 4298 | * Now we quickly check if we have found a full valid response. |
| 4299 | * If not so, we check the FD and buffer states before leaving. |
| 4300 | * A full response is indicated by the fact that we have seen |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4301 | * the double LF/CRLF, so the state is >= HTTP_MSG_BODY. Invalid |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4302 | * responses are checked first. |
| 4303 | * |
| 4304 | * Depending on whether the client is still there or not, we |
| 4305 | * may send an error response back or not. Note that normally |
| 4306 | * we should only check for HTTP status there, and check I/O |
| 4307 | * errors somewhere else. |
| 4308 | */ |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4309 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4310 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4311 | /* Invalid response */ |
| 4312 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) { |
| 4313 | /* we detected a parsing error. We want to archive this response |
| 4314 | * in the dedicated proxy area for later troubleshooting. |
| 4315 | */ |
| 4316 | hdr_response_bad: |
| 4317 | if (msg->msg_state == HTTP_MSG_ERROR || msg->err_pos >= 0) |
| 4318 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe); |
| 4319 | |
| 4320 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4321 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4322 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4323 | health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP); |
| 4324 | } |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4325 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4326 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4327 | rep->analysers = 0; |
| 4328 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4329 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4330 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4331 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
| 4332 | |
| 4333 | if (!(s->flags & SN_ERR_MASK)) |
| 4334 | s->flags |= SN_ERR_PRXCOND; |
| 4335 | if (!(s->flags & SN_FINST_MASK)) |
| 4336 | s->flags |= SN_FINST_H; |
| 4337 | |
| 4338 | return 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4339 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4340 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4341 | /* too large response does not fit in buffer. */ |
| 4342 | else if (rep->flags & BF_FULL) { |
| 4343 | goto hdr_response_bad; |
| 4344 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4345 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4346 | /* read error */ |
| 4347 | else if (rep->flags & BF_READ_ERROR) { |
| 4348 | if (msg->err_pos >= 0) |
| 4349 | 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] | 4350 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4351 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4352 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4353 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4354 | health_adjust(s->srv, HANA_STATUS_HTTP_READ_ERROR); |
| 4355 | } |
Willy Tarreau | 461f662 | 2008-08-15 23:43:19 +0200 | [diff] [blame] | 4356 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4357 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4358 | rep->analysers = 0; |
| 4359 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4360 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4361 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4362 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 4363 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4364 | if (!(s->flags & SN_ERR_MASK)) |
| 4365 | s->flags |= SN_ERR_SRVCL; |
| 4366 | if (!(s->flags & SN_FINST_MASK)) |
| 4367 | s->flags |= SN_FINST_H; |
Willy Tarreau | cebf57e | 2008-08-15 18:16:37 +0200 | [diff] [blame] | 4368 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4369 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4370 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4371 | /* read timeout : return a 504 to the client. */ |
| 4372 | else if (rep->flags & BF_READ_TIMEOUT) { |
| 4373 | if (msg->err_pos >= 0) |
| 4374 | 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] | 4375 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4376 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4377 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4378 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4379 | health_adjust(s->srv, HANA_STATUS_HTTP_READ_TIMEOUT); |
| 4380 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4381 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4382 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4383 | rep->analysers = 0; |
| 4384 | txn->status = 504; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4385 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4386 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4387 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_504)); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 4388 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4389 | if (!(s->flags & SN_ERR_MASK)) |
| 4390 | s->flags |= SN_ERR_SRVTO; |
| 4391 | if (!(s->flags & SN_FINST_MASK)) |
| 4392 | s->flags |= SN_FINST_H; |
| 4393 | return 0; |
| 4394 | } |
Willy Tarreau | a7c5276 | 2008-08-16 18:40:18 +0200 | [diff] [blame] | 4395 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4396 | /* close from server */ |
| 4397 | else if (rep->flags & BF_SHUTR) { |
| 4398 | if (msg->err_pos >= 0) |
| 4399 | 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] | 4400 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4401 | s->be->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4402 | if (s->srv) { |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4403 | s->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4404 | health_adjust(s->srv, HANA_STATUS_HTTP_BROKEN_PIPE); |
| 4405 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4406 | |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4407 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4408 | rep->analysers = 0; |
| 4409 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4410 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4411 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4412 | stream_int_retnclose(rep->cons, error_message(s, HTTP_ERR_502)); |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4413 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4414 | if (!(s->flags & SN_ERR_MASK)) |
| 4415 | s->flags |= SN_ERR_SRVCL; |
| 4416 | if (!(s->flags & SN_FINST_MASK)) |
| 4417 | s->flags |= SN_FINST_H; |
| 4418 | return 0; |
| 4419 | } |
Krzysztof Piotr Oledzki | 5fb1882 | 2009-10-13 21:14:09 +0200 | [diff] [blame] | 4420 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4421 | /* write error to client (we don't send any message then) */ |
| 4422 | else if (rep->flags & BF_WRITE_ERROR) { |
| 4423 | if (msg->err_pos >= 0) |
| 4424 | 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] | 4425 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4426 | s->be->counters.failed_resp++; |
| 4427 | rep->analysers = 0; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4428 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4429 | |
| 4430 | if (!(s->flags & SN_ERR_MASK)) |
| 4431 | s->flags |= SN_ERR_CLICL; |
| 4432 | if (!(s->flags & SN_FINST_MASK)) |
| 4433 | s->flags |= SN_FINST_H; |
| 4434 | |
| 4435 | /* process_session() will take care of the error */ |
| 4436 | return 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4437 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4438 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4439 | buffer_dont_close(rep); |
| 4440 | return 0; |
| 4441 | } |
| 4442 | |
| 4443 | /* More interesting part now : we know that we have a complete |
| 4444 | * response which at least looks like HTTP. We have an indicator |
| 4445 | * of each header's length, so we can parse them quickly. |
| 4446 | */ |
| 4447 | |
| 4448 | if (unlikely(msg->err_pos >= 0)) |
| 4449 | http_capture_bad_message(&s->be->invalid_rep, s, rep, msg, s->fe); |
| 4450 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4451 | /* |
| 4452 | * 1: get the status code |
| 4453 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4454 | n = msg->sol[msg->sl.st.c] - '0'; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4455 | if (n < 1 || n > 5) |
| 4456 | n = 0; |
| 4457 | s->srv->counters.p.http.rsp[n]++; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4458 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4459 | /* check if the response is HTTP/1.1 or above */ |
| 4460 | if ((msg->sl.st.v_l == 8) && |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4461 | ((msg->sol[5] > '1') || |
| 4462 | ((msg->sol[5] == '1') && |
| 4463 | (msg->sol[7] >= '1')))) |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4464 | txn->flags |= TX_RES_VER_11; |
| 4465 | |
| 4466 | /* "connection" has not been parsed yet */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4467 | 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] | 4468 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4469 | /* transfer length unknown*/ |
| 4470 | txn->flags &= ~TX_RES_XFER_LEN; |
| 4471 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4472 | 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] | 4473 | |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4474 | if (txn->status >= 100 && txn->status < 500) |
| 4475 | health_adjust(s->srv, HANA_STATUS_HTTP_OK); |
| 4476 | else |
| 4477 | health_adjust(s->srv, HANA_STATUS_HTTP_STS); |
| 4478 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4479 | /* |
| 4480 | * 2: check for cacheability. |
| 4481 | */ |
| 4482 | |
| 4483 | switch (txn->status) { |
| 4484 | case 200: |
| 4485 | case 203: |
| 4486 | case 206: |
| 4487 | case 300: |
| 4488 | case 301: |
| 4489 | case 410: |
| 4490 | /* RFC2616 @13.4: |
| 4491 | * "A response received with a status code of |
| 4492 | * 200, 203, 206, 300, 301 or 410 MAY be stored |
| 4493 | * by a cache (...) unless a cache-control |
| 4494 | * directive prohibits caching." |
| 4495 | * |
| 4496 | * RFC2616 @9.5: POST method : |
| 4497 | * "Responses to this method are not cacheable, |
| 4498 | * unless the response includes appropriate |
| 4499 | * Cache-Control or Expires header fields." |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4500 | */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4501 | if (likely(txn->meth != HTTP_METH_POST) && |
| 4502 | (s->be->options & (PR_O_CHK_CACHE|PR_O_COOK_NOC))) |
| 4503 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
| 4504 | break; |
| 4505 | default: |
| 4506 | break; |
| 4507 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4508 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4509 | /* |
| 4510 | * 3: we may need to capture headers |
| 4511 | */ |
| 4512 | s->logs.logwait &= ~LW_RESP; |
| 4513 | if (unlikely((s->logs.logwait & LW_RSPHDR) && s->fe->rsp_cap)) |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4514 | capture_headers(msg->sol, &txn->hdr_idx, |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4515 | txn->rsp.cap, s->fe->rsp_cap); |
| 4516 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4517 | /* 4: determine the transfer-length. |
| 4518 | * According to RFC2616 #4.4, amended by the HTTPbis working group, |
| 4519 | * the presence of a message-body in a RESPONSE and its transfer length |
| 4520 | * must be determined that way : |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4521 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4522 | * All responses to the HEAD request method MUST NOT include a |
| 4523 | * message-body, even though the presence of entity-header fields |
| 4524 | * might lead one to believe they do. All 1xx (informational), 204 |
| 4525 | * (No Content), and 304 (Not Modified) responses MUST NOT include a |
| 4526 | * message-body. All other responses do include a message-body, |
| 4527 | * although it MAY be of zero length. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4528 | * |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4529 | * 1. Any response which "MUST NOT" include a message-body (such as the |
| 4530 | * 1xx, 204 and 304 responses and any response to a HEAD request) is |
| 4531 | * always terminated by the first empty line after the header fields, |
| 4532 | * regardless of the entity-header fields present in the message. |
| 4533 | * |
| 4534 | * 2. If a Transfer-Encoding header field (Section 9.7) is present and |
| 4535 | * the "chunked" transfer-coding (Section 6.2) is used, the |
| 4536 | * transfer-length is defined by the use of this transfer-coding. |
| 4537 | * If a Transfer-Encoding header field is present and the "chunked" |
| 4538 | * transfer-coding is not present, the transfer-length is defined by |
| 4539 | * the sender closing the connection. |
| 4540 | * |
| 4541 | * 3. If a Content-Length header field is present, its decimal value in |
| 4542 | * OCTETs represents both the entity-length and the transfer-length. |
| 4543 | * If a message is received with both a Transfer-Encoding header |
| 4544 | * field and a Content-Length header field, the latter MUST be ignored. |
| 4545 | * |
| 4546 | * 4. If the message uses the media type "multipart/byteranges", and |
| 4547 | * the transfer-length is not otherwise specified, then this self- |
| 4548 | * delimiting media type defines the transfer-length. This media |
| 4549 | * type MUST NOT be used unless the sender knows that the recipient |
| 4550 | * can parse it; the presence in a request of a Range header with |
| 4551 | * multiple byte-range specifiers from a 1.1 client implies that the |
| 4552 | * client can parse multipart/byteranges responses. |
| 4553 | * |
| 4554 | * 5. By the server closing the connection. |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4555 | */ |
| 4556 | |
| 4557 | /* Skip parsing if no content length is possible. The response flags |
| 4558 | * 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] | 4559 | * 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] | 4560 | * FIXME: should we parse anyway and return an error on chunked encoding ? |
| 4561 | */ |
| 4562 | if (txn->meth == HTTP_METH_HEAD || |
| 4563 | (txn->status >= 100 && txn->status < 200) || |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4564 | txn->status == 204 || txn->status == 304) { |
| 4565 | txn->flags |= TX_RES_XFER_LEN; |
| 4566 | goto skip_content_length; |
| 4567 | } |
| 4568 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4569 | use_close_only = 0; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4570 | ctx.idx = 0; |
Willy Tarreau | 9e13c3c | 2009-12-22 09:59:58 +0100 | [diff] [blame] | 4571 | while ((txn->flags & TX_RES_VER_11) && |
| 4572 | http_find_header2("Transfer-Encoding", 17, msg->sol, &txn->hdr_idx, &ctx)) { |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4573 | if (ctx.vlen == 7 && strncasecmp(ctx.line + ctx.val, "chunked", 7) == 0) |
| 4574 | txn->flags |= (TX_RES_TE_CHNK | TX_RES_XFER_LEN); |
| 4575 | else if (txn->flags & TX_RES_TE_CHNK) { |
| 4576 | /* bad transfer-encoding (chunked followed by something else) */ |
| 4577 | use_close_only = 1; |
| 4578 | txn->flags &= ~(TX_RES_TE_CHNK | TX_RES_XFER_LEN); |
| 4579 | break; |
| 4580 | } |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4581 | } |
| 4582 | |
| 4583 | /* FIXME: below we should remove the content-length header(s) in case of chunked encoding */ |
| 4584 | ctx.idx = 0; |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4585 | while (!(txn->flags & TX_RES_TE_CHNK) && !use_close_only && |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4586 | http_find_header2("Content-Length", 14, msg->sol, &txn->hdr_idx, &ctx)) { |
| 4587 | signed long long cl; |
| 4588 | |
| 4589 | if (!ctx.vlen) |
| 4590 | goto hdr_response_bad; |
| 4591 | |
| 4592 | if (strl2llrc(ctx.line + ctx.val, ctx.vlen, &cl)) |
| 4593 | goto hdr_response_bad; /* parse failure */ |
| 4594 | |
| 4595 | if (cl < 0) |
| 4596 | goto hdr_response_bad; |
| 4597 | |
| 4598 | if ((txn->flags & TX_RES_CNT_LEN) && (msg->hdr_content_len != cl)) |
| 4599 | goto hdr_response_bad; /* already specified, was different */ |
| 4600 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4601 | txn->flags |= TX_RES_CNT_LEN | TX_RES_XFER_LEN; |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4602 | msg->hdr_content_len = cl; |
| 4603 | } |
| 4604 | |
Willy Tarreau | e8e785b | 2009-12-26 15:34:26 +0100 | [diff] [blame] | 4605 | /* FIXME: we should also implement the multipart/byterange method. |
| 4606 | * For now on, we resort to close mode in this case (unknown length). |
| 4607 | */ |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4608 | skip_content_length: |
Willy Tarreau | b8c82c2 | 2009-10-18 23:45:12 +0200 | [diff] [blame] | 4609 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4610 | /* end of job, return OK */ |
| 4611 | rep->analysers &= ~an_bit; |
| 4612 | rep->analyse_exp = TICK_ETERNITY; |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4613 | buffer_auto_close(rep); |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4614 | return 1; |
| 4615 | } |
| 4616 | |
| 4617 | /* This function performs all the processing enabled for the current response. |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 4618 | * It normally returns 1 unless it wants to break. It relies on buffers flags, |
| 4619 | * and updates t->rep->analysers. It might make sense to explode it into several |
| 4620 | * other functions. It works like process_request (see indications above). |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4621 | */ |
| 4622 | int http_process_res_common(struct session *t, struct buffer *rep, int an_bit, struct proxy *px) |
| 4623 | { |
| 4624 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4625 | struct http_msg *msg = &txn->rsp; |
| 4626 | struct proxy *cur_proxy; |
Willy Tarreau | f4f0412 | 2010-01-28 18:10:50 +0100 | [diff] [blame] | 4627 | struct cond_wordlist *wl; |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4628 | |
| 4629 | DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n", |
| 4630 | now_ms, __FUNCTION__, |
| 4631 | t, |
| 4632 | rep, |
| 4633 | rep->rex, rep->wex, |
| 4634 | rep->flags, |
| 4635 | rep->l, |
| 4636 | rep->analysers); |
| 4637 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 4638 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) /* we need more data */ |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4639 | return 0; |
| 4640 | |
| 4641 | rep->analysers &= ~an_bit; |
| 4642 | rep->analyse_exp = TICK_ETERNITY; |
| 4643 | |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4644 | /* Now we have to check if we need to modify the Connection header. |
| 4645 | * This is more difficult on the response than it is on the request, |
| 4646 | * because we can have two different HTTP versions and we don't know |
| 4647 | * how the client will interprete a response. For instance, let's say |
| 4648 | * that the client sends a keep-alive request in HTTP/1.0 and gets an |
| 4649 | * HTTP/1.1 response without any header. Maybe it will bound itself to |
| 4650 | * HTTP/1.0 because it only knows about it, and will consider the lack |
| 4651 | * of header as a close, or maybe it knows HTTP/1.1 and can consider |
| 4652 | * the lack of header as a keep-alive. Thus we will use two flags |
| 4653 | * indicating how a request MAY be understood by the client. In case |
| 4654 | * of multiple possibilities, we'll fix the header to be explicit. If |
| 4655 | * ambiguous cases such as both close and keepalive are seen, then we |
| 4656 | * will fall back to explicit close. Note that we won't take risks with |
| 4657 | * HTTP/1.0 clients which may not necessarily understand keep-alive. |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4658 | * See doc/internals/connection-header.txt for the complete matrix. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4659 | */ |
| 4660 | |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 4661 | if (unlikely((txn->meth == HTTP_METH_CONNECT && txn->status == 200) || |
| 4662 | txn->status == 101)) { |
| 4663 | /* Either we've established an explicit tunnel, or we're |
| 4664 | * switching the protocol. In both cases, we're very unlikely |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 4665 | * to understand the next protocols. We have to switch to tunnel |
| 4666 | * mode, so that we transfer the request and responses then let |
| 4667 | * this protocol pass unmodified. When we later implement specific |
| 4668 | * parsers for such protocols, we'll want to check the Upgrade |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 4669 | * header which contains information about that protocol for |
| 4670 | * responses with status 101 (eg: see RFC2817 about TLS). |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 4671 | */ |
| 4672 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_TUN; |
| 4673 | } |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 4674 | else if ((txn->status >= 200) && !(txn->flags & TX_HDR_CONN_PRS) && |
| 4675 | ((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN || |
| 4676 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE))) { |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4677 | int to_del = 0; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4678 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4679 | /* on unknown transfer length, we must close */ |
| 4680 | if (!(txn->flags & TX_RES_XFER_LEN) && |
| 4681 | (txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) |
| 4682 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_CLO; |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4683 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4684 | /* now adjust header transformations depending on current state */ |
| 4685 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN || |
| 4686 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_CLO) { |
| 4687 | to_del |= 2; /* remove "keep-alive" on any response */ |
| 4688 | if (!(txn->flags & TX_RES_VER_11)) |
| 4689 | to_del |= 1; /* remove "close" for HTTP/1.0 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4690 | } |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4691 | else { /* SCL / KAL */ |
| 4692 | to_del |= 1; /* remove "close" on any response */ |
| 4693 | if ((txn->flags & (TX_RES_VER_11|TX_REQ_VER_11)) == (TX_RES_VER_11|TX_REQ_VER_11)) |
| 4694 | to_del |= 2; /* remove "keep-alive" on pure 1.1 responses */ |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4695 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4696 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4697 | /* Parse and remove some headers from the connection header */ |
| 4698 | http_parse_connection_header(txn, msg, rep, to_del); |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4699 | |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4700 | /* Some keep-alive responses are converted to Server-close if |
| 4701 | * the server wants to close. |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4702 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4703 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) { |
| 4704 | if ((txn->flags & TX_HDR_CONN_CLO) || |
| 4705 | (txn->flags & (TX_HDR_CONN_KAL|TX_RES_VER_11)) == 0) |
| 4706 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4707 | } |
Willy Tarreau | 5b15447 | 2009-12-21 20:11:07 +0100 | [diff] [blame] | 4708 | } |
| 4709 | |
Willy Tarreau | b37c27e | 2009-10-18 22:53:08 +0200 | [diff] [blame] | 4710 | if (1) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4711 | /* |
| 4712 | * 3: we will have to evaluate the filters. |
| 4713 | * As opposed to version 1.2, now they will be evaluated in the |
| 4714 | * filters order and not in the header order. This means that |
| 4715 | * each filter has to be validated among all headers. |
| 4716 | * |
| 4717 | * Filters are tried with ->be first, then with ->fe if it is |
| 4718 | * different from ->be. |
| 4719 | */ |
| 4720 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4721 | cur_proxy = t->be; |
| 4722 | while (1) { |
| 4723 | struct proxy *rule_set = cur_proxy; |
| 4724 | |
| 4725 | /* try headers filters */ |
| 4726 | if (rule_set->rsp_exp != NULL) { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 4727 | if (apply_filters_to_response(t, rep, rule_set) < 0) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4728 | return_bad_resp: |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4729 | if (t->srv) { |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4730 | t->srv->counters.failed_resp++; |
Krzysztof Piotr Oledzki | 97f07b8 | 2009-12-15 22:31:24 +0100 | [diff] [blame] | 4731 | health_adjust(t->srv, HANA_STATUS_HTTP_RSP); |
| 4732 | } |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4733 | cur_proxy->counters.failed_resp++; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4734 | return_srv_prx_502: |
Willy Tarreau | 2df28e8 | 2008-08-17 15:20:19 +0200 | [diff] [blame] | 4735 | rep->analysers = 0; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4736 | txn->status = 502; |
Willy Tarreau | c88ea68 | 2009-12-29 14:56:36 +0100 | [diff] [blame] | 4737 | rep->prod->flags |= SI_FL_NOLINGER; |
Willy Tarreau | 0b89fbb | 2010-02-02 09:57:24 +0100 | [diff] [blame] | 4738 | buffer_ignore(rep, rep->l - rep->send_max); |
Willy Tarreau | 8e89b84 | 2009-10-18 23:56:35 +0200 | [diff] [blame] | 4739 | stream_int_retnclose(rep->cons, error_message(t, HTTP_ERR_502)); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4740 | if (!(t->flags & SN_ERR_MASK)) |
| 4741 | t->flags |= SN_ERR_PRXCOND; |
| 4742 | if (!(t->flags & SN_FINST_MASK)) |
| 4743 | t->flags |= SN_FINST_H; |
Willy Tarreau | dafde43 | 2008-08-17 01:00:46 +0200 | [diff] [blame] | 4744 | return 0; |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4745 | } |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4746 | } |
Willy Tarreau | 21d2af3 | 2008-02-14 20:25:24 +0100 | [diff] [blame] | 4747 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4748 | /* has the response been denied ? */ |
| 4749 | if (txn->flags & TX_SVDENY) { |
Willy Tarreau | 8365f93 | 2009-03-15 23:11:49 +0100 | [diff] [blame] | 4750 | if (t->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4751 | t->srv->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4752 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4753 | cur_proxy->counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4754 | if (t->listener->counters) |
| 4755 | t->listener->counters->denied_resp++; |
| 4756 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4757 | goto return_srv_prx_502; |
Willy Tarreau | 5140623 | 2008-03-10 22:04:20 +0100 | [diff] [blame] | 4758 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4759 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4760 | /* add response headers from the rule sets in the same order */ |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 4761 | list_for_each_entry(wl, &rule_set->rsp_add, list) { |
Willy Tarreau | 816b979 | 2009-09-15 21:25:21 +0200 | [diff] [blame] | 4762 | if (txn->status < 200) |
| 4763 | break; |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 4764 | if (wl->cond) { |
| 4765 | int ret = acl_exec_cond(wl->cond, px, t, txn, ACL_DIR_RTR); |
| 4766 | ret = acl_pass(ret); |
| 4767 | if (((struct acl_cond *)wl->cond)->pol == ACL_COND_UNLESS) |
| 4768 | ret = !ret; |
| 4769 | if (!ret) |
| 4770 | continue; |
| 4771 | } |
Willy Tarreau | deb9ed8 | 2010-01-03 21:03:22 +0100 | [diff] [blame] | 4772 | 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] | 4773 | goto return_bad_resp; |
Willy Tarreau | 0bbc3cf | 2006-10-15 14:26:02 +0200 | [diff] [blame] | 4774 | } |
| 4775 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4776 | /* check whether we're already working on the frontend */ |
| 4777 | if (cur_proxy == t->fe) |
| 4778 | break; |
| 4779 | cur_proxy = t->fe; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4780 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4781 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4782 | /* |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 4783 | * We may be facing a 100-continue response, in which case this |
| 4784 | * is not the right response, and we're waiting for the next one. |
| 4785 | * Let's allow this response to go to the client and wait for the |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4786 | * next one. |
| 4787 | */ |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 4788 | if (unlikely(txn->status == 100)) { |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4789 | hdr_idx_init(&txn->hdr_idx); |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 4790 | buffer_forward(rep, rep->lr - msg->sol); |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4791 | msg->msg_state = HTTP_MSG_RPBEFORE; |
| 4792 | txn->status = 0; |
| 4793 | rep->analysers |= AN_RES_WAIT_HTTP | an_bit; |
| 4794 | return 1; |
| 4795 | } |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 4796 | else if (unlikely(txn->status < 200)) |
| 4797 | goto skip_header_mangling; |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4798 | |
| 4799 | /* we don't have any 1xx status code now */ |
| 4800 | |
| 4801 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4802 | * 4: check for server cookie. |
| 4803 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4804 | if (t->be->cookie_name || t->be->appsession_name || t->fe->capture_name || |
| 4805 | (t->be->options & PR_O_CHK_CACHE)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4806 | manage_server_side_cookies(t, rep); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4807 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4808 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4809 | /* |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4810 | * 5: check for cache-control or pragma headers if required. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4811 | */ |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4812 | 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] | 4813 | check_response_for_cacheability(t, rep); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4814 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4815 | /* |
| 4816 | * 6: add server cookie in the response if needed |
| 4817 | */ |
| 4818 | 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] | 4819 | (!(t->be->options & PR_O_COOK_POST) || (txn->meth == HTTP_METH_POST))) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4820 | int len; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4821 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4822 | /* the server is known, it's not the one the client requested, we have to |
| 4823 | * insert a set-cookie here, except if we want to insert only on POST |
| 4824 | * requests and this one isn't. Note that servers which don't have cookies |
| 4825 | * (eg: some backup servers) will return a full cookie removal request. |
| 4826 | */ |
| 4827 | len = sprintf(trash, "Set-Cookie: %s=%s; path=/", |
| 4828 | t->be->cookie_name, |
| 4829 | 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] | 4830 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4831 | if (t->be->cookie_domain) |
| 4832 | len += sprintf(trash+len, "; domain=%s", t->be->cookie_domain); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4833 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4834 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 4835 | trash, len) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4836 | goto return_bad_resp; |
| 4837 | txn->flags |= TX_SCK_INSERTED; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4838 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4839 | /* Here, we will tell an eventual cache on the client side that we don't |
| 4840 | * want it to cache this reply because HTTP/1.0 caches also cache cookies ! |
| 4841 | * Some caches understand the correct form: 'no-cache="set-cookie"', but |
| 4842 | * others don't (eg: apache <= 1.3.26). So we use 'private' instead. |
| 4843 | */ |
| 4844 | if ((t->be->options & PR_O_COOK_NOC) && (txn->flags & TX_CACHEABLE)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4845 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4846 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 4847 | |
| 4848 | if (unlikely(http_header_add_tail2(rep, &txn->rsp, &txn->hdr_idx, |
Willy Tarreau | 58cc872 | 2009-12-28 06:57:33 +0100 | [diff] [blame] | 4849 | "Cache-control: private", 22) < 0)) |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4850 | goto return_bad_resp; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4851 | } |
| 4852 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4853 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4854 | /* |
| 4855 | * 7: check if result will be cacheable with a cookie. |
| 4856 | * We'll block the response if security checks have caught |
| 4857 | * nasty things such as a cacheable cookie. |
| 4858 | */ |
| 4859 | if (((txn->flags & (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) == |
| 4860 | (TX_CACHEABLE | TX_CACHE_COOK | TX_SCK_ANY)) && |
Willy Tarreau | 63c9e5f | 2009-12-22 16:01:27 +0100 | [diff] [blame] | 4861 | (t->be->options & PR_O_CHK_CACHE)) { |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4862 | |
| 4863 | /* we're in presence of a cacheable response containing |
| 4864 | * a set-cookie header. We'll block it as requested by |
| 4865 | * the 'checkcache' option, and send an alert. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4866 | */ |
Willy Tarreau | 8365f93 | 2009-03-15 23:11:49 +0100 | [diff] [blame] | 4867 | if (t->srv) |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4868 | t->srv->counters.failed_secu++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4869 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 4870 | cur_proxy->counters.denied_resp++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 4871 | if (t->listener->counters) |
| 4872 | t->listener->counters->denied_resp++; |
| 4873 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4874 | Alert("Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 4875 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 4876 | send_log(t->be, LOG_ALERT, |
| 4877 | "Blocking cacheable cookie in response from instance %s, server %s.\n", |
| 4878 | t->be->id, t->srv?t->srv->id:"<dispatch>"); |
| 4879 | goto return_srv_prx_502; |
| 4880 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4881 | |
| 4882 | /* |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4883 | * 8: adjust "Connection: close" or "Connection: keep-alive" if needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4884 | */ |
Willy Tarreau | 6046652 | 2010-01-18 19:08:45 +0100 | [diff] [blame] | 4885 | if (((txn->flags & TX_CON_WANT_MSK) != TX_CON_WANT_TUN) || |
| 4886 | ((t->fe->options|t->be->options) & PR_O_HTTP_CLOSE)) { |
| 4887 | unsigned int want_flags = 0; |
| 4888 | |
| 4889 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL || |
| 4890 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_SCL) { |
| 4891 | /* we want a keep-alive response here. Keep-alive header |
| 4892 | * required if either side is not 1.1. |
| 4893 | */ |
| 4894 | if ((txn->flags & (TX_REQ_VER_11|TX_RES_VER_11)) != (TX_REQ_VER_11|TX_RES_VER_11)) |
| 4895 | want_flags |= TX_CON_KAL_SET; |
| 4896 | } |
| 4897 | else { |
| 4898 | /* we want a close response here. Close header required if |
| 4899 | * the server is 1.1, regardless of the client. |
| 4900 | */ |
| 4901 | if (txn->flags & TX_RES_VER_11) |
| 4902 | want_flags |= TX_CON_CLO_SET; |
| 4903 | } |
| 4904 | |
| 4905 | if (want_flags != (txn->flags & (TX_CON_CLO_SET|TX_CON_KAL_SET))) |
| 4906 | http_change_connection_header(txn, msg, rep, want_flags); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4907 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4908 | |
Willy Tarreau | 5843d1a | 2010-02-01 15:13:32 +0100 | [diff] [blame] | 4909 | skip_header_mangling: |
Willy Tarreau | dc008c5 | 2010-02-01 16:20:08 +0100 | [diff] [blame] | 4910 | if ((txn->flags & TX_RES_XFER_LEN) || |
| 4911 | (txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_TUN) |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4912 | rep->analysers |= AN_RES_HTTP_XFER_BODY; |
Willy Tarreau | 0394594 | 2009-12-22 16:50:27 +0100 | [diff] [blame] | 4913 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4914 | /************************************************************* |
| 4915 | * OK, that's finished for the headers. We have done what we * |
| 4916 | * could. Let's switch to the DATA state. * |
| 4917 | ************************************************************/ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 4918 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4919 | t->logs.t_data = tv_ms_elapsed(&t->logs.tv_accept, &now); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4920 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4921 | /* if the user wants to log as soon as possible, without counting |
| 4922 | * bytes from the server, then this is the right moment. We have |
| 4923 | * to temporarily assign bytes_out to log what we currently have. |
| 4924 | */ |
| 4925 | if (t->fe->to_log && !(t->logs.logwait & LW_BYTES)) { |
| 4926 | t->logs.t_close = t->logs.t_data; /* to get a valid end date */ |
| 4927 | t->logs.bytes_out = txn->rsp.eoh; |
Willy Tarreau | a5555ec | 2008-11-30 19:02:32 +0100 | [diff] [blame] | 4928 | t->do_log(t); |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4929 | t->logs.bytes_out = 0; |
| 4930 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4931 | |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4932 | /* Note: we must not try to cheat by jumping directly to DATA, |
| 4933 | * otherwise we would not let the client side wake up. |
| 4934 | */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4935 | |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 4936 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4937 | } |
Willy Tarreau | e3fa6e5 | 2010-01-04 22:57:43 +0100 | [diff] [blame] | 4938 | return 1; |
Willy Tarreau | f5483bf | 2008-08-14 18:35:40 +0200 | [diff] [blame] | 4939 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 4940 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4941 | /* This function is an analyser which forwards response body (including chunk |
| 4942 | * sizes if any). It is called as soon as we must forward, even if we forward |
| 4943 | * zero byte. The only situation where it must not be called is when we're in |
| 4944 | * tunnel mode and we want to forward till the close. It's used both to forward |
| 4945 | * remaining data and to resync after end of body. It expects the msg_state to |
| 4946 | * be between MSG_BODY and MSG_DONE (inclusive). It returns zero if it needs to |
| 4947 | * read more data, or 1 once we can go on with next request or end the session. |
| 4948 | * When in MSG_DATA or MSG_TRAILERS, it will automatically forward hdr_content_len |
| 4949 | * bytes of pending data + the headers if not already done (between som and sov). |
| 4950 | * It eventually adjusts som to match sov after the data in between have been sent. |
| 4951 | */ |
| 4952 | int http_response_forward_body(struct session *s, struct buffer *res, int an_bit) |
| 4953 | { |
| 4954 | struct http_txn *txn = &s->txn; |
| 4955 | struct http_msg *msg = &s->txn.rsp; |
| 4956 | |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 4957 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4958 | return 0; |
| 4959 | |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 4960 | 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] | 4961 | ((res->flags & BF_SHUTW) && (res->to_forward || res->send_max)) || |
Willy Tarreau | 6c2cbe1 | 2010-01-03 17:07:49 +0100 | [diff] [blame] | 4962 | !s->req->analysers) { |
| 4963 | /* in case of error or if the other analyser went away, we can't analyse HTTP anymore */ |
| 4964 | buffer_ignore(res, res->l - res->send_max); |
Willy Tarreau | 90deb18 | 2010-01-07 00:20:41 +0100 | [diff] [blame] | 4965 | buffer_auto_read(res); |
| 4966 | buffer_auto_close(res); |
Willy Tarreau | 082b01c | 2010-01-02 23:58:04 +0100 | [diff] [blame] | 4967 | res->analysers &= ~an_bit; |
| 4968 | return 1; |
| 4969 | } |
| 4970 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 4971 | buffer_dont_close(res); |
Willy Tarreau | b608feb | 2010-01-02 22:47:18 +0100 | [diff] [blame] | 4972 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4973 | if (msg->msg_state < HTTP_MSG_CHUNK_SIZE) { |
| 4974 | /* we have msg->col and msg->sov which both point to the first |
| 4975 | * byte of message body. msg->som still points to the beginning |
| 4976 | * of the message. We must save the body in req->lr because it |
| 4977 | * survives buffer re-alignments. |
| 4978 | */ |
| 4979 | res->lr = res->data + msg->sov; |
| 4980 | if (txn->flags & TX_RES_TE_CHNK) |
| 4981 | msg->msg_state = HTTP_MSG_CHUNK_SIZE; |
| 4982 | else { |
| 4983 | msg->msg_state = HTTP_MSG_DATA; |
| 4984 | } |
| 4985 | } |
| 4986 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 4987 | while (1) { |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 4988 | http_silent_debug(__LINE__, s); |
Willy Tarreau | 638cd02 | 2010-01-03 07:42:04 +0100 | [diff] [blame] | 4989 | /* we may have some data pending */ |
| 4990 | if (msg->hdr_content_len || msg->som != msg->sov) { |
| 4991 | int bytes = msg->sov - msg->som; |
| 4992 | if (bytes < 0) /* sov may have wrapped at the end */ |
| 4993 | bytes += res->size; |
| 4994 | buffer_forward(res, bytes + msg->hdr_content_len); |
| 4995 | msg->hdr_content_len = 0; /* don't forward that again */ |
| 4996 | msg->som = msg->sov; |
| 4997 | } |
| 4998 | |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 4999 | if (msg->msg_state == HTTP_MSG_DATA) { |
| 5000 | /* must still forward */ |
| 5001 | if (res->to_forward) |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5002 | goto missing_data; |
Willy Tarreau | caabe41 | 2010-01-03 23:08:28 +0100 | [diff] [blame] | 5003 | |
| 5004 | /* nothing left to forward */ |
| 5005 | if (txn->flags & TX_RES_TE_CHNK) |
| 5006 | msg->msg_state = HTTP_MSG_DATA_CRLF; |
| 5007 | else |
| 5008 | msg->msg_state = HTTP_MSG_DONE; |
| 5009 | } |
| 5010 | else if (msg->msg_state == HTTP_MSG_CHUNK_SIZE) { |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5011 | /* read the chunk size and assign it to ->hdr_content_len, then |
| 5012 | * set ->sov to point to the body and switch to DATA or TRAILERS state. |
| 5013 | */ |
| 5014 | int ret = http_parse_chunk_size(res, msg); |
| 5015 | |
| 5016 | if (!ret) |
| 5017 | goto missing_data; |
| 5018 | else if (ret < 0) |
| 5019 | goto return_bad_res; |
| 5020 | /* otherwise we're in HTTP_MSG_DATA or HTTP_MSG_TRAILERS state */ |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5021 | } |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5022 | else if (msg->msg_state == HTTP_MSG_DATA_CRLF) { |
| 5023 | /* we want the CRLF after the data */ |
| 5024 | int ret; |
| 5025 | |
Willy Tarreau | d3347ee | 2010-01-04 02:02:25 +0100 | [diff] [blame] | 5026 | res->lr = res->w + res->send_max; |
| 5027 | if (res->lr >= res->data + res->size) |
| 5028 | res->lr -= res->size; |
| 5029 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5030 | ret = http_skip_chunk_crlf(res, msg); |
| 5031 | |
| 5032 | if (!ret) |
| 5033 | goto missing_data; |
| 5034 | else if (ret < 0) |
| 5035 | goto return_bad_res; |
| 5036 | /* we're in MSG_CHUNK_SIZE now */ |
| 5037 | } |
| 5038 | else if (msg->msg_state == HTTP_MSG_TRAILERS) { |
| 5039 | int ret = http_forward_trailers(res, msg); |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5040 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5041 | if (ret == 0) |
| 5042 | goto missing_data; |
| 5043 | else if (ret < 0) |
| 5044 | goto return_bad_res; |
| 5045 | /* we're in HTTP_MSG_DONE now */ |
| 5046 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5047 | else { |
| 5048 | /* other states, DONE...TUNNEL */ |
| 5049 | if (http_resync_states(s)) { |
| 5050 | http_silent_debug(__LINE__, s); |
| 5051 | /* some state changes occurred, maybe the analyser |
| 5052 | * was disabled too. |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5053 | */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5054 | if (unlikely(msg->msg_state == HTTP_MSG_ERROR)) |
| 5055 | goto return_bad_res; |
| 5056 | return 1; |
Willy Tarreau | 5523b32 | 2009-12-29 12:05:52 +0100 | [diff] [blame] | 5057 | } |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5058 | return 0; |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5059 | } |
| 5060 | } |
| 5061 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5062 | missing_data: |
Willy Tarreau | f5c8bd6 | 2010-01-04 07:10:34 +0100 | [diff] [blame] | 5063 | /* stop waiting for data if the input is closed before the end */ |
| 5064 | if (res->flags & BF_SHUTR) |
| 5065 | goto return_bad_res; |
| 5066 | |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5067 | if (!s->req->analysers) |
| 5068 | goto return_bad_res; |
| 5069 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5070 | /* forward the chunk size as well as any pending data */ |
| 5071 | if (msg->hdr_content_len || msg->som != msg->sov) { |
| 5072 | buffer_forward(res, msg->sov - msg->som + msg->hdr_content_len); |
| 5073 | msg->hdr_content_len = 0; /* don't forward that again */ |
| 5074 | msg->som = msg->sov; |
| 5075 | } |
| 5076 | |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5077 | /* the session handler will take care of timeouts and errors */ |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5078 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5079 | return 0; |
| 5080 | |
| 5081 | return_bad_res: /* let's centralize all bad resuests */ |
| 5082 | txn->rsp.msg_state = HTTP_MSG_ERROR; |
| 5083 | txn->status = 502; |
Willy Tarreau | 148d099 | 2010-01-10 10:21:21 +0100 | [diff] [blame] | 5084 | /* don't send any error message as we're in the body */ |
| 5085 | stream_int_retnclose(res->cons, NULL); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5086 | res->analysers = 0; |
| 5087 | s->be->counters.failed_resp++; |
| 5088 | if (s->srv) { |
| 5089 | s->srv->counters.failed_resp++; |
| 5090 | health_adjust(s->srv, HANA_STATUS_HTTP_HDRRSP); |
| 5091 | } |
| 5092 | |
| 5093 | if (!(s->flags & SN_ERR_MASK)) |
| 5094 | s->flags |= SN_ERR_PRXCOND; |
| 5095 | if (!(s->flags & SN_FINST_MASK)) |
| 5096 | s->flags |= SN_FINST_R; |
Willy Tarreau | 610ecce | 2010-01-04 21:15:02 +0100 | [diff] [blame] | 5097 | http_silent_debug(__LINE__, s); |
Willy Tarreau | d98cf93 | 2009-12-27 22:54:55 +0100 | [diff] [blame] | 5098 | return 0; |
| 5099 | } |
| 5100 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5101 | /* Iterate the same filter through all request headers. |
| 5102 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5103 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5104 | * DENY stats. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5105 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5106 | 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] | 5107 | { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5108 | char term; |
| 5109 | char *cur_ptr, *cur_end, *cur_next; |
| 5110 | int cur_idx, old_idx, last_hdr; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5111 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5112 | struct hdr_idx_elem *cur_hdr; |
| 5113 | int len, delta; |
Willy Tarreau | 0f7562b | 2007-01-07 15:46:13 +0100 | [diff] [blame] | 5114 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5115 | last_hdr = 0; |
| 5116 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5117 | cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5118 | old_idx = 0; |
| 5119 | |
| 5120 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5121 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5122 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5123 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5124 | (exp->action == ACT_ALLOW || |
| 5125 | exp->action == ACT_DENY || |
| 5126 | exp->action == ACT_TARPIT)) |
| 5127 | return 0; |
| 5128 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5129 | cur_idx = txn->hdr_idx.v[old_idx].next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5130 | if (!cur_idx) |
| 5131 | break; |
| 5132 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5133 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5134 | cur_ptr = cur_next; |
| 5135 | cur_end = cur_ptr + cur_hdr->len; |
| 5136 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5137 | |
| 5138 | /* Now we have one header between cur_ptr and cur_end, |
| 5139 | * and the next header starts at cur_next. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5140 | */ |
| 5141 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5142 | /* The annoying part is that pattern matching needs |
| 5143 | * that we modify the contents to null-terminate all |
| 5144 | * strings before testing them. |
| 5145 | */ |
| 5146 | |
| 5147 | term = *cur_end; |
| 5148 | *cur_end = '\0'; |
| 5149 | |
| 5150 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5151 | switch (exp->action) { |
| 5152 | case ACT_SETBE: |
| 5153 | /* It is not possible to jump a second time. |
| 5154 | * FIXME: should we return an HTTP/500 here so that |
| 5155 | * the admin knows there's a problem ? |
| 5156 | */ |
| 5157 | if (t->be != t->fe) |
| 5158 | break; |
| 5159 | |
| 5160 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 5161 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5162 | last_hdr = 1; |
| 5163 | break; |
| 5164 | |
| 5165 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5166 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5167 | last_hdr = 1; |
| 5168 | break; |
| 5169 | |
| 5170 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5171 | txn->flags |= TX_CLDENY; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5172 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5173 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 5174 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5175 | if (t->listener->counters) |
| 5176 | t->listener->counters->denied_resp++; |
| 5177 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5178 | break; |
| 5179 | |
| 5180 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5181 | txn->flags |= TX_CLTARPIT; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5182 | last_hdr = 1; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5183 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 5184 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5185 | if (t->listener->counters) |
| 5186 | t->listener->counters->denied_resp++; |
| 5187 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5188 | break; |
| 5189 | |
| 5190 | case ACT_REPLACE: |
| 5191 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5192 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 5193 | /* FIXME: if the user adds a newline in the replacement, the |
| 5194 | * index will not be recalculated for now, and the new line |
| 5195 | * will not be counted as a new header. |
| 5196 | */ |
| 5197 | |
| 5198 | cur_end += delta; |
| 5199 | cur_next += delta; |
| 5200 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5201 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5202 | break; |
| 5203 | |
| 5204 | case ACT_REMOVE: |
| 5205 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
| 5206 | cur_next += delta; |
| 5207 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5208 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5209 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5210 | txn->hdr_idx.used--; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5211 | cur_hdr->len = 0; |
| 5212 | cur_end = NULL; /* null-term has been rewritten */ |
| 5213 | break; |
| 5214 | |
| 5215 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5216 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5217 | if (cur_end) |
| 5218 | *cur_end = term; /* restore the string terminator */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5219 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5220 | /* keep the link from this header to next one in case of later |
| 5221 | * removal of next header. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5222 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5223 | old_idx = cur_idx; |
| 5224 | } |
| 5225 | return 0; |
| 5226 | } |
| 5227 | |
| 5228 | |
| 5229 | /* Apply the filter to the request line. |
| 5230 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 5231 | * or -1 if a replacement resulted in an invalid request line. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5232 | * Since it can manage the switch to another backend, it updates the per-proxy |
| 5233 | * DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5234 | */ |
| 5235 | int apply_filter_to_req_line(struct session *t, struct buffer *req, struct hdr_exp *exp) |
| 5236 | { |
| 5237 | char term; |
| 5238 | char *cur_ptr, *cur_end; |
| 5239 | int done; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5240 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5241 | int len, delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5242 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5243 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5244 | if (unlikely(txn->flags & (TX_CLDENY | TX_CLTARPIT))) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5245 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5246 | else if (unlikely(txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5247 | (exp->action == ACT_ALLOW || |
| 5248 | exp->action == ACT_DENY || |
| 5249 | exp->action == ACT_TARPIT)) |
| 5250 | return 0; |
| 5251 | else if (exp->action == ACT_REMOVE) |
| 5252 | return 0; |
| 5253 | |
| 5254 | done = 0; |
| 5255 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5256 | cur_ptr = txn->req.sol; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5257 | cur_end = cur_ptr + txn->req.sl.rq.l; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5258 | |
| 5259 | /* Now we have the request line between cur_ptr and cur_end */ |
| 5260 | |
| 5261 | /* The annoying part is that pattern matching needs |
| 5262 | * that we modify the contents to null-terminate all |
| 5263 | * strings before testing them. |
| 5264 | */ |
| 5265 | |
| 5266 | term = *cur_end; |
| 5267 | *cur_end = '\0'; |
| 5268 | |
| 5269 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5270 | switch (exp->action) { |
| 5271 | case ACT_SETBE: |
| 5272 | /* It is not possible to jump a second time. |
| 5273 | * FIXME: should we return an HTTP/500 here so that |
| 5274 | * the admin knows there's a problem ? |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5275 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5276 | if (t->be != t->fe) |
| 5277 | break; |
| 5278 | |
| 5279 | /* Swithing Proxy */ |
Willy Tarreau | 1d0dfb1 | 2009-07-07 15:10:31 +0200 | [diff] [blame] | 5280 | session_set_backend(t, (struct proxy *)exp->replace); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5281 | done = 1; |
| 5282 | break; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5283 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5284 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5285 | txn->flags |= TX_CLALLOW; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5286 | done = 1; |
| 5287 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5288 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5289 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5290 | txn->flags |= TX_CLDENY; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5291 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 5292 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5293 | if (t->listener->counters) |
| 5294 | t->listener->counters->denied_resp++; |
| 5295 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5296 | done = 1; |
| 5297 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5298 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5299 | case ACT_TARPIT: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5300 | txn->flags |= TX_CLTARPIT; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5301 | |
Krzysztof Piotr Oledzki | 052d4fd | 2009-10-04 14:52:57 +0200 | [diff] [blame] | 5302 | t->be->counters.denied_req++; |
Krzysztof Piotr Oledzki | aeebf9b | 2009-10-04 15:43:17 +0200 | [diff] [blame] | 5303 | if (t->listener->counters) |
| 5304 | t->listener->counters->denied_resp++; |
| 5305 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5306 | done = 1; |
| 5307 | break; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5308 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5309 | case ACT_REPLACE: |
| 5310 | *cur_end = term; /* restore the string terminator */ |
| 5311 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5312 | delta = buffer_replace2(req, cur_ptr, cur_end, trash, len); |
| 5313 | /* FIXME: if the user adds a newline in the replacement, the |
| 5314 | * index will not be recalculated for now, and the new line |
| 5315 | * will not be counted as a new header. |
| 5316 | */ |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5317 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5318 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5319 | cur_end += delta; |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5320 | cur_end = (char *)http_parse_reqline(&txn->req, req->data, |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5321 | HTTP_MSG_RQMETH, |
| 5322 | cur_ptr, cur_end + 1, |
| 5323 | NULL, NULL); |
| 5324 | if (unlikely(!cur_end)) |
| 5325 | return -1; |
Willy Tarreau | a496b60 | 2006-12-17 23:15:24 +0100 | [diff] [blame] | 5326 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5327 | /* we have a full request and we know that we have either a CR |
| 5328 | * or an LF at <ptr>. |
| 5329 | */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5330 | txn->meth = find_http_meth(cur_ptr, txn->req.sl.rq.m_l); |
| 5331 | 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] | 5332 | /* there is no point trying this regex on headers */ |
| 5333 | return 1; |
| 5334 | } |
| 5335 | } |
| 5336 | *cur_end = term; /* restore the string terminator */ |
| 5337 | return done; |
| 5338 | } |
Willy Tarreau | 97de624 | 2006-12-27 17:18:38 +0100 | [diff] [blame] | 5339 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5340 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5341 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5342 | /* |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5343 | * Apply all the req filters of proxy <px> to all headers in buffer <req> of session <s>. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5344 | * 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] | 5345 | * unparsable request. Since it can manage the switch to another backend, it |
| 5346 | * updates the per-proxy DENY stats. |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5347 | */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5348 | int apply_filters_to_request(struct session *s, struct buffer *req, struct proxy *px) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5349 | { |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5350 | struct http_txn *txn = &s->txn; |
| 5351 | struct hdr_exp *exp; |
| 5352 | |
| 5353 | for (exp = px->req_exp; exp; exp = exp->next) { |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5354 | int ret; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5355 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5356 | /* |
| 5357 | * The interleaving of transformations and verdicts |
| 5358 | * makes it difficult to decide to continue or stop |
| 5359 | * the evaluation. |
| 5360 | */ |
| 5361 | |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5362 | if (txn->flags & (TX_CLDENY|TX_CLTARPIT)) |
| 5363 | break; |
| 5364 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5365 | if ((txn->flags & TX_CLALLOW) && |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5366 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5367 | exp->action == ACT_TARPIT || exp->action == ACT_PASS)) |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5368 | continue; |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5369 | |
| 5370 | /* if this filter had a condition, evaluate it now and skip to |
| 5371 | * next filter if the condition does not match. |
| 5372 | */ |
| 5373 | if (exp->cond) { |
| 5374 | ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_REQ); |
| 5375 | ret = acl_pass(ret); |
| 5376 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 5377 | ret = !ret; |
| 5378 | |
| 5379 | if (!ret) |
| 5380 | continue; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5381 | } |
| 5382 | |
| 5383 | /* Apply the filter to the request line. */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5384 | ret = apply_filter_to_req_line(s, req, exp); |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5385 | if (unlikely(ret < 0)) |
| 5386 | return -1; |
| 5387 | |
| 5388 | if (likely(ret == 0)) { |
| 5389 | /* The filter did not match the request, it can be |
| 5390 | * iterated through all headers. |
| 5391 | */ |
Willy Tarreau | 6c123b1 | 2010-01-28 20:22:06 +0100 | [diff] [blame] | 5392 | apply_filter_to_req_headers(s, req, exp); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5393 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5394 | } |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5395 | return 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5396 | } |
| 5397 | |
| 5398 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5399 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5400 | /* |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5401 | * Try to retrieve the server associated to the appsession. |
| 5402 | * If the server is found, it's assigned to the session. |
| 5403 | */ |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5404 | 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] | 5405 | struct http_txn *txn = &t->txn; |
| 5406 | appsess *asession = NULL; |
| 5407 | char *sessid_temp = NULL; |
| 5408 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5409 | if (len > t->be->appsession_len) { |
| 5410 | len = t->be->appsession_len; |
| 5411 | } |
| 5412 | |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5413 | if (t->be->options2 & PR_O2_AS_REQL) { |
| 5414 | /* 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] | 5415 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5416 | /* 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] | 5417 | pool_free2(apools.sessid, txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5418 | } |
| 5419 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5420 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5421 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5422 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5423 | return; |
| 5424 | } |
| 5425 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 5426 | memcpy(txn->sessid, buf, len); |
| 5427 | txn->sessid[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5428 | } |
| 5429 | |
| 5430 | if ((sessid_temp = pool_alloc2(apools.sessid)) == NULL) { |
| 5431 | Alert("Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5432 | send_log(t->be, LOG_ALERT, "Not enough memory process_cli():asession->sessid:malloc().\n"); |
| 5433 | return; |
| 5434 | } |
| 5435 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5436 | memcpy(sessid_temp, buf, len); |
| 5437 | sessid_temp[len] = 0; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5438 | |
| 5439 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), sessid_temp); |
| 5440 | /* free previously allocated memory */ |
| 5441 | pool_free2(apools.sessid, sessid_temp); |
| 5442 | |
| 5443 | if (asession != NULL) { |
| 5444 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 5445 | if (!(t->be->options2 & PR_O2_AS_REQL)) |
| 5446 | asession->request_count++; |
| 5447 | |
| 5448 | if (asession->serverid != NULL) { |
| 5449 | struct server *srv = t->be->srv; |
| 5450 | while (srv) { |
| 5451 | if (strcmp(srv->id, asession->serverid) == 0) { |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 5452 | if ((srv->state & SRV_RUNNING) || |
| 5453 | (t->be->options & PR_O_PERSIST) || |
| 5454 | (t->flags & SN_FORCE_PRST)) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5455 | /* we found the server and it's usable */ |
| 5456 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 2a6d88d | 2010-01-24 13:10:43 +0100 | [diff] [blame] | 5457 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 5458 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
| 5459 | t->srv = srv; |
| 5460 | break; |
| 5461 | } else { |
| 5462 | txn->flags &= ~TX_CK_MASK; |
| 5463 | txn->flags |= TX_CK_DOWN; |
| 5464 | } |
| 5465 | } |
| 5466 | srv = srv->next; |
| 5467 | } |
| 5468 | } |
| 5469 | } |
| 5470 | } |
| 5471 | |
| 5472 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 5473 | * Manage client-side cookie. It can impact performance by about 2% so it is |
| 5474 | * desirable to call it only when needed. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5475 | */ |
| 5476 | void manage_client_side_cookies(struct session *t, struct buffer *req) |
| 5477 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5478 | struct http_txn *txn = &t->txn; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5479 | char *p1, *p2, *p3, *p4, *p5; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5480 | char *del_colon, *del_cookie, *colon; |
| 5481 | int app_cookies; |
| 5482 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5483 | char *cur_ptr, *cur_end, *cur_next; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 5484 | int cur_idx, old_idx; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5485 | |
Willy Tarreau | 2a32428 | 2006-12-05 00:05:46 +0100 | [diff] [blame] | 5486 | /* Iterate through the headers. |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5487 | * we start with the start line. |
| 5488 | */ |
Willy Tarreau | 83969f4 | 2007-01-22 08:55:47 +0100 | [diff] [blame] | 5489 | old_idx = 0; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5490 | cur_next = txn->req.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5491 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5492 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5493 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5494 | int val; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5495 | |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5496 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5497 | cur_ptr = cur_next; |
| 5498 | cur_end = cur_ptr + cur_hdr->len; |
| 5499 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5500 | |
| 5501 | /* We have one full header between cur_ptr and cur_end, and the |
| 5502 | * next header starts at cur_next. We're only interested in |
| 5503 | * "Cookie:" headers. |
| 5504 | */ |
| 5505 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5506 | val = http_header_match2(cur_ptr, cur_end, "Cookie", 6); |
| 5507 | if (!val) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5508 | old_idx = cur_idx; |
| 5509 | continue; |
| 5510 | } |
| 5511 | |
| 5512 | /* Now look for cookies. Conforming to RFC2109, we have to support |
| 5513 | * attributes whose name begin with a '$', and associate them with |
| 5514 | * the right cookie, if we want to delete this cookie. |
| 5515 | * So there are 3 cases for each cookie read : |
| 5516 | * 1) it's a special attribute, beginning with a '$' : ignore it. |
| 5517 | * 2) it's a server id cookie that we *MAY* want to delete : save |
| 5518 | * some pointers on it (last semi-colon, beginning of cookie...) |
| 5519 | * 3) it's an application cookie : we *MAY* have to delete a previous |
| 5520 | * "special" cookie. |
| 5521 | * At the end of loop, if a "special" cookie remains, we may have to |
| 5522 | * remove it. If no application cookie persists in the header, we |
| 5523 | * *MUST* delete it |
| 5524 | */ |
| 5525 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 5526 | colon = p1 = cur_ptr + val; /* first non-space char after 'Cookie:' */ |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5527 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5528 | /* del_cookie == NULL => nothing to be deleted */ |
| 5529 | del_colon = del_cookie = NULL; |
| 5530 | app_cookies = 0; |
| 5531 | |
| 5532 | while (p1 < cur_end) { |
| 5533 | /* skip spaces and colons, but keep an eye on these ones */ |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5534 | resync_name: |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5535 | while (p1 < cur_end) { |
| 5536 | if (*p1 == ';' || *p1 == ',') |
| 5537 | colon = p1; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 5538 | else if (!isspace((unsigned char)*p1)) |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5539 | break; |
| 5540 | p1++; |
| 5541 | } |
| 5542 | |
| 5543 | if (p1 == cur_end) |
| 5544 | break; |
| 5545 | |
| 5546 | /* p1 is at the beginning of the cookie name */ |
| 5547 | p2 = p1; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5548 | while (p2 < cur_end && *p2 != '=') { |
| 5549 | if (*p2 == ',' || *p2 == ';' || isspace((unsigned char)*p2)) { |
| 5550 | /* oops, the cookie name was truncated, resync */ |
| 5551 | p1 = p2; |
| 5552 | goto resync_name; |
| 5553 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5554 | p2++; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5555 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5556 | |
| 5557 | if (p2 == cur_end) |
| 5558 | break; |
| 5559 | |
| 5560 | p3 = p2 + 1; /* skips the '=' sign */ |
| 5561 | if (p3 == cur_end) |
| 5562 | break; |
| 5563 | |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5564 | /* parse the value, stripping leading and trailing spaces but keeping insiders. */ |
| 5565 | p5 = p4 = p3; |
| 5566 | while (p5 < cur_end && *p5 != ';' && *p5 != ',') { |
| 5567 | if (!isspace((unsigned char)*p5)) |
| 5568 | p4 = p5 + 1; |
| 5569 | p5++; |
| 5570 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5571 | |
| 5572 | /* here, we have the cookie name between p1 and p2, |
| 5573 | * and its value between p3 and p4. |
| 5574 | * we can process it : |
| 5575 | * |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5576 | * Cookie: NAME=VALUE ; |
| 5577 | * | || || |+-> p5 |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5578 | * | || || +--> p4 |
| 5579 | * | || |+-------> p3 |
| 5580 | * | || +--------> p2 |
| 5581 | * | |+------------> p1 |
| 5582 | * | +-------------> colon |
| 5583 | * +--------------------> cur_ptr |
| 5584 | */ |
| 5585 | |
| 5586 | if (*p1 == '$') { |
| 5587 | /* skip this one */ |
| 5588 | } |
| 5589 | else { |
| 5590 | /* first, let's see if we want to capture it */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5591 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5592 | txn->cli_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5593 | (p4 - p1 >= t->fe->capture_namelen) && |
| 5594 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5595 | int log_len = p4 - p1; |
| 5596 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 5597 | if ((txn->cli_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5598 | Alert("HTTP logging : out of memory.\n"); |
| 5599 | } else { |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5600 | if (log_len > t->fe->capture_len) |
| 5601 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 5602 | memcpy(txn->cli_cookie, p1, log_len); |
| 5603 | txn->cli_cookie[log_len] = 0; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5604 | } |
| 5605 | } |
| 5606 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5607 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 5608 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5609 | /* Cool... it's the right one */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5610 | struct server *srv = t->be->srv; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5611 | char *delim; |
| 5612 | |
| 5613 | /* if we're in cookie prefix mode, we'll search the delimitor so that we |
| 5614 | * have the server ID betweek p3 and delim, and the original cookie between |
| 5615 | * delim+1 and p4. Otherwise, delim==p4 : |
| 5616 | * |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5617 | * Cookie: NAME=SRV~VALUE ; |
| 5618 | * | || || | |+-> p5 |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5619 | * | || || | +--> p4 |
| 5620 | * | || || +--------> delim |
| 5621 | * | || |+-----------> p3 |
| 5622 | * | || +------------> p2 |
| 5623 | * | |+----------------> p1 |
| 5624 | * | +-----------------> colon |
| 5625 | * +------------------------> cur_ptr |
| 5626 | */ |
| 5627 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5628 | if (t->be->options & PR_O_COOK_PFX) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5629 | for (delim = p3; delim < p4; delim++) |
| 5630 | if (*delim == COOKIE_DELIM) |
| 5631 | break; |
| 5632 | } |
| 5633 | else |
| 5634 | delim = p4; |
| 5635 | |
| 5636 | |
| 5637 | /* Here, we'll look for the first running server which supports the cookie. |
| 5638 | * This allows to share a same cookie between several servers, for example |
| 5639 | * to dedicate backup servers to specific servers only. |
| 5640 | * However, to prevent clients from sticking to cookie-less backup server |
| 5641 | * when they have incidentely learned an empty cookie, we simply ignore |
| 5642 | * empty cookies and mark them as invalid. |
| 5643 | */ |
| 5644 | if (delim == p3) |
| 5645 | srv = NULL; |
| 5646 | |
| 5647 | while (srv) { |
Willy Tarreau | 92f2ab1 | 2007-02-02 22:14:47 +0100 | [diff] [blame] | 5648 | if (srv->cookie && (srv->cklen == delim - p3) && |
| 5649 | !memcmp(p3, srv->cookie, delim - p3)) { |
Willy Tarreau | 4de9149 | 2010-01-22 19:10:05 +0100 | [diff] [blame] | 5650 | if ((srv->state & SRV_RUNNING) || |
| 5651 | (t->be->options & PR_O_PERSIST) || |
| 5652 | (t->flags & SN_FORCE_PRST)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5653 | /* we found the server and it's usable */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5654 | txn->flags &= ~TX_CK_MASK; |
Willy Tarreau | 2a6d88d | 2010-01-24 13:10:43 +0100 | [diff] [blame] | 5655 | txn->flags |= (srv->state & SRV_RUNNING) ? TX_CK_VALID : TX_CK_DOWN; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5656 | t->flags |= SN_DIRECT | SN_ASSIGNED; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5657 | t->srv = srv; |
| 5658 | break; |
| 5659 | } else { |
| 5660 | /* we found a server, but it's down */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5661 | txn->flags &= ~TX_CK_MASK; |
| 5662 | txn->flags |= TX_CK_DOWN; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5663 | } |
| 5664 | } |
| 5665 | srv = srv->next; |
| 5666 | } |
| 5667 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5668 | if (!srv && !(txn->flags & TX_CK_DOWN)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5669 | /* no server matched this cookie */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5670 | txn->flags &= ~TX_CK_MASK; |
| 5671 | txn->flags |= TX_CK_INVALID; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5672 | } |
| 5673 | |
| 5674 | /* depending on the cookie mode, we may have to either : |
| 5675 | * - delete the complete cookie if we're in insert+indirect mode, so that |
| 5676 | * the server never sees it ; |
| 5677 | * - remove the server id from the cookie value, and tag the cookie as an |
| 5678 | * application cookie so that it does not get accidentely removed later, |
| 5679 | * if we're in cookie prefix mode |
| 5680 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5681 | if ((t->be->options & PR_O_COOK_PFX) && (delim != p4)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5682 | int delta; /* negative */ |
| 5683 | |
| 5684 | delta = buffer_replace2(req, p3, delim + 1, NULL, 0); |
| 5685 | p4 += delta; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5686 | p5 += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5687 | cur_end += delta; |
| 5688 | cur_next += delta; |
| 5689 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5690 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5691 | |
| 5692 | del_cookie = del_colon = NULL; |
| 5693 | app_cookies++; /* protect the header from deletion */ |
| 5694 | } |
| 5695 | else if (del_cookie == NULL && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 5696 | (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] | 5697 | del_cookie = p1; |
| 5698 | del_colon = colon; |
| 5699 | } |
| 5700 | } else { |
| 5701 | /* now we know that we must keep this cookie since it's |
| 5702 | * not ours. But if we wanted to delete our cookie |
| 5703 | * earlier, we cannot remove the complete header, but we |
| 5704 | * can remove the previous block itself. |
| 5705 | */ |
| 5706 | app_cookies++; |
| 5707 | |
| 5708 | if (del_cookie != NULL) { |
| 5709 | int delta; /* negative */ |
| 5710 | |
| 5711 | delta = buffer_replace2(req, del_cookie, p1, NULL, 0); |
| 5712 | p4 += delta; |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5713 | p5 += delta; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5714 | cur_end += delta; |
| 5715 | cur_next += delta; |
| 5716 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5717 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5718 | del_cookie = del_colon = NULL; |
| 5719 | } |
| 5720 | } |
| 5721 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5722 | if (t->be->appsession_name != NULL) { |
| 5723 | int cmp_len, value_len; |
| 5724 | char *value_begin; |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5725 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 5726 | if (t->be->options2 & PR_O2_AS_PFX) { |
| 5727 | cmp_len = MIN(p4 - p1, t->be->appsession_name_len); |
| 5728 | value_begin = p1 + t->be->appsession_name_len; |
| 5729 | value_len = p4 - p1 - t->be->appsession_name_len; |
| 5730 | } else { |
| 5731 | cmp_len = p2 - p1; |
| 5732 | value_begin = p3; |
| 5733 | value_len = p4 - p3; |
| 5734 | } |
| 5735 | |
| 5736 | /* let's see if the cookie is our appcookie */ |
| 5737 | if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) { |
| 5738 | /* Cool... it's the right one */ |
| 5739 | manage_client_side_appsession(t, value_begin, value_len); |
| 5740 | } |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 5741 | #if defined(DEBUG_HASH) |
| 5742 | Alert("manage_client_side_cookies\n"); |
| 5743 | appsession_hash_dump(&(t->be->htbl_proxy)); |
| 5744 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5745 | }/* end if ((t->proxy->appsession_name != NULL) ... */ |
| 5746 | } |
| 5747 | |
| 5748 | /* we'll have to look for another cookie ... */ |
Willy Tarreau | 305ae85 | 2010-01-03 19:45:54 +0100 | [diff] [blame] | 5749 | p1 = p5; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5750 | } /* while (p1 < cur_end) */ |
| 5751 | |
| 5752 | /* There's no more cookie on this line. |
| 5753 | * We may have marked the last one(s) for deletion. |
| 5754 | * We must do this now in two ways : |
| 5755 | * - if there is no app cookie, we simply delete the header ; |
| 5756 | * - if there are app cookies, we must delete the end of the |
| 5757 | * string properly, including the colon/semi-colon before |
| 5758 | * the cookie name. |
| 5759 | */ |
| 5760 | if (del_cookie != NULL) { |
| 5761 | int delta; |
| 5762 | if (app_cookies) { |
| 5763 | delta = buffer_replace2(req, del_colon, cur_end, NULL, 0); |
| 5764 | cur_end = del_colon; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5765 | cur_hdr->len += delta; |
| 5766 | } else { |
| 5767 | delta = buffer_replace2(req, cur_ptr, cur_next, NULL, 0); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5768 | |
| 5769 | /* FIXME: this should be a separate function */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 5770 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5771 | txn->hdr_idx.used--; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5772 | cur_hdr->len = 0; |
| 5773 | } |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 5774 | cur_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5775 | http_msg_move_end(&txn->req, delta); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 5776 | } |
| 5777 | |
| 5778 | /* keep the link from this header to next one */ |
| 5779 | old_idx = cur_idx; |
| 5780 | } /* end of cookie processing on this header */ |
| 5781 | } |
| 5782 | |
| 5783 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5784 | /* Iterate the same filter through all response headers contained in <rtr>. |
| 5785 | * Returns 1 if this filter can be stopped upon return, otherwise 0. |
| 5786 | */ |
| 5787 | int apply_filter_to_resp_headers(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 5788 | { |
| 5789 | char term; |
| 5790 | char *cur_ptr, *cur_end, *cur_next; |
| 5791 | int cur_idx, old_idx, last_hdr; |
| 5792 | struct http_txn *txn = &t->txn; |
| 5793 | struct hdr_idx_elem *cur_hdr; |
| 5794 | int len, delta; |
| 5795 | |
| 5796 | last_hdr = 0; |
| 5797 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5798 | cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5799 | old_idx = 0; |
| 5800 | |
| 5801 | while (!last_hdr) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5802 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5803 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5804 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5805 | (exp->action == ACT_ALLOW || |
| 5806 | exp->action == ACT_DENY)) |
| 5807 | return 0; |
| 5808 | |
| 5809 | cur_idx = txn->hdr_idx.v[old_idx].next; |
| 5810 | if (!cur_idx) |
| 5811 | break; |
| 5812 | |
| 5813 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 5814 | cur_ptr = cur_next; |
| 5815 | cur_end = cur_ptr + cur_hdr->len; |
| 5816 | cur_next = cur_end + cur_hdr->cr + 1; |
| 5817 | |
| 5818 | /* Now we have one header between cur_ptr and cur_end, |
| 5819 | * and the next header starts at cur_next. |
| 5820 | */ |
| 5821 | |
| 5822 | /* The annoying part is that pattern matching needs |
| 5823 | * that we modify the contents to null-terminate all |
| 5824 | * strings before testing them. |
| 5825 | */ |
| 5826 | |
| 5827 | term = *cur_end; |
| 5828 | *cur_end = '\0'; |
| 5829 | |
| 5830 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5831 | switch (exp->action) { |
| 5832 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5833 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5834 | last_hdr = 1; |
| 5835 | break; |
| 5836 | |
| 5837 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5838 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5839 | last_hdr = 1; |
| 5840 | break; |
| 5841 | |
| 5842 | case ACT_REPLACE: |
| 5843 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5844 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 5845 | /* FIXME: if the user adds a newline in the replacement, the |
| 5846 | * index will not be recalculated for now, and the new line |
| 5847 | * will not be counted as a new header. |
| 5848 | */ |
| 5849 | |
| 5850 | cur_end += delta; |
| 5851 | cur_next += delta; |
| 5852 | cur_hdr->len += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5853 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5854 | break; |
| 5855 | |
| 5856 | case ACT_REMOVE: |
| 5857 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 5858 | cur_next += delta; |
| 5859 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5860 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5861 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 5862 | txn->hdr_idx.used--; |
| 5863 | cur_hdr->len = 0; |
| 5864 | cur_end = NULL; /* null-term has been rewritten */ |
| 5865 | break; |
| 5866 | |
| 5867 | } |
| 5868 | } |
| 5869 | if (cur_end) |
| 5870 | *cur_end = term; /* restore the string terminator */ |
| 5871 | |
| 5872 | /* keep the link from this header to next one in case of later |
| 5873 | * removal of next header. |
| 5874 | */ |
| 5875 | old_idx = cur_idx; |
| 5876 | } |
| 5877 | return 0; |
| 5878 | } |
| 5879 | |
| 5880 | |
| 5881 | /* Apply the filter to the status line in the response buffer <rtr>. |
| 5882 | * Returns 0 if nothing has been done, 1 if the filter has been applied, |
| 5883 | * or -1 if a replacement resulted in an invalid status line. |
| 5884 | */ |
| 5885 | int apply_filter_to_sts_line(struct session *t, struct buffer *rtr, struct hdr_exp *exp) |
| 5886 | { |
| 5887 | char term; |
| 5888 | char *cur_ptr, *cur_end; |
| 5889 | int done; |
| 5890 | struct http_txn *txn = &t->txn; |
| 5891 | int len, delta; |
| 5892 | |
| 5893 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5894 | if (unlikely(txn->flags & TX_SVDENY)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5895 | return 1; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5896 | else if (unlikely(txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5897 | (exp->action == ACT_ALLOW || |
| 5898 | exp->action == ACT_DENY)) |
| 5899 | return 0; |
| 5900 | else if (exp->action == ACT_REMOVE) |
| 5901 | return 0; |
| 5902 | |
| 5903 | done = 0; |
| 5904 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5905 | cur_ptr = txn->rsp.sol; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5906 | cur_end = cur_ptr + txn->rsp.sl.rq.l; |
| 5907 | |
| 5908 | /* Now we have the status line between cur_ptr and cur_end */ |
| 5909 | |
| 5910 | /* The annoying part is that pattern matching needs |
| 5911 | * that we modify the contents to null-terminate all |
| 5912 | * strings before testing them. |
| 5913 | */ |
| 5914 | |
| 5915 | term = *cur_end; |
| 5916 | *cur_end = '\0'; |
| 5917 | |
| 5918 | if (regexec(exp->preg, cur_ptr, MAX_MATCH, pmatch, 0) == 0) { |
| 5919 | switch (exp->action) { |
| 5920 | case ACT_ALLOW: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5921 | txn->flags |= TX_SVALLOW; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5922 | done = 1; |
| 5923 | break; |
| 5924 | |
| 5925 | case ACT_DENY: |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5926 | txn->flags |= TX_SVDENY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5927 | done = 1; |
| 5928 | break; |
| 5929 | |
| 5930 | case ACT_REPLACE: |
| 5931 | *cur_end = term; /* restore the string terminator */ |
| 5932 | len = exp_replace(trash, cur_ptr, exp->replace, pmatch); |
| 5933 | delta = buffer_replace2(rtr, cur_ptr, cur_end, trash, len); |
| 5934 | /* FIXME: if the user adds a newline in the replacement, the |
| 5935 | * index will not be recalculated for now, and the new line |
| 5936 | * will not be counted as a new header. |
| 5937 | */ |
| 5938 | |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 5939 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5940 | cur_end += delta; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5941 | cur_end = (char *)http_parse_stsline(&txn->rsp, rtr->data, |
Willy Tarreau | 0278576 | 2007-04-03 14:45:44 +0200 | [diff] [blame] | 5942 | HTTP_MSG_RPVER, |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5943 | cur_ptr, cur_end + 1, |
| 5944 | NULL, NULL); |
| 5945 | if (unlikely(!cur_end)) |
| 5946 | return -1; |
| 5947 | |
| 5948 | /* we have a full respnse and we know that we have either a CR |
| 5949 | * or an LF at <ptr>. |
| 5950 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 5951 | 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] | 5952 | hdr_idx_set_start(&txn->hdr_idx, txn->rsp.sl.rq.l, *cur_end == '\r'); |
| 5953 | /* there is no point trying this regex on headers */ |
| 5954 | return 1; |
| 5955 | } |
| 5956 | } |
| 5957 | *cur_end = term; /* restore the string terminator */ |
| 5958 | return done; |
| 5959 | } |
| 5960 | |
| 5961 | |
| 5962 | |
| 5963 | /* |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 5964 | * Apply all the resp filters of proxy <px> to all headers in buffer <rtr> of session <s>. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5965 | * Returns 0 if everything is alright, or -1 in case a replacement lead to an |
| 5966 | * unparsable response. |
| 5967 | */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 5968 | int apply_filters_to_response(struct session *s, struct buffer *rtr, struct proxy *px) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5969 | { |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 5970 | struct http_txn *txn = &s->txn; |
| 5971 | struct hdr_exp *exp; |
| 5972 | |
| 5973 | for (exp = px->rsp_exp; exp; exp = exp->next) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5974 | int ret; |
| 5975 | |
| 5976 | /* |
| 5977 | * The interleaving of transformations and verdicts |
| 5978 | * makes it difficult to decide to continue or stop |
| 5979 | * the evaluation. |
| 5980 | */ |
| 5981 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 5982 | if (txn->flags & TX_SVDENY) |
| 5983 | break; |
| 5984 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 5985 | if ((txn->flags & TX_SVALLOW) && |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 5986 | (exp->action == ACT_ALLOW || exp->action == ACT_DENY || |
| 5987 | exp->action == ACT_PASS)) { |
| 5988 | exp = exp->next; |
| 5989 | continue; |
| 5990 | } |
| 5991 | |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 5992 | /* if this filter had a condition, evaluate it now and skip to |
| 5993 | * next filter if the condition does not match. |
| 5994 | */ |
| 5995 | if (exp->cond) { |
| 5996 | ret = acl_exec_cond(exp->cond, px, s, txn, ACL_DIR_RTR); |
| 5997 | ret = acl_pass(ret); |
| 5998 | if (((struct acl_cond *)exp->cond)->pol == ACL_COND_UNLESS) |
| 5999 | ret = !ret; |
| 6000 | if (!ret) |
| 6001 | continue; |
| 6002 | } |
| 6003 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6004 | /* Apply the filter to the status line. */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6005 | ret = apply_filter_to_sts_line(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6006 | if (unlikely(ret < 0)) |
| 6007 | return -1; |
| 6008 | |
| 6009 | if (likely(ret == 0)) { |
| 6010 | /* The filter did not match the response, it can be |
| 6011 | * iterated through all headers. |
| 6012 | */ |
Willy Tarreau | fdb563c | 2010-01-31 15:43:27 +0100 | [diff] [blame] | 6013 | apply_filter_to_resp_headers(s, rtr, exp); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6014 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6015 | } |
| 6016 | return 0; |
| 6017 | } |
| 6018 | |
| 6019 | |
| 6020 | |
| 6021 | /* |
Willy Tarreau | 396d2c6 | 2007-11-04 19:30:00 +0100 | [diff] [blame] | 6022 | * Manage server-side cookies. It can impact performance by about 2% so it is |
| 6023 | * desirable to call it only when needed. |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6024 | */ |
| 6025 | void manage_server_side_cookies(struct session *t, struct buffer *rtr) |
| 6026 | { |
| 6027 | struct http_txn *txn = &t->txn; |
| 6028 | char *p1, *p2, *p3, *p4; |
| 6029 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6030 | char *cur_ptr, *cur_end, *cur_next; |
| 6031 | int cur_idx, old_idx, delta; |
| 6032 | |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6033 | /* Iterate through the headers. |
| 6034 | * we start with the start line. |
| 6035 | */ |
| 6036 | old_idx = 0; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6037 | cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6038 | |
| 6039 | while ((cur_idx = txn->hdr_idx.v[old_idx].next)) { |
| 6040 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6041 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6042 | |
| 6043 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 6044 | cur_ptr = cur_next; |
| 6045 | cur_end = cur_ptr + cur_hdr->len; |
| 6046 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6047 | |
| 6048 | /* We have one full header between cur_ptr and cur_end, and the |
| 6049 | * next header starts at cur_next. We're only interested in |
| 6050 | * "Cookie:" headers. |
| 6051 | */ |
| 6052 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6053 | val = http_header_match2(cur_ptr, cur_end, "Set-Cookie", 10); |
| 6054 | if (!val) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6055 | old_idx = cur_idx; |
| 6056 | continue; |
| 6057 | } |
| 6058 | |
| 6059 | /* 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] | 6060 | txn->flags |= TX_SCK_ANY; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6061 | |
| 6062 | |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6063 | /* maybe we only wanted to see if there was a set-cookie. Note that |
| 6064 | * the cookie capture is declared in the fronend. |
| 6065 | */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6066 | if (t->be->cookie_name == NULL && |
| 6067 | t->be->appsession_name == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6068 | t->fe->capture_name == NULL) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6069 | return; |
| 6070 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6071 | p1 = cur_ptr + val; /* first non-space char after 'Set-Cookie:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6072 | |
| 6073 | 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] | 6074 | if (p1 == cur_end || *p1 == ';') /* end of cookie */ |
| 6075 | break; |
| 6076 | |
| 6077 | /* p1 is at the beginning of the cookie name */ |
| 6078 | p2 = p1; |
| 6079 | |
| 6080 | while (p2 < cur_end && *p2 != '=' && *p2 != ';') |
| 6081 | p2++; |
| 6082 | |
| 6083 | if (p2 == cur_end || *p2 == ';') /* next cookie */ |
| 6084 | break; |
| 6085 | |
| 6086 | p3 = p2 + 1; /* skip the '=' sign */ |
| 6087 | if (p3 == cur_end) |
| 6088 | break; |
| 6089 | |
| 6090 | p4 = p3; |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 6091 | while (p4 < cur_end && !isspace((unsigned char)*p4) && *p4 != ';') |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6092 | p4++; |
| 6093 | |
| 6094 | /* here, we have the cookie name between p1 and p2, |
| 6095 | * and its value between p3 and p4. |
| 6096 | * we can process it. |
| 6097 | */ |
| 6098 | |
| 6099 | /* first, let's see if we want to capture it */ |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6100 | if (t->fe->capture_name != NULL && |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 6101 | txn->srv_cookie == NULL && |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6102 | (p4 - p1 >= t->fe->capture_namelen) && |
| 6103 | memcmp(p1, t->fe->capture_name, t->fe->capture_namelen) == 0) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6104 | int log_len = p4 - p1; |
| 6105 | |
Willy Tarreau | 086b3b4 | 2007-05-13 21:45:51 +0200 | [diff] [blame] | 6106 | if ((txn->srv_cookie = pool_alloc2(pool2_capture)) == NULL) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6107 | Alert("HTTP logging : out of memory.\n"); |
| 6108 | } |
| 6109 | |
Willy Tarreau | fd39dda | 2008-10-17 12:01:58 +0200 | [diff] [blame] | 6110 | if (log_len > t->fe->capture_len) |
| 6111 | log_len = t->fe->capture_len; |
Willy Tarreau | 3bac9ff | 2007-03-18 17:31:28 +0100 | [diff] [blame] | 6112 | memcpy(txn->srv_cookie, p1, log_len); |
| 6113 | txn->srv_cookie[log_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6114 | } |
| 6115 | |
| 6116 | /* now check if we need to process it for persistence */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6117 | if ((p2 - p1 == t->be->cookie_len) && (t->be->cookie_name != NULL) && |
| 6118 | (memcmp(p1, t->be->cookie_name, p2 - p1) == 0)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6119 | /* Cool... it's the right one */ |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6120 | txn->flags |= TX_SCK_SEEN; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6121 | |
| 6122 | /* If the cookie is in insert mode on a known server, we'll delete |
| 6123 | * this occurrence because we'll insert another one later. |
| 6124 | * We'll delete it too if the "indirect" option is set and we're in |
| 6125 | * a direct access. */ |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6126 | if (((t->srv) && (t->be->options & PR_O_COOK_INS)) || |
| 6127 | ((t->flags & SN_DIRECT) && (t->be->options & PR_O_COOK_IND))) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6128 | /* this header must be deleted */ |
| 6129 | delta = buffer_replace2(rtr, cur_ptr, cur_next, NULL, 0); |
| 6130 | txn->hdr_idx.v[old_idx].next = cur_hdr->next; |
| 6131 | txn->hdr_idx.used--; |
| 6132 | cur_hdr->len = 0; |
| 6133 | cur_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6134 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6135 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6136 | txn->flags |= TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6137 | } |
| 6138 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6139 | (t->be->options & PR_O_COOK_RW)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6140 | /* replace bytes p3->p4 with the cookie name associated |
| 6141 | * with this server since we know it. |
| 6142 | */ |
| 6143 | delta = buffer_replace2(rtr, p3, p4, t->srv->cookie, t->srv->cklen); |
| 6144 | cur_hdr->len += delta; |
| 6145 | cur_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6146 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6147 | |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6148 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6149 | } |
| 6150 | else if ((t->srv) && (t->srv->cookie) && |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6151 | (t->be->options & PR_O_COOK_PFX)) { |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6152 | /* insert the cookie name associated with this server |
| 6153 | * before existing cookie, and insert a delimitor between them.. |
| 6154 | */ |
| 6155 | delta = buffer_replace2(rtr, p3, p3, t->srv->cookie, t->srv->cklen + 1); |
| 6156 | cur_hdr->len += delta; |
| 6157 | cur_next += delta; |
Willy Tarreau | fa355d4 | 2009-11-29 18:12:29 +0100 | [diff] [blame] | 6158 | http_msg_move_end(&txn->rsp, delta); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6159 | |
| 6160 | p3[t->srv->cklen] = COOKIE_DELIM; |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6161 | txn->flags |= TX_SCK_INSERTED | TX_SCK_DELETED; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6162 | } |
| 6163 | } |
| 6164 | /* next, let's see if the cookie is our appcookie */ |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6165 | else if (t->be->appsession_name != NULL) { |
| 6166 | int cmp_len, value_len; |
| 6167 | char *value_begin; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6168 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6169 | if (t->be->options2 & PR_O2_AS_PFX) { |
| 6170 | cmp_len = MIN(p4 - p1, t->be->appsession_name_len); |
| 6171 | value_begin = p1 + t->be->appsession_name_len; |
| 6172 | value_len = MIN(t->be->appsession_len, p4 - p1 - t->be->appsession_name_len); |
| 6173 | } else { |
| 6174 | cmp_len = p2 - p1; |
| 6175 | value_begin = p3; |
| 6176 | value_len = MIN(t->be->appsession_len, p4 - p3); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6177 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6178 | |
| 6179 | if (memcmp(p1, t->be->appsession_name, cmp_len) == 0) { |
| 6180 | /* Cool... it's the right one */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6181 | if (txn->sessid != NULL) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6182 | /* free previously allocated memory as we don't need it anymore */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6183 | pool_free2(apools.sessid, txn->sessid); |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6184 | } |
| 6185 | /* Store the sessid in the session for future use */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6186 | if ((txn->sessid = pool_alloc2(apools.sessid)) == NULL) { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6187 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 6188 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 6189 | return; |
| 6190 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6191 | memcpy(txn->sessid, value_begin, value_len); |
| 6192 | txn->sessid[value_len] = 0; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6193 | } |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6194 | } /* end if ((t->be->appsession_name != NULL) ... */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6195 | break; /* we don't want to loop again since there cannot be another cookie on the same line */ |
| 6196 | } /* we're now at the end of the cookie value */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6197 | /* keep the link from this header to next one */ |
| 6198 | old_idx = cur_idx; |
| 6199 | } /* end of cookie processing on this header */ |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6200 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6201 | if (txn->sessid != NULL) { |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6202 | appsess *asession = NULL; |
| 6203 | /* only do insert, if lookup fails */ |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6204 | asession = appsession_hash_lookup(&(t->be->htbl_proxy), txn->sessid); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6205 | if (asession == NULL) { |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 6206 | size_t server_id_len; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6207 | if ((asession = pool_alloc2(pool2_appsess)) == NULL) { |
| 6208 | Alert("Not enough Memory process_srv():asession:calloc().\n"); |
| 6209 | send_log(t->be, LOG_ALERT, "Not enough Memory process_srv():asession:calloc().\n"); |
| 6210 | return; |
| 6211 | } |
| 6212 | if ((asession->sessid = pool_alloc2(apools.sessid)) == NULL) { |
| 6213 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 6214 | 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] | 6215 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6216 | return; |
| 6217 | } |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6218 | memcpy(asession->sessid, txn->sessid, t->be->appsession_len); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6219 | asession->sessid[t->be->appsession_len] = 0; |
| 6220 | |
Willy Tarreau | 1fac753 | 2010-01-09 19:23:06 +0100 | [diff] [blame] | 6221 | server_id_len = strlen(t->srv->id) + 1; |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6222 | if ((asession->serverid = pool_alloc2(apools.serverid)) == NULL) { |
| 6223 | Alert("Not enough Memory process_srv():asession->sessid:malloc().\n"); |
| 6224 | 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] | 6225 | t->be->htbl_proxy.destroy(asession); |
Cyril Bonté | bf47aeb | 2009-10-15 00:15:40 +0200 | [diff] [blame] | 6226 | return; |
| 6227 | } |
| 6228 | asession->serverid[0] = '\0'; |
| 6229 | memcpy(asession->serverid, t->srv->id, server_id_len); |
| 6230 | |
| 6231 | asession->request_count = 0; |
| 6232 | appsession_hash_insert(&(t->be->htbl_proxy), asession); |
| 6233 | } |
| 6234 | |
| 6235 | asession->expire = tick_add_ifset(now_ms, t->be->timeout.appsession); |
| 6236 | asession->request_count++; |
| 6237 | } |
| 6238 | |
| 6239 | #if defined(DEBUG_HASH) |
| 6240 | Alert("manage_server_side_cookies\n"); |
| 6241 | appsession_hash_dump(&(t->be->htbl_proxy)); |
| 6242 | #endif |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6243 | } |
| 6244 | |
| 6245 | |
| 6246 | |
| 6247 | /* |
| 6248 | * Check if response is cacheable or not. Updates t->flags. |
| 6249 | */ |
| 6250 | void check_response_for_cacheability(struct session *t, struct buffer *rtr) |
| 6251 | { |
| 6252 | struct http_txn *txn = &t->txn; |
| 6253 | char *p1, *p2; |
| 6254 | |
| 6255 | char *cur_ptr, *cur_end, *cur_next; |
| 6256 | int cur_idx; |
| 6257 | |
Willy Tarreau | 5df5187 | 2007-11-25 16:20:08 +0100 | [diff] [blame] | 6258 | if (!(txn->flags & TX_CACHEABLE)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6259 | return; |
| 6260 | |
| 6261 | /* Iterate through the headers. |
| 6262 | * we start with the start line. |
| 6263 | */ |
| 6264 | cur_idx = 0; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6265 | cur_next = txn->rsp.sol + hdr_idx_first_pos(&txn->hdr_idx); |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6266 | |
| 6267 | while ((cur_idx = txn->hdr_idx.v[cur_idx].next)) { |
| 6268 | struct hdr_idx_elem *cur_hdr; |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6269 | int val; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6270 | |
| 6271 | cur_hdr = &txn->hdr_idx.v[cur_idx]; |
| 6272 | cur_ptr = cur_next; |
| 6273 | cur_end = cur_ptr + cur_hdr->len; |
| 6274 | cur_next = cur_end + cur_hdr->cr + 1; |
| 6275 | |
| 6276 | /* We have one full header between cur_ptr and cur_end, and the |
| 6277 | * next header starts at cur_next. We're only interested in |
| 6278 | * "Cookie:" headers. |
| 6279 | */ |
| 6280 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6281 | val = http_header_match2(cur_ptr, cur_end, "Pragma", 6); |
| 6282 | if (val) { |
| 6283 | if ((cur_end - (cur_ptr + val) >= 8) && |
| 6284 | strncasecmp(cur_ptr + val, "no-cache", 8) == 0) { |
| 6285 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
| 6286 | return; |
| 6287 | } |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6288 | } |
| 6289 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6290 | val = http_header_match2(cur_ptr, cur_end, "Cache-control", 13); |
| 6291 | if (!val) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6292 | continue; |
| 6293 | |
| 6294 | /* OK, right now we know we have a cache-control header at cur_ptr */ |
| 6295 | |
Willy Tarreau | aa9dce3 | 2007-03-18 23:50:16 +0100 | [diff] [blame] | 6296 | p1 = cur_ptr + val; /* first non-space char after 'cache-control:' */ |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6297 | |
| 6298 | if (p1 >= cur_end) /* no more info */ |
| 6299 | continue; |
| 6300 | |
| 6301 | /* p1 is at the beginning of the value */ |
| 6302 | p2 = p1; |
| 6303 | |
Willy Tarreau | 8f8e645 | 2007-06-17 21:51:38 +0200 | [diff] [blame] | 6304 | while (p2 < cur_end && *p2 != '=' && *p2 != ',' && !isspace((unsigned char)*p2)) |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6305 | p2++; |
| 6306 | |
| 6307 | /* we have a complete value between p1 and p2 */ |
| 6308 | if (p2 < cur_end && *p2 == '=') { |
| 6309 | /* we have something of the form no-cache="set-cookie" */ |
| 6310 | if ((cur_end - p1 >= 21) && |
| 6311 | strncasecmp(p1, "no-cache=\"set-cookie", 20) == 0 |
| 6312 | && (p1[20] == '"' || p1[20] == ',')) |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6313 | txn->flags &= ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6314 | continue; |
| 6315 | } |
| 6316 | |
| 6317 | /* OK, so we know that either p2 points to the end of string or to a comma */ |
| 6318 | if (((p2 - p1 == 7) && strncasecmp(p1, "private", 7) == 0) || |
| 6319 | ((p2 - p1 == 8) && strncasecmp(p1, "no-store", 8) == 0) || |
| 6320 | ((p2 - p1 == 9) && strncasecmp(p1, "max-age=0", 9) == 0) || |
| 6321 | ((p2 - p1 == 10) && strncasecmp(p1, "s-maxage=0", 10) == 0)) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6322 | txn->flags &= ~TX_CACHEABLE & ~TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6323 | return; |
| 6324 | } |
| 6325 | |
| 6326 | if ((p2 - p1 == 6) && strncasecmp(p1, "public", 6) == 0) { |
Willy Tarreau | 3d30059 | 2007-03-18 18:34:41 +0100 | [diff] [blame] | 6327 | txn->flags |= TX_CACHEABLE | TX_CACHE_COOK; |
Willy Tarreau | a15645d | 2007-03-18 16:22:39 +0100 | [diff] [blame] | 6328 | continue; |
| 6329 | } |
| 6330 | } |
| 6331 | } |
| 6332 | |
| 6333 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6334 | /* |
| 6335 | * Try to retrieve a known appsession in the URI, then the associated server. |
| 6336 | * If the server is found, it's assigned to the session. |
| 6337 | */ |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6338 | 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] | 6339 | { |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6340 | char *end_params, *first_param, *cur_param, *next_param; |
| 6341 | char separator; |
| 6342 | int value_len; |
| 6343 | |
| 6344 | int mode = t->be->options2 & PR_O2_AS_M_ANY; |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6345 | |
Willy Tarreau | e2e27a5 | 2007-04-01 00:01:37 +0200 | [diff] [blame] | 6346 | if (t->be->appsession_name == NULL || |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6347 | (t->txn.meth != HTTP_METH_GET && t->txn.meth != HTTP_METH_POST)) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6348 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6349 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6350 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6351 | first_param = NULL; |
| 6352 | switch (mode) { |
| 6353 | case PR_O2_AS_M_PP: |
| 6354 | first_param = memchr(begin, ';', len); |
| 6355 | break; |
| 6356 | case PR_O2_AS_M_QS: |
| 6357 | first_param = memchr(begin, '?', len); |
| 6358 | break; |
| 6359 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6360 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6361 | if (first_param == NULL) { |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6362 | return; |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6363 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6364 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6365 | switch (mode) { |
| 6366 | case PR_O2_AS_M_PP: |
| 6367 | if ((end_params = memchr(first_param, '?', len - (begin - first_param))) == NULL) { |
| 6368 | end_params = (char *) begin + len; |
| 6369 | } |
| 6370 | separator = ';'; |
| 6371 | break; |
| 6372 | case PR_O2_AS_M_QS: |
| 6373 | end_params = (char *) begin + len; |
| 6374 | separator = '&'; |
| 6375 | break; |
| 6376 | default: |
| 6377 | /* unknown mode, shouldn't happen */ |
| 6378 | return; |
| 6379 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6380 | |
Cyril Bonté | b21570a | 2009-11-29 20:04:48 +0100 | [diff] [blame] | 6381 | cur_param = next_param = end_params; |
| 6382 | while (cur_param > first_param) { |
| 6383 | cur_param--; |
| 6384 | if ((cur_param[0] == separator) || (cur_param == first_param)) { |
| 6385 | /* let's see if this is the appsession parameter */ |
| 6386 | if ((cur_param + t->be->appsession_name_len + 1 < next_param) && |
| 6387 | ((t->be->options2 & PR_O2_AS_PFX) || cur_param[t->be->appsession_name_len + 1] == '=') && |
| 6388 | (strncasecmp(cur_param + 1, t->be->appsession_name, t->be->appsession_name_len) == 0)) { |
| 6389 | /* Cool... it's the right one */ |
| 6390 | cur_param += t->be->appsession_name_len + (t->be->options2 & PR_O2_AS_PFX ? 1 : 2); |
| 6391 | value_len = MIN(t->be->appsession_len, next_param - cur_param); |
| 6392 | if (value_len > 0) { |
| 6393 | manage_client_side_appsession(t, cur_param, value_len); |
| 6394 | } |
| 6395 | break; |
| 6396 | } |
| 6397 | next_param = cur_param; |
| 6398 | } |
| 6399 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6400 | #if defined(DEBUG_HASH) |
Aleksandar Lazic | 697bbb0 | 2008-08-13 19:57:02 +0200 | [diff] [blame] | 6401 | Alert("get_srv_from_appsession\n"); |
Willy Tarreau | 51041c7 | 2007-09-09 21:56:53 +0200 | [diff] [blame] | 6402 | appsession_hash_dump(&(t->be->htbl_proxy)); |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6403 | #endif |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6404 | } |
| 6405 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6406 | /* |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 6407 | * In a GET or HEAD request, check if the requested URI matches the stats uri |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 6408 | * for the current backend. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6409 | * |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 6410 | * It is assumed that the request is either a HEAD or GET and that the |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 6411 | * t->be->uri_auth field is valid. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6412 | * |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 6413 | * Returns 1 if stats should be provided, otherwise 0. |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6414 | */ |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 6415 | int stats_check_uri(struct session *t, struct proxy *backend) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6416 | { |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6417 | struct http_txn *txn = &t->txn; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6418 | struct uri_auth *uri_auth = backend->uri_auth; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6419 | char *h; |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6420 | |
Krzysztof Piotr Oledzki | 8c8bd45 | 2010-01-29 19:29:32 +0100 | [diff] [blame] | 6421 | if (!uri_auth) |
| 6422 | return 0; |
| 6423 | |
| 6424 | if (txn->meth != HTTP_METH_GET && txn->meth != HTTP_METH_HEAD) |
| 6425 | return 0; |
| 6426 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6427 | memset(&t->data_ctx.stats, 0, sizeof(t->data_ctx.stats)); |
| 6428 | |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6429 | /* check URI size */ |
Willy Tarreau | 4dbc4a2 | 2007-03-03 16:23:22 +0100 | [diff] [blame] | 6430 | if (uri_auth->uri_len > txn->req.sl.rq.u_l) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6431 | return 0; |
| 6432 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6433 | h = txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 8d5d7f2 | 2007-01-21 19:16:41 +0100 | [diff] [blame] | 6434 | |
Willy Tarreau | 0214c3a | 2007-01-07 13:47:30 +0100 | [diff] [blame] | 6435 | /* the URI is in h */ |
| 6436 | if (memcmp(h, uri_auth->uri_prefix, uri_auth->uri_len) != 0) |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6437 | return 0; |
| 6438 | |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 6439 | h += uri_auth->uri_len; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6440 | 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] | 6441 | if (memcmp(h, ";up", 3) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6442 | t->data_ctx.stats.flags |= STAT_HIDE_DOWN; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 6443 | break; |
| 6444 | } |
| 6445 | h++; |
| 6446 | } |
| 6447 | |
| 6448 | if (uri_auth->refresh) { |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6449 | h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len; |
| 6450 | 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] | 6451 | if (memcmp(h, ";norefresh", 10) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6452 | t->data_ctx.stats.flags |= STAT_NO_REFRESH; |
Willy Tarreau | e7150cd | 2007-07-25 14:43:32 +0200 | [diff] [blame] | 6453 | break; |
| 6454 | } |
| 6455 | h++; |
| 6456 | } |
| 6457 | } |
| 6458 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6459 | h = txn->req.sol + txn->req.sl.rq.u + uri_auth->uri_len; |
| 6460 | 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] | 6461 | if (memcmp(h, ";csv", 4) == 0) { |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6462 | t->data_ctx.stats.flags |= STAT_FMT_CSV; |
Willy Tarreau | 55bb845 | 2007-10-17 18:44:57 +0200 | [diff] [blame] | 6463 | break; |
| 6464 | } |
| 6465 | h++; |
| 6466 | } |
| 6467 | |
Willy Tarreau | 39f7e6d | 2008-03-17 21:38:24 +0100 | [diff] [blame] | 6468 | t->data_ctx.stats.flags |= STAT_SHOW_STAT | STAT_SHOW_INFO; |
| 6469 | |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6470 | return 1; |
| 6471 | } |
| 6472 | |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6473 | /* |
| 6474 | * 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] | 6475 | * WARNING: it's unlikely that we've reached HTTP_MSG_BODY here so we must not |
| 6476 | * assume that msg->sol = buf->data + msg->som. |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6477 | */ |
| 6478 | void http_capture_bad_message(struct error_snapshot *es, struct session *s, |
| 6479 | struct buffer *buf, struct http_msg *msg, |
| 6480 | struct proxy *other_end) |
| 6481 | { |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 6482 | es->len = buf->r - (buf->data + msg->som); |
| 6483 | 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] | 6484 | if (msg->err_pos >= 0) |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 6485 | es->pos = msg->err_pos - msg->som; |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6486 | else |
Willy Tarreau | 2df8d71 | 2009-05-01 11:33:17 +0200 | [diff] [blame] | 6487 | es->pos = buf->lr - (buf->data + msg->som); |
Willy Tarreau | 4076a15 | 2009-04-02 15:18:36 +0200 | [diff] [blame] | 6488 | es->when = date; // user-visible date |
| 6489 | es->sid = s->uniq_id; |
| 6490 | es->srv = s->srv; |
| 6491 | es->oe = other_end; |
| 6492 | es->src = s->cli_addr; |
| 6493 | } |
Willy Tarreau | b251390 | 2006-12-17 14:52:38 +0100 | [diff] [blame] | 6494 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6495 | /* |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6496 | * Print a debug line with a header |
| 6497 | */ |
| 6498 | void debug_hdr(const char *dir, struct session *t, const char *start, const char *end) |
| 6499 | { |
| 6500 | int len, max; |
| 6501 | 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] | 6502 | 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] | 6503 | max = end - start; |
| 6504 | UBOUND(max, sizeof(trash) - len - 1); |
| 6505 | len += strlcpy2(trash + len, start, max + 1); |
| 6506 | trash[len++] = '\n'; |
| 6507 | write(1, trash, len); |
| 6508 | } |
| 6509 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6510 | /* |
| 6511 | * Initialize a new HTTP transaction for session <s>. It is assumed that all |
| 6512 | * the required fields are properly allocated and that we only need to (re)init |
| 6513 | * them. This should be used before processing any new request. |
| 6514 | */ |
| 6515 | void http_init_txn(struct session *s) |
| 6516 | { |
| 6517 | struct http_txn *txn = &s->txn; |
| 6518 | struct proxy *fe = s->fe; |
| 6519 | |
| 6520 | txn->flags = 0; |
| 6521 | txn->status = -1; |
| 6522 | |
| 6523 | txn->req.sol = txn->req.eol = NULL; |
| 6524 | txn->req.som = txn->req.eoh = 0; /* relative to the buffer */ |
| 6525 | txn->rsp.sol = txn->rsp.eol = NULL; |
| 6526 | txn->rsp.som = txn->rsp.eoh = 0; /* relative to the buffer */ |
| 6527 | txn->req.hdr_content_len = 0LL; |
| 6528 | txn->rsp.hdr_content_len = 0LL; |
| 6529 | txn->req.msg_state = HTTP_MSG_RQBEFORE; /* at the very beginning of the request */ |
| 6530 | txn->rsp.msg_state = HTTP_MSG_RPBEFORE; /* at the very beginning of the response */ |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 6531 | |
| 6532 | txn->auth.method = HTTP_AUTH_UNKNOWN; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6533 | |
| 6534 | txn->req.err_pos = txn->rsp.err_pos = -2; /* block buggy requests/responses */ |
| 6535 | if (fe->options2 & PR_O2_REQBUG_OK) |
| 6536 | txn->req.err_pos = -1; /* let buggy requests pass */ |
| 6537 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 6538 | if (txn->req.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6539 | memset(txn->req.cap, 0, fe->nb_req_cap * sizeof(void *)); |
| 6540 | |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 6541 | if (txn->rsp.cap) |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6542 | memset(txn->rsp.cap, 0, fe->nb_rsp_cap * sizeof(void *)); |
| 6543 | |
| 6544 | if (txn->hdr_idx.v) |
| 6545 | hdr_idx_init(&txn->hdr_idx); |
| 6546 | } |
| 6547 | |
| 6548 | /* to be used at the end of a transaction */ |
| 6549 | void http_end_txn(struct session *s) |
| 6550 | { |
| 6551 | struct http_txn *txn = &s->txn; |
| 6552 | |
| 6553 | /* these ones will have been dynamically allocated */ |
| 6554 | pool_free2(pool2_requri, txn->uri); |
| 6555 | pool_free2(pool2_capture, txn->cli_cookie); |
| 6556 | pool_free2(pool2_capture, txn->srv_cookie); |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6557 | pool_free2(apools.sessid, txn->sessid); |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 6558 | |
Willy Tarreau | a3377ee | 2010-01-10 10:49:11 +0100 | [diff] [blame] | 6559 | txn->sessid = NULL; |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6560 | txn->uri = NULL; |
| 6561 | txn->srv_cookie = NULL; |
| 6562 | txn->cli_cookie = NULL; |
Willy Tarreau | 4602363 | 2010-01-07 22:51:47 +0100 | [diff] [blame] | 6563 | |
| 6564 | if (txn->req.cap) { |
| 6565 | struct cap_hdr *h; |
| 6566 | for (h = s->fe->req_cap; h; h = h->next) |
| 6567 | pool_free2(h->pool, txn->req.cap[h->index]); |
| 6568 | memset(txn->req.cap, 0, s->fe->nb_req_cap * sizeof(void *)); |
| 6569 | } |
| 6570 | |
| 6571 | if (txn->rsp.cap) { |
| 6572 | struct cap_hdr *h; |
| 6573 | for (h = s->fe->rsp_cap; h; h = h->next) |
| 6574 | pool_free2(h->pool, txn->rsp.cap[h->index]); |
| 6575 | memset(txn->rsp.cap, 0, s->fe->nb_rsp_cap * sizeof(void *)); |
| 6576 | } |
| 6577 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6578 | } |
| 6579 | |
| 6580 | /* to be used at the end of a transaction to prepare a new one */ |
| 6581 | void http_reset_txn(struct session *s) |
| 6582 | { |
| 6583 | http_end_txn(s); |
| 6584 | http_init_txn(s); |
| 6585 | |
| 6586 | s->be = s->fe; |
| 6587 | s->req->analysers = s->listener->analysers; |
| 6588 | s->logs.logwait = s->fe->to_log; |
| 6589 | s->srv = s->prev_srv = s->srv_conn = NULL; |
Emeric Brun | b982a3d | 2010-01-04 15:45:53 +0100 | [diff] [blame] | 6590 | /* re-init store persistence */ |
| 6591 | s->store_count = 0; |
| 6592 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6593 | s->pend_pos = NULL; |
| 6594 | s->conn_retries = s->be->conn_retries; |
| 6595 | |
| 6596 | s->req->flags |= BF_READ_DONTWAIT; /* one read is usually enough */ |
| 6597 | |
Willy Tarreau | 739cfba | 2010-01-25 23:11:14 +0100 | [diff] [blame] | 6598 | /* We must trim any excess data from the response buffer, because we |
| 6599 | * may have blocked an invalid response from a server that we don't |
| 6600 | * want to accidentely forward once we disable the analysers, nor do |
| 6601 | * we want those data to come along with next response. A typical |
| 6602 | * example of such data would be from a buggy server responding to |
| 6603 | * a HEAD with some data, or sending more than the advertised |
| 6604 | * content-length. |
| 6605 | */ |
| 6606 | if (unlikely(s->rep->l > s->rep->send_max)) { |
| 6607 | s->rep->l = s->rep->send_max; |
| 6608 | s->rep->r = s->rep->w + s->rep->l; |
| 6609 | if (s->rep->r >= s->rep->data + s->rep->size) |
| 6610 | s->rep->r -= s->rep->size; |
| 6611 | } |
| 6612 | |
Willy Tarreau | 0937bc4 | 2009-12-22 15:03:09 +0100 | [diff] [blame] | 6613 | s->req->rto = s->fe->timeout.client; |
| 6614 | s->req->wto = s->be->timeout.server; |
| 6615 | s->req->cto = s->be->timeout.connect; |
| 6616 | |
| 6617 | s->rep->rto = s->be->timeout.server; |
| 6618 | s->rep->wto = s->fe->timeout.client; |
| 6619 | s->rep->cto = TICK_ETERNITY; |
| 6620 | |
| 6621 | s->req->rex = TICK_ETERNITY; |
| 6622 | s->req->wex = TICK_ETERNITY; |
| 6623 | s->req->analyse_exp = TICK_ETERNITY; |
| 6624 | s->rep->rex = TICK_ETERNITY; |
| 6625 | s->rep->wex = TICK_ETERNITY; |
| 6626 | s->rep->analyse_exp = TICK_ETERNITY; |
| 6627 | } |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 6628 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6629 | /************************************************************************/ |
| 6630 | /* The code below is dedicated to ACL parsing and matching */ |
| 6631 | /************************************************************************/ |
| 6632 | |
| 6633 | |
| 6634 | |
| 6635 | |
| 6636 | /* 1. Check on METHOD |
| 6637 | * We use the pre-parsed method if it is known, and store its number as an |
| 6638 | * integer. If it is unknown, we use the pointer and the length. |
| 6639 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6640 | 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] | 6641 | { |
| 6642 | int len, meth; |
| 6643 | |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6644 | len = strlen(*text); |
| 6645 | meth = find_http_meth(*text, len); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6646 | |
| 6647 | pattern->val.i = meth; |
| 6648 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6649 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6650 | if (!pattern->ptr.str) |
| 6651 | return 0; |
| 6652 | pattern->len = len; |
| 6653 | } |
| 6654 | return 1; |
| 6655 | } |
| 6656 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6657 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6658 | acl_fetch_meth(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6659 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6660 | { |
| 6661 | int meth; |
| 6662 | struct http_txn *txn = l7; |
| 6663 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6664 | if (!txn) |
| 6665 | return 0; |
| 6666 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6667 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6668 | return 0; |
| 6669 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6670 | meth = txn->meth; |
| 6671 | test->i = meth; |
| 6672 | if (meth == HTTP_METH_OTHER) { |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6673 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6674 | /* ensure the indexes are not affected */ |
| 6675 | return 0; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6676 | test->len = txn->req.sl.rq.m_l; |
| 6677 | test->ptr = txn->req.sol; |
| 6678 | } |
| 6679 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 6680 | return 1; |
| 6681 | } |
| 6682 | |
| 6683 | static int acl_match_meth(struct acl_test *test, struct acl_pattern *pattern) |
| 6684 | { |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 6685 | int icase; |
| 6686 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6687 | if (test->i != pattern->val.i) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6688 | return ACL_PAT_FAIL; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6689 | |
| 6690 | if (test->i != HTTP_METH_OTHER) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6691 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6692 | |
| 6693 | /* Other method, we must compare the strings */ |
| 6694 | if (pattern->len != test->len) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6695 | return ACL_PAT_FAIL; |
Willy Tarreau | c8d7c96 | 2007-06-17 08:20:33 +0200 | [diff] [blame] | 6696 | |
| 6697 | icase = pattern->flags & ACL_PAT_F_IGNORE_CASE; |
| 6698 | if ((icase && strncasecmp(pattern->ptr.str, test->ptr, test->len) != 0) || |
| 6699 | (!icase && strncmp(pattern->ptr.str, test->ptr, test->len) != 0)) |
Willy Tarreau | 1138281 | 2008-07-09 16:18:21 +0200 | [diff] [blame] | 6700 | return ACL_PAT_FAIL; |
| 6701 | return ACL_PAT_PASS; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6702 | } |
| 6703 | |
| 6704 | /* 2. Check on Request/Status Version |
| 6705 | * We simply compare strings here. |
| 6706 | */ |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6707 | 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] | 6708 | { |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6709 | pattern->ptr.str = strdup(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6710 | if (!pattern->ptr.str) |
| 6711 | return 0; |
Willy Tarreau | ae8b796 | 2007-06-09 23:10:04 +0200 | [diff] [blame] | 6712 | pattern->len = strlen(*text); |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6713 | return 1; |
| 6714 | } |
| 6715 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6716 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6717 | acl_fetch_rqver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6718 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6719 | { |
| 6720 | struct http_txn *txn = l7; |
| 6721 | char *ptr; |
| 6722 | int len; |
| 6723 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6724 | if (!txn) |
| 6725 | return 0; |
| 6726 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6727 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6728 | return 0; |
| 6729 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6730 | len = txn->req.sl.rq.v_l; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6731 | ptr = txn->req.sol + txn->req.sl.rq.v; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6732 | |
| 6733 | while ((len-- > 0) && (*ptr++ != '/')); |
| 6734 | if (len <= 0) |
| 6735 | return 0; |
| 6736 | |
| 6737 | test->ptr = ptr; |
| 6738 | test->len = len; |
| 6739 | |
| 6740 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 6741 | return 1; |
| 6742 | } |
| 6743 | |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6744 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6745 | acl_fetch_stver(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6746 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6747 | { |
| 6748 | struct http_txn *txn = l7; |
| 6749 | char *ptr; |
| 6750 | int len; |
| 6751 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6752 | if (!txn) |
| 6753 | return 0; |
| 6754 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6755 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6756 | return 0; |
| 6757 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6758 | len = txn->rsp.sl.st.v_l; |
| 6759 | ptr = txn->rsp.sol; |
| 6760 | |
| 6761 | while ((len-- > 0) && (*ptr++ != '/')); |
| 6762 | if (len <= 0) |
| 6763 | return 0; |
| 6764 | |
| 6765 | test->ptr = ptr; |
| 6766 | test->len = len; |
| 6767 | |
| 6768 | test->flags = ACL_TEST_F_READ_ONLY | ACL_TEST_F_VOL_1ST; |
| 6769 | return 1; |
| 6770 | } |
| 6771 | |
| 6772 | /* 3. Check on Status Code. We manipulate integers here. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6773 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6774 | acl_fetch_stcode(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6775 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6776 | { |
| 6777 | struct http_txn *txn = l7; |
| 6778 | char *ptr; |
| 6779 | int len; |
| 6780 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6781 | if (!txn) |
| 6782 | return 0; |
| 6783 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6784 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6785 | return 0; |
| 6786 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6787 | len = txn->rsp.sl.st.c_l; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6788 | ptr = txn->rsp.sol + txn->rsp.sl.st.c; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6789 | |
| 6790 | test->i = __strl2ui(ptr, len); |
| 6791 | test->flags = ACL_TEST_F_VOL_1ST; |
| 6792 | return 1; |
| 6793 | } |
| 6794 | |
| 6795 | /* 4. Check on URL/URI. A pointer to the URI is stored. */ |
Willy Tarreau | d41f8d8 | 2007-06-10 10:06:18 +0200 | [diff] [blame] | 6796 | static int |
Willy Tarreau | 97be145 | 2007-06-10 11:47:14 +0200 | [diff] [blame] | 6797 | acl_fetch_url(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6798 | struct acl_expr *expr, struct acl_test *test) |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6799 | { |
| 6800 | struct http_txn *txn = l7; |
| 6801 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6802 | if (!txn) |
| 6803 | return 0; |
| 6804 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6805 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6806 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6807 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6808 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6809 | /* ensure the indexes are not affected */ |
| 6810 | return 0; |
| 6811 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6812 | test->len = txn->req.sl.rq.u_l; |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6813 | test->ptr = txn->req.sol + txn->req.sl.rq.u; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6814 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 6815 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 6816 | test->flags = ACL_TEST_F_VOL_1ST; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6817 | return 1; |
| 6818 | } |
| 6819 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6820 | static int |
| 6821 | acl_fetch_url_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6822 | struct acl_expr *expr, struct acl_test *test) |
| 6823 | { |
| 6824 | struct http_txn *txn = l7; |
| 6825 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6826 | if (!txn) |
| 6827 | return 0; |
| 6828 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6829 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6830 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6831 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6832 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6833 | /* ensure the indexes are not affected */ |
| 6834 | return 0; |
| 6835 | |
| 6836 | /* Parse HTTP request */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6837 | 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] | 6838 | test->ptr = (void *)&((struct sockaddr_in *)&l4->srv_addr)->sin_addr; |
| 6839 | test->i = AF_INET; |
| 6840 | |
| 6841 | /* |
| 6842 | * If we are parsing url in frontend space, we prepare backend stage |
| 6843 | * to not parse again the same url ! optimization lazyness... |
| 6844 | */ |
| 6845 | if (px->options & PR_O_HTTP_PROXY) |
| 6846 | l4->flags |= SN_ADDR_SET; |
| 6847 | |
| 6848 | test->flags = ACL_TEST_F_READ_ONLY; |
| 6849 | return 1; |
| 6850 | } |
| 6851 | |
| 6852 | static int |
| 6853 | acl_fetch_url_port(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6854 | struct acl_expr *expr, struct acl_test *test) |
| 6855 | { |
| 6856 | struct http_txn *txn = l7; |
| 6857 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6858 | if (!txn) |
| 6859 | return 0; |
| 6860 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6861 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6862 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6863 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 6864 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6865 | /* ensure the indexes are not affected */ |
| 6866 | return 0; |
| 6867 | |
| 6868 | /* Same optimization as url_ip */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 6869 | 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] | 6870 | test->i = ntohs(((struct sockaddr_in *)&l4->srv_addr)->sin_port); |
| 6871 | |
| 6872 | if (px->options & PR_O_HTTP_PROXY) |
| 6873 | l4->flags |= SN_ADDR_SET; |
| 6874 | |
| 6875 | test->flags = ACL_TEST_F_READ_ONLY; |
| 6876 | return 1; |
| 6877 | } |
| 6878 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6879 | /* 5. Check on HTTP header. A pointer to the beginning of the value is returned. |
| 6880 | * This generic function is used by both acl_fetch_chdr() and acl_fetch_shdr(). |
| 6881 | */ |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6882 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6883 | 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] | 6884 | struct acl_expr *expr, struct acl_test *test) |
| 6885 | { |
| 6886 | struct http_txn *txn = l7; |
| 6887 | struct hdr_idx *idx = &txn->hdr_idx; |
| 6888 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6889 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6890 | if (!txn) |
| 6891 | return 0; |
| 6892 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6893 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 6894 | /* search for header from the beginning */ |
| 6895 | ctx->idx = 0; |
| 6896 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6897 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 6898 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 6899 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6900 | test->len = ctx->vlen; |
| 6901 | test->ptr = (char *)ctx->line + ctx->val; |
| 6902 | return 1; |
| 6903 | } |
| 6904 | |
| 6905 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 6906 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 6907 | return 0; |
| 6908 | } |
| 6909 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6910 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6911 | acl_fetch_chdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6912 | struct acl_expr *expr, struct acl_test *test) |
| 6913 | { |
| 6914 | struct http_txn *txn = l7; |
| 6915 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6916 | if (!txn) |
| 6917 | return 0; |
| 6918 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6919 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6920 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6921 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6922 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6923 | /* ensure the indexes are not affected */ |
| 6924 | return 0; |
| 6925 | |
| 6926 | return acl_fetch_hdr(px, l4, txn, txn->req.sol, expr, test); |
| 6927 | } |
| 6928 | |
| 6929 | static int |
| 6930 | acl_fetch_shdr(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6931 | struct acl_expr *expr, struct acl_test *test) |
| 6932 | { |
| 6933 | struct http_txn *txn = l7; |
| 6934 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6935 | if (!txn) |
| 6936 | return 0; |
| 6937 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6938 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6939 | return 0; |
| 6940 | |
| 6941 | return acl_fetch_hdr(px, l4, txn, txn->rsp.sol, expr, test); |
| 6942 | } |
| 6943 | |
| 6944 | /* 6. Check on HTTP header count. The number of occurrences is returned. |
| 6945 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 6946 | */ |
| 6947 | static int |
| 6948 | 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] | 6949 | struct acl_expr *expr, struct acl_test *test) |
| 6950 | { |
| 6951 | struct http_txn *txn = l7; |
| 6952 | struct hdr_idx *idx = &txn->hdr_idx; |
| 6953 | struct hdr_ctx ctx; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6954 | int cnt; |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 6955 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6956 | if (!txn) |
| 6957 | return 0; |
| 6958 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 6959 | ctx.idx = 0; |
| 6960 | cnt = 0; |
| 6961 | while (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, &ctx)) |
| 6962 | cnt++; |
| 6963 | |
| 6964 | test->i = cnt; |
| 6965 | test->flags = ACL_TEST_F_VOL_HDR; |
| 6966 | return 1; |
| 6967 | } |
| 6968 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6969 | static int |
| 6970 | acl_fetch_chdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6971 | struct acl_expr *expr, struct acl_test *test) |
| 6972 | { |
| 6973 | struct http_txn *txn = l7; |
| 6974 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6975 | if (!txn) |
| 6976 | return 0; |
| 6977 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6978 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6979 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6980 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6981 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 6982 | /* ensure the indexes are not affected */ |
| 6983 | return 0; |
| 6984 | |
| 6985 | return acl_fetch_hdr_cnt(px, l4, txn, txn->req.sol, expr, test); |
| 6986 | } |
| 6987 | |
| 6988 | static int |
| 6989 | acl_fetch_shdr_cnt(struct proxy *px, struct session *l4, void *l7, int dir, |
| 6990 | struct acl_expr *expr, struct acl_test *test) |
| 6991 | { |
| 6992 | struct http_txn *txn = l7; |
| 6993 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 6994 | if (!txn) |
| 6995 | return 0; |
| 6996 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 6997 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 6998 | return 0; |
| 6999 | |
| 7000 | return acl_fetch_hdr_cnt(px, l4, txn, txn->rsp.sol, expr, test); |
| 7001 | } |
| 7002 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7003 | /* 7. Check on HTTP header's integer value. The integer value is returned. |
| 7004 | * FIXME: the type is 'int', it may not be appropriate for everything. |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7005 | * 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] | 7006 | */ |
| 7007 | static int |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7008 | 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] | 7009 | struct acl_expr *expr, struct acl_test *test) |
| 7010 | { |
| 7011 | struct http_txn *txn = l7; |
| 7012 | struct hdr_idx *idx = &txn->hdr_idx; |
| 7013 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7014 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7015 | if (!txn) |
| 7016 | return 0; |
| 7017 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7018 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 7019 | /* search for header from the beginning */ |
| 7020 | ctx->idx = 0; |
| 7021 | |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7022 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 7023 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 7024 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 7025 | test->i = strl2ic((char *)ctx->line + ctx->val, ctx->vlen); |
| 7026 | return 1; |
| 7027 | } |
| 7028 | |
| 7029 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 7030 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 7031 | return 0; |
| 7032 | } |
| 7033 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7034 | static int |
| 7035 | acl_fetch_chdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7036 | struct acl_expr *expr, struct acl_test *test) |
| 7037 | { |
| 7038 | struct http_txn *txn = l7; |
| 7039 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7040 | if (!txn) |
| 7041 | return 0; |
| 7042 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7043 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7044 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7045 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7046 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7047 | /* ensure the indexes are not affected */ |
| 7048 | return 0; |
| 7049 | |
| 7050 | return acl_fetch_hdr_val(px, l4, txn, txn->req.sol, expr, test); |
| 7051 | } |
| 7052 | |
| 7053 | static int |
| 7054 | acl_fetch_shdr_val(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7055 | struct acl_expr *expr, struct acl_test *test) |
| 7056 | { |
| 7057 | struct http_txn *txn = l7; |
| 7058 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7059 | if (!txn) |
| 7060 | return 0; |
| 7061 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7062 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7063 | return 0; |
| 7064 | |
| 7065 | return acl_fetch_hdr_val(px, l4, txn, txn->rsp.sol, expr, test); |
| 7066 | } |
| 7067 | |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 7068 | /* 7. Check on HTTP header's IPv4 address value. The IPv4 address is returned. |
| 7069 | * This generic function is used by both acl_fetch_chdr* and acl_fetch_shdr*. |
| 7070 | */ |
| 7071 | static int |
| 7072 | acl_fetch_hdr_ip(struct proxy *px, struct session *l4, void *l7, char *sol, |
| 7073 | struct acl_expr *expr, struct acl_test *test) |
| 7074 | { |
| 7075 | struct http_txn *txn = l7; |
| 7076 | struct hdr_idx *idx = &txn->hdr_idx; |
| 7077 | struct hdr_ctx *ctx = (struct hdr_ctx *)test->ctx.a; |
| 7078 | |
| 7079 | if (!txn) |
| 7080 | return 0; |
| 7081 | |
| 7082 | if (!(test->flags & ACL_TEST_F_FETCH_MORE)) |
| 7083 | /* search for header from the beginning */ |
| 7084 | ctx->idx = 0; |
| 7085 | |
| 7086 | if (http_find_header2(expr->arg.str, expr->arg_len, sol, idx, ctx)) { |
| 7087 | test->flags |= ACL_TEST_F_FETCH_MORE; |
| 7088 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 7089 | /* Same optimization as url_ip */ |
| 7090 | memset(&l4->srv_addr.sin_addr, 0, sizeof(l4->srv_addr.sin_addr)); |
| 7091 | url2ip((char *)ctx->line + ctx->val, &l4->srv_addr.sin_addr); |
| 7092 | test->ptr = (void *)&l4->srv_addr.sin_addr; |
| 7093 | test->i = AF_INET; |
| 7094 | return 1; |
| 7095 | } |
| 7096 | |
| 7097 | test->flags &= ~ACL_TEST_F_FETCH_MORE; |
| 7098 | test->flags |= ACL_TEST_F_VOL_HDR; |
| 7099 | return 0; |
| 7100 | } |
| 7101 | |
| 7102 | static int |
| 7103 | acl_fetch_chdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7104 | struct acl_expr *expr, struct acl_test *test) |
| 7105 | { |
| 7106 | struct http_txn *txn = l7; |
| 7107 | |
| 7108 | if (!txn) |
| 7109 | return 0; |
| 7110 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7111 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 7112 | return 0; |
| 7113 | |
| 7114 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7115 | /* ensure the indexes are not affected */ |
| 7116 | return 0; |
| 7117 | |
| 7118 | return acl_fetch_hdr_ip(px, l4, txn, txn->req.sol, expr, test); |
| 7119 | } |
| 7120 | |
| 7121 | static int |
| 7122 | acl_fetch_shdr_ip(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7123 | struct acl_expr *expr, struct acl_test *test) |
| 7124 | { |
| 7125 | struct http_txn *txn = l7; |
| 7126 | |
| 7127 | if (!txn) |
| 7128 | return 0; |
| 7129 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7130 | if (txn->rsp.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | 106f979 | 2009-09-19 07:54:16 +0200 | [diff] [blame] | 7131 | return 0; |
| 7132 | |
| 7133 | return acl_fetch_hdr_ip(px, l4, txn, txn->rsp.sol, expr, test); |
| 7134 | } |
| 7135 | |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 7136 | /* 8. Check on URI PATH. A pointer to the PATH is stored. The path starts at |
| 7137 | * the first '/' after the possible hostname, and ends before the possible '?'. |
| 7138 | */ |
| 7139 | static int |
| 7140 | acl_fetch_path(struct proxy *px, struct session *l4, void *l7, int dir, |
| 7141 | struct acl_expr *expr, struct acl_test *test) |
| 7142 | { |
| 7143 | struct http_txn *txn = l7; |
| 7144 | char *ptr, *end; |
Willy Tarreau | 33a7e69 | 2007-06-10 19:45:56 +0200 | [diff] [blame] | 7145 | |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7146 | if (!txn) |
| 7147 | return 0; |
| 7148 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7149 | if (txn->req.msg_state < HTTP_MSG_BODY) |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7150 | return 0; |
Willy Tarreau | b686644 | 2008-07-14 23:54:42 +0200 | [diff] [blame] | 7151 | |
Willy Tarreau | c11416f | 2007-06-17 16:58:38 +0200 | [diff] [blame] | 7152 | if (txn->rsp.msg_state != HTTP_MSG_RPBEFORE) |
| 7153 | /* ensure the indexes are not affected */ |
| 7154 | return 0; |
| 7155 | |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7156 | 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] | 7157 | ptr = http_get_path(txn); |
| 7158 | if (!ptr) |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 7159 | return 0; |
| 7160 | |
| 7161 | /* OK, we got the '/' ! */ |
| 7162 | test->ptr = ptr; |
| 7163 | |
| 7164 | while (ptr < end && *ptr != '?') |
| 7165 | ptr++; |
| 7166 | |
| 7167 | test->len = ptr - test->ptr; |
| 7168 | |
| 7169 | /* we do not need to set READ_ONLY because the data is in a buffer */ |
| 7170 | test->flags = ACL_TEST_F_VOL_1ST; |
| 7171 | return 1; |
| 7172 | } |
| 7173 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 7174 | static int |
| 7175 | acl_fetch_proto_http(struct proxy *px, struct session *s, void *l7, int dir, |
| 7176 | struct acl_expr *expr, struct acl_test *test) |
| 7177 | { |
| 7178 | struct buffer *req = s->req; |
| 7179 | struct http_txn *txn = &s->txn; |
| 7180 | struct http_msg *msg = &txn->req; |
Willy Tarreau | 737b0c1 | 2007-06-10 21:28:46 +0200 | [diff] [blame] | 7181 | |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 7182 | /* Note: hdr_idx.v cannot be NULL in this ACL because the ACL is tagged |
| 7183 | * as a layer7 ACL, which involves automatic allocation of hdr_idx. |
| 7184 | */ |
| 7185 | |
| 7186 | if (!s || !req) |
| 7187 | return 0; |
| 7188 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7189 | if (unlikely(msg->msg_state >= HTTP_MSG_BODY)) { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 7190 | /* Already decoded as OK */ |
| 7191 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 7192 | return 1; |
| 7193 | } |
| 7194 | |
| 7195 | /* Try to decode HTTP request */ |
| 7196 | if (likely(req->lr < req->r)) |
| 7197 | http_msg_analyzer(req, msg, &txn->hdr_idx); |
| 7198 | |
Willy Tarreau | 655dce9 | 2009-11-08 13:10:58 +0100 | [diff] [blame] | 7199 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 7200 | if ((msg->msg_state == HTTP_MSG_ERROR) || (req->flags & BF_FULL)) { |
| 7201 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 7202 | return 1; |
| 7203 | } |
| 7204 | /* wait for final state */ |
| 7205 | test->flags |= ACL_TEST_F_MAY_CHANGE; |
| 7206 | return 0; |
| 7207 | } |
| 7208 | |
| 7209 | /* OK we got a valid HTTP request. We have some minor preparation to |
| 7210 | * perform so that further checks can rely on HTTP tests. |
| 7211 | */ |
Willy Tarreau | 962c3f4 | 2010-01-10 00:15:35 +0100 | [diff] [blame] | 7212 | txn->meth = find_http_meth(msg->sol, msg->sl.rq.m_l); |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 7213 | if (txn->meth == HTTP_METH_GET || txn->meth == HTTP_METH_HEAD) |
| 7214 | s->flags |= SN_REDIRECTABLE; |
| 7215 | |
| 7216 | if (unlikely(msg->sl.rq.v_l == 0) && !http_upgrade_v09_to_v10(req, msg, txn)) { |
| 7217 | test->flags |= ACL_TEST_F_SET_RES_FAIL; |
| 7218 | return 1; |
| 7219 | } |
| 7220 | |
| 7221 | test->flags |= ACL_TEST_F_SET_RES_PASS; |
| 7222 | return 1; |
| 7223 | } |
| 7224 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7225 | static int |
| 7226 | acl_fetch_http_auth(struct proxy *px, struct session *s, void *l7, int dir, |
| 7227 | struct acl_expr *expr, struct acl_test *test) |
| 7228 | { |
| 7229 | |
| 7230 | if (!s) |
| 7231 | return 0; |
| 7232 | |
| 7233 | if (!get_http_auth(s)) |
| 7234 | return 0; |
| 7235 | |
| 7236 | test->ctx.a[0] = expr->arg.ul; |
| 7237 | test->ctx.a[1] = s->txn.auth.user; |
| 7238 | test->ctx.a[2] = s->txn.auth.pass; |
| 7239 | |
| 7240 | test->flags |= ACL_TEST_F_READ_ONLY | ACL_TEST_F_NULL_MATCH; |
| 7241 | |
| 7242 | return 1; |
| 7243 | } |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7244 | |
| 7245 | /************************************************************************/ |
| 7246 | /* All supported keywords must be declared here. */ |
| 7247 | /************************************************************************/ |
| 7248 | |
| 7249 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 7250 | static struct acl_kw_list acl_kws = {{ },{ |
Willy Tarreau | 2492d5b | 2009-07-11 00:06:00 +0200 | [diff] [blame] | 7251 | { "req_proto_http", acl_parse_nothing, acl_fetch_proto_http, acl_match_nothing, ACL_USE_L7REQ_PERMANENT }, |
| 7252 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 7253 | { "method", acl_parse_meth, acl_fetch_meth, acl_match_meth, ACL_USE_L7REQ_PERMANENT }, |
| 7254 | { "req_ver", acl_parse_ver, acl_fetch_rqver, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 7255 | { "resp_ver", acl_parse_ver, acl_fetch_stver, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 7256 | { "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] | 7257 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 7258 | { "url", acl_parse_str, acl_fetch_url, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 7259 | { "url_beg", acl_parse_str, acl_fetch_url, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 7260 | { "url_end", acl_parse_str, acl_fetch_url, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 7261 | { "url_sub", acl_parse_str, acl_fetch_url, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 7262 | { "url_dir", acl_parse_str, acl_fetch_url, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 7263 | { "url_dom", acl_parse_str, acl_fetch_url, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 7264 | { "url_reg", acl_parse_reg, acl_fetch_url, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 7265 | { "url_ip", acl_parse_ip, acl_fetch_url_ip, acl_match_ip, ACL_USE_L7REQ_VOLATILE }, |
| 7266 | { "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] | 7267 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 7268 | /* note: we should set hdr* to use ACL_USE_HDR_VOLATILE, and chdr* to use L7REQ_VOLATILE */ |
| 7269 | { "hdr", acl_parse_str, acl_fetch_chdr, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 7270 | { "hdr_reg", acl_parse_reg, acl_fetch_chdr, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 7271 | { "hdr_beg", acl_parse_str, acl_fetch_chdr, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 7272 | { "hdr_end", acl_parse_str, acl_fetch_chdr, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 7273 | { "hdr_sub", acl_parse_str, acl_fetch_chdr, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 7274 | { "hdr_dir", acl_parse_str, acl_fetch_chdr, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 7275 | { "hdr_dom", acl_parse_str, acl_fetch_chdr, acl_match_dom, ACL_USE_L7REQ_VOLATILE }, |
| 7276 | { "hdr_cnt", acl_parse_int, acl_fetch_chdr_cnt,acl_match_int, ACL_USE_L7REQ_VOLATILE }, |
| 7277 | { "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] | 7278 | { "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] | 7279 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 7280 | { "shdr", acl_parse_str, acl_fetch_shdr, acl_match_str, ACL_USE_L7RTR_VOLATILE }, |
| 7281 | { "shdr_reg", acl_parse_reg, acl_fetch_shdr, acl_match_reg, ACL_USE_L7RTR_VOLATILE }, |
| 7282 | { "shdr_beg", acl_parse_str, acl_fetch_shdr, acl_match_beg, ACL_USE_L7RTR_VOLATILE }, |
| 7283 | { "shdr_end", acl_parse_str, acl_fetch_shdr, acl_match_end, ACL_USE_L7RTR_VOLATILE }, |
| 7284 | { "shdr_sub", acl_parse_str, acl_fetch_shdr, acl_match_sub, ACL_USE_L7RTR_VOLATILE }, |
| 7285 | { "shdr_dir", acl_parse_str, acl_fetch_shdr, acl_match_dir, ACL_USE_L7RTR_VOLATILE }, |
| 7286 | { "shdr_dom", acl_parse_str, acl_fetch_shdr, acl_match_dom, ACL_USE_L7RTR_VOLATILE }, |
| 7287 | { "shdr_cnt", acl_parse_int, acl_fetch_shdr_cnt,acl_match_int, ACL_USE_L7RTR_VOLATILE }, |
| 7288 | { "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] | 7289 | { "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] | 7290 | |
Willy Tarreau | 0ceba5a | 2008-07-25 19:31:03 +0200 | [diff] [blame] | 7291 | { "path", acl_parse_str, acl_fetch_path, acl_match_str, ACL_USE_L7REQ_VOLATILE }, |
| 7292 | { "path_reg", acl_parse_reg, acl_fetch_path, acl_match_reg, ACL_USE_L7REQ_VOLATILE }, |
| 7293 | { "path_beg", acl_parse_str, acl_fetch_path, acl_match_beg, ACL_USE_L7REQ_VOLATILE }, |
| 7294 | { "path_end", acl_parse_str, acl_fetch_path, acl_match_end, ACL_USE_L7REQ_VOLATILE }, |
| 7295 | { "path_sub", acl_parse_str, acl_fetch_path, acl_match_sub, ACL_USE_L7REQ_VOLATILE }, |
| 7296 | { "path_dir", acl_parse_str, acl_fetch_path, acl_match_dir, ACL_USE_L7REQ_VOLATILE }, |
| 7297 | { "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] | 7298 | |
Willy Tarreau | f3d2598 | 2007-05-08 22:45:09 +0200 | [diff] [blame] | 7299 | #if 0 |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7300 | { "line", acl_parse_str, acl_fetch_line, acl_match_str }, |
| 7301 | { "line_reg", acl_parse_reg, acl_fetch_line, acl_match_reg }, |
| 7302 | { "line_beg", acl_parse_str, acl_fetch_line, acl_match_beg }, |
| 7303 | { "line_end", acl_parse_str, acl_fetch_line, acl_match_end }, |
| 7304 | { "line_sub", acl_parse_str, acl_fetch_line, acl_match_sub }, |
| 7305 | { "line_dir", acl_parse_str, acl_fetch_line, acl_match_dir }, |
| 7306 | { "line_dom", acl_parse_str, acl_fetch_line, acl_match_dom }, |
| 7307 | |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7308 | { "cook", acl_parse_str, acl_fetch_cook, acl_match_str }, |
| 7309 | { "cook_reg", acl_parse_reg, acl_fetch_cook, acl_match_reg }, |
| 7310 | { "cook_beg", acl_parse_str, acl_fetch_cook, acl_match_beg }, |
| 7311 | { "cook_end", acl_parse_str, acl_fetch_cook, acl_match_end }, |
| 7312 | { "cook_sub", acl_parse_str, acl_fetch_cook, acl_match_sub }, |
| 7313 | { "cook_dir", acl_parse_str, acl_fetch_cook, acl_match_dir }, |
| 7314 | { "cook_dom", acl_parse_str, acl_fetch_cook, acl_match_dom }, |
| 7315 | { "cook_pst", acl_parse_none, acl_fetch_cook, acl_match_pst }, |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7316 | #endif |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7317 | |
Krzysztof Piotr Oledzki | f9423ae | 2010-01-29 19:26:18 +0100 | [diff] [blame] | 7318 | { "http_auth", acl_parse_nothing, acl_fetch_http_auth, acl_match_auth }, |
| 7319 | { "http_auth_group", acl_parse_strcat, acl_fetch_http_auth, acl_match_auth }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7320 | { NULL, NULL, NULL, NULL }, |
Willy Tarreau | 8797c06 | 2007-05-07 00:55:35 +0200 | [diff] [blame] | 7321 | }}; |
| 7322 | |
| 7323 | |
| 7324 | __attribute__((constructor)) |
| 7325 | static void __http_protocol_init(void) |
| 7326 | { |
| 7327 | acl_register_keywords(&acl_kws); |
| 7328 | } |
| 7329 | |
| 7330 | |
Willy Tarreau | 58f10d7 | 2006-12-04 02:26:12 +0100 | [diff] [blame] | 7331 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 7332 | * Local variables: |
| 7333 | * c-indent-level: 8 |
| 7334 | * c-basic-offset: 8 |
| 7335 | * End: |
| 7336 | */ |